origin-csp.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. var t = require('assert')
  2. module.exports = ({extensions, popup, advanced, content}) => {
  3. before(async () => {
  4. // popup
  5. await popup.bringToFront()
  6. // defaults button
  7. await popup.click('button:nth-of-type(2)')
  8. // advanced
  9. await advanced.bringToFront()
  10. // remove origin
  11. if (await advanced.evaluate(() => Object.keys(state.origins).length > 1)) {
  12. // expand origin
  13. if (!await advanced.evaluate(() => document.querySelector('.m-list li:nth-of-type(2)').classList.contains('m-expanded'))) {
  14. await advanced.click('.m-list li:nth-of-type(2)')
  15. }
  16. await advanced.click('.m-list li:nth-of-type(2) .m-footer .m-button')
  17. }
  18. // add origin
  19. await advanced.select('.m-select', 'http')
  20. await advanced.type('[type=text]', 'localhost:3000')
  21. await advanced.click('button')
  22. await advanced.waitFor(200)
  23. // expand origin
  24. if (!await advanced.evaluate(() =>
  25. document.querySelector('.m-list li:nth-of-type(2)').classList.contains('m-expanded'))) {
  26. await advanced.click('.m-list li:nth-of-type(2)')
  27. }
  28. // enable header detection
  29. if (!await advanced.evaluate(() => state.header)) {
  30. await advanced.click('.m-switch')
  31. }
  32. // enable path matching
  33. await advanced.evaluate(() => {
  34. document.querySelector('.m-list li:nth-of-type(2) input').value = 'csp-match-path'
  35. document.querySelector('.m-list li:nth-of-type(2) input').dispatchEvent(new Event('keyup'))
  36. })
  37. // there is debounce timeout of 750ms in the options UI
  38. await advanced.waitFor(800)
  39. })
  40. describe('not correct content-type + non matching path', () => {
  41. before(async () => {
  42. await advanced.bringToFront()
  43. // enable csp
  44. if (!await advanced.evaluate(() => state.origins['http://localhost:3000'].csp)) {
  45. await advanced.click('.m-list li:nth-of-type(2) .m-switch')
  46. }
  47. // go to page serving content with strict csp
  48. await content.goto('http://localhost:3000/csp-no-header-no-path')
  49. await content.bringToFront()
  50. await content.waitFor(200)
  51. })
  52. it('non matching urls should be skipped', async () => {
  53. t.strictEqual(
  54. await content.evaluate(() => {
  55. try {
  56. window.localStorage
  57. }
  58. catch (err) {
  59. return err.message.split(':')[1].trim()
  60. }
  61. }),
  62. `The document is sandboxed and lacks the 'allow-same-origin' flag.`,
  63. 'localStorage should not be accessible'
  64. )
  65. })
  66. })
  67. describe('correct content-type + non matching path', () => {
  68. before(async () => {
  69. await advanced.bringToFront()
  70. // enable csp
  71. if (!await advanced.evaluate(() => state.origins['http://localhost:3000'].csp)) {
  72. await advanced.click('.m-list li:nth-of-type(2) .m-switch')
  73. }
  74. // go to page serving content with strict csp
  75. await content.goto('http://localhost:3000/csp-match-header')
  76. await content.bringToFront()
  77. await content.waitFor(200)
  78. })
  79. it('non matching urls cannot be checked for enabled csp', async () => {
  80. t.strictEqual(
  81. await content.evaluate(() => {
  82. try {
  83. window.localStorage
  84. }
  85. catch (err) {
  86. return err.message.split(':')[1].trim()
  87. }
  88. }),
  89. `The document is sandboxed and lacks the 'allow-same-origin' flag.`,
  90. 'localStorage should not be accessible'
  91. )
  92. })
  93. })
  94. describe('not correct content-type + matching path', () => {
  95. before(async () => {
  96. await advanced.bringToFront()
  97. // enable csp
  98. if (!await advanced.evaluate(() => state.origins['http://localhost:3000'].csp)) {
  99. await advanced.click('.m-list li:nth-of-type(2) .m-switch')
  100. }
  101. // go to page serving content with strict csp
  102. await content.goto('http://localhost:3000/csp-match-path')
  103. await content.bringToFront()
  104. await content.waitFor(200)
  105. })
  106. it('webRequest.onHeadersReceived event is enabled', async () => {
  107. t.strictEqual(
  108. await content.evaluate(() =>
  109. window.localStorage.toString()
  110. ),
  111. '[object Storage]',
  112. 'localStorage should be accessible'
  113. )
  114. })
  115. })
  116. describe('disable - enable - disable', () => {
  117. it('full cycle', async () => {
  118. // 1. disable
  119. await advanced.bringToFront()
  120. // disable csp
  121. if (await advanced.evaluate(() => state.origins['http://localhost:3000'].csp)) {
  122. await advanced.click('.m-list li:nth-of-type(2) .m-switch')
  123. }
  124. // go to page serving content with strict csp
  125. await content.goto('http://localhost:3000/csp-match-path')
  126. await content.bringToFront()
  127. await content.waitFor(200)
  128. t.strictEqual(
  129. await content.evaluate(() => {
  130. try {
  131. window.localStorage
  132. }
  133. catch (err) {
  134. return err.message.split(':')[1].trim()
  135. }
  136. }),
  137. `The document is sandboxed and lacks the 'allow-same-origin' flag.`,
  138. 'localStorage should not be accessible'
  139. )
  140. // 2. enable
  141. await advanced.bringToFront()
  142. // enable csp
  143. if (!await advanced.evaluate(() => state.origins['http://localhost:3000'].csp)) {
  144. await advanced.click('.m-list li:nth-of-type(2) .m-switch')
  145. }
  146. // go to page serving content with strict csp
  147. await content.goto('http://localhost:3000/csp-match-path')
  148. await content.bringToFront()
  149. await content.waitFor(200)
  150. t.strictEqual(
  151. await content.evaluate(() =>
  152. window.localStorage.toString()
  153. ),
  154. '[object Storage]',
  155. 'localStorage should be accessible'
  156. )
  157. // 3. disable
  158. await advanced.bringToFront()
  159. // disable csp
  160. if (await advanced.evaluate(() => state.origins['http://localhost:3000'].csp)) {
  161. await advanced.click('.m-list li:nth-of-type(2) .m-switch')
  162. }
  163. // go to page serving content with strict csp
  164. await content.goto('http://localhost:3000/csp-match-path')
  165. await content.bringToFront()
  166. await content.waitFor(200)
  167. t.strictEqual(
  168. await content.evaluate(() => {
  169. try {
  170. window.localStorage
  171. }
  172. catch (err) {
  173. return err.message.split(':')[1].trim()
  174. }
  175. }),
  176. `The document is sandboxed and lacks the 'allow-same-origin' flag.`,
  177. 'localStorage should not be accessible'
  178. )
  179. })
  180. })
  181. describe('persist state', () => {
  182. it('enable csp', async () => {
  183. await advanced.bringToFront()
  184. // enable csp
  185. if (!await advanced.evaluate(() => state.origins['http://localhost:3000'].csp)) {
  186. await advanced.click('.m-list li:nth-of-type(2) .m-switch')
  187. }
  188. await advanced.reload()
  189. await advanced.waitFor(200)
  190. // expand origin
  191. await advanced.click('.m-list li:nth-of-type(2)')
  192. t.strictEqual(
  193. await advanced.evaluate(() =>
  194. document.querySelector('.m-list li:nth-of-type(2) .m-switch').classList.contains('is-checked')
  195. ),
  196. true,
  197. 'csp checkbox should be enabled'
  198. )
  199. })
  200. it('disable csp', async () => {
  201. await advanced.bringToFront()
  202. // disable csp
  203. if (await advanced.evaluate(() => state.origins['http://localhost:3000'].csp)) {
  204. await advanced.click('.m-list li:nth-of-type(2) .m-switch')
  205. }
  206. await advanced.reload()
  207. await advanced.waitFor(200)
  208. // expand origin
  209. await advanced.click('.m-list li:nth-of-type(2)')
  210. t.strictEqual(
  211. await advanced.evaluate(() =>
  212. document.querySelector('.m-list li:nth-of-type(2) .m-switch').classList.contains('is-checked')
  213. ),
  214. false,
  215. 'csp checkbox should be disabled'
  216. )
  217. })
  218. })
  219. describe('enable csp + suspend the event page', () => {
  220. before(async () => {
  221. await advanced.bringToFront()
  222. // enable csp
  223. if (!await advanced.evaluate(() => state.origins['http://localhost:3000'].csp)) {
  224. await advanced.click('.m-list li:nth-of-type(2) .m-switch')
  225. }
  226. // chrome://extensions
  227. await extensions.bringToFront()
  228. // enable developer mode
  229. await extensions.evaluate(() => {
  230. Array.from(
  231. document.querySelector('extensions-manager').shadowRoot
  232. .querySelector('extensions-item-list').shadowRoot
  233. .querySelectorAll('extensions-item'))[0].shadowRoot
  234. .querySelector('#enable-toggle').click()
  235. })
  236. // disable the extension
  237. await extensions.evaluate(() => {
  238. Array.from(
  239. document.querySelector('extensions-manager').shadowRoot
  240. .querySelector('extensions-item-list').shadowRoot
  241. .querySelectorAll('extensions-item'))[0].shadowRoot
  242. .querySelector('#enable-toggle').click()
  243. })
  244. await extensions.waitFor(200)
  245. // check
  246. t.equal(
  247. await extensions.evaluate(() =>
  248. Array.from(
  249. document.querySelector('extensions-manager').shadowRoot
  250. .querySelector('extensions-item-list').shadowRoot
  251. .querySelectorAll('extensions-item'))[0].shadowRoot
  252. .querySelector('#inspect-views a').innerText
  253. ),
  254. 'background page (Inactive)',
  255. 'background page should be inactive'
  256. )
  257. // go to page serving content with strict csp
  258. await content.goto('http://localhost:3000/csp-match-path')
  259. await content.bringToFront()
  260. await content.waitFor(200)
  261. })
  262. it('the tab is reloaded on event page wakeup', async () => {
  263. t.strictEqual(
  264. await content.evaluate(() =>
  265. window.localStorage.toString()
  266. ),
  267. '[object Storage]',
  268. 'localStorage should be accessible'
  269. )
  270. })
  271. })
  272. }