summaryrefslogtreecommitdiffstats
path: root/browser/components/sessionstore/test/browser_467409-backslashplosion.js
blob: fe1a82116004dbb67945d0f1e069f1354bd156b5 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Test Summary:
// 1.  Open about:sessionrestore where formdata is a JS object, not a string
// 1a. Check that #sessionData on the page is readable after JSON.parse (skipped, checking formdata is sufficient)
// 1b. Check that there are no backslashes in the formdata
// 1c. Check that formdata doesn't require JSON.parse
//
// 2.  Use the current state (currently about:sessionrestore with data) and then open that in a new instance of about:sessionrestore
// 2a. Check that there are no backslashes in the formdata
// 2b. Check that formdata doesn't require JSON.parse
//
// 3.  [backwards compat] Use a stringified state as formdata when opening about:sessionrestore
// 3a. Make sure there are nodes in the tree on about:sessionrestore (skipped, checking formdata is sufficient)
// 3b. Check that there are no backslashes in the formdata
// 3c. Check that formdata doesn't require JSON.parse

const CRASH_STATE = {
  windows: [
    {
      tabs: [
        { entries: [{ url: "about:mozilla", triggeringPrincipal_base64 }] },
      ],
    },
  ],
};
const STATE = createEntries(CRASH_STATE);
const STATE2 = createEntries({ windows: [{ tabs: [STATE] }] });
const STATE3 = createEntries(JSON.stringify(CRASH_STATE));

function createEntries(sessionData) {
  return {
    entries: [{ url: "about:sessionrestore", triggeringPrincipal_base64 }],
    formdata: { id: { sessionData }, url: "about:sessionrestore" },
  };
}

add_task(async function test_nested_about_sessionrestore() {
  // Prepare a blank tab.
  let tab = BrowserTestUtils.addTab(gBrowser, "about:blank");
  let browser = tab.linkedBrowser;
  await promiseBrowserLoaded(browser);

  // test 1
  await promiseTabState(tab, STATE);
  await checkState("test1", tab);

  // test 2
  await promiseTabState(tab, STATE2);
  await checkState("test2", tab);

  // test 3
  await promiseTabState(tab, STATE3);
  await checkState("test3", tab);

  // Cleanup.
  gBrowser.removeTab(tab);
});

async function checkState(prefix, tab) {
  // Flush and query tab state.
  await TabStateFlusher.flush(tab.linkedBrowser);
  let { formdata } = JSON.parse(ss.getTabState(tab));

  ok(
    formdata.id.sessionData,
    prefix + ": we have form data for about:sessionrestore"
  );

  let sessionData_raw = JSON.stringify(formdata.id.sessionData);
  ok(
    !/\\/.test(sessionData_raw),
    prefix + ": #sessionData contains no backslashes"
  );
  info(sessionData_raw);

  let gotError = false;
  try {
    JSON.parse(formdata.id.sessionData);
  } catch (e) {
    info(prefix + ": got error: " + e);
    gotError = true;
  }
  ok(gotError, prefix + ": attempting to JSON.parse form data threw error");
}