browser_net_resend_csp.js (4991B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 /** 7 * Tests if resending an image request uses the same content type 8 * and hence is not blocked by the CSP of the page. 9 */ 10 11 add_task(async function () { 12 if ( 13 Services.prefs.getBoolPref( 14 "devtools.netmonitor.features.newEditAndResend", 15 true 16 ) 17 ) { 18 ok( 19 true, 20 "Skip this test when pref is true, because this panel won't be default when that is the case." 21 ); 22 return; 23 } 24 const { tab, monitor } = await initNetMonitor(CSP_RESEND_URL, { 25 requestCount: 1, 26 }); 27 const { document, store, windowRequire } = monitor.panelWin; 28 const Actions = windowRequire("devtools/client/netmonitor/src/actions/index"); 29 store.dispatch(Actions.batchEnable(false)); 30 31 // Executes 1 request 32 await performRequests(monitor, tab, 1); 33 34 // Select the image request 35 const imgRequest = document.querySelectorAll(".request-list-item")[0]; 36 EventUtils.sendMouseEvent({ type: "mousedown" }, imgRequest); 37 38 // Stores original request for comparison of values later 39 const { getSelectedRequest } = windowRequire( 40 "devtools/client/netmonitor/src/selectors/index" 41 ); 42 const origReq = getSelectedRequest(store.getState()); 43 44 // Context Menu > "Resend" 45 EventUtils.sendMouseEvent({ type: "contextmenu" }, imgRequest); 46 47 const waitForResentRequest = waitForNetworkEvents(monitor, 1); 48 await selectContextMenuItem(monitor, "request-list-context-resend-only"); 49 await waitForResentRequest; 50 51 // Selects request that was resent 52 const selReq = getSelectedRequest(store.getState()); 53 54 // Finally, some sanity checks 55 ok(selReq.url.endsWith("test-image.png"), "Correct request selected"); 56 Assert.strictEqual(origReq.url, selReq.url, "Orig and Sel url match"); 57 58 Assert.strictEqual(selReq.cause.type, "img", "Correct type of selected"); 59 Assert.strictEqual( 60 origReq.cause.type, 61 selReq.cause.type, 62 "Orig and Sel type match" 63 ); 64 65 const cspOBJ = await SpecialPowers.spawn(tab.linkedBrowser, [], async () => { 66 return JSON.parse(content.document.cspJSON); 67 }); 68 69 const policies = cspOBJ["csp-policies"]; 70 is(policies.length, 1, "CSP: should be one policy"); 71 const policy = policies[0]; 72 is(`${policy["img-src"]}`, "*", "CSP: img-src should be *"); 73 74 await teardown(monitor); 75 }); 76 77 /** 78 * Tests if resending an image request uses the same content type 79 * and hence is not blocked by the CSP of the page. 80 */ 81 82 add_task(async function () { 83 if ( 84 Services.prefs.getBoolPref( 85 "devtools.netmonitor.features.newEditAndResend", 86 true 87 ) 88 ) { 89 const { tab, monitor } = await initNetMonitor(CSP_RESEND_URL, { 90 requestCount: 1, 91 }); 92 const { document, store, windowRequire } = monitor.panelWin; 93 const Actions = windowRequire( 94 "devtools/client/netmonitor/src/actions/index" 95 ); 96 store.dispatch(Actions.batchEnable(false)); 97 98 // Executes 1 request 99 await performRequests(monitor, tab, 1); 100 101 // Select the image request 102 const imgRequest = document.querySelectorAll(".request-list-item")[0]; 103 EventUtils.sendMouseEvent({ type: "mousedown" }, imgRequest); 104 105 // Stores original request for comparison of values later 106 const { getSelectedRequest } = windowRequire( 107 "devtools/client/netmonitor/src/selectors/index" 108 ); 109 const origReq = getSelectedRequest(store.getState()); 110 111 // Context Menu > "Resend" 112 EventUtils.sendMouseEvent({ type: "contextmenu" }, imgRequest); 113 114 info("Opening the new request panel"); 115 const waitForPanels = waitUntil( 116 () => 117 document.querySelector(".http-custom-request-panel") && 118 document.querySelector("#http-custom-request-send-button").disabled === 119 false 120 ); 121 122 await selectContextMenuItem(monitor, "request-list-context-edit-resend"); 123 await waitForPanels; 124 125 const waitForResentRequest = waitForNetworkEvents(monitor, 1); 126 const buttonSend = document.querySelector( 127 "#http-custom-request-send-button" 128 ); 129 buttonSend.click(); 130 await waitForResentRequest; 131 132 // Selects request that was resent 133 const selReq = getSelectedRequest(store.getState()); 134 135 // Finally, some sanity checks 136 ok(selReq.url.endsWith("test-image.png"), "Correct request selected"); 137 Assert.strictEqual(origReq.url, selReq.url, "Orig and Sel url match"); 138 139 Assert.strictEqual(selReq.cause.type, "img", "Correct type of selected"); 140 Assert.strictEqual( 141 origReq.cause.type, 142 selReq.cause.type, 143 "Orig and Sel type match" 144 ); 145 146 const cspOBJ = await SpecialPowers.spawn( 147 tab.linkedBrowser, 148 [], 149 async () => { 150 return JSON.parse(content.document.cspJSON); 151 } 152 ); 153 154 const policies = cspOBJ["csp-policies"]; 155 is(policies.length, 1, "CSP: should be one policy"); 156 const policy = policies[0]; 157 is(`${policy["img-src"]}`, "*", "CSP: img-src should be *"); 158 159 await teardown(monitor); 160 } 161 });