File: test_scheme_relative_sources.html

package info (click to toggle)
firefox-esr 128.14.0esr-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,230,248 kB
  • sloc: cpp: 7,104,262; javascript: 6,088,450; ansic: 3,654,017; python: 1,212,326; xml: 594,604; asm: 420,654; java: 182,969; sh: 71,124; makefile: 20,739; perl: 13,449; objc: 12,399; yacc: 4,583; cs: 3,846; pascal: 2,973; lex: 1,720; ruby: 1,194; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10
file content (91 lines) | stat: -rw-r--r-- 2,221 bytes parent folder | download | duplicates (22)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<!DOCTYPE HTML>
<html>
<head>
  <title>Bug 921493 - CSP: test allowlisting of scheme-relative sources</title>
  <!-- Including SimpleTest.js so we can use waitForExplicitFinish !-->
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<iframe style="width:100%;" id="testframe"></iframe>

<script class="testbody" type="text/javascript">

SimpleTest.waitForExplicitFinish();

/* Description of the test:
 * We load http and https pages and verify that scheme relative sources
 * are allowed unless its a downgrade from https -> http.
 *
 * Please note that the policy contains 'unsafe-inline' so we can use
 * an inline script to query the result from within the sandboxed iframe
 * and report it back to the parent document.
 */

var POLICY = "default-src 'none'; script-src 'unsafe-inline' example.com;";

var tests = [
  {
    description: "http -> http",
    from: "http",
    to: "http",
    result: "allowed",
  },
  {
    description: "http -> https",
    from: "http",
    to: "https",
    result: "allowed",
  },
  {
    description: "https -> https",
    from: "https",
    to: "https",
    result: "allowed",
  },
  {
    description: "https -> http",
    from: "https",
    to: "http",
    result: "blocked",
  }
];

var counter = 0;
var curTest;

function loadNextTest() {
  if (counter == tests.length) {
    window.removeEventListener("message", receiveMessage);
    SimpleTest.finish();
    return;
  }

  curTest = tests[counter++];

  var src = curTest.from +
             "://example.com/tests/dom/security/test/csp/file_scheme_relative_sources.sjs" +
            "?scheme=" + curTest.to +
            "&policy=" + escape(POLICY);

  document.getElementById("testframe").src = src;
}

// using a postMessage handler to report the result back from
// within the sandboxed iframe without 'allow-same-origin'.
window.addEventListener("message", receiveMessage);

function receiveMessage(event) {

  is(event.data.result, curTest.result,
     "should be " + curTest.result + " in test (" + curTest.description + ")!");

  loadNextTest();
}

// get the test started
loadNextTest();

</script>
</body>
</html>