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
8 changes: 8 additions & 0 deletions src/oauth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,12 @@ describe("xAI oauth — user id decoding", () => {
xaiUserIdFromAccessToken(jwtWithPayload({ sub: 123 })),
).toBeUndefined();
});

test("treats a sub carrying CR, LF or NUL as absent", () => {
for (const bad of ["u\r\nx-grok-user-id: victim", "u\nx", "u\u0000x"]) {
expect(
xaiUserIdFromAccessToken(jwtWithPayload({ sub: bad })),
).toBeUndefined();
}
});
});
7 changes: 6 additions & 1 deletion src/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,10 @@ export function xaiUserIdFromAccessToken(access: string): string | undefined {
return undefined;
}
const payload = AccessTokenPayload(decoded);
return payload instanceof type.errors ? undefined : payload.sub;
if (payload instanceof type.errors) return undefined;
// The sub becomes a request header value; CR, LF or NUL would split or
// truncate it, so such a claim is treated as absent.
return payload.sub === undefined || /[\r\n\0]/.test(payload.sub)
? undefined
: payload.sub;
}
Loading