Skip to content

Commit 00b6d85

Browse files
authored
Broaden scope of localstorage keys disallowed (#1080)
Access to all localstorage keys starting with `oidc.` will be disallowed when executing code. Improves: #1079
1 parent ed90fd8 commit 00b6d85

File tree

4 files changed

+13
-18
lines changed

4 files changed

+13
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
88

99
### Added
1010

11-
- Disabling of `localstorage.getItem()` access to `authKey`, `oidc.user:https://staging-auth-v1.raspberrypi.org:editor-api` and `oidc.user:https://auth-v1.raspberrypi.org:editor-api` at runtime (#1079)
11+
- Disabling of `localstorage` access to `authKey` and `oidc.*` keys at runtime (#1079, #1080)
1212

1313
## [0.26.0] - 2024-09-13
1414

cypress/e2e/spec-html.cy.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,11 @@ it("blocks access to specific localStorage keys but allows other keys", () => {
2424
localStorage.clear();
2525
localStorage.setItem("foo", "bar");
2626
localStorage.setItem("authKey", "secret");
27-
localStorage.setItem(
28-
"oidc.user:https://staging-auth-v1.raspberrypi.org:editor-api",
29-
"staging-token",
30-
);
3127
localStorage.setItem(
3228
"oidc.user:https://auth-v1.raspberrypi.org:editor-api",
33-
"prod-token",
29+
"token",
3430
);
31+
localStorage.setItem("oidc.something:else", "another-token");
3532

3633
cy.visit(baseUrl);
3734
cy.get(".btn--run").click();
@@ -45,13 +42,11 @@ it("blocks access to specific localStorage keys but allows other keys", () => {
4542
expect(authKeyResult).to.equal(null);
4643

4744
const stagingOidcResult = win.localStorage.getItem(
48-
"oidc.user:https://staging-auth-v1.raspberrypi.org:editor-api",
45+
"oidc.user:https://auth-v1.raspberrypi.org:editor-api",
4946
);
5047
expect(stagingOidcResult).to.equal(null);
5148

52-
const prodOidcResult = win.localStorage.getItem(
53-
"oidc.user:https://auth-v1.raspberrypi.org:editor-api",
54-
);
49+
const prodOidcResult = win.localStorage.getItem("oidc.something:else");
5550
expect(prodOidcResult).to.equal(null);
5651

5752
const fooResult = win.localStorage.getItem("foo");

src/components/Editor/Runners/HtmlRunner/HtmlRunner.jsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -302,31 +302,31 @@ function HtmlRunner() {
302302
const disableLocalStorageScript = `
303303
<script>
304304
(function() {
305-
const disallowedKeys = ['authKey', 'oidc.user:https://staging-auth-v1.raspberrypi.org:editor-api', 'oidc.user:https://auth-v1.raspberrypi.org:editor-api'];
306-
307305
const originalGetItem = window.localStorage.getItem.bind(window.localStorage);
308306
const originalSetItem = window.localStorage.setItem.bind(window.localStorage);
309307
const originalRemoveItem = window.localStorage.removeItem.bind(window.localStorage);
310308
const originalClear = window.localStorage.clear.bind(window.localStorage);
311309
310+
const isDisallowedKey = (key) => key === 'authKey' || key.startsWith('oidc.');
311+
312312
Object.defineProperty(window, 'localStorage', {
313313
value: {
314314
getItem: function(key) {
315-
if (disallowedKeys.includes(key)) {
315+
if (isDisallowedKey(key)) {
316316
console.log(\`localStorage.getItem for "\${key}" is disabled\`);
317317
return null;
318318
}
319319
return originalGetItem(key);
320320
},
321321
setItem: function(key, value) {
322-
if (disallowedKeys.includes(key)) {
322+
if (isDisallowedKey(key)) {
323323
console.log(\`localStorage.setItem for "\${key}" is disabled\`);
324324
return;
325325
}
326326
return originalSetItem(key, value);
327327
},
328328
removeItem: function(key) {
329-
if (disallowedKeys.includes(key)) {
329+
if (isDisallowedKey(key)) {
330330
console.log(\`localStorage.removeItem for "\${key}" is disabled\`);
331331
return;
332332
}

src/components/Editor/Runners/HtmlRunner/HtmlRunner.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,11 @@ describe("When run is triggered", () => {
295295
expect(generatedHtml).toContain("getItem: function(key) {");
296296

297297
expect(generatedHtml).toContain(
298-
"const disallowedKeys = ['authKey', 'oidc.user:https://staging-auth-v1.raspberrypi.org:editor-api', 'oidc.user:https://auth-v1.raspberrypi.org:editor-api']",
298+
"const isDisallowedKey = (key) => key === 'authKey' || key.startsWith('oidc.');",
299299
);
300-
expect(generatedHtml).toContain("if (disallowedKeys.includes(key))");
300+
expect(generatedHtml).toContain("if (isDisallowedKey(key))");
301301
expect(generatedHtml).toContain(
302-
'localStorage.getItem for "${key}" is disabled',
302+
'localStorage.getItem for "${key}" is disabled', // eslint-disable-line no-template-curly-in-string
303303
);
304304
expect(generatedHtml).toContain("return null;");
305305
expect(generatedHtml).toContain("</script>");

0 commit comments

Comments
 (0)