Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4b6ecd57d0 | |||
| b1421f7dd5 | |||
| 00e38c9e17 | |||
| 9e650e1f75 |
@@ -85,17 +85,31 @@ impl crate::proxy::Proxy {
|
||||
description: "Could not start stream".to_string(),
|
||||
});
|
||||
|
||||
// Validate single-use stream token via the shared helper so this
|
||||
// handler and its unit tests exercise the same code path.
|
||||
// Validate single-use stream token
|
||||
let provided_token = req.query::<String>("token").unwrap_or_default();
|
||||
if let Err(msg) = super::validate_stream_token(&self, &provided_token).await {
|
||||
error!("Stream token validation failed: {msg}");
|
||||
return Err(AppError {
|
||||
status_code: StatusCode::UNAUTHORIZED,
|
||||
description: msg,
|
||||
});
|
||||
{
|
||||
let mut token_guard = self.stream_token.write().await;
|
||||
match token_guard.take() {
|
||||
Some(expected) if expected == provided_token => {
|
||||
// Token consumed successfully (single-use)
|
||||
info!("Stream token validated and consumed");
|
||||
}
|
||||
Some(_) => {
|
||||
error!("Invalid stream token provided");
|
||||
return Err(AppError {
|
||||
status_code: StatusCode::UNAUTHORIZED,
|
||||
description: "Invalid stream token".to_string(),
|
||||
});
|
||||
}
|
||||
None => {
|
||||
error!("Stream token already consumed");
|
||||
return Err(AppError {
|
||||
status_code: StatusCode::UNAUTHORIZED,
|
||||
description: "Stream token already used".to_string(),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
info!("Stream token validated and consumed");
|
||||
|
||||
info!("WebTransport connection initiated");
|
||||
let (wt_stream_send, wt_stream_recv, wt_datagram_send) = match setup_webtransport(req).await
|
||||
|
||||
@@ -85,9 +85,8 @@ pub async fn validate_stream_token(proxy: &Proxy, provided: &str) -> std::result
|
||||
match token_guard.take() {
|
||||
Some(expected) if expected == provided => Ok(()),
|
||||
Some(_) => {
|
||||
// Wrong token: still consumed by the `take()` above. Any validation
|
||||
// attempt — correct or not — invalidates the token, so a wrong
|
||||
// guess cannot be followed by a correct one.
|
||||
// Put the token back since it wasn't matched
|
||||
// Actually no — the design is that any attempt consumes it for security
|
||||
Err("Invalid stream token".to_string())
|
||||
}
|
||||
None => Err("Stream token already used".to_string()),
|
||||
|
||||
Reference in New Issue
Block a user