Skip to content

Commit

Permalink
Fix exception when signature is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
eager-signal authored Nov 6, 2024
1 parent 9608a5f commit 12ba275
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ describe('Auth', async () => {
expect(await validateAt(user, await generatePassAt(user + 'a', 1), 1)).toBe(false);
});

it('rejects missing signature', async () => {
let pass = await generatePassAt(user, 1);
// pass is ts:hex-sig, remove the signature
pass = pass.substring(0, pass.indexOf(':'));
expect(await validateAt(user, pass, 11)).toBe(false);
});

it('rejects long signature', async () => {
let pass = await generatePassAt(user, 1);
// pass is ts:hex-sig, change the sig length
Expand Down
4 changes: 3 additions & 1 deletion src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export async function createAuthWithClock(secret: string, maxAgeSeconds: number,
const truncatedSignatureLength = 10;

const [ts, sig] = password.split(':');
if (!ts || !sig) {
return false;
}
const actual = Buffer.from(sig, 'hex');
if (actual.length !== truncatedSignatureLength) {
// timingSafeEqual throws if the buffers are not the same length
Expand All @@ -56,4 +59,3 @@ export async function createAuthWithClock(secret: string, maxAgeSeconds: number,
export async function createAuth(secret: string, maxAgeSeconds: number): Promise<Auth> {
return await createAuthWithClock(secret, maxAgeSeconds, () => Math.floor(new Date().getTime() / 1000));
}

2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ function parseBasicAuth(auth: string): Credentials | ParseError {

const [username, ...rest] = decoded.split(':');
const password = rest.join(':');
if (!password) {
if (!username || !password) {
return {state: 'error', error: error(400, 'invalid auth format')};
}
return {state: 'success', user: username, password: password};
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
/* Include 'undefined' in index signature results */
"noUncheckedIndexedAccess": true,
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type */
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
Expand Down

0 comments on commit 12ba275

Please sign in to comment.