FAQ - Help and questions

How do I return errors to clients without leaking internal details?

#security#errors#webpublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

Send the client a short, generic message and a reference number, and write the full error with the same number to your own log. Database messages, file paths, stack traces and library errors tell an attacker how the system is built. Map internal errors to public responses in one function.

Checked with rustc 1.93.0, Windows 11

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

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.

src/main.rsrust
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));
    }
}
text
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#

  1. Define an application error type with variants that carry detail, and convert library errors into it with From.
  2. Write one function that turns it into a status code and a public message.
  3. Log the full error (with the request ID) at that point.
  4. 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.
  5. 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".

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#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet