Skip to content

Commit c05977e

Browse files
authored
added support for allowedLocalStorageKeys (#2957)
### Fixes # <!-- Mention the issues this PR addresses --> ### Checks - [ ] Ran `yarn test-build` - [ ] Updated relevant documentations - [ ] Updated matching config options in altair-static ### Changes proposed in this pull request: <!-- Describe the changes being introduced in this PR --> ## Summary by Sourcery Allow pre-request scripts to read specific browser localStorage keys via the storage API, configurable through settings. New Features: - Add a settings option to whitelist localStorage keys accessible (read-only) from pre-request scripts via the storage API. Enhancements: - Log validation errors when settings JSON fails schema validation to aid debugging. Documentation: - Document that altair.storage.get can also read from localStorage for keys permitted by the script.allowedLocalStorageKeys setting. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Pre-request scripts can read browser localStorage keys listed in the new allowedLocalStorageKeys setting (read-only; access emits a warning). * **Improvements** * Added debug logging for settings validation failures. * **Documentation** * Updated pre-request script storage documentation to describe allowedLocalStorageKeys and localStorage read behavior. <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent dc3a8bc commit c05977e

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

packages/altair-app/src/app/modules/altair/services/pre-request/pre-request.service.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ export class PreRequestService {
3333
private notifyService = inject(NotifyService);
3434
private dbService = inject(DbService);
3535

36-
3736
async executeScript(
3837
script: string,
3938
data: ScriptContextData
@@ -169,7 +168,21 @@ export class PreRequestService {
169168
requestScriptLogs: clonedMutableData.requestScriptLogs ?? [],
170169
};
171170
}
172-
private getStorageItem(key: string) {
171+
private async getStorageItem(key: string) {
172+
// Check allowedLocalStorageKeys from settings for read-only localStorage data
173+
const allowedLocalStorageKeys = await firstValueFrom(
174+
this.store
175+
.select((state) => state.settings['script.allowedLocalStorageKeys'])
176+
.pipe(take(1))
177+
);
178+
if (allowedLocalStorageKeys?.includes(key)) {
179+
this.notifyService.warning(
180+
`Accessing local storage key "${key}" (read-only) from request script.`,
181+
'Request script'
182+
);
183+
return localStorage.getItem(key);
184+
}
185+
173186
return this.dbService
174187
.getItem(`${storageNamespace}:${key}`)
175188
.pipe(take(1))

packages/altair-app/src/app/modules/altair/utils/settings_addons.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { jsonc } from '../utils';
22
import { JSONSchema6, JSONSchema6Definition } from 'json-schema';
33
import settingsValidator from 'altair-graphql-core/build/typegen/validate-settings';
4+
import { debug } from './logger';
45

56
export interface SchemaFormProperty extends JSONSchema6 {
67
key: string;
@@ -12,6 +13,9 @@ export const settingsSchema = settingsValidator.schema;
1213
export const validateSettings = (settings: string) => {
1314
const data = jsonc(settings);
1415
const valid = settingsValidator(data);
16+
if (!valid) {
17+
debug.log('validator errors', settingsValidator.errors);
18+
}
1519

1620
return valid;
1721
};

packages/altair-core/src/types/state/settings.interfaces.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,14 @@ export interface SettingsState {
163163
*/
164164
'script.allowedCookies'?: string[];
165165

166+
/**
167+
* List of local storage keys to be accessible in the pre-request script.
168+
* These will be made available read-only via the `storage` API in the script context.
169+
* @example ['key1', 'key2']
170+
* @default []
171+
*/
172+
'script.allowedLocalStorageKeys'?: string[];
173+
166174
/**
167175
* Enable the scrollbar in the tab list
168176
*/

packages/altair-docs/docs/features/prerequest-scripts.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ const res = await altair.helpers.request(
8181

8282
### altair.storage
8383

84-
**`altair.storage.get(key: string): Promise<any>`** - Retrieves a value persisted in storage.
84+
**`altair.storage.get(key: string): Promise<any>`** - Retrieves a value persisted in storage. Also retrieves values from localStorage if the key is included in the `script.allowedLocalStorageKeys` setting.
8585

8686
**`altair.storage.set(key: string, value: any): Promise<void>`** - Stores (persists) a value in storage for retrieval later.
8787

0 commit comments

Comments
 (0)