Skip to content
Merged
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
49 changes: 23 additions & 26 deletions packages/trpc/src/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,34 +72,31 @@ export const protectedProcedure = t.procedure

export const jwtProcedure = t.procedure.use(async ({ ctx, next }) => {
const authHeader = ctx.headers.get("authorization");
if (!authHeader?.startsWith("Bearer ")) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "Bearer token required",
});
}
const bearer = authHeader?.startsWith("Bearer ") ? authHeader.slice(7) : null;

@coderabbitai coderabbitai Bot Apr 30, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Parse bearer scheme case-insensitively to avoid rejecting valid headers.

Authorization auth-scheme tokens are case-insensitive. The current check only accepts exact "Bearer " casing, so valid variants (for example bearer <token>) won’t be parsed.

Suggested patch
-	const bearer = authHeader?.startsWith("Bearer ") ? authHeader.slice(7) : null;
+	const bearerMatch = authHeader?.match(/^Bearer\s+(.+)$/i);
+	const bearer = bearerMatch?.[1]?.trim() ?? null;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const bearer = authHeader?.startsWith("Bearer ") ? authHeader.slice(7) : null;
const bearerMatch = authHeader?.match(/^Bearer\s+(.+)$/i);
const bearer = bearerMatch?.[1]?.trim() ?? null;
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/trpc/src/trpc.ts` at line 75, The current extraction of the token
into the variable bearer uses a case-sensitive startsWith check on authHeader
and thus rejects valid schemes like "bearer ". Update the logic in the area that
sets bearer (the authHeader handling) to perform a case-insensitive check for
the "bearer " scheme—e.g., compare authHeader.toLowerCase().startsWith("bearer
") or use a case-insensitive regex—and then slice the original authHeader after
the scheme length to extract the token so casing is preserved in the token
value.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!


const token = authHeader.slice(7);

try {
const { payload } = await ctx.auth.api.verifyJWT({ body: { token } });
if (payload?.sub) {
const organizationIds = (payload.organizationIds as string[]) ?? [];
return next({
ctx: {
userId: payload.sub,
email: (payload.email as string) ?? "",
organizationIds,
activeOrganizationId: organizationIds[0] ?? null,
},
if (bearer) {
try {
const { payload } = await ctx.auth.api.verifyJWT({
body: { token: bearer },
});
if (payload?.sub) {
const organizationIds = (payload.organizationIds as string[]) ?? [];
return next({
ctx: {
userId: payload.sub,
email: (payload.email as string) ?? "",
organizationIds,
activeOrganizationId: organizationIds[0] ?? null,
},
});
}
} catch (error) {
// A live session is the legit fallback for an unverifiable token
// (expired/missing). A TRPCError from verifyJWT is an explicit
// rejection (revoked/forged) — surface it instead of laundering
// it into session auth.
if (error instanceof TRPCError) throw error;
}
} catch (error) {
// A live session is the legit fallback for an unverifiable token
// (expired/missing). A TRPCError from verifyJWT is an explicit
// rejection (revoked/forged) — surface it instead of laundering it
// into session auth.
if (error instanceof TRPCError) throw error;
}

if (ctx.session) {
Expand All @@ -124,7 +121,7 @@ export const jwtProcedure = t.procedure.use(async ({ ctx, next }) => {

throw new TRPCError({
code: "UNAUTHORIZED",
message: "Invalid bearer token",
message: "Not authenticated. Provide a bearer JWT, x-api-key, or session.",
});
});

Expand Down
Loading