summaryrefslogtreecommitdiffstats
path: root/dom/security/test/csp/file_iframe_srcdoc.sjs
blob: 9f8774e4ccbbdb196ffa3454b1436b43c0480868 (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
// Custom *.sjs file specifically for the needs of
// https://bugzilla.mozilla.org/show_bug.cgi?id=1073952

"use strict";
Components.utils.importGlobalProperties(["URLSearchParams"]);

const SCRIPT = `
  <script>
    parent.parent.postMessage({result: &quot;allowed&quot;}, &quot;*&quot;);
  </script>`;

const SIMPLE_IFRAME_SRCDOC =
  `
  <!DOCTYPE html>
  <html>
  <head><meta charset="utf-8"></head>
  <body>
    <iframe sandbox="allow-scripts" srcdoc="` +
  SCRIPT +
  `"></iframe>
  </body>
  </html>`;

const INNER_SRCDOC_IFRAME = `
  <iframe sandbox='allow-scripts' srcdoc='<script>
      parent.parent.parent.postMessage({result: &quot;allowed&quot;}, &quot;*&quot;);
    </script>'>
  </iframe>`;

const NESTED_IFRAME_SRCDOC =
  `
  <!DOCTYPE html>
  <html>
  <head><meta charset="utf-8"></head>
  <body>
    <iframe sandbox="allow-scripts" srcdoc="` +
  INNER_SRCDOC_IFRAME +
  `"></iframe>
  </body>
  </html>`;

const INNER_DATAURI_IFRAME = `
  <iframe sandbox='allow-scripts' src='data:text/html,<script>
      parent.parent.parent.postMessage({result: &quot;allowed&quot;}, &quot;*&quot;);
    </script>'>
  </iframe>`;

const NESTED_IFRAME_SRCDOC_DATAURI =
  `
  <!DOCTYPE html>
  <html>
  <head><meta charset="utf-8"></head>
  <body>
    <iframe sandbox="allow-scripts" srcdoc="` +
  INNER_DATAURI_IFRAME +
  `"></iframe>
  </body>
  </html>`;

function handleRequest(request, response) {
  const query = new URLSearchParams(request.queryString);

  response.setHeader("Cache-Control", "no-cache", false);
  if (typeof query.get("csp") === "string") {
    response.setHeader("Content-Security-Policy", query.get("csp"), false);
  }
  response.setHeader("Content-Type", "text/html", false);

  if (query.get("action") === "simple_iframe_srcdoc") {
    response.write(SIMPLE_IFRAME_SRCDOC);
    return;
  }

  if (query.get("action") === "nested_iframe_srcdoc") {
    response.write(NESTED_IFRAME_SRCDOC);
    return;
  }

  if (query.get("action") === "nested_iframe_srcdoc_datauri") {
    response.write(NESTED_IFRAME_SRCDOC_DATAURI);
    return;
  }

  // we should never get here, but just in case
  // return something unexpected
  response.write("do'h");
}