Skip to content

fix(backend): sanitize error handler to prevent internal info leak#204

Merged
devJaja merged 2 commits into
Epta-Node:mainfrom
DevSolex:fix/error-handler-info-leak
Jul 24, 2026
Merged

fix(backend): sanitize error handler to prevent internal info leak#204
devJaja merged 2 commits into
Epta-Node:mainfrom
DevSolex:fix/error-handler-info-leak

Conversation

@DevSolex

Copy link
Copy Markdown
Contributor

Summary

Fixes the global error handler leaking internal implementation details (raw error messages, stack traces, file paths) to API clients.

Closes #159

Changes

backend/src/api/middleware/errorHandler.ts

  • Check NODE_ENV to determine error detail level
  • Production: return err.code || 'INTERNAL_SERVER_ERROR' and a safe generic message — never expose raw err.message or stack trace
  • Development: return full err.message and err.stack for debugging
  • Always log the full error server-side via Pino (err, requestId, path, method)
  • Always include requestId and path in every response for traceability

backend/tests/errorHandler.test.ts (new)

12 tests covering all acceptance criteria:

  • Production: no raw message leaks, no stack trace, uses err.code when present, falls back to INTERNAL_SERVER_ERROR, respects err.statusCode, always includes requestId and path
  • Development: raw message exposed, stack trace exposed, no generic message field, always includes requestId

Test Results

PASS tests/errorHandler.test.ts
  errorHandler — production mode
    ✓ returns a generic error message (no raw err.message)
    ✓ does not include a stack trace
    ✓ uses err.code when present
    ✓ falls back to INTERNAL_SERVER_ERROR when err.code is absent
    ✓ respects err.statusCode
    ✓ always includes requestId in the response
    ✓ always includes path in the response
  errorHandler — development mode
    ✓ includes the raw error message
    ✓ includes the stack trace
    ✓ does not include a generic message field
    ✓ always includes requestId in the response

Acceptance Criteria

  • Check NODE_ENV to determine error detail level
  • Production: return generic error code, not raw message
  • Development: return full error message and stack trace
  • Always log full error server-side via Pino
  • Always include requestId in the response for debugging
  • Test: verify no internal details leak in production mode
  • Test: verify full details available in development mode

DevSolex added 2 commits July 24, 2026 21:18
Verify that createStatsRouter is correctly wired into the Express app:

- Mount at /api/stats returns 200 (not 404) with correct stats shape
- Empty database returns zeroed totals and 100% uptime with 24 hourly buckets
- Inserted agents/tasks/payments are reflected in the response
- Closed database is handled gracefully and returns 500
- Full-app wiring test confirms /api/stats is mounted via createApp

The router mount and OpenAPI registration were already present in
app.ts and openapi.ts; this commit adds the missing test coverage.
…pta-Node#159)

- Check NODE_ENV to determine error detail level
- Production: return generic error code (err.code || INTERNAL_SERVER_ERROR)
  and a safe user-facing message; never expose raw err.message or stack
- Development: include full error message and stack trace for debugging
- Always log full error server-side via Pino (err, requestId, path, method)
- Always include requestId and path in every response for traceability
- Add tests: production mode leaks no internal details; development mode
  exposes full error message and stack trace
@vercel

vercel Bot commented Jul 24, 2026

Copy link
Copy Markdown

@DevSolex is attempting to deploy a commit to the Jaja's projects Team on Vercel.

A member of the Team first needs to authorize it.

@devJaja
devJaja self-requested a review July 24, 2026 20:28

@devJaja devJaja left a comment

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.

LGTM @DevSolex

@devJaja
devJaja merged commit b1a0a66 into Epta-Node:main Jul 24, 2026
6 of 7 checks passed
@DevSolex

Copy link
Copy Markdown
Contributor Author

LGTM @DevSolex

Appreciate the review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Backend] Fix Error Handler Leaking Internal Error Messages to Clients

2 participants