Replies: 2 comments 1 reply
-
Hi 👋 The goal is to make it difficult to expose secrets without doing so explicitly via #[derive(Serialize)]
struct Test {
#[serde(serialize_with = "expose_secret")]
field: Secret<String>,
}
let to_serialize = Test {
field: Secret::from("hello"),
};
let serialized = serde_json::to_string(&to_serialize).unwrap();
assert_eq!(serialized, r#"{"field":"hello"}"#) Using
Your use case is a little different from mine. I'd like to understand why you're interested in serializing data without exposing the secret?
pub fn redacted<S: Serializer, T>(
secret: &Secret<T>,
serializer: S,
) -> Result<S::Ok, S::Error> {
format!("{secret:?}").serialize(serializer)
}
#[derive(Serialize)]
struct Test {
#[serde(serialize_with = "redacted")]
field: Secret<String>,
}
let to_serialize = Test {
field: Secret::from("hello"),
};
let serialized = serde_json::to_string(&to_serialize).unwrap();
assert_eq!(
serialized,
r#"{"field":"[REDACTED alloc::string::String]"}"#
) Doing this would prevent you from If that's something that would be useful to you, I'd be happy to include it in |
Beta Was this translation helpful? Give feedback.
-
Yes my use case is to prevent secrets ending up in formatted log entries (serde_json). Thanks for the explanation, and the example. |
Beta Was this translation helpful? Give feedback.
-
regarding:
Why can serialize expose the secret?
Can't you implement serialize without exposing the secret ?
https://serde.rs/impl-serialize.html
Maybe I am missing something but if I can't manipulate how serialize gets or gets not access to the secret that limits the scope of this crate a lot.
Beta Was this translation helpful? Give feedback.
All reactions