Skip to content

Commit 0802f82

Browse files
zekeMarshallOfSound
authored andcommitted
doc: add CSP examples (electron#13167)
* doc: add CSP examples * Deafult to zero-permissions CSP
1 parent fc12b5c commit 0802f82

1 file changed

Lines changed: 27 additions & 6 deletions

File tree

docs/tutorial/security.md

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -339,12 +339,7 @@ CSP allows the server serving content to restrict and control the resources
339339
Electron can load for that given web page. `https://your-page.com` should
340340
be allowed to load scripts from the origins you defined while scripts from
341341
`https://evil.attacker.com` should not be allowed to run. Defining a CSP is an
342-
easy way to improve your applications security.
343-
344-
### How?
345-
346-
Electron respects [the `Content-Security-Policy` HTTP header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy)
347-
and the respective `<meta>` tag.
342+
easy way to improve your application's security.
348343

349344
The following CSP will allow Electron to execute scripts from the current
350345
website and from `apis.mydomain.com`.
@@ -357,6 +352,32 @@ Content-Security-Policy: '*'
357352
Content-Security-Policy: script-src 'self' https://apis.mydomain.com
358353
```
359354

355+
### CSP HTTP Header
356+
357+
Electron respects the [`Content-Security-Policy` HTTP header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy)
358+
which can be set using Electron's
359+
[`webRequest.onHeadersReceived`](../api/web-request.md#webrequestonheadersreceivedfilter-listener)
360+
handler:
361+
362+
```javascript
363+
const {session} = require('electron')
364+
365+
session.defaultSession.webRequest.onHeadersReceived((details, callback) => {
366+
callback({responseHeaders: `default-src 'none'`})
367+
})
368+
```
369+
370+
### CSP Meta Tag
371+
372+
CSP's preferred delivery mechanism is an HTTP header. It can be useful, however,
373+
to set a policy on a page directly in the markup using a `<meta>` tag:
374+
375+
```html
376+
<meta http-equiv="Content-Security-Policy" content="default-src 'none'">
377+
```
378+
379+
#### `webRequest.onHeadersReceived([filter, ]listener)`
380+
360381

361382
## 7) Override and Disable `eval`
362383

0 commit comments

Comments
 (0)