Exposure of exception messages

Exposure of exception messages

Description

Including err.message or err.toString() in an HTTP response may disclose internal file paths, SQL errors, configuration values, or library details to the client.

Potential impact

An error may reveal sensitive values and service structure. Differences between error responses can also help someone refine subsequent inputs.

Remediation

  • Use a shared Express or Node error handler to return an appropriate status and a generic message. Keep exception details out of plain-text bodies, JSON, and headers.
  • Send required diagnostics only to server logs, and turn off development debug responses in production.

Examples

Before

javascript
app.get("/profile", async (req, res) => {
  try {
    const profile = await loadProfile(req.user.id);
    res.json(profile);
  } catch (err) {
    res.status(500).send(err.message);
  }
});

After

javascript
app.get("/profile", async (req, res) => {
  try {
    const profile = await loadProfile(req.user.id);
    res.json(profile);
  } catch (err) {
    logger.error({ err }, "profile load failed");
    res.status(500).json({ error: "Internal server error" });
  }
});

Both handlers return 500 if the profile fails to load. The revised handler logs the error and sends only a fixed message in the JSON response.

References