summaryrefslogtreecommitdiffstats
path: root/dom/security/test/csp/file_evalscript_main.js
blob: cc2c3fbe73b125c4c349a3bfcde12a93fc2f5580 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// some javascript for the CSP eval() tests

function logResult(str, passed) {
  var elt = document.createElement("div");
  var color = passed ? "#cfc;" : "#fcc";
  elt.setAttribute(
    "style",
    "background-color:" +
      color +
      "; width:100%; border:1px solid black; padding:3px; margin:4px;"
  );
  elt.innerHTML = str;
  document.body.appendChild(elt);
}

window._testResults = {};

// check values for return values from blocked timeout or intervals
var verifyZeroRetVal = (function (window) {
  return function (val, details) {
    logResult(
      (val === 0 ? "PASS: " : "FAIL: ") +
        "Blocked interval/timeout should have zero return value; " +
        details,
      val === 0
    );
    window.parent.verifyZeroRetVal(val, details);
  };
})(window);

// callback for when stuff is allowed by CSP
var onevalexecuted = (function (window) {
  return function (shouldrun, what, data) {
    window._testResults[what] = "ran";
    window.parent.scriptRan(shouldrun, what, data);
    logResult(
      (shouldrun ? "PASS: " : "FAIL: ") + what + " : " + data,
      shouldrun
    );
  };
})(window);

// callback for when stuff is blocked
var onevalblocked = (function (window) {
  return function (shouldrun, what, data) {
    window._testResults[what] = "blocked";
    window.parent.scriptBlocked(shouldrun, what, data);
    logResult(
      (shouldrun ? "FAIL: " : "PASS: ") + what + " : " + data,
      !shouldrun
    );
  };
})(window);

// Defer until document is loaded so that we can write the pretty result boxes
// out.
addEventListener(
  "load",
  function () {
    // setTimeout(String) test -- mutate something in the window._testResults
    // obj, then check it.
    {
      var str_setTimeoutWithStringRan =
        'onevalexecuted(false, "setTimeout(String)", "setTimeout with a string was enabled.");';
      function fcn_setTimeoutWithStringCheck() {
        if (this._testResults["setTimeout(String)"] !== "ran") {
          onevalblocked(
            false,
            "setTimeout(String)",
            "setTimeout with a string was blocked"
          );
        }
      }
      setTimeout(fcn_setTimeoutWithStringCheck.bind(window), 10);
      var res = setTimeout(str_setTimeoutWithStringRan, 10);
      verifyZeroRetVal(res, "setTimeout(String)");
    }

    // setInterval(String) test -- mutate something in the window._testResults
    // obj, then check it.
    {
      var str_setIntervalWithStringRan =
        'onevalexecuted(false, "setInterval(String)", "setInterval with a string was enabled.");';
      function fcn_setIntervalWithStringCheck() {
        if (this._testResults["setInterval(String)"] !== "ran") {
          onevalblocked(
            false,
            "setInterval(String)",
            "setInterval with a string was blocked"
          );
        }
      }
      setTimeout(fcn_setIntervalWithStringCheck.bind(window), 10);
      var res = setInterval(str_setIntervalWithStringRan, 10);
      verifyZeroRetVal(res, "setInterval(String)");

      // emergency cleanup, just in case.
      if (res != 0) {
        setTimeout(function () {
          clearInterval(res);
        }, 15);
      }
    }

    // setTimeout(function) test -- mutate something in the window._testResults
    // obj, then check it.
    {
      function fcn_setTimeoutWithFunctionRan() {
        onevalexecuted(
          true,
          "setTimeout(function)",
          "setTimeout with a function was enabled."
        );
      }
      function fcn_setTimeoutWithFunctionCheck() {
        if (this._testResults["setTimeout(function)"] !== "ran") {
          onevalblocked(
            true,
            "setTimeout(function)",
            "setTimeout with a function was blocked"
          );
        }
      }
      setTimeout(fcn_setTimeoutWithFunctionRan.bind(window), 10);
      setTimeout(fcn_setTimeoutWithFunctionCheck.bind(window), 10);
    }

    // eval() test -- should throw exception as per spec
    try {
      eval('onevalexecuted(false, "eval(String)", "eval() was enabled.");');
    } catch (e) {
      onevalblocked(false, "eval(String)", "eval() was blocked");
    }

    // eval(foo,bar) test -- should throw exception as per spec
    try {
      eval(
        'onevalexecuted(false, "eval(String,scope)", "eval() was enabled.");',
        1
      );
    } catch (e) {
      onevalblocked(
        false,
        "eval(String,object)",
        "eval() with scope was blocked"
      );
    }

    // [foo,bar].sort(eval) test -- should throw exception as per spec
    try {
      [
        'onevalexecuted(false, "[String, obj].sort(eval)", "eval() was enabled.");',
        1,
      ].sort(eval);
    } catch (e) {
      onevalblocked(
        false,
        "[String, obj].sort(eval)",
        "eval() with scope via sort was blocked"
      );
    }

    // [].sort.call([foo,bar], eval) test -- should throw exception as per spec
    try {
      [].sort.call(
        [
          'onevalexecuted(false, "[String, obj].sort(eval)", "eval() was enabled.");',
          1,
        ],
        eval
      );
    } catch (e) {
      onevalblocked(
        false,
        "[].sort.call([String, obj], eval)",
        "eval() with scope via sort/call was blocked"
      );
    }

    // new Function() test -- should throw exception as per spec
    try {
      var fcn = new Function(
        'onevalexecuted(false, "new Function(String)", "new Function(String) was enabled.");'
      );
      fcn();
    } catch (e) {
      onevalblocked(
        false,
        "new Function(String)",
        "new Function(String) was blocked."
      );
    }

    // ShadowRealm.prototype.evaluate -- should throw exception as per spec.
    try {
      var sr = new ShadowRealm();
      sr.evaluate("var x = 10");
      onevalexecuted(
        false,
        "ShadowRealm.prototype.evaluate(String)",
        "ShadowRealm.prototype.evaluate(String) was enabled."
      );
    } catch (e) {
      onevalblocked(
        false,
        "ShadowRealm.prototype.evaluate(String)",
        "ShadowRealm.prototype.evaluate(String) was blocked."
      );
    }

    // setTimeout(eval, 0, str)
    {
      // error is not catchable here, instead, we're going to side-effect
      // 'worked'.
      var worked = false;

      setTimeout(eval, 0, "worked = true");
      setTimeout(
        function (worked) {
          if (worked) {
            onevalexecuted(
              false,
              "setTimeout(eval, 0, str)",
              "setTimeout(eval, 0, string) was enabled."
            );
          } else {
            onevalblocked(
              false,
              "setTimeout(eval, 0, str)",
              "setTimeout(eval, 0, str) was blocked."
            );
          }
        },
        0,
        worked
      );
    }
  },
  false
);