Skip to content

Commit 231386e

Browse files
committed
feat: validate store name
1 parent e9dd764 commit 231386e

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/main.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,31 @@ describe(`getStore`, () => {
11211121
)
11221122
})
11231123

1124+
test('Throws when the name of the store starts with the `deploy:` prefix', async () => {
1125+
const { fetch } = new MockFetch()
1126+
1127+
globalThis.fetch = fetch
1128+
1129+
expect(() =>
1130+
getStore({
1131+
name: 'deploy:foo',
1132+
token: apiToken,
1133+
siteID,
1134+
}),
1135+
).toThrowError('Store name cannot start with the string `deploy:`, which is a reserved namespace')
1136+
1137+
const context = {
1138+
siteID,
1139+
token: apiToken,
1140+
}
1141+
1142+
env.NETLIFY_BLOBS_CONTEXT = Buffer.from(JSON.stringify(context)).toString('base64')
1143+
1144+
expect(() => getStore('deploy:foo')).toThrowError(
1145+
'Store name cannot start with the string `deploy:`, which is a reserved namespace',
1146+
)
1147+
})
1148+
11241149
test('Throws when there is no `fetch` implementation available', async () => {
11251150
// @ts-expect-error Assigning a value that doesn't match the type.
11261151
globalThis.fetch = undefined

src/store.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@ export class Store {
3333

3434
constructor(options: StoreOptions) {
3535
this.client = options.client
36-
this.name = 'deployID' in options ? `deploy:${options.deployID}` : encodeURIComponent(options.name)
36+
37+
if ('deployID' in options) {
38+
this.name = `deploy:${encodeURIComponent(options.deployID)}`
39+
} else if (options?.name.startsWith('deploy:')) {
40+
throw new Error('Store name cannot start with the string `deploy:`, which is a reserved namespace')
41+
} else {
42+
this.name = encodeURIComponent(options.name)
43+
}
3744
}
3845

3946
async delete(key: string) {

0 commit comments

Comments
 (0)