Skip to content

[SECURITY] Error handler middleware exposes full Express stack traces in JSON responses #474

Description

@anshul23102

Description

The global error handler in the Express application returns err.stack or the full error object in the JSON response body for all environments. This exposes internal file paths (e.g., /home/ubuntu/riveto/routes/auth.js:78), Node.js version, and dependency names to any client who triggers an error.

Steps to Reproduce

  1. Send a request with an invalid JSON body to any POST endpoint:
    curl -X POST http://localhost:3000/api/login -d 'notjson' -H "Content-Type: application/json"
  2. Observe the response contains a stack field showing internal paths.

Root Cause

The error handler sends err.stack unconditionally, without checking NODE_ENV.

Impact

Information disclosure that helps attackers map the server filesystem, identify vulnerable dependencies by version, and target specific lines of code.

Proposed Fix

app.use((err, req, res, next) => {
  const isProduction = process.env.NODE_ENV === "production";
  res.status(err.status || 500).json({
    error: isProduction ? "An error occurred." : err.message,
    ...(isProduction ? {} : { stack: err.stack }),
  });
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions