Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions transports/pnet/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## 0.27.0

- Redact `PreSharedKey` from `Debug` output and add `PreSharedKey::to_key_file` for
explicit raw key file export.

- Raise MSRV to 1.88.0.
See [PR 6273](https://github.com/libp2p/rust-libp2p/pull/6273).

Expand Down
52 changes: 50 additions & 2 deletions transports/pnet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ impl PreSharedKey {
.expect("shake128 failed");
Fingerprint(out)
}

/// Export this key in key file format compatible with go-libp2p.
pub fn to_key_file(self) -> String {
format!("/key/swarm/psk/1.0.0/\n/base16/\n{}\n", to_hex(&self.0))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we require this? This doesn't look like canonical

This comment was marked as outdated.

}
}

fn parse_hex_key(s: &str) -> Result<[u8; KEY_SIZE], KeyParseError> {
Expand Down Expand Up @@ -130,8 +135,8 @@ impl FromStr for PreSharedKey {

impl fmt::Debug for PreSharedKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("PreSharedKey")
.field(&to_hex(&self.0))
f.debug_struct("PreSharedKey")
.field("fingerprint", &self.fingerprint().to_string())
.finish()
}
}
Expand Down Expand Up @@ -343,6 +348,19 @@ mod tests {
.quickcheck(prop as fn(PreSharedKey) -> _);
}

#[test]
fn psk_to_key_file_parse() {
fn prop(key: PreSharedKey) -> bool {
let text = key.to_key_file();
text.parse::<PreSharedKey>()
.map(|res| res == key)
.unwrap_or(false)
}
QuickCheck::new()
.tests(10)
.quickcheck(prop as fn(PreSharedKey) -> _);
}

#[test]
fn psk_parse_failure() {
use KeyParseError::*;
Expand Down Expand Up @@ -373,4 +391,34 @@ mod tests {
let actual = key.fingerprint().to_string();
assert_eq!(expected, actual);
}

#[test]
fn debug_formatting_does_not_leak_raw_psk() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think these tests are required

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?
debug_formatting_does_not_leak_raw_psk directly covers the security promise of the PR. PreSharedKey Debug redacts the raw key, and derived PnetConfig Debug does not accidentally re-expose it through the field. This is valuable regression coverage.

let raw_hex = "6189c5cf0b87fb800c1a9feeda73c6ab5e998db48fb9e6a978575c770ceef683";
let key = format!("/key/swarm/psk/1.0.0/\n/base16/\n{raw_hex}")
.parse::<PreSharedKey>()
.unwrap();
let fingerprint = key.fingerprint().to_string();

let debug_output = format!("{key:?}");
let config_debug_output = format!("{:?}", PnetConfig::new(key));

assert!(!debug_output.contains(raw_hex));
assert!(!config_debug_output.contains(raw_hex));
assert!(debug_output.contains(&fingerprint));
assert!(config_debug_output.contains(&fingerprint));
}

#[test]
fn key_file_export_contains_raw_psk() {
let raw_hex = "6189c5cf0b87fb800c1a9feeda73c6ab5e998db48fb9e6a978575c770ceef683";
let key = format!("/key/swarm/psk/1.0.0/\n/base16/\n{raw_hex}")
.parse::<PreSharedKey>()
.unwrap();

let key_file = key.to_key_file();

assert!(key_file.contains(raw_hex));
assert_eq!(key_file.parse::<PreSharedKey>().unwrap(), key);
}
}
Loading