security-warnings-spec.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. const assert = require('assert')
  2. const http = require('http')
  3. const fs = require('fs')
  4. const path = require('path')
  5. const url = require('url')
  6. const {remote} = require('electron')
  7. const {BrowserWindow} = remote
  8. const {closeWindow} = require('./window-helpers')
  9. describe('security warnings', () => {
  10. let server
  11. let w = null
  12. let useCsp = true
  13. before((done) => {
  14. // Create HTTP Server
  15. server = http.createServer((request, response) => {
  16. const uri = url.parse(request.url).pathname
  17. let filename = path.join(__dirname, './fixtures/pages', uri)
  18. fs.stat(filename, (error, stats) => {
  19. if (error) {
  20. response.writeHead(404, { 'Content-Type': 'text/plain' })
  21. response.end()
  22. return
  23. }
  24. if (stats.isDirectory()) {
  25. filename += '/index.html'
  26. }
  27. fs.readFile(filename, 'binary', (err, file) => {
  28. if (err) {
  29. response.writeHead(404, { 'Content-Type': 'text/plain' })
  30. response.end()
  31. return
  32. }
  33. const cspHeaders = { 'Content-Security-Policy': `script-src 'self' 'unsafe-inline'` }
  34. response.writeHead(200, useCsp ? cspHeaders : undefined)
  35. response.write(file, 'binary')
  36. response.end()
  37. })
  38. })
  39. }).listen(8881, () => done())
  40. })
  41. after(() => {
  42. // Close server
  43. server.close()
  44. server = null
  45. })
  46. afterEach(() => {
  47. useCsp = true
  48. return closeWindow(w).then(() => { w = null })
  49. })
  50. it('should warn about Node.js integration with remote content', (done) => {
  51. w = new BrowserWindow({ show: false })
  52. w.webContents.once('console-message', (e, level, message) => {
  53. assert(message.includes('Node.js Integration with Remote Content'), message)
  54. done()
  55. })
  56. w.loadURL(`http://127.0.0.1:8881/base-page-security.html`)
  57. })
  58. it('should warn about disabled webSecurity', (done) => {
  59. w = new BrowserWindow({
  60. show: false,
  61. webPreferences: {
  62. webSecurity: false,
  63. nodeIntegration: false
  64. }
  65. })
  66. w.webContents.once('console-message', (e, level, message) => {
  67. assert(message.includes('Disabled webSecurity'), message)
  68. done()
  69. })
  70. w.loadURL(`http://127.0.0.1:8881/base-page-security.html`)
  71. })
  72. it('should warn about insecure Content-Security-Policy', (done) => {
  73. w = new BrowserWindow({
  74. show: false,
  75. webPreferences: {
  76. nodeIntegration: false
  77. }
  78. })
  79. w.webContents.once('console-message', (e, level, message) => {
  80. assert(message.includes('Insecure Content-Security-Policy'), message)
  81. done()
  82. })
  83. useCsp = false
  84. w.loadURL(`http://127.0.0.1:8881/base-page-security.html`)
  85. })
  86. it('should warn about allowRunningInsecureContent', (done) => {
  87. w = new BrowserWindow({
  88. show: false,
  89. webPreferences: {
  90. allowRunningInsecureContent: true,
  91. nodeIntegration: false
  92. }
  93. })
  94. w.webContents.once('console-message', (e, level, message) => {
  95. assert(message.includes('allowRunningInsecureContent'), message)
  96. done()
  97. })
  98. w.loadURL(`http://127.0.0.1:8881/base-page-security.html`)
  99. })
  100. it('should warn about experimentalFeatures', (done) => {
  101. w = new BrowserWindow({
  102. show: false,
  103. webPreferences: {
  104. experimentalFeatures: true,
  105. nodeIntegration: false
  106. }
  107. })
  108. w.webContents.once('console-message', (e, level, message) => {
  109. assert(message.includes('experimentalFeatures'), message)
  110. done()
  111. })
  112. w.loadURL(`http://127.0.0.1:8881/base-page-security.html`)
  113. })
  114. it('should warn about enableBlinkFeatures', (done) => {
  115. w = new BrowserWindow({
  116. show: false,
  117. webPreferences: {
  118. enableBlinkFeatures: ['my-cool-feature'],
  119. nodeIntegration: false
  120. }
  121. })
  122. w.webContents.once('console-message', (e, level, message) => {
  123. assert(message.includes('enableBlinkFeatures'), message)
  124. done()
  125. })
  126. w.loadURL(`http://127.0.0.1:8881/base-page-security.html`)
  127. })
  128. it('should warn about allowpopups', (done) => {
  129. w = new BrowserWindow({
  130. show: false,
  131. webPreferences: {
  132. nodeIntegration: false
  133. }
  134. })
  135. w.webContents.once('console-message', (e, level, message) => {
  136. assert(message.includes('allowpopups'), message)
  137. done()
  138. })
  139. w.loadURL(`http://127.0.0.1:8881/webview-allowpopups.html`)
  140. })
  141. it('should warn about insecure resources', (done) => {
  142. w = new BrowserWindow({
  143. show: true,
  144. webPreferences: {
  145. nodeIntegration: false
  146. }
  147. })
  148. w.webContents.once('console-message', (e, level, message) => {
  149. assert(message.includes('Insecure Resources'), message)
  150. done()
  151. })
  152. w.loadURL(`http://127.0.0.1:8881/insecure-resources.html`)
  153. w.webContents.openDevTools()
  154. })
  155. })