capability.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // wrapped by build app
  2. define("dojox/secure/capability", ["dijit","dojo","dojox"], function(dijit,dojo,dojox){
  3. dojo.provide("dojox.secure.capability");
  4. dojox.secure.badProps = /^__|^(apply|call|callee|caller|constructor|eval|prototype|this|unwatch|valueOf|watch)$|__$/;
  5. dojox.secure.capability = {
  6. keywords: ["break", "case", "catch", "const", "continue","debugger", "default", "delete", "do",
  7. "else", "enum","false", "finally", "for", "function","if", "in", "instanceof", "new",
  8. "null","yield","return", "switch",
  9. "throw", "true", "try", "typeof", "var", "void", "while"],
  10. validate : function(/*string*/script,/*Array*/safeLibraries,/*Object*/safeGlobals) {
  11. // summary:
  12. // pass in the text of a script. If it passes and it can be eval'ed, it should be safe.
  13. // Note that this does not do full syntax checking, it relies on eval to reject invalid scripts.
  14. // There are also known false rejections:
  15. // Nesting vars inside blocks will not declare the variable for the outer block
  16. // Named functions are not treated as declaration so they are generally not allowed unless the name is declared with a var.
  17. // Var declaration that involve multiple comma delimited variable assignments are not accepted
  18. //
  19. // script:
  20. // the script to execute
  21. //
  22. // safeLibraries:
  23. // The safe libraries that can be called (the functions can not be access/modified by the untrusted code, only called)
  24. //
  25. // safeGlobals:
  26. // These globals can be freely interacted with by the untrusted code
  27. var keywords = this.keywords;
  28. for (var i = 0; i < keywords.length; i++) {
  29. safeGlobals[keywords[i]]=true;
  30. }
  31. var badThis = "|this| keyword in object literal without a Class call";
  32. var blocks = []; // keeps track of the outer references from each inner block
  33. if(script.match(/[\u200c-\u200f\u202a-\u202e\u206a-\u206f\uff00-\uffff]/)){
  34. throw new Error("Illegal unicode characters detected");
  35. }
  36. if(script.match(/\/\*@cc_on/)){
  37. throw new Error("Conditional compilation token is not allowed");
  38. }
  39. script = script.replace(/\\["'\\\/bfnrtu]/g, '@'). // borrows some tricks from json.js
  40. // now clear line comments, block comments, regular expressions, and strings.
  41. // By doing it all at once, the regular expression uses left to right parsing, and the most
  42. // left token is read first. It is also more compact.
  43. replace(/\/\/.*|\/\*[\w\W]*?\*\/|("[^"]*")|('[^']*')/g,function(t) {
  44. return t.match(/^\/\/|^\/\*/) ? ' ' : '0'; // comments are replaced with a space, strings and regex are replaced with a single safe token (0)
  45. }).
  46. replace(/\.\s*([a-z\$_A-Z][\w\$_]*)|([;,{])\s*([a-z\$_A-Z][\w\$_]*\s*):/g,function(t,prop,prefix,key) {
  47. // find all the dot property references, all the object literal keys, and labels
  48. prop = prop || key;
  49. if(/^__|^(apply|call|callee|caller|constructor|eval|prototype|this|unwatch|valueOf|watch)$|__$/.test(prop)){
  50. throw new Error("Illegal property name " + prop);
  51. }
  52. return (prefix && (prefix + "0:")) || '~'; // replace literal keys with 0: and replace properties with the innocuous ~
  53. });
  54. script.replace(/([^\[][\]\}]\s*=)|((\Wreturn|\S)\s*\[\s*\+?)|([^=!][=!]=[^=])/g,function(oper) {// check for illegal operator usages
  55. if(!oper.match(/((\Wreturn|[=\&\|\:\?\,])\s*\[)|\[\s*\+$/)){ // the whitelist for [ operator for array initializer context or [+num] syntax
  56. throw new Error("Illegal operator " + oper.substring(1));
  57. }
  58. });
  59. script = script.replace(new RegExp("(" + safeLibraries.join("|") + ")[\\s~]*\\(","g"),function(call) { // find library calls and make them look safe
  60. return "new("; // turn into a known safe call
  61. });
  62. function findOuterRefs(block,func) {
  63. var outerRefs = {};
  64. block.replace(/#\d+/g,function(b) { // graft in the outer references from the inner scopes
  65. var refs = blocks[b.substring(1)];
  66. for (var i in refs) {
  67. if(i == badThis) {
  68. throw i;
  69. }
  70. if(i == 'this' && refs[':method'] && refs['this'] == 1) {
  71. // if we are in an object literal the function may be a bindable method, this must only be in the local scope
  72. i = badThis;
  73. }
  74. if(i != ':method'){
  75. outerRefs[i] = 2; // the reference is more than just local
  76. }
  77. }
  78. });
  79. block.replace(/(\W|^)([a-z_\$A-Z][\w_\$]*)/g,function(t,a,identifier) { // find all the identifiers
  80. if(identifier.charAt(0)=='_'){
  81. throw new Error("Names may not start with _");
  82. }
  83. outerRefs[identifier] = 1;
  84. });
  85. return outerRefs;
  86. }
  87. var newScript,outerRefs;
  88. function parseBlock(t,func,a,b,params,block) {
  89. block.replace(/(^|,)0:\s*function#(\d+)/g,function(t,a,b) { // find functions in object literals
  90. // note that if named functions are allowed, it could be possible to have label: function name() {} which is a security breach
  91. var refs = blocks[b];
  92. refs[':method'] = 1;//mark it as a method
  93. });
  94. block = block.replace(/(^|[^_\w\$])Class\s*\(\s*([_\w\$]+\s*,\s*)*#(\d+)/g,function(t,p,a,b) { // find Class calls
  95. var refs = blocks[b];
  96. delete refs[badThis];
  97. return (p||'') + (a||'') + "#" + b;
  98. });
  99. outerRefs = findOuterRefs(block,func); // find the variables in this block
  100. function parseVars(t,a,b,decl) { // find var decls
  101. decl.replace(/,?([a-z\$A-Z][_\w\$]*)/g,function(t,identifier) {
  102. if(identifier == 'Class'){
  103. throw new Error("Class is reserved");
  104. }
  105. delete outerRefs[identifier]; // outer reference is safely referenced here
  106. });
  107. }
  108. if(func) {
  109. parseVars(t,a,a,params); // the parameters are declare variables
  110. }
  111. block.replace(/(\W|^)(var) ([ \t,_\w\$]+)/g,parseVars); // and vars declare variables
  112. // FIXME: Give named functions #name syntax so they can be detected as vars in outer scopes (but be careful of nesting)
  113. return (a || '') + (b || '') + "#" + (blocks.push(outerRefs)-1); // return a block reference so the outer block can fetch it
  114. }
  115. do {
  116. // get all the blocks, starting with inside and moving out, capturing the parameters of functions and catchs as variables along the way
  117. newScript = script.replace(/((function|catch)(\s+[_\w\$]+)?\s*\(([^\)]*)\)\s*)?{([^{}]*)}/g, parseBlock);
  118. }
  119. while(newScript != script && (script = newScript)); // keep going until we can't find anymore blocks
  120. parseBlock(0,0,0,0,0,script); //findOuterRefs(script); // find the references in the outside scope
  121. for (i in outerRefs) {
  122. if(!(i in safeGlobals)) {
  123. throw new Error("Illegal reference to " + i);
  124. }
  125. }
  126. }
  127. };
  128. });