Skip to content

Commit 5da54c3

Browse files
chore: cargo fmt security tests
1 parent fb11192 commit 5da54c3

6 files changed

Lines changed: 34 additions & 23 deletions

File tree

crates/heartwood-core/src/derive.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub fn derive(root: &TreeRoot, purpose: &str, index: u32) -> Result<Identity, He
6060
}
6161
Err(_) => {
6262
derived.zeroize(); // don't leak failed attempt
63-
// Invalid scalar (exceeds curve order), try next index
63+
// Invalid scalar (exceeds curve order), try next index
6464
if current_index == MAX_INDEX {
6565
return Err(HeartwoodError::IndexOverflow);
6666
}

crates/heartwood-device/src/storage.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,8 @@ fn set_dir_permissions(_path: &Path) -> io::Result<()> {
8686
fn write_secret_file(path: &Path, data: &[u8]) -> io::Result<()> {
8787
use std::os::unix::fs::OpenOptionsExt;
8888

89-
let mut file = fs::OpenOptions::new()
90-
.write(true)
91-
.create(true)
92-
.truncate(true)
93-
.mode(0o600)
94-
.open(path)?;
89+
let mut file =
90+
fs::OpenOptions::new().write(true).create(true).truncate(true).mode(0o600).open(path)?;
9591
io::Write::write_all(&mut file, data)?;
9692
Ok(())
9793
}

crates/heartwood-device/src/web.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ async fn api_audit(State(state): State<Arc<AppState>>) -> impl IntoResponse {
4141
let entries: Vec<_> = log.entries().iter().collect();
4242
match serde_json::to_value(&entries) {
4343
Ok(val) => (StatusCode::OK, axum::Json(val)),
44-
Err(_) => (StatusCode::INTERNAL_SERVER_ERROR, axum::Json(json!({"error": "serialisation failed"}))),
44+
Err(_) => (
45+
StatusCode::INTERNAL_SERVER_ERROR,
46+
axum::Json(json!({"error": "serialisation failed"})),
47+
),
4548
}
4649
}
4750

crates/heartwood-nip46/src/methods.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,17 @@ impl fmt::Debug for Nip46Request {
6565
Self::Nip04Encrypt(p) => write!(f, "Nip04Encrypt([{} params])", p.len()),
6666
Self::Nip04Decrypt(p) => write!(f, "Nip04Decrypt([{} params])", p.len()),
6767
Self::HeartwoodDerive(p) => write!(f, "HeartwoodDerive([{} params])", p.len()),
68-
Self::HeartwoodDerivePersona(p) => write!(f, "HeartwoodDerivePersona([{} params])", p.len()),
68+
Self::HeartwoodDerivePersona(p) => {
69+
write!(f, "HeartwoodDerivePersona([{} params])", p.len())
70+
}
6971
Self::HeartwoodListIdentities => write!(f, "HeartwoodListIdentities"),
7072
Self::HeartwoodSwitch(p) => write!(f, "HeartwoodSwitch([{} params])", p.len()),
71-
Self::HeartwoodCreateProof(p) => write!(f, "HeartwoodCreateProof([{} params])", p.len()),
72-
Self::HeartwoodVerifyProof(p) => write!(f, "HeartwoodVerifyProof([{} params])", p.len()),
73+
Self::HeartwoodCreateProof(p) => {
74+
write!(f, "HeartwoodCreateProof([{} params])", p.len())
75+
}
76+
Self::HeartwoodVerifyProof(p) => {
77+
write!(f, "HeartwoodVerifyProof([{} params])", p.len())
78+
}
7379
Self::HeartwoodRecover(p) => write!(f, "HeartwoodRecover([{} params])", p.len()),
7480
}
7581
}
@@ -122,7 +128,11 @@ impl Nip46Response {
122128
pub fn ok(id: impl Into<String>, result: serde_json::Value) -> Self {
123129
let id = id.into();
124130
if contains_nsec(&result) {
125-
return Self { id, result: None, error: Some("response contained secret key material".into()) };
131+
return Self {
132+
id,
133+
result: None,
134+
error: Some("response contained secret key material".into()),
135+
};
126136
}
127137
Self { id, result: Some(result), error: None }
128138
}

crates/heartwood-nip46/src/session.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,7 @@ impl SessionManager {
109109

110110
/// Return a list of all active (non-expired) client public keys.
111111
pub fn list(&self) -> Vec<&str> {
112-
self.sessions
113-
.iter()
114-
.filter(|(_, s)| !s.is_expired())
115-
.map(|(k, _)| k.as_str())
116-
.collect()
112+
self.sessions.iter().filter(|(_, s)| !s.is_expired()).map(|(k, _)| k.as_str()).collect()
117113
}
118114

119115
/// Return the number of active sessions.

crates/heartwood-nip46/tests/security_test.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ fn request_debug_no_params_variant() {
2626

2727
#[test]
2828
fn response_blocks_nsec_in_result() {
29-
let resp = Nip46Response::ok("1", serde_json::json!({
30-
"nsec": "nsec1abc123"
31-
}));
29+
let resp = Nip46Response::ok(
30+
"1",
31+
serde_json::json!({
32+
"nsec": "nsec1abc123"
33+
}),
34+
);
3235
assert!(resp.result().is_none());
3336
assert!(resp.error().is_some());
3437
assert!(resp.error().unwrap().contains("secret key material"));
@@ -42,9 +45,12 @@ fn response_blocks_nsec_in_nested_array() {
4245

4346
#[test]
4447
fn response_allows_clean_result() {
45-
let resp = Nip46Response::ok("1", serde_json::json!({
46-
"pubkey": "d6b3a6496c529d8e7f6e10cc7bb89f794ef931770c700f68a859cd24234a2645"
47-
}));
48+
let resp = Nip46Response::ok(
49+
"1",
50+
serde_json::json!({
51+
"pubkey": "d6b3a6496c529d8e7f6e10cc7bb89f794ef931770c700f68a859cd24234a2645"
52+
}),
53+
);
4854
assert!(resp.result().is_some());
4955
assert!(resp.error().is_none());
5056
}

0 commit comments

Comments
 (0)