summaryrefslogtreecommitdiffstats
path: root/dom/security/test/general/test_same_site_cookies_about.html
blob: faf2caab9a76f4b66cd41241aea9e7d8392aabb1 (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Bug 1454721 - Add same-site cookie test for about:blank and about:srcdoc</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<img id="cookieImage">
<iframe id="testframe"></iframe>

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

/*
 * Description of the test:
 * 1) We load an image from http://mochi.test which sets a same site cookie
 * 2) We then load the following iframes:
 *    (a) cross-origin iframe
 *    (b) same-origin iframe
 *    which both load a:
 *     * nested about:srcdoc frame and nested about:blank frame
 *     * navigate about:srcdoc frame and navigate about:blank frame
 * 3) We evaluate that the same-site cookie is available in the same-origin case.
 */

SimpleTest.waitForExplicitFinish();

const SAME_ORIGIN = "http://mochi.test:8888/"
const CROSS_ORIGIN = "http://example.com/";
const PATH = "tests/dom/security/test/general/file_same_site_cookies_about.sjs";

let curTest = 0;

var tests = [
  // NAVIGATION TESTS
  {
    description: "nested same origin iframe about:srcdoc navigation [mochi.test -> mochi.test -> about:srcdoc -> mochi.test]",
    frameSRC: SAME_ORIGIN + PATH + "?loadsrcdocframeNav",
    result: "myKey=mySameSiteAboutCookie", // cookie should be set for baseline test
  },
  {
    description: "nested cross origin iframe about:srcdoc navigation [mochi.test -> example.com -> about:srcdoc -> mochi.test]",
    frameSRC: CROSS_ORIGIN + PATH + "?loadsrcdocframeNav",
    result: "", // no same-site cookie should be available
  },
  {
    description: "nested same origin iframe about:blank navigation [mochi.test -> mochi.test -> about:blank -> mochi.test]",
    frameSRC: SAME_ORIGIN + PATH + "?loadblankframeNav",
    result: "myKey=mySameSiteAboutCookie", // cookie should be set for baseline test
  },
  {
    description: "nested cross origin iframe about:blank navigation [mochi.test -> example.com -> about:blank -> mochi.test]",
    frameSRC: CROSS_ORIGIN + PATH + "?loadblankframeNav",
    result: "", // no same-site cookie should be available
  },
  // INCLUSION TESTS
  {
    description: "nested same origin iframe about:srcdoc inclusion [mochi.test -> mochi.test -> about:srcdoc -> mochi.test]",
    frameSRC: SAME_ORIGIN + PATH + "?loadsrcdocframeInc",
    result: "myKey=mySameSiteAboutCookie", // cookie should be set for baseline test
  },
  {
    description: "nested cross origin iframe about:srcdoc inclusion [mochi.test -> example.com -> about:srcdoc -> mochi.test]",
    frameSRC: CROSS_ORIGIN + PATH + "?loadsrcdocframeInc",
    result: "", // no same-site cookie should be available
  },
  {
    description: "nested same origin iframe about:blank inclusion [mochi.test -> mochi.test -> about:blank -> mochi.test]",
    frameSRC: SAME_ORIGIN + PATH + "?loadblankframeInc",
    result: "myKey=mySameSiteAboutCookie", // cookie should be set for baseline test
  },
  {
    description: "nested cross origin iframe about:blank inclusion [mochi.test -> example.com -> about:blank -> mochi.test]",
    frameSRC: CROSS_ORIGIN + PATH + "?loadblankframeInc",
    result: "", // no same-site cookie should be available
  },
];

window.addEventListener("message", receiveMessage);
function receiveMessage(event) {
  is(event.data.result, tests[curTest].result, tests[curTest].description);
  curTest += 1;

  // lets see if we ran all the tests
  if (curTest == tests.length) {
    window.removeEventListener("message", receiveMessage);
    SimpleTest.finish();
    return;
  }
  // otherwise it's time to run the next test
  setCookieAndInitTest();
}

function setupQueryResultAndRunTest() {
  let testframe = document.getElementById("testframe");
  testframe.src = tests[curTest].frameSRC + curTest;
}

function setCookieAndInitTest() {
  var cookieImage = document.getElementById("cookieImage");
  cookieImage.onload = function() {
    ok(true, "trying to set cookie for test (" + tests[curTest].description + ")");
    setupQueryResultAndRunTest();
  }
  cookieImage.onerror = function() {
    ok(false, "could not load image for test (" + tests[curTest].description + ")");
  }
  cookieImage.src = SAME_ORIGIN + PATH + "?setSameSiteCookie" + curTest;
}

// fire up the test
setCookieAndInitTest();

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