mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-09-04 06:57:03 +03:00
KnockoutJS secure bindings using Proxy
This commit is contained in:
parent
82ab4c4c6d
commit
9eb7f38485
3 changed files with 71 additions and 43 deletions
27
vendors/knockout/src/binding/bindingProvider.js
vendored
27
vendors/knockout/src/binding/bindingProvider.js
vendored
|
|
@ -31,20 +31,12 @@ 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
|
||||
// Example result: with(sc1) { with(sc0) { return (expression) } }
|
||||
// Deprecated: with is no longer recommended
|
||||
/*
|
||||
functionBody = "$context = new Proxy(
|
||||
$context,
|
||||
{
|
||||
has:()=>true,
|
||||
get:(target,key)=>Reflect.has(target, key) ? target[key] : target['$data'][key]
|
||||
}
|
||||
);with($context){return{" + rewrittenBindings + "}}";
|
||||
*/
|
||||
var rewrittenBindings = ko.expressionRewriting.preProcessBindings(bindingsString),
|
||||
functionBody = "with($data){return{" + rewrittenBindings + "}}";
|
||||
bindingFunction = new Function("$context", "$root", "$parent", "$data", "$element", functionBody);
|
||||
|
|
@ -53,6 +45,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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue