-
Notifications
You must be signed in to change notification settings - Fork 11
1004: Prevent access to the session across all scopes, and tighten localStorage access scope #1275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d4b4795
1004: Prevent access to the session across all scopes, and tighten lo…
danhalson 7728cc6
1004: Update changelog
danhalson dccf77a
1004: Update comment to explain why we're disabling localStorage and …
danhalson dfdb65c
1004: Remove comment
danhalson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -306,55 +306,90 @@ function HtmlRunner() { | |
| if (!externalLink) { | ||
| const indexPage = parse(focussedComponent(previewFile).content); | ||
| const body = indexPage.querySelector("body") || indexPage; | ||
| const htmlRoot = indexPage.querySelector("html") ?? indexPage; | ||
|
|
||
| // insert script to disable access to specific localStorage keys | ||
| // localstorage.getItem() is a potential security risk when executing untrusted code | ||
| const disableLocalStorageScript = ` | ||
| <script> | ||
| (function() { | ||
| const originalGetItem = window.localStorage.getItem.bind(window.localStorage); | ||
| const originalSetItem = window.localStorage.setItem.bind(window.localStorage); | ||
| const originalRemoveItem = window.localStorage.removeItem.bind(window.localStorage); | ||
| const originalClear = window.localStorage.clear.bind(window.localStorage); | ||
|
|
||
| const isDisallowedKey = (key) => key === 'authKey' || key.startsWith('oidc.'); | ||
|
|
||
| Object.defineProperty(window, 'localStorage', { | ||
| value: { | ||
| getItem: function(key) { | ||
| if (isDisallowedKey(key)) { | ||
| console.log(\`localStorage.getItem for "\${key}" is disabled\`); | ||
| return null; | ||
| } | ||
| return originalGetItem(key); | ||
| }, | ||
| setItem: function(key, value) { | ||
| if (isDisallowedKey(key)) { | ||
| console.log(\`localStorage.setItem for "\${key}" is disabled\`); | ||
| return; | ||
| } | ||
| return originalSetItem(key, value); | ||
| }, | ||
| removeItem: function(key) { | ||
| if (isDisallowedKey(key)) { | ||
| console.log(\`localStorage.removeItem for "\${key}" is disabled\`); | ||
| return; | ||
| } | ||
| return originalRemoveItem(key); | ||
| <script> | ||
| (function () { | ||
| "use strict"; | ||
| const isBlocked = (key) => | ||
| typeof key === "string" && (key === "authKey" || key.startsWith("oidc.")); | ||
| const wrapLocal = (storage) => | ||
| storage && { | ||
| getItem(key) { | ||
| return isBlocked(key) ? null : storage.getItem(key); | ||
| }, | ||
| setItem(key, value) { | ||
| if (!isBlocked(key)) storage.setItem(key, value); | ||
| }, | ||
| removeItem(key) { | ||
| if (!isBlocked(key)) storage.removeItem(key); | ||
| }, | ||
| clear() {}, | ||
| key(index) { | ||
| const name = storage.key(index); | ||
| return isBlocked(name) ? null : name; | ||
| }, | ||
| get length() { | ||
| return storage?.length ?? 0; | ||
| }, | ||
| }; | ||
| const apply = (host) => { | ||
| if (!host) return; | ||
| try { | ||
| const guarded = wrapLocal(host.localStorage); | ||
| if (!guarded) return; | ||
| Object.defineProperty(host, "localStorage", { | ||
| configurable: false, | ||
| enumerable: false, | ||
| get: () => guarded, | ||
| set: () => undefined, | ||
| }); | ||
| } catch (_) { | ||
| /* ignore if non-configurable */ | ||
| } | ||
| }; | ||
| [window, window.parent, window.top, document.defaultView].forEach(apply); | ||
| })(); | ||
| </script> | ||
| `; | ||
|
|
||
| const disableSessionStorageScript = ` | ||
| <script> | ||
| (function () { | ||
| "use strict"; | ||
| const stub = { | ||
| getItem: () => null, | ||
| setItem: () => undefined, | ||
| removeItem: () => undefined, | ||
| clear: () => undefined, | ||
| key: () => null, | ||
| get length() { | ||
| return 0; | ||
| }, | ||
| clear: function() { | ||
| console.log('localStorage.clear is disabled'); | ||
| return; | ||
| }; | ||
| const apply = (host) => { | ||
| if (!host) return; | ||
| try { | ||
| Object.defineProperty(host, "sessionStorage", { | ||
| configurable: false, | ||
| enumerable: false, | ||
| get: () => stub, | ||
| set: () => undefined, | ||
| }); | ||
| } catch (_) { | ||
| /* ignore if non-configurable */ | ||
|
||
| } | ||
| }, | ||
| writable: false, | ||
| configurable: false | ||
| }); | ||
| })(); | ||
| </script> | ||
| `; | ||
|
|
||
| body.insertAdjacentHTML("afterbegin", disableLocalStorageScript); | ||
| }; | ||
| [window, window.parent, window.top, document.defaultView].forEach(apply); | ||
| })(); | ||
| </script> | ||
| `; | ||
|
|
||
| // insert scripts to disable access to specific localStorage keys and sessionStorage | ||
| // entirely, they are both potential security risks when executing untrusted code | ||
| htmlRoot.insertAdjacentHTML("afterbegin", disableLocalStorageScript); | ||
| htmlRoot.insertAdjacentHTML("afterbegin", disableSessionStorageScript); | ||
|
|
||
| replaceHrefNodes(indexPage, projectCode); | ||
| replaceSrcNodes(indexPage, projectMedia, projectCode); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this helpful or can it be dropped?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... or is there any value in raising something?