-
Notifications
You must be signed in to change notification settings - Fork 8
Redact secrets from logs and downgrade PII log levels #468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ChristianPavilonis
wants to merge
3
commits into
main
Choose a base branch
from
fix/secrets-logged
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| // NOTE: This file is also included in build.rs via #[path]. | ||
| // It must remain self-contained (no `crate::` imports). | ||
|
|
||
| //! A wrapper type that redacts sensitive values in [`Debug`] and [`Display`] output. | ||
| //! | ||
| //! Use [`Redacted`] for secrets, passwords, API keys, and other sensitive values | ||
| //! that must never appear in logs or error messages. | ||
|
|
||
| use core::fmt; | ||
|
|
||
| use serde::{Deserialize, Serialize}; | ||
|
|
||
| /// Wraps a value so that [`Debug`] and [`Display`] print `[REDACTED]` | ||
| /// instead of the inner contents. | ||
| /// | ||
| /// Access the real value via [`expose`](Redacted::expose). Callers must | ||
| /// never log or display the returned reference. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ``` | ||
| /// use trusted_server_common::redacted::Redacted; | ||
| /// | ||
| /// let secret = Redacted::new("my-secret-key".to_string()); | ||
| /// assert_eq!(format!("{:?}", secret), "[REDACTED]"); | ||
| /// assert_eq!(secret.expose(), "my-secret-key"); | ||
| /// ``` | ||
| #[derive(Clone, Serialize, Deserialize)] | ||
| #[serde(transparent)] | ||
| pub struct Redacted<T>(T); | ||
ChristianPavilonis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| impl<T> Redacted<T> { | ||
| /// Creates a new [`Redacted`] value. | ||
| #[allow(dead_code)] | ||
| pub fn new(value: T) -> Self { | ||
| Self(value) | ||
| } | ||
|
|
||
| /// Exposes the inner value for use in operations that need the actual secret. | ||
| /// | ||
| /// Callers should never log or display the returned reference. | ||
| pub fn expose(&self) -> &T { | ||
| &self.0 | ||
| } | ||
| } | ||
|
|
||
| impl<T: Default> Default for Redacted<T> { | ||
| fn default() -> Self { | ||
| Self(T::default()) | ||
| } | ||
| } | ||
|
|
||
| impl<T> fmt::Debug for Redacted<T> { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| write!(f, "[REDACTED]") | ||
| } | ||
| } | ||
|
|
||
| impl<T> fmt::Display for Redacted<T> { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| write!(f, "[REDACTED]") | ||
| } | ||
| } | ||
|
|
||
| impl From<String> for Redacted<String> { | ||
| fn from(value: String) -> Self { | ||
| Self(value) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn debug_output_is_redacted() { | ||
| let secret = Redacted::new("super-secret".to_string()); | ||
| assert_eq!( | ||
| format!("{:?}", secret), | ||
| "[REDACTED]", | ||
| "should print [REDACTED] in debug output" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn display_output_is_redacted() { | ||
| let secret = Redacted::new("super-secret".to_string()); | ||
| assert_eq!( | ||
| format!("{}", secret), | ||
| "[REDACTED]", | ||
| "should print [REDACTED] in display output" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn expose_returns_inner_value() { | ||
| let secret = Redacted::new("super-secret".to_string()); | ||
| assert_eq!( | ||
| secret.expose(), | ||
| "super-secret", | ||
| "should return the inner value" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn default_creates_empty_redacted() { | ||
| let secret: Redacted<String> = Redacted::default(); | ||
| assert_eq!(secret.expose(), "", "should default to empty string"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn from_string_creates_redacted() { | ||
| let secret = Redacted::from("my-key".to_string()); | ||
| assert_eq!(secret.expose(), "my-key", "should create from String"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn clone_preserves_inner_value() { | ||
| let secret = Redacted::new("cloneable".to_string()); | ||
| let cloned = secret.clone(); | ||
| assert_eq!( | ||
| cloned.expose(), | ||
| "cloneable", | ||
| "should preserve value after clone" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn serde_roundtrip() { | ||
| let secret = Redacted::new("serialize-me".to_string()); | ||
| let json = serde_json::to_string(&secret).expect("should serialize"); | ||
| assert_eq!(json, "\"serialize-me\"", "should serialize transparently"); | ||
|
|
||
| let deserialized: Redacted<String> = | ||
| serde_json::from_str(&json).expect("should deserialize"); | ||
| assert_eq!( | ||
| deserialized.expose(), | ||
| "serialize-me", | ||
| "should deserialize transparently" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn struct_with_redacted_field_debug() { | ||
| #[derive(Debug)] | ||
| #[allow(dead_code)] | ||
| struct Config { | ||
| name: String, | ||
| api_key: Redacted<String>, | ||
| } | ||
|
|
||
| let config = Config { | ||
| name: "test".to_string(), | ||
| api_key: Redacted::new("secret-key-123".to_string()), | ||
| }; | ||
|
|
||
| let debug = format!("{:?}", config); | ||
| assert!( | ||
| debug.contains("[REDACTED]"), | ||
| "should contain [REDACTED] for the api_key field" | ||
| ); | ||
| assert!( | ||
| !debug.contains("secret-key-123"), | ||
| "should not contain the actual secret" | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.