|
|
|
|
| 1 |
/* ***** BEGIN LICENSE BLOCK ***** |
| 2 |
* Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 3 |
* |
| 4 |
* The contents of this file are subject to the Mozilla Public License Version |
| 5 |
* 1.1 (the "License"); you may not use this file except in compliance with |
| 6 |
* the License. You may obtain a copy of the License at |
| 7 |
* http://www.mozilla.org/MPL/ |
| 8 |
* |
| 9 |
* Software distributed under the License is distributed on an "AS IS" basis, |
| 10 |
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 11 |
* for the specific language governing rights and limitations under the |
| 12 |
* License. |
| 13 |
* |
| 14 |
* The Original Code is the Content Security Policy Data Structures testing code. |
| 15 |
* |
| 16 |
* The Initial Developer of the Original Code is |
| 17 |
* Sid Stamm <sid@mozilla.com> |
| 18 |
* |
| 19 |
* Contributor(s): |
| 20 |
* |
| 21 |
* Alternatively, the contents of this file may be used under the terms of |
| 22 |
* either the GNU General Public License Version 2 or later (the "GPL"), or |
| 23 |
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 24 |
* in which case the provisions of the GPL or the LGPL are applicable instead |
| 25 |
* of those above. If you wish to allow use of your version of this file only |
| 26 |
* under the terms of either the GPL or the LGPL, and not to allow others to |
| 27 |
* use your version of this file under the terms of the MPL, indicate your |
| 28 |
* decision by deleting the provisions above and replace them with the notice |
| 29 |
* and other provisions required by the GPL or the LGPL. If you do not delete |
| 30 |
* the provisions above, a recipient may use your version of this file under |
| 31 |
* the terms of any one of the MPL, the GPL or the LGPL. |
| 32 |
* |
| 33 |
* ***** END LICENSE BLOCK ***** */ |
| 34 |
|
| 35 |
/** |
| 36 |
* Tester for CSPUtils.jsm |
| 37 |
* Launch me with xpcshell. |
| 38 |
**/ |
| 39 |
|
| 40 |
//make dump rhino-friendly |
| 41 |
var dump = print; |
| 42 |
|
| 43 |
//import "CSPUtils.jsm" ; |
| 44 |
load('CSPUtils.jsm'); |
| 45 |
load('../../netwerk/test/httpserver/httpd.js'); |
| 46 |
|
| 47 |
var g_SHOW_PASSES = false; |
| 48 |
var failedTests = []; |
| 49 |
var g_tests = 0; |
| 50 |
var g_passed = 0; |
| 51 |
|
| 52 |
function test(codeThunk, expectedResult) { |
| 53 |
var x; |
| 54 |
g_tests++; |
| 55 |
try { |
| 56 |
x = codeThunk(); |
| 57 |
} catch(e) { |
| 58 |
x = e; |
| 59 |
} |
| 60 |
var success = false; |
| 61 |
if(expectedResult == undefined) { |
| 62 |
success = (x == undefined); |
| 63 |
} else if(expectedResult instanceof Function) { |
| 64 |
success = expectedResult(x); |
| 65 |
} else { |
| 66 |
success = (x == expectedResult); |
| 67 |
} |
| 68 |
|
| 69 |
if(success) { |
| 70 |
if(g_SHOW_PASSES) { print(" pass :: " + codeThunk.name); } |
| 71 |
g_passed++; |
| 72 |
} else { |
| 73 |
print(" [FAIL] :: in " + codeThunk.name + ": " + x); |
| 74 |
print(x.stack); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
function ASSERT(foo, message) { |
| 79 |
if(!foo) { |
| 80 |
throw message; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
function ASSERT_NOT_NULL(foo, message) { |
| 85 |
if(foo == null) { |
| 86 |
throw message; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
function ASSERT_NULL(foo, message) { |
| 91 |
if(foo != null) { |
| 92 |
throw message; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
// NOTE: this is the object version of ASSERT_EQ |
| 97 |
function ASSERT_EQUALS(foo, bar, message) { |
| 98 |
if(!foo.equals(bar)) { |
| 99 |
throw message; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
function ASSERT_EQ(foo, bar, message) { |
| 104 |
if(foo != bar) { |
| 105 |
throw message; |
| 106 |
} |
| 107 |
} |
| 108 |
function ASSERT_NEQ(foo, bar, message) { |
| 109 |
if(foo == bar) { |
| 110 |
throw message; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
function ASSERT_HAS_KEY(foo, key, message) { |
| 115 |
for(var x in foo) { |
| 116 |
if(x == key) { return; } |
| 117 |
} |
| 118 |
throw message; |
| 119 |
} |
| 120 |
function ASSERT_HAS_VALUE(foo, val, message) { |
| 121 |
for(var x in foo) { |
| 122 |
if(foo[x] == val) { return; } |
| 123 |
} |
| 124 |
throw message; |
| 125 |
} |
| 126 |
|
| 127 |
// END OF TESTING LIBRARY |
| 128 |
//////////////////////////////////////////////////////////// |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
// Begin Actual Tests |
| 133 |
|
| 134 |
// setup |
| 135 |
const POLICY_FROM_URI = "allow self; img-src *"; |
| 136 |
const POLICY_URI = "http://localhost:3400/policy"; |
| 137 |
var httpServer = new nsHttpServer(); |
| 138 |
function policyresponder(request,response) { |
| 139 |
response.setStatusLine(request.httpVersion, 200, "OK"); |
| 140 |
response.setHeader("Content-Type", "text/csp", false); |
| 141 |
response.write(POLICY_FROM_URI); |
| 142 |
} |
| 143 |
//server.registerDirectory("/", nsILocalFileForBasePath); |
| 144 |
httpServer.registerPathHandler("/policy", policyresponder); |
| 145 |
httpServer.start(3400); |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
///////// TEST THE CSPHost object //////////////////////// |
| 150 |
test( |
| 151 |
function test_CSPHost_fromstring() { |
| 152 |
var h; |
| 153 |
|
| 154 |
h = CSPHost.fromString("*"); |
| 155 |
ASSERT_NOT_NULL(h, "* lone wildcard should work"); |
| 156 |
|
| 157 |
h = CSPHost.fromString("foo.bar"); |
| 158 |
ASSERT_NOT_NULL(h, "standard tuple failed"); |
| 159 |
|
| 160 |
h = CSPHost.fromString("*.bar"); |
| 161 |
ASSERT_NOT_NULL(h, "wildcard failed"); |
| 162 |
|
| 163 |
h = CSPHost.fromString("com"); |
| 164 |
ASSERT_NULL(h, "lone symbol should fail"); |
| 165 |
|
| 166 |
h = CSPHost.fromString("f00b4r.com"); |
| 167 |
ASSERT_NOT_NULL(h, "Numbers in hosts should work"); |
| 168 |
|
| 169 |
h = CSPHost.fromString("foo-bar.com"); |
| 170 |
ASSERT_NOT_NULL(h, "dashes in hosts should work"); |
| 171 |
|
| 172 |
// Not enforced in CSPHost |
| 173 |
//h = CSPHost.fromString("foo!bar.com"); |
| 174 |
//ASSERT_NULL(h, "special chars in hosts should fail"); |
| 175 |
}); |
| 176 |
|
| 177 |
test( |
| 178 |
function test_CSPHost_clone() { |
| 179 |
h = CSPHost.fromString("*.a.b.c"); |
| 180 |
h2 = h.clone(); |
| 181 |
for(var i in h._segments) { |
| 182 |
ASSERT_EQ(h._segments[i], h2._segments[i], "cloned segments should match"); |
| 183 |
} |
| 184 |
}); |
| 185 |
|
| 186 |
test( |
| 187 |
function test_CSPHost_permits() { |
| 188 |
var h = CSPHost.fromString("*.b.c"); |
| 189 |
var h2 = CSPHost.fromString("a.b.c"); |
| 190 |
ASSERT( h.permits(h2), "CSPHost *.b.c should allow CSPHost a.b.c") |
| 191 |
ASSERT( h.permits("a.b.c"), "CSPHost *.b.c should allow string a.b.c") |
| 192 |
ASSERT(!h.permits("b.c"), "CSPHost *.b.c should not allow string b.c") |
| 193 |
ASSERT(!h.permits("a.a.c"), "CSPHost *.b.c should not allow string a.a.c") |
| 194 |
|
| 195 |
ASSERT(!h2.permits(h), "CSPHost a.b.c should not allow CSPHost *.b.c") |
| 196 |
ASSERT(!h2.permits("b.c"), "CSPHost a.b.c should not allow string b.c") |
| 197 |
ASSERT( h2.permits("a.b.c"),"CSPHost a.b.c should allow string a.b.c") |
| 198 |
}); |
| 199 |
|
| 200 |
test( |
| 201 |
function test_CSPHost_intersectWith() { |
| 202 |
var h = CSPHost.fromString("*.b.c"); |
| 203 |
ASSERT_EQ("*.a.b.c", h.intersectWith(CSPHost.fromString("*.a.b.c")), |
| 204 |
"*.a.b.c ^ *.b.c should be *.a.b.c"); |
| 205 |
ASSERT_NULL(h.intersectWith(CSPHost.fromString("*.d.e")), |
| 206 |
"*.b.c ^ *.d.e should not work (null)"); |
| 207 |
}); |
| 208 |
|
| 209 |
///////////////////// Test the Source object ////////////////////// |
| 210 |
|
| 211 |
test( |
| 212 |
function test_CSPSource_fromString() { |
| 213 |
ASSERT_NOT_NULL(CSPSource.fromString("a.com"), |
| 214 |
"basic source should not be null."); |
| 215 |
|
| 216 |
ASSERT_NOT_NULL(CSPSource.fromString("a2-c.com"), |
| 217 |
"ldh characters should all work for host."); |
| 218 |
|
| 219 |
ASSERT_NOT_NULL(CSPSource.fromString("*.a.com"), |
| 220 |
"wildcard should work in first token for host."); |
| 221 |
|
| 222 |
print(" --- Ignore the following two errors if they print ---"); |
| 223 |
ASSERT_NULL(CSPSource.fromString("x.*.a.com"), |
| 224 |
"wildcard should not work in non-first token for host."); |
| 225 |
|
| 226 |
ASSERT_NULL(CSPSource.fromString("a#2-c.com"), |
| 227 |
"funny characters (#) should not work for host, _HOST_RE is probably broken."); |
| 228 |
print(" --- Stop ignoring errors that print ---\n"); |
| 229 |
|
| 230 |
ASSERT_NOT_NULL(CSPSource.fromString("a.com:23"), |
| 231 |
"failed to parse host with port."); |
| 232 |
ASSERT_NOT_NULL(CSPSource.fromString("https://a.com"), |
| 233 |
"failed to parse host with scheme."); |
| 234 |
ASSERT_NOT_NULL(CSPSource.fromString("https://a.com:200"), |
| 235 |
"failed to parse host with scheme and port."); |
| 236 |
}); |
| 237 |
|
| 238 |
test( |
| 239 |
function test_CSPSource_fromString_withSelf() { |
| 240 |
var src; |
| 241 |
src = CSPSource.fromString("a.com", "https://self.com"); |
| 242 |
ASSERT(src.permits("https://a.com:443"), |
| 243 |
"src should inherit port *"); |
| 244 |
ASSERT(!src.permits("http://a.com"), |
| 245 |
"src should inherit and require https scheme"); |
| 246 |
ASSERT(src.permits("https://a.com"), |
| 247 |
"src should inherit scheme 'https'"); |
| 248 |
|
| 249 |
src = CSPSource.fromString("self", "https://self.com"); |
| 250 |
ASSERT(src.permits("https://self.com:443"), |
| 251 |
"src should inherit port *"); |
| 252 |
ASSERT(!src.permits("http://self.com"), |
| 253 |
"src should inherit and require https scheme"); |
| 254 |
ASSERT(src.permits("https://self.com"), |
| 255 |
"src should inherit scheme 'https'"); |
| 256 |
ASSERT(!src.permits("https://a.com"), |
| 257 |
"src should reject other hosts"); |
| 258 |
}); |
| 259 |
|
| 260 |
///////////////////// Test the source list ////////////////////// |
| 261 |
|
| 262 |
test( |
| 263 |
function test_CSPSourceList_fromString() { |
| 264 |
var sd = CSPSourceList.fromString("none"); |
| 265 |
ASSERT_NOT_NULL(sd, "'none' -- should parse"); |
| 266 |
ASSERT_EQ(0, sd._sources.length, "'none' should be a zero-length list"); |
| 267 |
|
| 268 |
sd = CSPSourceList.fromString("*"); |
| 269 |
ASSERT_EQ(1, sd._sources.length, "'*' should be a one-length list"); |
| 270 |
|
| 271 |
print(" --- Ignore the following three errors if they print ---"); |
| 272 |
ASSERT_NULL(CSPSourceList.fromString("f!oo.bar"), |
| 273 |
"funny char in host"); |
| 274 |
ASSERT_NULL(CSPSourceList.fromString("ht1ps://f-oo.bar"), |
| 275 |
"funny char in scheme"); |
| 276 |
ASSERT_NULL(CSPSourceList.fromString("https://f-oo.bar:3f"), |
| 277 |
"funny char in port"); |
| 278 |
print(" --- Stop ignoring errors that print ---\n"); |
| 279 |
}); |
| 280 |
|
| 281 |
test( |
| 282 |
function test_CSPSourceList_fromString_twohost() { |
| 283 |
var str = "foo.bar:21 https://ras.bar"; |
| 284 |
var sd = CSPSourceList.fromString(str); |
| 285 |
ASSERT_NOT_NULL(sd, "two-host list should parse"); |
| 286 |
ASSERT_EQ(2, sd._sources.length, |
| 287 |
"two-host list should parse to two hosts"); |
| 288 |
ASSERT_EQ(str, sd.toString(), |
| 289 |
"two-host list should contain original data"); |
| 290 |
}); |
| 291 |
|
| 292 |
test( |
| 293 |
function test_CSPSourceList_permits() { |
| 294 |
var nullSourceList = CSPSourceList.fromString("none"); |
| 295 |
var simpleSourceList = CSPSourceList.fromString("a.com"); |
| 296 |
var doubleSourceList = CSPSourceList.fromString("https://foo.com http://bar.com:88"); |
| 297 |
var allSourceList = CSPSourceList.fromString("*"); |
| 298 |
|
| 299 |
ASSERT( !nullSourceList.permits("a.com"), |
| 300 |
"'none' should permit none.") |
| 301 |
ASSERT( simpleSourceList.permits("a.com"), |
| 302 |
"a.com should permit a.com"); |
| 303 |
ASSERT( !simpleSourceList.permits("b.com"), |
| 304 |
"wrong host"); |
| 305 |
ASSERT( doubleSourceList.permits("http://bar.com:88"), |
| 306 |
"double list permits http://bar.com:88"); |
| 307 |
ASSERT( !doubleSourceList.permits("https://bar.com:88"), |
| 308 |
"double list permits https://bar.com:88"); |
| 309 |
ASSERT( !doubleSourceList.permits("http://bar.com:443"), |
| 310 |
"double list does not permit http://bar.com:443"); |
| 311 |
ASSERT( doubleSourceList.permits("https://foo.com:88"), |
| 312 |
"double list permits https://foo.com:88"); |
| 313 |
ASSERT( !doubleSourceList.permits("foo.com"), |
| 314 |
"double list does not permit foo.com"); |
| 315 |
|
| 316 |
ASSERT( allSourceList.permits("http://x.com:23"), |
| 317 |
"* does not permit specific host"); |
| 318 |
ASSERT( allSourceList.permits("http://a.b.c.d.e.f.x.com"), |
| 319 |
"* does not permit a long host with no port"); |
| 320 |
|
| 321 |
}); |
| 322 |
|
| 323 |
test( |
| 324 |
function test_CSPSourceList_intersect() { |
| 325 |
var nullSourceList = CSPSourceList.fromString("none"); |
| 326 |
var simpleSourceList = CSPSourceList.fromString("a.com"); |
| 327 |
var doubleSourceList = CSPSourceList.fromString("https://foo.com http://bar.com:88"); |
| 328 |
var singleFooSourceList = CSPSourceList.fromString("https://foo.com"); |
| 329 |
var allSourceList = CSPSourceList.fromString("*"); |
| 330 |
|
| 331 |
ASSERT(nullSourceList.intersectWith(simpleSourceList).isNone(), |
| 332 |
"Intersection of one source with 'none' source list should be none."); |
| 333 |
ASSERT(nullSourceList.intersectWith(doubleSourceList).isNone(), |
| 334 |
"Intersection of two sources with 'none' source list should be none."); |
| 335 |
ASSERT(nullSourceList.intersectWith(allSourceList).isNone(), |
| 336 |
"Intersection of '*' with 'none' source list should be none."); |
| 337 |
|
| 338 |
ASSERT_EQUALS(allSourceList.intersectWith(simpleSourceList), simpleSourceList, |
| 339 |
"Intersection of one source with '*' source list should be one source."); |
| 340 |
ASSERT_EQUALS(allSourceList.intersectWith(doubleSourceList), doubleSourceList, |
| 341 |
"Intersection of two sources with '*' source list should be two sources."); |
| 342 |
|
| 343 |
ASSERT(simpleSourceList.intersectWith(doubleSourceList).isNone(), |
| 344 |
"Non-overlapping source lists should intersect to 'none'"); |
| 345 |
|
| 346 |
ASSERT_EQUALS(singleFooSourceList, |
| 347 |
doubleSourceList.intersectWith(singleFooSourceList), |
| 348 |
"subset and superset should intersect to subset."); |
| 349 |
|
| 350 |
//TODO: write more tests? |
| 351 |
|
| 352 |
}); |
| 353 |
|
| 354 |
///////////////////// Test the Whole CSP rep object ////////////////////// |
| 355 |
|
| 356 |
test( |
| 357 |
function test_CSPRep_fromString() { |
| 358 |
|
| 359 |
// check default init |
| 360 |
//ASSERT(!(new CSPRep())._isInitialized, "Uninitialized rep thinks it is.") |
| 361 |
|
| 362 |
var cspr; |
| 363 |
var cspr_allowval; |
| 364 |
|
| 365 |
// check default policy "allow *" |
| 366 |
cspr = CSPRep.fromString("allow *"); |
| 367 |
ASSERT_HAS_KEY(cspr._directives, CSPRep.SRC_DIRECTIVES.ALLOW, |
| 368 |
"ALLOW directive is missing when specified in fromString"); |
| 369 |
|
| 370 |
// ... and check that the other directives were auto-filled with the |
| 371 |
// ALLOW one. |
| 372 |
var SD = CSPRep.SRC_DIRECTIVES; |
| 373 |
cspr_allowval = cspr._directives[SD.ALLOW]; |
| 374 |
for(var d in CSPRep.SRC_DIRECTIVES) { |
| 375 |
ASSERT_HAS_KEY(cspr._directives, SD[d], "Missing key " + d); |
| 376 |
ASSERT_EQ(cspr._directives[SD[d]].toString(), cspr_allowval.toString(), |
| 377 |
"Implicit directive " + d + " has non-allow value."); |
| 378 |
} |
| 379 |
}); |
| 380 |
|
| 381 |
|
| 382 |
test( |
| 383 |
function test_CSPRep_fromString_oneDir() { |
| 384 |
|
| 385 |
var cspr; |
| 386 |
var SD = CSPRep.SRC_DIRECTIVES; |
| 387 |
var DEFAULTS = [SD.STYLE_SRC, SD.MEDIA_SRC, SD.IMG_SRC, |
| 388 |
SD.FRAME_ANCESTORS, SD.FRAME_SRC]; |
| 389 |
|
| 390 |
// check one-directive policies |
| 391 |
cspr = CSPRep.fromString("allow bar.com; script-src https://foo.com"); |
| 392 |
|
| 393 |
for(var x in DEFAULTS) { |
| 394 |
ASSERT(cspr.permits("http://bar.com:22", DEFAULTS[x]), |
| 395 |
DEFAULTS[x] + " does not use default rule."); |
| 396 |
ASSERT(!cspr.permits("https://foo.com:400", DEFAULTS[x]), |
| 397 |
DEFAULTS[x] + " does not use default rule."); |
| 398 |
} |
| 399 |
ASSERT(!cspr.permits("http://bar.com:22", SD.SCRIPT_SRC), |
| 400 |
"script-src false positive in policy."); |
| 401 |
ASSERT(cspr.permits("https://foo.com:400", SD.SCRIPT_SRC), |
| 402 |
"script-src false negative in policy."); |
| 403 |
}); |
| 404 |
|
| 405 |
test( |
| 406 |
function test_CSPRep_fromString_twodir() { |
| 407 |
var cspr; |
| 408 |
var SD = CSPRep.SRC_DIRECTIVES; |
| 409 |
var DEFAULTS = [SD.STYLE_SRC, SD.MEDIA_SRC, SD.FRAME_ANCESTORS, SD.FRAME_SRC]; |
| 410 |
|
| 411 |
// check two-directive policies |
| 412 |
cspr = CSPRep.fromString("allow allow.com; " |
| 413 |
+ "script-src https://foo.com; " |
| 414 |
+ "img-src bar.com"); |
| 415 |
for(var x in DEFAULTS) { |
| 416 |
ASSERT(cspr.permits("http://allow.com:22", DEFAULTS[x]), |
| 417 |
DEFAULTS[x] + " does not use default rule."); |
| 418 |
ASSERT(!cspr.permits("https://foo.com:400", DEFAULTS[x]), |
| 419 |
DEFAULTS[x] + " does not use default rule."); |
| 420 |
ASSERT(!cspr.permits("http://bar.com:400", DEFAULTS[x]), |
| 421 |
DEFAULTS[x] + " does not use default rule."); |
| 422 |
} |
| 423 |
ASSERT(!cspr.permits("http://allow.com:22", SD.IMG_SRC), |
| 424 |
"img-src does not use default rule."); |
| 425 |
ASSERT(!cspr.permits("https://foo.com:400", SD.IMG_SRC), |
| 426 |
"img-src does not use default rule."); |
| 427 |
ASSERT(cspr.permits("http://bar.com:400", SD.IMG_SRC), |
| 428 |
"img-src does not use default rule."); |
| 429 |
|
| 430 |
ASSERT(!cspr.permits("http://allow.com:22", SD.SCRIPT_SRC), |
| 431 |
"script-src does not use default rule."); |
| 432 |
ASSERT(cspr.permits("https://foo.com:400", SD.SCRIPT_SRC), |
| 433 |
"script-src does not use default rule."); |
| 434 |
ASSERT(!cspr.permits("http://bar.com:400", SD.SCRIPT_SRC), |
| 435 |
"script-src does not use default rule."); |
| 436 |
}); |
| 437 |
|
| 438 |
test(function test_CSPRep_fromString_withself() { |
| 439 |
var cspr; |
| 440 |
var SD = CSPRep.SRC_DIRECTIVES; |
| 441 |
var self = "https://self.com:34"; |
| 442 |
|
| 443 |
// check one-directive policies |
| 444 |
cspr = CSPRep.fromString("allow self; script-src self https://*:*", |
| 445 |
self); |
| 446 |
ASSERT(!cspr.permits("https://foo.com:400", SD.IMG_SRC), |
| 447 |
"img-src does not enforce default rule, 'self'."); |
| 448 |
ASSERT(cspr.permits(self, SD.IMG_SRC), |
| 449 |
"img-src does not allow self"); |
| 450 |
ASSERT(!cspr.permits("http://evil.com", SD.SCRIPT_SRC), |
| 451 |
"script-src is too relaxed"); |
| 452 |
ASSERT(cspr.permits(self, SD.SCRIPT_SRC), |
| 453 |
"script-src should allow self"); |
| 454 |
ASSERT(cspr.permits("https://evil.com:100", SD.SCRIPT_SRC), |
| 455 |
"script-src is too strict on host/port"); |
| 456 |
}); |
| 457 |
|
| 458 |
///////////////////// TEST POLICY_URI ////////////////////// |
| 459 |
// TODO: set up a mock server and serve the policy URI |
| 460 |
test(function test_CSPRep_fromPolicyURI() { |
| 461 |
var cspr; |
| 462 |
var SD = CSPRep.SRC_DIRECTIVES; |
| 463 |
var self = "http://localhost:3400"; |
| 464 |
|
| 465 |
cspr = CSPRep.fromString("policy-uri " + POLICY_URI, self); |
| 466 |
cspr_static = CSPRep.fromString(POLICY_FROM_URI, self); |
| 467 |
|
| 468 |
ASSERT_NOT_NULL(cspr, "policy-uri failed to load"); |
| 469 |
|
| 470 |
// other directives inherit self |
| 471 |
for(var i in SD) { |
| 472 |
ASSERT_EQUALS(cspr._directives[SD[i]], |
| 473 |
cspr_static._directives[SD[i]], |
| 474 |
SD[i] + " parsed wrong from policy uri"); |
| 475 |
} |
| 476 |
}); |
| 477 |
|
| 478 |
test(function test_CSPRep_fromPolicyURI_failswhenmixed() { |
| 479 |
var cspr; |
| 480 |
var self = "http://localhost:3400"; |
| 481 |
var closed_policy = CSPRep.fromString("allow none"); |
| 482 |
var my_uri_policy = "policy-uri http://localhost:3400/policy"; |
| 483 |
|
| 484 |
print(" --- Ignore the following two errors if they print ---"); |
| 485 |
cspr = CSPRep.fromString("allow *; " + my_uri_policy, self); |
| 486 |
ASSERT_EQUALS(cspr, closed_policy, |
| 487 |
"Parsing should fail when 'policy-uri' " + |
| 488 |
"is mixed with allow directive"); |
| 489 |
cspr = CSPRep.fromString("img-src self; " + my_uri_policy, self); |
| 490 |
ASSERT_EQUALS(cspr, closed_policy, |
| 491 |
"Parsing should fail when 'policy-uri' " + |
| 492 |
"is mixed with other directives"); |
| 493 |
print(" --- Stop ignoring errors that print ---\n"); |
| 494 |
|
| 495 |
}); |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
///////////////////// END TESTING ////////////////////// |
| 500 |
|
| 501 |
//teardown |
| 502 |
httpServer.stop(function() { }); |
| 503 |
|
| 504 |
if(g_passed < g_tests) { |
| 505 |
print(g_tests + " tests run: " + g_passed + " passed, " + (g_tests-g_passed) + " failure(s)."); |
| 506 |
} else { |
| 507 |
print("All " + g_tests + " tests passed."); |
| 508 |
} |