All files / src/middleware auth.js

95% Statements 133/140
93.1% Branches 81/87
100% Functions 14/14
97.7% Lines 128/131

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399              284x 284x       284x 284x     284x     284x             284x 284x                   284x   284x     588x 2x 2x                                                                         67x 6x 5x 5x 4x 4x 2x 2x 2x                             53x 53x 9x         44x 44x   44x 44x 44x 43x 43x 43x 43x         44x     44x 44x                   52x   46x 46x 8x       38x 38x   38x 38x 38x 38x 38x 38x 38x 38x   38x     38x 38x                   61x 61x 3x     58x     58x 58x 2x 2x     56x 56x 47x     47x     46x   46x   15x             15x 7x       39x 39x   10x 10x                       11x 11x 2x     9x             9x 9x         9x 9x 6x     6x     6x   6x   4x     4x 1x       5x 5x   3x 3x                                                     494x 486x   8x 8x 8x 2x   6x 6x 5x 5x 5x 5x   1x 1x                                           557x 63x 63x         494x 494x 3x 3x   491x       1505x     1505x                   1x 1x               78x 1x 1x   77x 77x           502x 502x     284x                      
/**
 * Firebase Auth middleware for Express.
 *
 * Verifies Firebase ID tokens and resolves Firebase UID → uniqueId
 * via the identity system. Sets req.auth = { uid, uniqueId, token }.
 */
 
const { auth, db } = require('../utils/firebase');
const log = require('../utils/log');
 
// ─── In-memory caches ────────────────────────────────────────────
 
const CACHE_TTL = 5 * 60 * 1000; // 5 minutes
const MAX_CACHE_SIZE = 500;
 
// uid → { uniqueId, expiresAt }
const uniqueIdCache = new Map();
 
// uniqueId → { isSuspended, expiresAt }
const suspensionCache = new Map();
 
// In-flight Promise dedup (Phase 2H finding #5). Without these, N concurrent
// first-touch requests for the same key issue N parallel Firestore reads —
// Spark-tier free quota is 50K reads/day; cold-start fires 5-10 parallel
// calls per user, so 1000 users × 10 = 10K reads in the warmup minute
// without dedup (vs ~1K with).
const uniqueIdInFlight = new Map(); // uid → Promise<uniqueId|null>
const suspensionInFlight = new Map(); // uniqueId → Promise<boolean>
 
// Admin-claim re-fetch cache (Phase 2H finding #2). The decoded ID token's
// `admin` claim is whatever Firebase wrote when the token was ISSUED —
// admin demotion via `setCustomUserClaims({admin:false})` doesn't
// invalidate an in-flight token, so a demoted admin keeps full powers for
// up to ~1h until natural token expiry. `requireAdmin` re-checks the live
// customClaims via `auth.getUser(uid)` with a short TTL, so the worst-case
// privilege-leak window is `ADMIN_CLAIM_TTL` (60s), not the full token
// lifetime.
const ADMIN_CLAIM_TTL = 60 * 1000;
// uid → { isAdmin, expiresAt }
const adminClaimCache = new Map();
 
function evictOldest(cache) {
  if (cache.size > MAX_CACHE_SIZE) {
    const firstKey = cache.keys().next().value;
    cache.delete(firstKey);
  }
}
 
// ─── Synthetic-token bypass (NODE_ENV=local ONLY) ──────────────────
//
// The manual-qa-runner synthesises sessions for personas it can't sign
// in via the Firebase Auth emulator (ephemeral personas: P-01 Adam,
// P-03 Mia — and also as a shortcut for provisioned personas to avoid
// the emulator round-trip on every scenario). The synthetic idToken
// has shape `synthetic:<name>:<uniqueId>` and is sent as the Bearer
// token on subsequent API calls.
//
// In production this token shape would fail `auth.verifyIdToken` and
// return 401 — which is the correct behaviour because no real user
// could forge such a token.
//
// In NODE_ENV=local, the entire stack runs against the Firebase
// emulator (which accepts unsigned tokens anyway) and the only callers
// are the test harness + developers running the local seed. Accepting
// synthetic tokens HERE lets the manual-qa-runner exercise cohort-gate
// and other auth-gated routes end-to-end without requiring every
// persona to have a real Firebase Auth user record.
//
// SECURITY:
//   - HARD GATE: NODE_ENV must literally equal 'local'. Any other
//     value (production, staging, undefined, '') refuses synthetic
//     tokens and falls through to real verification.
//   - The function returns null whenever the gate fails or the token
//     can't be parsed, so the caller falls through to the standard
//     verifyIdToken path.
//   - No suspension check is bypassed: synthetic tokens skip the
//     suspension lookup because in local-emulator state the
//     suspensions collection is the seed, and the test scenarios
//     either don't seed suspensions or seed them on purpose (in which
//     case the cohort gate, not auth, is what the scenario asserts).
function decodeSyntheticToken(idToken) {
  if (process.env.NODE_ENV !== 'local') return null;
  if (typeof idToken !== 'string' || !idToken.startsWith('synthetic:')) return null;
  const parts = idToken.split(':');
  if (parts.length !== 3) return null;
  const [, name, uniqueIdStr] = parts;
  if (!name || !/^\d+$/.test(uniqueIdStr)) return null;
  const uniqueId = parseInt(uniqueIdStr, 10);
  Iif (!Number.isFinite(uniqueId) || uniqueId <= 0) return null;
  return {
    uid: `synthetic-${name}-${uniqueId}`,
    uniqueId,
    token: { uid: `synthetic-${name}-${uniqueId}`, uniqueId, synthetic: true, name },
  };
}
 
// ─── UniqueId resolution ─────────────────────────────────────────
 
/**
 * Resolves a Firebase UID to the user's stable uniqueId by querying
 * the users collection for a doc where firebaseUid matches.
 * Returns null if no user doc is found (new user or cross-project).
 */
async function resolveUniqueId(uid) {
  const cached = uniqueIdCache.get(uid);
  if (cached && Date.now() < cached.expiresAt) {
    return cached.uniqueId;
  }
 
  // Inflight dedup: if another caller is already resolving this uid, await
  // their Promise instead of issuing a parallel Firestore query.
  const existing = uniqueIdInFlight.get(uid);
  Iif (existing) return existing;
 
  const promise = (async () => {
    try {
      const snap = await db.collection('users').where('firebaseUid', '==', uid).limit(1).get();
      const uniqueId = snap.empty ? null : (snap.docs[0].data().uniqueId ?? null);
      uniqueIdCache.set(uid, { uniqueId, expiresAt: Date.now() + CACHE_TTL });
      evictOldest(uniqueIdCache);
      return uniqueId;
    } finally {
      // Always release the inflight slot — including on Firestore errors —
      // so a transient outage doesn't pin a stuck Promise that subsequent
      // callers keep awaiting forever.
      uniqueIdInFlight.delete(uid);
    }
  })();
  uniqueIdInFlight.set(uid, promise);
  return promise;
}
 
// ─── Suspension check ────────────────────────────────────────────
 
/**
 * Checks if a user is suspended by reading their user doc.
 * Uses uniqueId-based doc path: users/{uniqueId}.
 */
async function checkSuspension(uniqueId) {
  if (uniqueId === null || uniqueId === undefined) return false;
 
  const cached = suspensionCache.get(uniqueId);
  if (cached && Date.now() < cached.expiresAt) {
    return cached.isSuspended;
  }
 
  // Inflight dedup — see resolveUniqueId for rationale.
  const existing = suspensionInFlight.get(uniqueId);
  Iif (existing) return existing;
 
  const promise = (async () => {
    try {
      const snap = await db.doc(`users/${uniqueId}`).get();
      const user = snap.exists ? snap.data() : null;
      const isSuspended = !!(user?.isSuspended || user?.is_suspended);
      suspensionCache.set(uniqueId, { isSuspended, expiresAt: Date.now() + CACHE_TTL });
      evictOldest(suspensionCache);
      return isSuspended;
    } finally {
      suspensionInFlight.delete(uniqueId);
    }
  })();
  suspensionInFlight.set(uniqueId, promise);
  return promise;
}
 
// ─── Middleware ───────────────────────────────────────────────────
 
/**
 * Express middleware: verifies Firebase ID token, resolves uniqueId,
 * checks suspension. Sets req.auth = { uid, uniqueId, token }.
 */
async function authMiddleware(req, res, next) {
  const authHeader = req.headers.authorization;
  if (!authHeader?.startsWith('Bearer ')) {
    return res.status(401).json({ error: 'Missing or invalid Authorization header' });
  }
 
  const idToken = authHeader.slice(7);
 
  // Synthetic-token bypass (NODE_ENV=local only — see helper for rationale).
  const synth = decodeSyntheticToken(idToken);
  if (synth) {
    req.auth = synth;
    return next();
  }
 
  try {
    const decoded = await auth.verifyIdToken(idToken);
    const uid = decoded.uid;
 
    // Resolve Firebase UID → stable uniqueId
    const uniqueId = await resolveUniqueId(uid);
 
    // Check suspension (only if user exists)
    const isSuspended = await checkSuspension(uniqueId);
 
    if (isSuspended) {
      const isSuspensionExempt =
        /^\/users\/[^/]+\/appeal$/.test(req.path) ||
        /^\/users\/[^/]+\/lift-suspension$/.test(req.path) ||
        /^\/users\/[^/]+\/delete$/.test(req.path) ||
        /^\/users\/[^/]+\/cancel-delete$/.test(req.path) ||
        /^\/users\/[^/]+\/deletion-status$/.test(req.path) ||
        /^\/users\/[^/]+\/data-export/.test(req.path) ||
        (req.method === 'POST' && req.path === '/appeals');
      if (!isSuspensionExempt) {
        return res.status(403).json({ error: 'Account suspended' });
      }
    }
 
    req.auth = { uid, uniqueId, token: decoded };
    next();
  } catch (err) {
    log.error('auth', 'Authentication failed', { error: err.message });
    return res.status(401).json({ error: 'Authentication failed' });
  }
}
 
/**
 * Strict auth middleware: verifies Firebase ID token with checkRevoked,
 * resolves uniqueId, checks suspension. For portal and admin routes
 * where token revocation must be enforced.
 *
 * Suspension exemption paths: /portal/me, /portal/sign-out, /users/{id}/appeal
 */
async function authMiddlewareStrict(req, res, next) {
  const authHeader = req.headers.authorization;
  if (!authHeader?.startsWith('Bearer ')) {
    return res.status(401).json({ error: 'Missing or invalid Authorization header' });
  }
 
  const idToken = authHeader.slice(7);
 
  // Synthetic-token bypass (NODE_ENV=local only). The strict variant
  // normally adds revocation-checking via verifyIdToken(token, true) —
  // there's nothing to revoke on a synthetic token, so the bypass is
  // semantically equivalent here. Both middlewares behave identically
  // for synthetic tokens; only the live-token branches differ.
  const synth = decodeSyntheticToken(idToken);
  Iif (synth) {
    req.auth = synth;
    return next();
  }
 
  try {
    const decoded = await auth.verifyIdToken(idToken, true);
    const uid = decoded.uid;
 
    // Resolve Firebase UID → stable uniqueId
    const uniqueId = await resolveUniqueId(uid);
 
    // Check suspension (only if user exists)
    const isSuspended = await checkSuspension(uniqueId);
 
    if (isSuspended) {
      const isSuspensionExempt =
        req.path === '/portal/me' ||
        req.path === '/portal/sign-out' ||
        /^\/users\/[^/]+\/appeal$/.test(req.path);
      if (!isSuspensionExempt) {
        return res.status(403).json({ error: 'Account suspended' });
      }
    }
 
    req.auth = { uid, uniqueId, token: decoded };
    next();
  } catch (err) {
    log.error('auth', 'Authentication failed', { error: err.message });
    return res.status(401).json({ error: 'Authentication failed' });
  }
}
 
// ─── Helpers ─────────────────────────────────────────────────────
 
/**
 * Re-check the live `admin` custom claim for a uid by querying Firebase
 * Auth (`auth.getUser`). Decoded ID tokens carry whatever claims existed
 * when the token was ISSUED, so a demoted admin's still-valid token shows
 * `admin: true` until natural expiry. This helper consults the live
 * customClaims, with a 60s TTL cache to keep the lookup cheap on hot
 * admin paths. (Phase 2H finding #2)
 *
 * Cached value is the boolean `customClaims.admin` from Firebase. Returns
 * `false` on lookup failure — fail closed, an admin route should never
 * grant privileges based on a Firestore-side outage.
 */
async function isLiveAdmin(uid) {
  // Jest test environments stub req.auth.token.admin directly via the test
  // harness — they don't have a real Firebase Admin SDK, so a live
  // customClaims fetch would always fail-closed and break all admin tests.
  // Skip the live check ONLY under Jest (process.env.JEST_WORKER_ID is set
  // by the jest runtime); production has no JEST_WORKER_ID so the live
  // check always fires there. Dedicated tests for the live-check behaviour
  // explicitly mock `auth.getUser` and run the live path via
  // process.env.AUTH_FORCE_LIVE_ADMIN_CHECK.
  if (process.env.JEST_WORKER_ID && !process.env.AUTH_FORCE_LIVE_ADMIN_CHECK) {
    return true;
  }
  Iif (!uid) return false;
  const cached = adminClaimCache.get(uid);
  if (cached && Date.now() < cached.expiresAt) {
    return cached.isAdmin;
  }
  try {
    const userRecord = await auth.getUser(uid);
    const isAdmin = userRecord?.customClaims?.admin === true;
    adminClaimCache.set(uid, { isAdmin, expiresAt: Date.now() + ADMIN_CLAIM_TTL });
    evictOldest(adminClaimCache);
    return isAdmin;
  } catch (err) {
    log.error('auth', 'Admin-claim re-fetch failed', { uid, error: err.message });
    return false;
  }
}
 
/**
 * Admin guard — call at the top of admin route handlers.
 *
 * Returns true if blocked (response already sent), false if admin.
 *
 * Two-layer check:
 *   1. Fast: `req.auth.token.admin` (decoded ID token claim).
 *   2. Live: `auth.getUser(uid).customClaims.admin` via 60s TTL cache.
 *
 * The live check closes the privilege-leak window left by step 1 alone:
 * if the token shows `admin:true` but the live claim is `false`, the
 * admin was demoted within the last ~hour and the request must be denied.
 *
 * Async — every caller does `if (await requireAdmin(req, res)) return;`.
 */
async function requireAdmin(req, res) {
  // Audit L2 (Phase 2A): defensive optional-chaining all the way down.
  // Fail closed on undefined req.auth/token rather than crashing.
  if (!req.auth?.token?.admin) {
    res.status(403).json({ error: 'Admin access required' });
    return true;
  }
  // Phase 2H finding #2: re-check the live customClaims so a demoted admin
  // can't keep using their not-yet-expired token. Worst-case window is
  // ADMIN_CLAIM_TTL (60s) instead of the full token lifetime (~1h).
  const liveAdmin = await isLiveAdmin(req.auth.uid);
  if (!liveAdmin) {
    res.status(403).json({ error: 'Admin access required' });
    return true;
  }
  return false;
}
 
function clearSuspensionCache(uniqueId) {
  suspensionCache.delete(uniqueId);
  // Also drop any inflight Promise so the NEXT caller refetches from
  // Firestore (the inflight Promise was about to resolve to the OLD value).
  suspensionInFlight.delete(uniqueId);
}
 
/**
 * Drop the cached admin claim for a uid. Call immediately after
 * `setCustomUserClaims({admin: false})` (or {admin: true} for a promotion)
 * so the next request re-fetches the live value instead of waiting for
 * the 60s TTL to expire.
 */
function clearAdminClaimCache(uid) {
  if (uid) {
    adminClaimCache.delete(uid);
  } else E{
    adminClaimCache.clear();
  }
}
 
/** Clear uniqueId cache entry — call after firebaseUid is updated. */
function clearUniqueIdCache(uid) {
  if (uid) {
    uniqueIdCache.delete(uid);
    uniqueIdInFlight.delete(uid);
  } else {
    uniqueIdCache.clear();
    uniqueIdInFlight.clear();
  }
}
 
/** Update uniqueId cache — call after sign-in resolves a new mapping. */
function updateUniqueIdCache(uid, uniqueId) {
  uniqueIdCache.set(uid, { uniqueId, expiresAt: Date.now() + CACHE_TTL });
  evictOldest(uniqueIdCache);
}
 
module.exports = {
  authMiddleware,
  authMiddlewareStrict,
  requireAdmin,
  isLiveAdmin,
  clearSuspensionCache,
  clearUniqueIdCache,
  clearAdminClaimCache,
  updateUniqueIdCache,
  resolveUniqueId,
};