Skip to content

fix: correctly decode jwt keys #2573

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

Merged
merged 1 commit into from
Jun 11, 2025
Merged
Changes from all commits
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
18 changes: 13 additions & 5 deletions crates/rpc-types-engine/src/jwt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl JwtSecret {
///
/// This strips the leading `0x`, if any.
pub fn from_hex<S: AsRef<str>>(hex: S) -> Result<Self, JwtError> {
let hex = hex.as_ref();
let hex = hex.as_ref().trim();
match hex::decode_to_array(hex) {
Ok(b) => Ok(Self(b)),
Err(hex::FromHexError::InvalidStringLength | hex::FromHexError::OddLength) => {
Expand Down Expand Up @@ -291,11 +291,19 @@ mod tests {
#[test]
fn from_hex() {
let key = "f79ae8046bc11c9927afe911db7143c51a806c4a537cc08e0d37140b0192f430";
let secret: Result<JwtSecret, _> = JwtSecret::from_hex(key);
assert!(secret.is_ok());
let secret_0: Result<JwtSecret, _> = JwtSecret::from_hex(key);
assert!(secret_0.is_ok());

let secret: Result<JwtSecret, _> = JwtSecret::from_hex(key);
assert!(secret.is_ok());
let key = "0xf79ae8046bc11c9927afe911db7143c51a806c4a537cc08e0d37140b0192f430";
let secret_1: Result<JwtSecret, _> = JwtSecret::from_hex(key);
assert!(secret_1.is_ok());

let key = "0xf79ae8046bc11c9927afe911db7143c51a806c4a537cc08e0d37140b0192f430 ";
let secret_2: Result<JwtSecret, _> = JwtSecret::from_hex(key);
assert!(secret_2.is_ok());

assert_eq!(secret_0.as_ref().unwrap().clone(), secret_1.unwrap());
assert_eq!(secret_0.unwrap(), secret_2.unwrap());
Comment on lines +305 to +306
Copy link
Contributor

Choose a reason for hiding this comment

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

There's a potential issue with the assertion sequence. The first assertion uses secret_0.as_ref().unwrap().clone(), but then the second assertion uses secret_0.unwrap(), which would consume the Result value. This creates a "use after move" situation.

Consider either:

  1. Unwrapping all values first:
let unwrapped_0 = secret_0.unwrap();
let unwrapped_1 = secret_1.unwrap();
let unwrapped_2 = secret_2.unwrap();
assert_eq!(unwrapped_0, unwrapped_1);
assert_eq!(unwrapped_0, unwrapped_2);
  1. Or cloning before unwrapping in the first assertion:
assert_eq!(secret_0.clone().unwrap(), secret_1.unwrap());
assert_eq!(secret_0.unwrap(), secret_2.unwrap());

Either approach would prevent the move issue while maintaining the test's intent.

Suggested change
assert_eq!(secret_0.as_ref().unwrap().clone(), secret_1.unwrap());
assert_eq!(secret_0.unwrap(), secret_2.unwrap());
let unwrapped_0 = secret_0.unwrap();
let unwrapped_1 = secret_1.unwrap();
let unwrapped_2 = secret_2.unwrap();
assert_eq!(unwrapped_0, unwrapped_1);
assert_eq!(unwrapped_0, unwrapped_2);

Spotted by Diamond

Is this helpful? React 👍 or 👎 to let us know.

}

#[test]
Expand Down