Skip to content

Commit

Permalink
validate instanceId format
Browse files Browse the repository at this point in the history
  • Loading branch information
slivingston committed Feb 20, 2025
1 parent 1d98219 commit 5b024f9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
const uuidRegex =
/^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$/;

export function instanceKeepAlive(instanceId: string, token: string) {
if (!uuidRegex.test(instanceId)) {
throw new Error('instanceId not of valid form');
}
return fetch(`https://api.rerobots.net/instance/${instanceId}/ka`, {
method: 'POST',
headers: {
Expand Down
6 changes: 6 additions & 0 deletions tests/instanceKeepAlive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ test('unauthenticated calls fail', () => {
instanceKeepAlive('1926b858-0d00-4b84-b7ca-5c56be880525', ''),
).rejects.toThrow();
});

test('call with broken ID format fails', () => {
expect(() =>
instanceKeepAlive('926b858-0d00-4b84-b7ca-5c56be880525', ''),
).toThrow(/instanceId/);
});

0 comments on commit 5b024f9

Please # to comment.