Skip to content
This repository has been archived by the owner on Oct 30, 2023. It is now read-only.

Commit

Permalink
docs: Use Node's URL parser in the 5th security recommendation (elect…
Browse files Browse the repository at this point in the history
…ron#33463)

Rule 13 recommends using Node's URL parser for handling url inputs. At
the moment, this is not being followed in the code example for rule 5,
which falls back on checking that the url ends with a '/'. If this was
forgotten when a user copies this code it could introduce security
vulnerabilities if an attacker uses an URL in the following way:

"https://example.com.attacker.com"

Using Node's URL parser fixes this potential missuse and enables the
'/' to be omited from the code example.

Co-authored-by: Baitinq <you@example.com>
  • Loading branch information
2 people authored and khalwa committed Feb 22, 2023
1 parent 39b9ae0 commit 2975591
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions docs/tutorial/security.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,19 +279,20 @@ security-conscious developers might want to assume the very opposite.

```js title='main.js (Main Process)'
const { session } = require('electron')
const URL = require('url').URL

session
.fromPartition('some-partition')
.setPermissionRequestHandler((webContents, permission, callback) => {
const url = webContents.getURL()
const parsedUrl = new URL(webContents.getURL())

if (permission === 'notifications') {
// Approves the permissions request
callback(true)
}

// Verify URL
if (!url.startsWith('https://example.com/')) {
if (parsedUrl.protocol !== 'https:' || parsedUrl.host !== 'example.com') {
// Denies the permissions request
return callback(false)
}
Expand Down

0 comments on commit 2975591

Please sign in to comment.