Enable strictNullChecks and remediate null-safety issues in backend#137
Enable strictNullChecks and remediate null-safety issues in backend#137
strictNullChecks and remediate null-safety issues in backend#137Conversation
There was a problem hiding this comment.
Pull request overview
This PR advances Phase 4.2 of the backend type-safety plan by enabling strictNullChecks in the Netlify Functions TypeScript config and remediating the nullability issues that surfaced from the compiler after that flag was turned on.
Changes:
- Enable
strictNullChecksintsconfig.netlify.functions.json(keepingnoImplicitAny, leaving fullstrictoff). - Harden null-safety in runtime code: use
toDefinedHeaders-narrowed headers inauth.ts, guarduser.hashbefore callingcomparePasswordinMongodbService.ts, castJWT_SECRETtojwt.Secretincrypt.ts, and tighten thesuccess204NoContenttype toSuccess<undefined>inmessages.ts. - Mark Phase 4.2 checklist complete and append the Phase 4.3 gate note in the planning docs.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
tsconfig.netlify.functions.json |
Enables strictNullChecks for the functions compilation path |
src/functions/auth.ts |
Passes the already-narrowed eventHeaders (from toDefinedHeaders) to getUserIdPassword instead of the raw event.headers |
src/lib/MongodbService.ts |
Adds an early-exit guard rejecting with 401 when user.hash is missing before calling comparePassword |
src/lib/crypt.ts |
Casts process.env.JWT_SECRET to jwt.Secret to satisfy the compiler in getAuthToken |
src/lib/messages.ts |
Narrows success204NoContent to Success<undefined> to accurately reflect a 204 No Content body |
docs/plans/phase-4_2-checklist.md |
Marks all Phase 4.2 checklist items as complete |
docs/plans/phase-04-type-safety-and-env-handling.md |
Adds the Phase 4.3 gate note recording the decision to proceed to broader strict evaluation |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/lib/crypt.ts
Outdated
| const jwtSecret = process.env.JWT_SECRET as jwt.Secret | ||
| return jwt.sign({ user, exp }, jwtSecret) |
There was a problem hiding this comment.
The fix for the strictNullChecks error uses process.env.JWT_SECRET as jwt.Secret to silence the compiler, but this is a type-cast workaround that doesn't add actual null safety. Unlike backend-utilities.ts (lines 39–41) and MongodbService.ts (lines 92–94), which both guard the env var at module load time with if (process.env.JWT_SECRET === undefined) throw "...", crypt.ts has no such guard. If JWT_SECRET is undefined at runtime, the cast will silently pass undefined to jwt.sign, which will then throw a JsonWebTokenError at the call site rather than failing fast at startup. The correct fix is to add a module-level guard — read the secret once, narrow the type to string (or jwt.Secret), and use that constant inside getAuthToken, consistent with the rest of the codebase.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 8 out of 8 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Motivation
strictNullChecksfor functions compilation to catch nullability issues early.strictescalation after completing null-safe remediations.Description
strictNullChecksintsconfig.netlify.functions.jsonwhile keepingnoImplicitAnyenabled.docs/plans/phase-4_2-checklist.mdand add a Phase 4.3 gate note todocs/plans/phase-04-type-safety-and-env-handling.mddescribing the next evaluation step.toDefinedHeaders(event.headers)result insrc/functions/auth.tswhen extracting credentials.src/lib/MongodbService.tsby checking for missinguser.hashbefore callingcomparePassword.src/lib/crypt.tsby castingprocess.env.JWT_SECRETtojwt.SecretingetAuthToken.src/lib/messages.tsby declaringsuccess204NoContent: Success<undefined>.Testing
yarn run typecheckand it completed green after the null-safety fixes.yarn test:backendand it passed.yarn test:frontendand it passed.Codex Task