Answer#
An error message written for a developer ("no such column users.pw_hash2") is information for an attacker: a table name, a column name, a technology. Treat the text of internal errors as private, and give the client only what it needs to act: whether it was its fault (a 4xx status) or ours (5xx), a short message, and a number it can quote to support.
Put the translation in one place, so no handler can forget it.
use std::fmt;
#[derive(Debug)]
enum AppError {
NotFound,
Database(String), // internal detail: table names, SQL, paths
}
impl fmt::Display for AppError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
AppError::NotFound => write!(f, "not found"),
AppError::Database(d) => write!(f, "database error: {d}"),
}
}
}
/// What the client sees: a status and a short message. The detail goes to our log, with a reference.
fn to_response(err: &AppError, request_no: u32) -> (u16, String) {
match err {
AppError::NotFound => (404, "not found".to_string()),
other => {
eprintln!("[ref {request_no}] {other}"); // the log keeps the full text
(500, format!("internal error, reference {request_no}"))
}
}
}
fn main() {
let errors = [AppError::NotFound, AppError::Database("no such column users.pw_hash2".into())];
for (n, e) in errors.iter().enumerate() {
println!("client sees: {:?}", to_response(e, 1000 + n as u32));
}
}client sees: (404, "not found")
[ref 1001] database error: no such column users.pw_hash2
client sees: (500, "internal error, reference 1001")The line starting with [ref 1001] goes to standard error, which stands for the server log. The lines starting with client sees are what is sent. The detailed text appears in the log only. A real implementation would use a unique request ID (random or from a counter), the same one in the log and in the response.
How to do it#
- Define an application error type with variants that carry detail, and convert library errors into it with
From. - Write one function that turns it into a status code and a public message.
- Log the full error (with the request ID) at that point.
- Return generic text for failed logins, forgotten passwords and permission checks. For permission failures, consider answering 404 where the existence of the resource is itself private.
- Turn off debug pages and stack traces in production builds and configuration.
Common mistakes#
format!("{e}")or{e:?}of a library error in the response body.- Different messages for different failure reasons on authentication (see the thread on account enumeration).
- Putting the internal detail into a header "for support".
Related questions#
Is it fine to be specific about invalid input?#
Yes, when it helps the user and reveals nothing internal: "the name must be 1 to 32 characters" is good. "Column name is too long for VARCHAR(32)" is not.
What status code should authorization failures return?#
403 when the resource is known to exist and the user may know that, 404 when its existence should stay private. Be consistent.
Sources#
- OWASP Error Handling Cheat Sheet: https://cheatsheetseries.owasp.org/cheatsheets/Error_Handling_Cheat_Sheet.html
- CWE-209, "Generation of Error Message Containing Sensitive Information": https://cwe.mitre.org/data/definitions/209.html