A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search
fix(auth): close account enumeration oracles in login and password reset
Mohamed Bassem036da4c0a1b7
diff --git a/packages/trpc/auth.ts b/packages/trpc/auth.tsindex 3a4c2c9b..5525edb8 100644--- a/packages/trpc/auth.ts+++ b/packages/trpc/auth.ts@@ -13,6 +13,18 @@ const BCRYPT_SALT_ROUNDS = 10;const API_KEY_PREFIX_V1 = "ak1";const API_KEY_PREFIX_V2 = "ak2";+// A *real* bcrypt hash of a random secret, used to burn the same amount of CPU+// on login paths that have no password to check against. It must be a valid+// hash at the same cost factor as real passwords: bcrypt parses the hash to+// recover the cost and salt, so handing it an arbitrary string makes it bail+// out immediately without deriving anything, which is exactly the timing leak+// these comparisons exist to close. Nothing can match it -- the input is+// random and thrown away.+const DUMMY_PASSWORD_HASH = bcrypt.hashSync(+ randomBytes(32).toString("hex"),+ BCRYPT_SALT_ROUNDS,+);+function generateApiKeySecret() {const secret = randomBytes(16).toString("hex");return {@@ -181,15 +193,14 @@ export async function validatePassword(if (!user) {// Run a bcrypt comparison anyways to hide the fact of whether the user exists or not (protecting against timing attacks)- await bcrypt.compare(- password +- "b6bfd1e907eb40462e73986f6cd628c036dc079b101186d36d53b824af3c9d2e",- "a-dummy-password-that-should-never-match",- );+ await bcrypt.compare(password, DUMMY_PASSWORD_HASH);throw new Error("User not found");}if (!user.password) {+ // Same reasoning: returning early here would make accounts without a+ // password (OAuth-only) measurably faster to probe than password accounts.+ await bcrypt.compare(password, DUMMY_PASSWORD_HASH);throw new Error("This user doesn't have a password defined");}diff --git a/packages/trpc/models/users.ts b/packages/trpc/models/users.tsindex a5d8327d..b47433f4 100644--- a/packages/trpc/models/users.ts+++ b/packages/trpc/models/users.ts@@ -305,9 +305,16 @@ export class User {});});- await sendPasswordResetEmail(email, user.name, token);+ // Deliberately not awaited. Delivery latency is only incurred for real+ // accounts, so awaiting it makes this endpoint a timing oracle for which+ // emails are registered -- and a hard oracle whenever SMTP is unhealthy,+ // since only real accounts could reach the throw below (500 for a user+ // that exists, 200 for one that doesn't).+ void sendPasswordResetEmail(email, user.name, token).catch((error) => {+ console.error("Failed to send password reset email:", error);+ });} catch (error) {- console.error("Failed to send password reset email:", error);+ console.error("Failed to create password reset token:", error);throw new TRPCError({code: "INTERNAL_SERVER_ERROR",message: "Failed to send password reset email",