| 1 |
dojo.provide("dojox.secure.capability"); |
| 2 |
|
| 3 |
dojox.secure.badProps = /^__|^(apply|call|callee|caller|constructor|eval|prototype|this|unwatch|valueOf|watch)$|__$/; |
| 4 |
dojox.secure.capability = { |
| 5 |
keywords: ["break", "case", "catch", "const", "continue","debugger", "default", "delete", "do", |
| 6 |
"else", "enum","false", "finally", "for", "function","if", "in", "instanceof", "new", |
| 7 |
"null","yield","return", "switch", |
| 8 |
"throw", "true", "try", "typeof", "var", "void", "while"], |
| 9 |
validate : function(script,safeLibraries,safeGlobals) { |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
var keywords = this.keywords; |
| 27 |
for (var i = 0; i < keywords.length; i++) { |
| 28 |
safeGlobals[keywords[i]]=true; |
| 29 |
} |
| 30 |
var badThis = "|this| keyword in object literal without a Class call"; |
| 31 |
var blocks = []; |
| 32 |
if(script.match(/[\u200c-\u200f\u202a-\u202e\u206a-\u206f\uff00-\uffff]/)){ |
| 33 |
throw new Error("Illegal unicode characters detected"); |
| 34 |
} |
| 35 |
if(script.match(/\/\*@cc_on/)){ |
| 36 |
throw new Error("Conditional compilation token is not allowed"); |
| 37 |
} |
| 38 |
script = script.replace(/\\["'\\\/bfnrtu]/g, '@'). |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
replace(/\/\/.*|\/\*[\w\W]*?\*\/|("[^"]*")|('[^']*')/g,function(t) { |
| 43 |
return t.match(/^\/\/|^\/\*/) ? ' ' : '0'; |
| 44 |
}). |
| 45 |
replace(/\.\s*([a-z\$_A-Z][\w\$_]*)|([;,{])\s*([a-z\$_A-Z][\w\$_]*\s*):/g,function(t,prop,prefix,key) { |
| 46 |
|
| 47 |
prop = prop || key; |
| 48 |
if(/^__|^(apply|call|callee|caller|constructor|eval|prototype|this|unwatch|valueOf|watch)$|__$/.test(prop)){ |
| 49 |
throw new Error("Illegal property name " + prop); |
| 50 |
} |
| 51 |
return (prefix && (prefix + "0:")) || '~'; |
| 52 |
}); |
| 53 |
script.replace(/([^\[][\]\}]\s*=)|((\Wreturn|\S)\s*\[\s*\+?)|([^=!][=!]=[^=])/g,function(oper) { |
| 54 |
if(!oper.match(/((\Wreturn|[=\&\|\:\?\,])\s*\[)|\[\s*\+$/)){ |
| 55 |
throw new Error("Illegal operator " + oper.substring(1)); |
| 56 |
} |
| 57 |
}); |
| 58 |
script = script.replace(new RegExp("(" + safeLibraries.join("|") + ")[\\s~]*\\(","g"),function(call) { |
| 59 |
return "new("; |
| 60 |
}); |
| 61 |
function findOuterRefs(block,func) { |
| 62 |
var outerRefs = {}; |
| 63 |
block.replace(/#\d+/g,function(b) { |
| 64 |
var refs = blocks[b.substring(1)]; |
| 65 |
for (var i in refs) { |
| 66 |
if(i == badThis) { |
| 67 |
throw i; |
| 68 |
} |
| 69 |
if(i == 'this' && refs[':method'] && refs['this'] == 1) { |
| 70 |
|
| 71 |
i = badThis; |
| 72 |
} |
| 73 |
if(i != ':method'){ |
| 74 |
outerRefs[i] = 2; |
| 75 |
} |
| 76 |
} |
| 77 |
}); |
| 78 |
block.replace(/(\W|^)([a-z_\$A-Z][\w_\$]*)/g,function(t,a,identifier) { |
| 79 |
if(identifier.charAt(0)=='_'){ |
| 80 |
throw new Error("Names may not start with _"); |
| 81 |
} |
| 82 |
outerRefs[identifier] = 1; |
| 83 |
}); |
| 84 |
return outerRefs; |
| 85 |
} |
| 86 |
var newScript,outerRefs; |
| 87 |
function parseBlock(t,func,a,b,params,block) { |
| 88 |
block.replace(/(^|,)0:\s*function#(\d+)/g,function(t,a,b) { |
| 89 |
|
| 90 |
var refs = blocks[b]; |
| 91 |
refs[':method'] = 1; |
| 92 |
}); |
| 93 |
block = block.replace(/(^|[^_\w\$])Class\s*\(\s*([_\w\$]+\s*,\s*)*#(\d+)/g,function(t,p,a,b) { |
| 94 |
var refs = blocks[b]; |
| 95 |
delete refs[badThis]; |
| 96 |
return (p||'') + (a||'') + "#" + b; |
| 97 |
}); |
| 98 |
outerRefs = findOuterRefs(block,func); |
| 99 |
function parseVars(t,a,b,decl) { |
| 100 |
decl.replace(/,?([a-z\$A-Z][_\w\$]*)/g,function(t,identifier) { |
| 101 |
if(identifier == 'Class'){ |
| 102 |
throw new Error("Class is reserved"); |
| 103 |
} |
| 104 |
delete outerRefs[identifier]; |
| 105 |
}); |
| 106 |
} |
| 107 |
|
| 108 |
if(func) { |
| 109 |
parseVars(t,a,a,params); |
| 110 |
} |
| 111 |
block.replace(/(\W|^)(var) ([ \t,_\w\$]+)/g,parseVars); |
| 112 |
|
| 113 |
return (a || '') + (b || '') + "#" + (blocks.push(outerRefs)-1); |
| 114 |
} |
| 115 |
do { |
| 116 |
|
| 117 |
newScript = script.replace(/((function|catch)(\s+[_\w\$]+)?\s*\(([^\)]*)\)\s*)?{([^{}]*)}/g, parseBlock); |
| 118 |
} |
| 119 |
while(newScript != script && (script = newScript)); |
| 120 |
parseBlock(0,0,0,0,0,script); |
| 121 |
for (i in outerRefs) { |
| 122 |
if(!(i in safeGlobals)) { |
| 123 |
throw new Error("Illegal reference to " + i); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
} |
| 128 |
}; |