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.
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);
}{"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:
NewUserfor requests: only the fields a client may send.UserRowfor the database: everything.UserViewfor 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#
- Do not derive both
SerializeandDeserializeon database structs. - For request types, add
#[serde(deny_unknown_fields)]so unexpected fields produce an error (see the thread on JSON). - Check authorization on the server for every field that changes who can do what (roles, owner IDs).
- 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
Debugon a struct with a password field and printing it (see the thread on keeping secrets out of logs). - Accepting an
owner_idfrom the client instead of using the signed-in user.
Related questions#
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#
- Serde documentation, field attributes: https://serde.rs/field-attrs.html
- OWASP Mass Assignment Cheat Sheet: https://cheatsheetseries.owasp.org/cheatsheets/Mass_Assignment_Cheat_Sheet.html