Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
KnockoutJS secure bindings using Proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
the-djmaze committed Oct 9, 2024
1 parent 82ab4c4 commit 9eb7f38
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 43 deletions.
18 changes: 18 additions & 0 deletions vendors/knockout/build/output/knockout-latest.debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -1618,6 +1618,7 @@ ko.bindingProvider = new class
try {
let cacheKey = bindingsString,
bindingFunction = bindingCache.get(cacheKey);
/*
if (!bindingFunction) {
// Build the source for a function that evaluates "expression"
// For each scope variable, add an extra level of "with" nesting
Expand All @@ -1631,6 +1632,23 @@ ko.bindingProvider = new class
return bindingFunction(bindingContext,
bindingContext["$root"], bindingContext["$parent"], bindingContext["$data"] || {}, node
);
*/
if (!bindingFunction) {
// Build the source for a function that evaluates "expression"
// Use one "with" that has one secure scope handling Proxy
// Deprecated: with is no longer recommended
var rewrittenBindings = ko.expressionRewriting.preProcessBindings(bindingsString),
functionBody = "$context = new Proxy(\
$context,\
{\
has: () => true,\
get: (target, key) => target[key] || target['$data'][key]\
}\
);with($context){return{" + rewrittenBindings + "}}";
bindingFunction = new Function("$context", functionBody);
bindingCache.set(cacheKey, bindingFunction);
}
return bindingFunction(bindingContext);
} catch (ex) {
ex.message = "Unable to parse bindings.\nBindings value: " + bindingsString
+ "\nMessage: " + ex.message;
Expand Down
Loading

0 comments on commit 9eb7f38

Please sign in to comment.