advanced-csp.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. var t = require('assert')
  2. module.exports = ({browser, extensions, popup, advanced, content}) => {
  3. before(async () => {
  4. // add origin
  5. await advanced.bringToFront()
  6. await advanced.select('.m-select', 'http')
  7. await advanced.type('[type=text]', 'localhost:3000')
  8. await advanced.click('button')
  9. await advanced.waitFor(() => document.querySelectorAll('.m-list li').length === 2)
  10. // disable csp
  11. if (await advanced.evaluate(() => state.csp)) {
  12. await advanced.click('.m-switch:nth-of-type(2)')
  13. }
  14. // enable path matching
  15. await advanced.evaluate(() => {
  16. document.querySelector('.m-list li:nth-of-type(2) input')
  17. .value = 'csp'
  18. document.querySelector('.m-list li:nth-of-type(2) input')
  19. .dispatchEvent(new Event('keyup'))
  20. })
  21. // there is debounce timeout of 750ms in the options UI
  22. await advanced.waitFor(800)
  23. })
  24. describe('preserve state', () => {
  25. it('options page', async () => {
  26. await advanced.bringToFront()
  27. // enable csp
  28. if (!await advanced.evaluate(() => state.csp)) {
  29. await advanced.click('.m-switch:nth-of-type(2)')
  30. }
  31. await advanced.reload()
  32. await advanced.waitFor('#options')
  33. await advanced.waitFor(100)
  34. t.strictEqual(
  35. await advanced.evaluate(() =>
  36. document.querySelector('.m-switch:nth-of-type(2)')
  37. .classList.contains('is-checked')
  38. ),
  39. true,
  40. 'csp checkbox should be enabled'
  41. )
  42. // disable csp
  43. if (await advanced.evaluate(() => state.csp)) {
  44. await advanced.click('.m-switch:nth-of-type(2)')
  45. }
  46. await advanced.reload()
  47. await advanced.waitFor('#options')
  48. await advanced.waitFor(100)
  49. t.strictEqual(
  50. await advanced.evaluate(() =>
  51. document.querySelector('.m-switch:nth-of-type(2)')
  52. .classList.contains('is-checked')
  53. ),
  54. false,
  55. 'csp checkbox should be disabled'
  56. )
  57. })
  58. })
  59. describe('enable csp', () => {
  60. it('webRequest.onHeadersReceived event is enabled', async () => {
  61. await advanced.bringToFront()
  62. // enable csp
  63. if (!await advanced.evaluate(() => state.csp)) {
  64. await advanced.click('.m-switch:nth-of-type(2)')
  65. }
  66. // go to page serving content with strict csp
  67. await content.goto('http://localhost:3000/csp')
  68. await content.bringToFront()
  69. await content.waitFor('#_html')
  70. t.strictEqual(
  71. await content.evaluate(() =>
  72. window.localStorage.toString()
  73. ),
  74. '[object Storage]',
  75. 'localStorage should be accessible'
  76. )
  77. })
  78. })
  79. describe('disable csp', () => {
  80. it('webRequest.onHeadersReceived event is disabled', async () => {
  81. await advanced.bringToFront()
  82. // disable csp
  83. if (await advanced.evaluate(() => state.csp)) {
  84. await advanced.click('.m-switch:nth-of-type(2)')
  85. }
  86. // go to page serving content with strict csp
  87. await content.goto('http://localhost:3000/csp')
  88. await content.bringToFront()
  89. await content.waitFor('#_html')
  90. t.strictEqual(
  91. await content.evaluate(() => {
  92. try {
  93. window.localStorage
  94. }
  95. catch (err) {
  96. return err.message.split(':')[1].trim()
  97. }
  98. }),
  99. `The document is sandboxed and lacks the 'allow-same-origin' flag.`,
  100. 'localStorage should not be accessible'
  101. )
  102. })
  103. })
  104. describe('enable csp + suspend the event page', () => {
  105. it('the tab is reloaded on event page wakeup', async () => {
  106. await advanced.bringToFront()
  107. // enable csp
  108. if (!await advanced.evaluate(() => state.csp)) {
  109. await advanced.click('.m-switch:nth-of-type(2)')
  110. }
  111. await extensions.bringToFront()
  112. // enable developer mode
  113. await extensions.click('#dev-toggle label')
  114. // disable the extension
  115. await extensions.click('.enable-checkbox label')
  116. // enable the extension
  117. await extensions.click('.enable-checkbox label')
  118. // check
  119. t.equal(
  120. await extensions.evaluate(() =>
  121. document.querySelector('.active-views a').innerText
  122. ),
  123. 'background page (Inactive)',
  124. 'background page should be disabled'
  125. )
  126. // go to page serving content with strict csp
  127. await content.goto('http://localhost:3000/csp')
  128. await content.bringToFront()
  129. await content.waitFor('#_html')
  130. t.strictEqual(
  131. await content.evaluate(() =>
  132. window.localStorage.toString()
  133. ),
  134. '[object Storage]',
  135. 'localStorage should be accessible'
  136. )
  137. })
  138. })
  139. }