-
Notifications
You must be signed in to change notification settings - Fork 52
Harden nsec storage with SecretString and one-time key init #772
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
arkanoider
wants to merge
3
commits into
main
Choose a base branch
from
harden/nsec-zeroization
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,85 @@ | ||
| //! Helpers for loading and parsing the Mostro Nostr private key with | ||
| //! zeroization of transient buffers. | ||
|
|
||
| use crate::config::constants::NSEC_ENV_VAR; | ||
| use crate::config::types::NostrSettings; | ||
| use mostro_core::error::MostroError::{self, *}; | ||
| use mostro_core::error::ServiceError; | ||
| use nostr_sdk::Keys; | ||
| use secrecy::{ExposeSecret, SecretString}; | ||
| use serde::Serializer; | ||
| use zeroize::Zeroize; | ||
|
|
||
| /// Serialize a [`SecretString`] for config files (wizard / TOML export only). | ||
| pub fn serialize_nsec<S>(secret: &SecretString, serializer: S) -> Result<S::Ok, S::Error> | ||
| where | ||
| S: Serializer, | ||
| { | ||
| serializer.serialize_str(secret.expose_secret()) | ||
| } | ||
|
|
||
| /// Read `MOSTRO_NSEC_PRIVKEY` from the process environment, trim whitespace, | ||
| /// and wrap in a [`SecretString`]. Returns `None` when unset or blank. | ||
| pub fn read_nsec_env_var() -> Option<SecretString> { | ||
| let mut nsec_from_env = std::env::var(NSEC_ENV_VAR).ok()?; | ||
| let trimmed = nsec_from_env.trim(); | ||
| if trimmed.is_empty() { | ||
| nsec_from_env.zeroize(); | ||
| return None; | ||
| } | ||
| let secret = SecretString::from(trimmed.to_owned()); | ||
| nsec_from_env.zeroize(); | ||
| Some(secret) | ||
| } | ||
|
|
||
| /// Parse a bech32 nsec into [`Keys`], exposing the secret only in this scope. | ||
| pub fn parse_mostro_keys(secret: &SecretString) -> Result<Keys, MostroError> { | ||
| let nsec = secret.expose_secret(); | ||
| if nsec.is_empty() { | ||
| return Err(MostroInternalErr(ServiceError::NostrError( | ||
| "Nostr private key is not configured".to_string(), | ||
| ))); | ||
| } | ||
| Keys::parse(nsec).map_err(|e| { | ||
| tracing::error!("Failed to parse nostr private key: {}", e); | ||
| MostroInternalErr(ServiceError::NostrError(e.to_string())) | ||
| }) | ||
| } | ||
|
|
||
| /// Take the nsec from nostr settings (env override must already be applied), | ||
| /// parse it into [`Keys`], and clear the field so global settings no longer | ||
| /// retain plaintext. | ||
| pub fn take_nsec_for_init(nostr: &mut NostrSettings) -> Result<Keys, MostroError> { | ||
| let secret = std::mem::take(&mut nostr.nsec_privkey); | ||
| let keys = parse_mostro_keys(&secret)?; | ||
| nostr.nsec_privkey = SecretString::default(); | ||
| Ok(keys) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use secrecy::ExposeSecret; | ||
|
|
||
| #[test] | ||
| fn take_nsec_clears_settings_field() { | ||
| let mut nostr = NostrSettings { | ||
| nsec_privkey: SecretString::from( | ||
| "nsec13as48eum93hkg7plv526r9gjpa0uc52zysqm93pmnkca9e69x6tsdjmdxd", | ||
| ), | ||
| relays: vec![], | ||
| }; | ||
| let keys = take_nsec_for_init(&mut nostr).expect("valid test nsec"); | ||
| assert!(nostr.nsec_privkey.expose_secret().is_empty()); | ||
| assert!(!keys.public_key().to_hex().is_empty()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parse_mostro_keys_rejects_empty() { | ||
| let err = parse_mostro_keys(&SecretString::default()).unwrap_err(); | ||
| assert!(matches!( | ||
| err, | ||
| MostroInternalErr(ServiceError::NostrError(_)) | ||
| )); | ||
| } | ||
| } | ||
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
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.