summaryrefslogtreecommitdiffstats
path: root/dom/security/test/https-only/test_http_background_request.html
blob: a21eb61ba7d9edc070d6908a02b09e0535f4eaf1 (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Bug 1663396: Test HTTPS-Only-Mode top-level background request not leaking sensitive info</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">

/*
 * Description of the test:
 * Send a top-level request and make sure that the the top-level https-only background request
 * (a) does only use pre-path information
 * (b) does not happen if the pref is set to false
 */

SimpleTest.waitForExplicitFinish();
SimpleTest.requestFlakyTimeout("have to test that https-only mode background request does not happen");
SimpleTest.requestLongerTimeout(8);

const SJS_PATH = "tests/dom/security/test/https-only/file_http_background_request.sjs?sensitive";

const EXPECTED_KICK_OFF_REQUEST = "http://example.com/" + SJS_PATH;
const EXPECTED_KICK_OFF_REQUEST_LOCAL = "http://localhost:8/" + SJS_PATH;
const EXPECTED_UPGRADE_REQUEST = EXPECTED_KICK_OFF_REQUEST.replace("http://", "https://");
let expectedBackgroundRequest = "";
let requestCounter = 0;

function examiner() {
  SpecialPowers.addObserver(this, "specialpowers-http-notify-request");
}
examiner.prototype  = {
  observe(subject, topic, data) {
    if (topic !== "specialpowers-http-notify-request") {
      return;
    }
    // On Android we have other requests appear here as well. Let's make
    // sure we only evaluate requests triggered by the test.
    if (!data.startsWith("http://example.com") &&
        !data.startsWith("https://example.com") &&
        !data.startsWith("http://localhost:8") &&
        !data.startsWith("https://localhost:8")) {
      return;
    }
    ++requestCounter;
    if (requestCounter == 1) {
      ok(
        data === EXPECTED_KICK_OFF_REQUEST || data === EXPECTED_KICK_OFF_REQUEST_LOCAL,
        "kick off request needs to be http"
      );
      return;
    }
    if (requestCounter == 2) {
      is(data, EXPECTED_UPGRADE_REQUEST, "upgraded request needs to be https");
      return;
    }
    if (requestCounter == 3) {
      is(data, expectedBackgroundRequest, "background request needs to be http and no sensitive info like path");
       return;
    }
    ok(false, "we should never get here, but just in case");
  },
  remove() {
    SpecialPowers.removeObserver(this, "specialpowers-http-notify-request");
  }
}
window.BackgroundRequestExaminer = new examiner();

// https-only top-level background request occurs after 3 seconds, hence
// we use 4 seconds to make sure the background request did not happen.
function resolveAfter4Seconds() { 
  return new Promise(resolve => {
    setTimeout(() => {
      resolve();
    }, 4000);
  });
}

async function runTests() {
  // (a) Test http background request to only use prePath information
  expectedBackgroundRequest = "http://example.com/";
  requestCounter = 0;
  await SpecialPowers.pushPrefEnv({ set: [
    ["dom.security.https_only_mode", true],
    ["dom.security.https_only_mode_send_http_background_request", true],
  ]});
  let testWin = window.open(EXPECTED_KICK_OFF_REQUEST, "_blank");
  await resolveAfter4Seconds();
  is(requestCounter, 3, "three requests total (kickoff, upgraded, background)");
  testWin.close();

  // (x) Test no http background request happens when localhost
  expectedBackgroundRequest = "";
  requestCounter = 0;
  testWin = window.open(EXPECTED_KICK_OFF_REQUEST_LOCAL, "_blank");
  await resolveAfter4Seconds();
  is(requestCounter, 1, "one requests total (kickoff, no upgraded, no background)");
  testWin.close();

  // (b) Test no http background request happens if pref is set to false
  expectedBackgroundRequest = "";
  requestCounter = 0;
  await SpecialPowers.pushPrefEnv({ set: [
    ["dom.security.https_only_mode", true],
    ["dom.security.https_only_mode_send_http_background_request", false],
  ]});
  testWin = window.open(EXPECTED_KICK_OFF_REQUEST, "_blank");
  await resolveAfter4Seconds();
  is(requestCounter, 2, "two requests total (kickoff, upgraded, no background)");
  testWin.close();

  // clean up and finish tests
  window.BackgroundRequestExaminer.remove();
  SimpleTest.finish();
}

runTests();

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