Skip to content

fix: disallow empty key #134

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 1 commit into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,7 @@ describe('set', () => {
siteID,
})

expect(async () => await blobs.set('', 'value')).rejects.toThrowError('Blob key must not be empty.')
expect(async () => await blobs.set('/key', 'value')).rejects.toThrowError(
'Blob key must not start with forward slash (/).',
)
Expand Down
4 changes: 4 additions & 0 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,10 @@ export class Store {
}

private static validateKey(key: string) {
if (key === '') {
throw new Error('Blob key must not be empty.')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also make this part of the check on line 318 and adjust the message to something like:

Blob key must be a sequence of Unicode characters whose UTF-8 encoding is between 1 and 600 bytes long.

Just an idea, no strong preference.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's nifty - but if I were debugging this exception, I think i'd be way more confused by the words "Unicode" and "UTF-8" than necessary. Gonna keep it like it is!

}

if (key.startsWith('/') || key.startsWith('%2F')) {
throw new Error('Blob key must not start with forward slash (/).')
}
Expand Down