Skip to content

Commit 82f0eba

Browse files
authored
Merge pull request #27 from script-development/security/storage-prefix-validation
security(storage): reject prefixes containing ":"
2 parents 4202cf4 + e89e38c commit 82f0eba

4 files changed

Lines changed: 18 additions & 2 deletions

File tree

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/storage/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@script-development/fs-storage",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "Framework-agnostic localStorage service factory with prefix namespacing",
55
"homepage": "https://packages.script.nl/packages/storage",
66
"license": "MIT",

packages/storage/src/storage.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import type { StorageService, Get } from "./types";
22

33
export const createStorageService = (prefix: string): StorageService => {
4+
if (prefix.includes(":")) {
5+
throw new Error(
6+
`createStorageService: prefix must not contain ":" — got ${JSON.stringify(prefix)}. The colon is reserved as the prefix/key separator; a prefix containing ":" would allow clear() to match and delete keys from other prefixes (e.g., prefix "app" would delete everything stored under "app:admin").`,
7+
);
8+
}
9+
410
const prefixKey = (key: string): string => `${prefix}:${key}`;
511

612
const put = (key: string, value: unknown): void => {

packages/storage/tests/storage.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ describe("storage service", () => {
2828
expect(storage).toHaveProperty("remove");
2929
expect(storage).toHaveProperty("clear");
3030
});
31+
32+
it("should throw when the prefix contains a colon", () => {
33+
expect(() => createStorageService("app:admin")).toThrow(/must not contain ":"/u);
34+
expect(() => createStorageService(":leading")).toThrow(/must not contain ":"/u);
35+
expect(() => createStorageService("trailing:")).toThrow(/must not contain ":"/u);
36+
});
37+
38+
it("should include the offending prefix in the error message", () => {
39+
expect(() => createStorageService("bad:prefix")).toThrow(/"bad:prefix"/u);
40+
});
3141
});
3242

3343
describe("put", () => {

0 commit comments

Comments
 (0)