FAQ - Help and questions

How do I keep password hashes out of JSON responses and stop clients setting admin flags?

#security#serde#json#authorizationpublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

Mark sensitive fields with serde skip attributes so they are never written to a response and never read from a request, or better, use separate request and response types. This blocks mass assignment, where a client adds fields such as is_admin to a JSON body and the server stores them.

Checked with rustc 1.93.0, serde 1.0.229, serde_json 1.0.154, Windows 11

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

#1

Answer#

If one struct is used for the database row, the incoming JSON and the outgoing JSON, then every field is both readable and writable from outside, by default. Two mistakes follow: sending password_hash back to a browser, and accepting "is_admin": true from the client (mass assignment).

Serde attributes control each direction: skip_serializing leaves a field out of output, skip_deserializing ignores it in input, and skip does both.

src/main.rsrust
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
struct User {
    id: u32,
    name: String,
    #[serde(skip)] // never written to a response, never read from a request
    password_hash: String,
    #[serde(skip_deserializing, default)] // never accepted from a request
    is_admin: bool,
}

fn main() {
    let stored = User { id: 7, name: "ada".into(), password_hash: "$argon2id$...".into(), is_admin: true };
    println!("{}", serde_json::to_string(&stored).unwrap());

    let from_client: User =
        serde_json::from_str(r#"{"id":1,"name":"mallory","is_admin":true,"password_hash":"x"}"#).unwrap();
    println!("is_admin after parsing client JSON: {}", from_client.is_admin);
    println!("hash taken from client JSON: {:?}", from_client.password_hash);
}
text
{"id":7,"name":"ada","is_admin":true}
is_admin after parsing client JSON: false
hash taken from client JSON: ""

The first line is what a response contains: no hash. The other two lines parse a client body that tried to set is_admin and password_hash; both were ignored and kept their defaults. (The id is still accepted from the client here; decide separately whether a client may choose it.)

A stronger design#

Attributes are a safety net. The clearer approach is separate types:

  • NewUser for requests: only the fields a client may send.
  • UserRow for the database: everything.
  • UserView for responses: only what may be shown.

The compiler then stops you from returning a row by mistake, and a new column added to the database is not shown by default.

How to do it#

  1. Do not derive both Serialize and Deserialize on database structs.
  2. For request types, add #[serde(deny_unknown_fields)] so unexpected fields produce an error (see the thread on JSON).
  3. Check authorization on the server for every field that changes who can do what (roles, owner IDs).
  4. Review the JSON your API returns in tests, and assert that sensitive keys are missing.

Common mistakes#

  • Relying on the front end to hide fields.
  • Deriving Debug on a struct with a password field and printing it (see the thread on keeping secrets out of logs).
  • Accepting an owner_id from the client instead of using the signed-in user.

Does skip_deserializing reject the field or ignore it?#

Ignore, as the output shows. Combine with deny_unknown_fields on structs where you prefer an error; check how the two attributes interact for your serde version before relying on a specific error.

Is there a way to see all JSON my API can return?#

Keep response types in one module and search for Serialize derives. Fewer serialisable types means fewer places to leak from.

Sources#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet