summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/content-security-policy/nonce-hiding/script-nonces-hidden.html
blob: d9718d904c24326b86bd2853dcdfebe11d644da5 (plain)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
162
163
164
165
166
167
168
169
170
171
172
<!DOCTYPE html>
<script src="/resources/testharness.js" nonce="abc"></script>
<script src="/resources/testharnessreport.js" nonce="abc"></script>

<!-- `Content-Security-Policy: script-src 'nonce-abc'; img-src 'none'` delivered via headers -->

<body>
<!-- Basics -->
<script nonce="abc" id="testScript">
  document.currentScript.setAttribute('executed', 'yay');
</script>

<script nonce="abc">
    var script = document.querySelector('#testScript');

    test(t => {
      // Query Selector
      assert_equals(document.querySelector('body [nonce]'), script);
      assert_equals(document.querySelector('body [nonce=""]'), script);
      assert_equals(document.querySelector('body [nonce=abc]'), null);

      assert_equals(script.getAttribute('nonce'), '');
      assert_equals(script.nonce, 'abc');
    }, "Reading 'nonce' content attribute and IDL attribute.");

    // Clone node.
    test(t => {
      script.setAttribute('executed', 'boo');
      var s2 = script.cloneNode();
      assert_equals(s2.nonce, 'abc', 'IDL attribute');
      assert_equals(s2.getAttribute('nonce'), '');
    }, "Cloned node retains nonce.");

    async_test(t => {
      var s2 = script.cloneNode();
      document.head.appendChild(s2);
      assert_equals(s2.nonce, 'abc');
      assert_equals(s2.getAttribute('nonce'), '');

      window.addEventListener('load', t.step_func_done(_ => {
        // The cloned script won't execute, as its 'already started' flag is set.
        assert_equals(s2.getAttribute('executed'), 'boo');
      }));
    }, "Cloned node retains nonce when inserted.");

    // Set the content attribute to 'foo'
    test(t => {
      script.setAttribute('nonce', 'foo');
      assert_equals(script.getAttribute('nonce'), 'foo');
      assert_equals(script.nonce, 'foo');
    }, "Writing 'nonce' content attribute.");

    // Set the IDL attribute to 'bar'
    test(t => {
      script.nonce = 'bar';
      assert_equals(script.nonce, 'bar');
      assert_equals(script.getAttribute('nonce'), 'foo');
    }, "Writing 'nonce' IDL attribute.");

    // Fragment parser.
    var documentWriteTest = async_test("Document-written script executes.");
    document.write(`<script nonce='abc'>
      documentWriteTest.done();
      test(t => {
        var script = document.currentScript;
        assert_equals(script.getAttribute('nonce'), '');
        assert_equals(script.nonce, 'abc');
      }, "Document-written script's nonce value.");
    </scr` + `ipt>`);

    // Create node.
    async_test(t => {
      var s = document.createElement('script');
      s.innerText = script.innerText;
      s.nonce = 'abc';
      assert_equals(s.nonce, 'abc');
      assert_equals(s.getAttribute('nonce'), null);
      document.head.appendChild(s);
      assert_equals(s.nonce, 'abc');
      assert_equals(s.getAttribute('nonce'), null);

      window.addEventListener('load', t.step_func_done(_ => {
        assert_equals(s.getAttribute('executed'), 'yay');
      }));
    }, "createElement.nonce.");

    async_test(t => {
      var s = document.createElement('script');
      s.innerText = script.innerText;
      s.nonce = 'zyx';
      s.setAttribute('nonce', 'abc');
      assert_equals(s.nonce, 'abc');
      document.head.appendChild(s);
      assert_equals(s.nonce, 'abc');
      assert_equals(s.getAttribute('nonce'), '');

      window.addEventListener('load', t.step_func_done(_ => {
        assert_equals(s.getAttribute('executed'), 'yay');
      }));
    }, "setAttribute('nonce') overwrites '.nonce' upon insertion.");

    // Create node.
    async_test(t => {
      var s = document.createElement('script');
      s.innerText = script.innerText;
      s.setAttribute('nonce', 'abc');
      assert_equals(s.getAttribute('nonce'), 'abc', "Pre-insertion content");
      assert_equals(s.nonce, 'abc', "Pre-insertion IDL");
      document.head.appendChild(s);
      assert_equals(s.nonce, 'abc', "Post-insertion IDL");
      assert_equals(s.getAttribute('nonce'), '', "Post-insertion content");

      window.addEventListener('load', t.step_func_done(_ => {
        assert_equals(s.getAttribute('executed'), 'yay');
      }));
    }, "createElement.setAttribute.");
</script>

<!-- Custom Element -->
<script nonce="abc">
  var eventList = [];
  class NonceElement extends HTMLElement {
    static get observedAttributes() {
      return ['nonce'];
    }

    constructor() {
      super();
    }

    attributeChangedCallback(name, oldValue, newValue) {
      eventList.push({
        type: "AttributeChanged",
        name: name,
        oldValue: oldValue,
        newValue: newValue
      });
    }

    connectedCallback() {
      eventList.push({
        type: "Connected",
      });
    }
  }

  customElements.define("nonce-element", NonceElement);
</script>
<nonce-element nonce="abc"></nonce-element>
<script nonce="abc">
  test(t => {
    assert_object_equals(eventList[0], { type: "AttributeChanged", name: "nonce", oldValue: null, newValue: "abc" }, "AttributeChanged 1");
    assert_object_equals(eventList[1], { type: "Connected" }, "Connected");
    assert_object_equals(eventList[2], { type: "AttributeChanged", name: "nonce", oldValue: "abc", newValue: "" }, "AttributeChanged 2");
    assert_equals(eventList.length, 3);
  }, "Custom elements expose the correct events.");
</script>

<!-- CSS Leakage -->
<style>
    #cssTest { display: block; }
    #cssTest[nonce=abc] { background: url(/security/resources/abe.png); }
</style>
<script nonce="abc" id="cssTest">
    test(t => {
      const script = document.querySelector('#cssTest');
      t.add_cleanup(() => script.remove());
      var style = getComputedStyle(script);
      assert_equals(style['display'], 'block');
      assert_equals(style['background-image'], 'none');
    }, "Nonces don't leak via CSS side-channels.");
</script>