| 1 |
dojo.provide("dojox.secure.sandbox"); |
| 2 |
dojo.require("dojox.secure.DOM"); |
| 3 |
dojo.require("dojox.secure.capability"); |
| 4 |
dojo.require("dojo.NodeList-fx"); |
| 5 |
dojo.require("dojo._base.url"); |
| 6 |
|
| 7 |
(function() { |
| 8 |
var oldTimeout = setTimeout; |
| 9 |
var oldInterval = setInterval; |
| 10 |
if({}.__proto__){ |
| 11 |
|
| 12 |
var fixMozArrayFunction = function (name) { |
| 13 |
var method = Array.prototype[name]; |
| 14 |
if(method && !method.fixed){ |
| 15 |
(Array.prototype[name] = function () { |
| 16 |
if (this == window) { |
| 17 |
throw new TypeError("Called with wrong this"); |
| 18 |
} |
| 19 |
return method.apply(this, arguments); |
| 20 |
}).fixed = true; |
| 21 |
} |
| 22 |
}; |
| 23 |
|
| 24 |
fixMozArrayFunction('concat'); |
| 25 |
fixMozArrayFunction('reverse'); |
| 26 |
fixMozArrayFunction('sort'); |
| 27 |
fixMozArrayFunction("slice"); |
| 28 |
fixMozArrayFunction("forEach"); |
| 29 |
fixMozArrayFunction("filter"); |
| 30 |
fixMozArrayFunction("reduce"); |
| 31 |
fixMozArrayFunction("reduceRight"); |
| 32 |
fixMozArrayFunction("every"); |
| 33 |
fixMozArrayFunction("map"); |
| 34 |
fixMozArrayFunction("some"); |
| 35 |
} |
| 36 |
var xhrGet = function(){ |
| 37 |
return dojo.xhrGet.apply(dojo,arguments); |
| 38 |
}; |
| 39 |
dojox.secure.sandbox = function(element) { |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
var wrap = dojox.secure.DOM(element); |
| 53 |
element = wrap(element); |
| 54 |
var document = element.ownerDocument; |
| 55 |
var mixin, dojo = dojox.secure._safeDojoFunctions(element,wrap); |
| 56 |
var imports= []; |
| 57 |
var safeCalls = ["isNaN","isFinite","parseInt","parseFloat","escape","unescape", |
| 58 |
"encodeURI","encodeURIComponent","decodeURI","decodeURIComponent", |
| 59 |
"alert","confirm","prompt", |
| 60 |
"Error","EvalError","RangeError","ReferenceError","SyntaxError","TypeError", |
| 61 |
"Date","RegExp","Number","Object","Array","String","Math", |
| 62 |
|
| 63 |
"setTimeout","setInterval","clearTimeout","clearInterval", |
| 64 |
"dojo","get","set","forEach","load","evaluate"]; |
| 65 |
for(var i in dojo){ |
| 66 |
safeCalls.push(i); |
| 67 |
imports.push("var " + i + "=dojo." + i); |
| 68 |
} |
| 69 |
|
| 70 |
eval(imports.join(";")); |
| 71 |
function get(obj,prop) { |
| 72 |
|
| 73 |
prop = '' + prop; |
| 74 |
if(dojox.secure.badProps.test(prop)) { |
| 75 |
throw new Error("bad property access"); |
| 76 |
} |
| 77 |
if(obj.__get__) { |
| 78 |
return obj.__get__(prop); |
| 79 |
} |
| 80 |
return obj[prop]; |
| 81 |
} |
| 82 |
function set(obj,prop,value) { |
| 83 |
|
| 84 |
prop = '' + prop; |
| 85 |
get(obj,prop); |
| 86 |
if(obj.__set) { |
| 87 |
return obj.__set(prop); |
| 88 |
} |
| 89 |
obj[prop] = value; |
| 90 |
return value; |
| 91 |
} |
| 92 |
function forEach(obj,fun) { |
| 93 |
|
| 94 |
if(typeof fun != "function"){ |
| 95 |
throw new TypeError(); |
| 96 |
} |
| 97 |
if("length" in obj) { |
| 98 |
|
| 99 |
if(obj.__get__) { |
| 100 |
|
| 101 |
var len = obj.__get__('length'); |
| 102 |
for (var i = 0; i < len; i++) { |
| 103 |
if(i in obj) { |
| 104 |
fun.call(obj, obj.__get__(i), i, obj); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
else { |
| 109 |
|
| 110 |
len = obj.length; |
| 111 |
for (i = 0; i < len; i++) { |
| 112 |
if(i in obj) { |
| 113 |
fun.call(obj, obj[i], i, obj); |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
else { |
| 119 |
|
| 120 |
for (i in obj) { |
| 121 |
fun.call(obj, get(obj,i), i, obj); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
function Class(superclass, properties, classProperties) { |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
var proto,superConstructor,ourConstructor; |
| 162 |
var arg; |
| 163 |
for (var i = 0, l = arguments.length; typeof (arg = arguments[i]) == 'function' && i < l; i++) { |
| 164 |
|
| 165 |
if(proto) { |
| 166 |
mixin(proto,arg.prototype); |
| 167 |
} |
| 168 |
else { |
| 169 |
|
| 170 |
|
| 171 |
superConstructor = arg; |
| 172 |
var F = function() {}; |
| 173 |
F.prototype = arg.prototype; |
| 174 |
proto = new F; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
if(arg) { |
| 179 |
|
| 180 |
for (var j in arg) { |
| 181 |
|
| 182 |
var value = arg[j]; |
| 183 |
if(typeof value == 'function') { |
| 184 |
arg[j] = function() { |
| 185 |
if(this instanceof Class){ |
| 186 |
return arguments.callee.__rawMethod__.apply(this,arguments); |
| 187 |
} |
| 188 |
throw new Error("Method called on wrong object"); |
| 189 |
}; |
| 190 |
arg[j].__rawMethod__ = value; |
| 191 |
} |
| 192 |
} |
| 193 |
if(arg.hasOwnProperty('constructor')) { |
| 194 |
ourConstructor = arg.constructor; |
| 195 |
} |
| 196 |
} |
| 197 |
proto = proto ? mixin(proto,arg) : arg; |
| 198 |
function Class() { |
| 199 |
|
| 200 |
if(superConstructor){ |
| 201 |
superConstructor.apply(this,arguments); |
| 202 |
} |
| 203 |
if(ourConstructor){ |
| 204 |
ourConstructor.apply(this,arguments); |
| 205 |
} |
| 206 |
} |
| 207 |
mixin(Class,arguments[i]); |
| 208 |
proto.constructor = Class; |
| 209 |
Class.prototype = proto; |
| 210 |
return Class; |
| 211 |
} |
| 212 |
function checkString(func){ |
| 213 |
if(typeof func != 'function') { |
| 214 |
throw new Error("String is not allowed in setTimeout/setInterval"); |
| 215 |
} |
| 216 |
} |
| 217 |
function setTimeout(func,time) { |
| 218 |
|
| 219 |
checkString(func); |
| 220 |
return oldTimeout(func,time); |
| 221 |
} |
| 222 |
function setInterval(func,time) { |
| 223 |
|
| 224 |
checkString(func); |
| 225 |
return oldInterval(func,time); |
| 226 |
} |
| 227 |
function evaluate(script){ |
| 228 |
|
| 229 |
return wrap.evaluate(script); |
| 230 |
} |
| 231 |
var load = wrap.load = function(url){ |
| 232 |
|
| 233 |
if (url.match(/^[\w\s]*:/)){ |
| 234 |
throw new Error("Access denied to cross-site requests"); |
| 235 |
} |
| 236 |
return xhrGet({url:(new dojo._Url(wrap.rootUrl,url))+'',secure:true}); |
| 237 |
} |
| 238 |
wrap.evaluate = function(script){ |
| 239 |
|
| 240 |
dojox.secure.capability.validate(script,safeCalls, |
| 241 |
{document:1,element:1}); |
| 242 |
|
| 243 |
|
| 244 |
if(script.match(/^\s*[\[\{]/)) { |
| 245 |
var result = eval('(' + script + ')'); |
| 246 |
|
| 247 |
} |
| 248 |
else { |
| 249 |
eval(script); |
| 250 |
} |
| 251 |
|
| 252 |
}; |
| 253 |
return { |
| 254 |
loadJS : function(url){ |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
wrap.rootUrl = url; |
| 262 |
return xhrGet({url:url,secure:true}).addCallback(function(result) { |
| 263 |
evaluate(result,element ); |
| 264 |
}); |
| 265 |
}, |
| 266 |
loadHTML : function(url){ |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
wrap.rootUrl = url; |
| 276 |
return xhrGet({url:url,secure:true}).addCallback(function(result){ |
| 277 |
element.innerHTML = result; |
| 278 |
}); |
| 279 |
}, |
| 280 |
evaluate : function(script){ |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
return wrap.evaluate(script); |
| 287 |
} |
| 288 |
|
| 289 |
}; |
| 290 |
}; |
| 291 |
})(); |
| 292 |
dojox.secure._safeDojoFunctions = function(element,wrap) { |
| 293 |
|
| 294 |
var safeFunctions = ["mixin","require","isString","isArray","isFunction","isObject","isArrayLike","isAlien", |
| 295 |
"hitch","delegate","partial","trim","disconnect","subscribe","unsubscribe","Deferred","toJson","style","attr"]; |
| 296 |
|
| 297 |
var doc = element.ownerDocument; |
| 298 |
var unwrap = dojox.secure.unwrap; |
| 299 |
dojo.NodeList.prototype.addContent.safetyCheck = function(content){ |
| 300 |
wrap.safeHTML(content); |
| 301 |
}; |
| 302 |
dojo.NodeList.prototype.style.safetyCheck = function(name,value){ |
| 303 |
if(name=='behavior'){ |
| 304 |
throw new Error("Can not set behavior"); |
| 305 |
} |
| 306 |
wrap.safeCSS(value); |
| 307 |
}; |
| 308 |
dojo.NodeList.prototype.attr.safetyCheck = function(name,value){ |
| 309 |
if (value && (name == 'src' || name == 'href' || name=='style')){ |
| 310 |
throw new Error("Illegal to set " + name); |
| 311 |
} |
| 312 |
}; |
| 313 |
var safe = { |
| 314 |
query : function(query,root) { |
| 315 |
return wrap(dojo.query(query,unwrap(root || element))); |
| 316 |
}, |
| 317 |
connect: function(el,event) { |
| 318 |
var obj = el; |
| 319 |
arguments[0] = unwrap(el); |
| 320 |
if(obj!=arguments[0] && event.substring(0,2) != 'on'){ |
| 321 |
|
| 322 |
throw new Error("Invalid event name for element"); |
| 323 |
} |
| 324 |
return dojo.connect.apply(dojo,arguments); |
| 325 |
}, |
| 326 |
body : function() { |
| 327 |
return element; |
| 328 |
}, |
| 329 |
byId : function(id) { |
| 330 |
return element.ownerDocument.getElementById(id); |
| 331 |
}, |
| 332 |
fromJson : function(str) { |
| 333 |
|
| 334 |
dojox.secure.capability.validate(str,[],{}); |
| 335 |
return dojo.fromJson(str); |
| 336 |
} |
| 337 |
}; |
| 338 |
for (var i = 0; i < safeFunctions.length; i++) { |
| 339 |
safe[safeFunctions[i]] = dojo[safeFunctions[i]]; |
| 340 |
} |
| 341 |
return safe; |
| 342 |
}; |
| 343 |
|