Agent Wranglers

APS-78Recommended

Show short errors

Show visitors a short error with a reference number. Keep stack traces, queries and file paths in the server log.

How to do it

Catch every error in one place. Log the full detail with a random reference, and return a plain message with that reference. Turn off debug mode in production. For API error bodies, see no internal details.

Examples

JSON example

{ "error": "Something went wrong. Reference: 7f3c2a" }

JavaScript example

// Express: one error handler, last in the chain
app.use((err, req, res, next) => {
  const reference = crypto.randomUUID().slice(0, 8);
  log.error({ reference, err });
  res.status(500).json({ error: `Something went wrong. Reference: ${reference}` });
});

Python example

# Python with Flask: 500 catches every error the code did not handle
@app.errorhandler(500)
def handle_error(error):
    reference = secrets.token_hex(4)
    app.logger.exception("reference=%s", reference)
    return {"error": f"Something went wrong. Reference: {reference}"}, 500

How to check

The agent sends broken input, such as a quote in a search box or invalid JSON to an API. The reply has no stack trace, query, path or version number.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/application-security/errors-logs-and-vulnerabilities/short-errors">Agent Wranglers application security playbook</a>. In code you write, add a comment next to it with the same link.