Skip to content

Commit 1fd0e93

Browse files
committed
fix(cluster): make join-token tamper test robust against zero MAC bytes
The tamper tests in both nodedb-cluster and nodedb-ctl replaced the last hex byte of the MAC with the literal "00". When the real MAC byte happened to already be 0x00 — which occurs ~1/256 of the time — the replacement was a no-op and the test incorrectly passed a token with an unmodified MAC to verify_token, causing the assertion to fail. Replace the fixed "00" substitution with XOR 0xFF, which guarantees the byte always changes regardless of its original value.
1 parent 1128235 commit 1fd0e93

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

nodedb-cluster/src/auth/join_token.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,13 @@ mod tests {
175175
.as_secs()
176176
+ 60;
177177
let hex = issue_token(&secret, 1, expiry).unwrap();
178-
// Flip last two hex chars (end of MAC).
178+
// Flip the last MAC byte. XOR with 0xFF so it always changes even
179+
// if it was already 0x00 (a fixed "00" replacement would be a
180+
// no-op ~1/256 of the time, since the MAC varies with the expiry).
179181
let mut tampered = hex.clone();
180182
let len = tampered.len();
181-
tampered.replace_range(len - 2..len, "00");
183+
let orig = u8::from_str_radix(&tampered[len - 2..len], 16).unwrap();
184+
tampered.replace_range(len - 2..len, &format!("{:02x}", orig ^ 0xFF));
182185
assert_eq!(
183186
verify_token(&tampered, &secret).unwrap_err(),
184187
TokenError::InvalidMac

nodedb/src/ctl/join_token.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,14 @@ mod tests {
8282
assert_eq!(got_node, for_node);
8383
assert_eq!(got_exp, expiry);
8484

85-
// Flip a byte in the MAC portion.
85+
// Flip a byte in the MAC portion. XOR with 0xFF so the byte is
86+
// guaranteed to change even if it was already 0x00 (a fixed "00"
87+
// replacement would be a no-op ~1/256 of the time, since the MAC
88+
// varies with the wall-clock-derived expiry).
8689
let mut tampered = hex.clone();
8790
let flip = tampered.len() - 4;
88-
tampered.replace_range(flip..flip + 2, "00");
91+
let orig = u8::from_str_radix(&tampered[flip..flip + 2], 16).expect("valid hex byte");
92+
tampered.replace_range(flip..flip + 2, &format!("{:02x}", orig ^ 0xFF));
8993
assert!(tok::verify_token(&tampered, &secret).is_err());
9094

9195
// Wrong secret.

0 commit comments

Comments
 (0)