Skip to content
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix: early bail out
  • Loading branch information
G3root committed Aug 7, 2024
commit 0aa50d33cf5817fa45b45af595cc741034b0e15e
12 changes: 3 additions & 9 deletions src/server/api/middlewares/bearer-token.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { verifySecureHash } from "@/lib/crypto";
import type { Context } from "hono";
import { createMiddleware } from "hono/factory";
import { nanoid } from "nanoid";
import { ApiError } from "../error";

export type accessTokenAuthMiddlewareOptions =
Expand Down Expand Up @@ -48,19 +49,12 @@ async function authenticateWithAccessToken(

const accessToken = await findAccessToken(clientId, c);

if (!accessToken) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The early bailout is vulnerable to timing attacks because the attacker can verify whether the ID exists in the database based on the timing.

throw new ApiError({
code: "UNAUTHORIZED",
message: "Bearer token is invalid",
});
}

const isAccessTokenValid = await verifySecureHash(
clientSecret,
accessToken.clientSecret,
accessToken?.clientSecret ?? nanoid(),
);

if (!isAccessTokenValid) {
if (!isAccessTokenValid || !accessToken) {
throw new ApiError({
code: "UNAUTHORIZED",
message: "Bearer token is invalid",
Expand Down