Fix secret validation (hopefully)
parent
74661986f2
commit
bef104ead5
@ -1,28 +0,0 @@
|
|||||||
use crate::secret_validation::SecretValidator;
|
|
||||||
use hmac::{Hmac, Mac};
|
|
||||||
use hyper::HeaderMap;
|
|
||||||
use sha2::Sha256;
|
|
||||||
|
|
||||||
pub struct GithubSecretValidator;
|
|
||||||
|
|
||||||
static X_HUB_SIGNATURE_256_HEADER: &str = "X-Hub-Signature-256";
|
|
||||||
|
|
||||||
impl SecretValidator for GithubSecretValidator {
|
|
||||||
fn validate(&self, headers: &HeaderMap, body: &[u8], secret: &[u8]) -> bool {
|
|
||||||
log::debug!("Validating GitHub Secret");
|
|
||||||
if let Some(github_sum) = headers.get(X_HUB_SIGNATURE_256_HEADER) {
|
|
||||||
let mut mac = Hmac::<Sha256>::new_from_slice(secret).unwrap();
|
|
||||||
mac.update(body);
|
|
||||||
|
|
||||||
let decoded_secret = if let Ok(decoded) = hex::decode(github_sum) {
|
|
||||||
decoded
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
mac.verify_slice(&decoded_secret).is_ok()
|
|
||||||
} else {
|
|
||||||
log::debug!("Missing Signature Header");
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,43 @@
|
|||||||
|
use crate::secret_validation::SecretValidator;
|
||||||
|
use hmac::{Hmac, Mac};
|
||||||
|
use hyper::HeaderMap;
|
||||||
|
use sha2::Sha256;
|
||||||
|
|
||||||
|
pub struct HMacSecretValidator;
|
||||||
|
|
||||||
|
static SUM_HEADERS: &[&str] = &[
|
||||||
|
"X-Forgejo-Signature",
|
||||||
|
"X-Gitea-Signature",
|
||||||
|
"X-Gogs-Signature",
|
||||||
|
"X-Hub-Signature-256",
|
||||||
|
];
|
||||||
|
|
||||||
|
impl SecretValidator for HMacSecretValidator {
|
||||||
|
fn validate(&self, headers: &HeaderMap, body: &[u8], secret: &[u8]) -> bool {
|
||||||
|
log::debug!("Validating HMac Secret");
|
||||||
|
let header = headers
|
||||||
|
.iter()
|
||||||
|
.filter(|(name, _)| SUM_HEADERS.iter().find(|h| **name == **h).is_some())
|
||||||
|
.next();
|
||||||
|
|
||||||
|
if let Some((_, sum)) = header {
|
||||||
|
let mut mac = Hmac::<Sha256>::new_from_slice(secret).unwrap();
|
||||||
|
mac.update(body);
|
||||||
|
let Ok(sum) = sum.to_str() else {
|
||||||
|
log::error!("Received signature is not a valid string");
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
let Ok(decoded_secret) = hex::decode(sum.trim_start_matches("sha256=")) else {
|
||||||
|
log::error!("Received signature cannot be decoded from hex");
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
log::debug!("Verifying found signature");
|
||||||
|
|
||||||
|
mac.verify_slice(&decoded_secret).is_ok()
|
||||||
|
} else {
|
||||||
|
log::error!("Missing Signature Header");
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue