Skip to content

Commit

Permalink
0.0.14
Browse files Browse the repository at this point in the history
  • Loading branch information
junkurihara committed Mar 14, 2024
1 parent 9271d82 commit ef08298
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ resolver = "2"

[workspace.package]
edition = "2021"
version = "0.0.13"
version = "0.0.14"
authors = ["Jun Kurihara"]
homepage = "https://github.com/junkurihara/httpsig-rs"
repository = "https://github.com/junkurihara/httpsig-rs"
Expand Down
2 changes: 1 addition & 1 deletion httpsig-hyper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ rust-version.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
httpsig = { path = "../httpsig", version = "0.0.13" }
httpsig = { path = "../httpsig", version = "0.0.14" }

thiserror = { version = "1.0.58" }
tracing = { version = "0.1.40" }
Expand Down
17 changes: 17 additions & 0 deletions httpsig-hyper/src/hyper_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,23 @@ MCowBQYDK2VwAyEA1ixMQcxO46PLlgQfYS46ivFd+n0CcDHSKUnuhm3i1O0=
assert!(verification_res.is_ok());
}

#[tokio::test]
async fn test_expired_signature() {
let mut req = build_request().await;
let secret_key = SecretKey::from_pem(EDDSA_SECRET_KEY).unwrap();
let mut signature_params = HttpSignatureParams::try_new(&build_covered_components()).unwrap();
signature_params.set_key_info(&secret_key);
let created = signature_params.created.unwrap();
signature_params.set_expires(created - 1);
assert!(signature_params.is_expired());

req.set_message_signature(&signature_params, &secret_key, None).await.unwrap();

let public_key = PublicKey::from_pem(EDDSA_PUBLIC_KEY).unwrap();
let verification_res = req.verify_message_signature(&public_key, None).await;
assert!(verification_res.is_err());
}

#[tokio::test]
async fn test_set_verify_with_signature_name() {
let mut req = build_request().await;
Expand Down
4 changes: 4 additions & 0 deletions httpsig/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ pub enum HttpSigError {
#[error("Failed to build signature base: {0}")]
BuildSignatureBaseError(String),

/// Expired signature params
#[error("Expired signature params: {0}")]
ExpiredSignatureParams(String),

/* ----- Other errors ----- */
/// NotYetImplemented
#[error("Not yet implemented: {0}")]
Expand Down
5 changes: 5 additions & 0 deletions httpsig/src/signature_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ impl HttpSignatureBase {
verifying_key: &impl VerifyingKey,
signature_headers: &HttpSignatureHeaders,
) -> HttpSigResult<()> {
if signature_headers.signature_params().is_expired() {
return Err(HttpSigError::ExpiredSignatureParams(
"Signature params is expired".to_string(),
));
}
let signature_bytes = signature_headers.signature.0.as_slice();
verifying_key.verify(&self.as_bytes(), signature_bytes)
}
Expand Down
15 changes: 15 additions & 0 deletions httpsig/src/signature_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ impl HttpSignatureParams {
self.expires = Some(self.created.unwrap() + duration_secs);
self
}

/// Check if the signature params is expired if `exp` field is present.
/// If `exp` field is not present, it always returns false.
pub fn is_expired(&self) -> bool {
if let Some(exp) = self.expires {
exp < SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs()
} else {
false
}
}
}

impl std::fmt::Display for HttpSignatureParams {
Expand Down Expand Up @@ -263,6 +273,11 @@ MCowBQYDK2VwAyEA1ixMQcxO46PLlgQfYS46ivFd+n0CcDHSKUnuhm3i1O0=
params.set_expires_with_duration(Some(100));
assert!(params.expires.is_some());
assert_eq!(params.expires.unwrap(), params.created.unwrap() + 100);
assert!(!params.is_expired());

let created = params.created.unwrap();
params.set_expires(created - 1);
assert!(params.is_expired());
}

#[test]
Expand Down

0 comments on commit ef08298

Please sign in to comment.