Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/reject-spaced-secret-keys.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@gatepost/client': patch
---

Refuse secret keys with surrounding white space in browser clients.
2 changes: 1 addition & 1 deletion packages/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function checkOptions(options: ClientOptions): void {
if (apiKey === '') {
throw new RangeError('apiKey must not be empty.');
}
if (apiKey !== undefined && SECRET_KEY.test(apiKey) && 'document' in globalThis) {
if (apiKey !== undefined && SECRET_KEY.test(apiKey.trim()) && 'document' in globalThis) {
throw new TypeError('A web page must not hold a secret key. Use a publishable key here.');
}
}
Expand Down
16 changes: 16 additions & 0 deletions packages/client/test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,22 @@ describe('the options', () => {
expect(() => new PostcodeClient({ apiKey: 'nipost_pk_live_abc' })).not.toThrow();
});

it.each([
' nipost_live_abc',
'\tnipost_live_abc',
'\nnipost_live_abc',
' nipost_test_abc',
'\tnipost_test_abc',
'\nnipost_test_abc',
'\r\nnipost_live_abc \t',
'\r\nnipost_test_abc \t',
])('refuses a secret key with surrounding white space in a web page: %j', (apiKey) => {
vi.stubGlobal('document', {});
expect(() => new PostcodeClient({ apiKey })).toThrow(
'A web page must not hold a secret key. Use a publishable key here.',
);
});

it('allows a secret key on a server, where no document exists', () => {
expect(() => new PostcodeClient({ apiKey: 'nipost_live_abc' })).not.toThrow();
});
Expand Down