fix(web-auth): url-decode token query param for websocket auth

This commit is contained in:
xintaofei
2026-04-18 20:18:58 +08:00
parent ff9fbad50a
commit cf9573c0ce

View File

@@ -10,10 +10,21 @@ pub async fn require_token(
next: Next,
token: String,
) -> Response {
// Allow WebSocket upgrade requests to authenticate via query param
// Allow WebSocket upgrade requests to authenticate via query param.
// The token value is URL-encoded by the client, so decode before comparing.
if let Some(query) = request.uri().query() {
if query.contains(&format!("token={}", token)) {
return next.run(request).await;
for pair in query.split('&') {
let Some((key, value)) = pair.split_once('=') else {
continue;
};
if key != "token" {
continue;
}
if let Ok(decoded) = urlencoding::decode(value) {
if decoded == token {
return next.run(request).await;
}
}
}
}