File: test_break_endless_upgrade_downgrade_loop.html

package info (click to toggle)
firefox 147.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,324 kB
  • sloc: cpp: 7,607,156; javascript: 6,532,492; ansic: 3,775,158; python: 1,415,368; xml: 634,556; asm: 438,949; java: 186,241; sh: 62,751; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (101 lines) | stat: -rw-r--r-- 3,552 bytes parent folder | download | duplicates (12)
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
<!DOCTYPE HTML>
<html>
<head>
<title>Bug 1691888: Break endless upgrade downgrade loops when using https-only</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>

<script class="testbody" type="text/javascript">
"use strict";
/*
 * Description of the test:
 * We perform three tests where our upgrade/downgrade redirect loop detector should break the
 * endless loop:
 * Test 1: Meta Refresh
 * Test 2: JS Redirect
 * Test 3: 302 redirect
 * Test 4: Redirect to different origin. No redirect loop should be detected
 */

SimpleTest.waitForExplicitFinish();

const HTTP_REQUEST_URL =
  "http://example.com/tests/dom/security/test/https-only/file_break_endless_upgrade_downgrade_loop.sjs";
const HTTPS_REQUEST_URL =
  "https://example.com/tests/dom/security/test/https-only/file_break_endless_upgrade_downgrade_loop.sjs";

const testQueries = [
  // Test 1: Meta Refresh Redirect
  { scheme: "http", query: "test1", error: true },
  { scheme: "https", query: "test1", error: true },
  // Test 2: JS win.location Redirect
  { scheme: "http", query: "test2", error: true },
  { scheme: "https", query: "test2", error: true },
  // Test 3: 302 Redirect
  { scheme: "http", query: "test3", error: true },
  { scheme: "https", query: "test3", error: true },
  // Test 4: 302 Redirect with a different path
  { scheme: "http", query: "test4", error: false },
  { scheme: "https", query: "test4", error: false },
];

let currentTest = 0;
// do each test two time. One time starting with https:// one time with http://
let testWin;
window.addEventListener("message", receiveMessageWhenLoaded);

function postMessageWhenLoaded() {
  SimpleTest.waitForCondition(async () => {
      return await SpecialPowers.spawn(testWin, [], () => {
        let innerHTML = content.document.body.innerHTML;
        return innerHTML == "OK :)"
          || innerHTML == "DO NOT DISPLAY THIS"
          || innerHTML.includes("about-httpsonly-title-alert");
      }).catch(() => false);
    },
    () => window.postMessage("https-only-page-loaded", "*"),
    "waiting for page load to complete"
  );
}

async function receiveMessageWhenLoaded() {
  const currentTestParams = testQueries[currentTest];
  let testName = currentTestParams.scheme + ":" + currentTestParams.query

  let innerHTML = await SpecialPowers.spawn(testWin, [], () => {
    return content.document.body.innerHTML;
  });
  if(currentTestParams.error) {
    ok(innerHTML.includes("about-httpsonly-title-alert"), testName + ": the error page should be shown");
  } else {
    is(innerHTML, "OK :)", testName + ": different path with https loaded ");
  }
  testWin.close();

  if (++currentTest < testQueries.length) {
    runNextTest();
    return;
  }
  // no more tests to run -> cleanup
  window.removeEventListener("https-only-page-load", receiveMessageWhenLoaded);
  SimpleTest.finish();
}

async function runNextTest() {
  const currentTestParams = testQueries[currentTest];
  let uri = `${currentTestParams.scheme}://example.com/tests/dom/security/test/https-only/file_break_endless_upgrade_downgrade_loop.sjs?${currentTestParams.query}`;
  testWin = window.open(uri, "_blank");
  postMessageWhenLoaded();
}

SpecialPowers.pushPrefEnv({ set: [
    ["dom.security.https_only_mode", true],
    ["dom.security.https_only_mode_break_upgrade_downgrade_endless_loop", true],
    ["dom.security.https_only_mode_ever_enabled", true], // clear this pref at the end
  ]}, runNextTest);

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