Skip to content
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

Implement blake3 cryptographic hash verification for query string #116

Merged
merged 4 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
26 changes: 26 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ rgb = { version = "0.8.37", optional = true }

once_cell = "1.18.0"
regex = "1.10.2"
blake3 = { version = "1.5.0", optional = true }

[features]
default = ["webp", "mimalloc", "reqwest-rustls"]
default = ["webp", "mimalloc", "reqwest-rustls", "qhash"]

reqwest-rustls = ["reqwest/rustls-tls"]
reqwest-native-tls = ["reqwest/default-tls"]
Expand All @@ -37,5 +38,7 @@ mimalloc = ["dep:mimalloc"]

optimized = ["libwebp-sys?/sse41", "libwebp-sys?/avx2", "libwebp-sys?/neon"]

qhash = ["blake3"]

[profile.release]
lto = true
54 changes: 54 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,60 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
// parse query string
let query = QString::from(req.query_string());

#[cfg(feature = "qhash")]
{
use std::collections::BTreeSet;

let secret = env::var("HASH_SECRET");
if let Ok(secret) = secret {
let qhash = query.get("qhash");

if qhash.is_none() {
return Err("No qhash provided".into());
}

let qhash = qhash.unwrap();

if qhash.len() != 8 {
return Err("Invalid qhash provided".into());
}

// Store sorted key-value pairs
let mut set = BTreeSet::new();
{
let pairs = query.to_pairs();
for (key, value) in &pairs {
if matches!(*key, "qhash" | "range" | "rewrite") {
continue;
}
set.insert((key.as_bytes().to_owned(), value.as_bytes().to_owned()));
}
}

let (tx, rx) = oneshot::channel::<String>();
FireMasterK marked this conversation as resolved.
Show resolved Hide resolved
spawn_blocking(move || {
let mut hasher = blake3::Hasher::new();

for (key, value) in set {
hasher.update(&key);
hasher.update(&value);
}

hasher.update(secret.as_bytes());

let hash = hasher.finalize().to_hex();
let hash = hash[..8].to_owned();
FireMasterK marked this conversation as resolved.
Show resolved Hide resolved
tx.send(hash).unwrap();
});

let hash = rx.await.unwrap();

if hash != qhash {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason this isn't a constant-time comparison? In an async context it's harder to predict what the CPU is doing, but it's still good practice from a security perspective.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is a very small concern in this case, the query hash verification is mainly to prevent bad actors from crafting specialized URLs. (Usually bots) This is also why we only use the first 8 characters, too.

return Err("Invalid qhash provided".into());
}
}
}

let res = query.get("host");
let res = res.map(|s| s.to_string());

Expand Down
Loading