Skip to content

Commit ff3fae8

Browse files
MasterPtatoNathanFlurry
authored andcommitted
fix: allow token through ws protocols
1 parent c51be81 commit ff3fae8

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

packages/core/guard/server/src/routing/pegboard_gateway.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ use hyper::header::HeaderName;
66
use rivet_guard_core::proxy_service::{RouteConfig, RouteTarget, RoutingOutput, RoutingTimeout};
77
use universaldb::utils::IsolationLevel::*;
88

9+
use super::SEC_WEBSOCKET_PROTOCOL;
910
use crate::{errors, shared_state::SharedState};
1011

1112
const ACTOR_READY_TIMEOUT: Duration = Duration::from_secs(10);
1213
pub const X_RIVET_ACTOR: HeaderName = HeaderName::from_static("x-rivet-actor");
13-
const SEC_WEBSOCKET_PROTOCOL: HeaderName = HeaderName::from_static("sec-websocket-protocol");
1414
const WS_PROTOCOL_ACTOR: &str = "rivet_actor.";
1515

1616
/// Route requests to actor services based on hostname and path

packages/core/guard/server/src/routing/runner.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use gas::prelude::*;
33
use rivet_guard_core::proxy_service::RoutingOutput;
44
use std::sync::Arc;
55

6-
use super::X_RIVET_TOKEN;
6+
use super::{SEC_WEBSOCKET_PROTOCOL, X_RIVET_TOKEN};
7+
pub(crate) const WS_PROTOCOL_TOKEN: &str = "rivet_target.";
78

89
/// Route requests to the API service
910
#[tracing::instrument(skip_all)]
@@ -18,11 +19,33 @@ pub async fn route_request(
1819
return Ok(None);
1920
}
2021

22+
let is_websocket = headers
23+
.get("upgrade")
24+
.and_then(|v| v.to_str().ok())
25+
.map(|v| v.eq_ignore_ascii_case("websocket"))
26+
.unwrap_or(false);
27+
2128
// Check auth (if enabled)
2229
if let Some(auth) = &ctx.config().auth {
2330
let token = headers
2431
.get(X_RIVET_TOKEN)
2532
.and_then(|x| x.to_str().ok())
33+
// Fallback to checking websocket protocol if rivet token is not set
34+
.or_else(|| {
35+
if is_websocket {
36+
headers
37+
.get(SEC_WEBSOCKET_PROTOCOL)
38+
.and_then(|protocols| protocols.to_str().ok())
39+
.and_then(|protocols| {
40+
protocols
41+
.split(',')
42+
.map(|p| p.trim())
43+
.find_map(|p| p.strip_prefix(WS_PROTOCOL_TOKEN))
44+
})
45+
} else {
46+
None
47+
}
48+
})
2649
.ok_or_else(|| {
2750
crate::errors::MissingHeader {
2851
header: X_RIVET_TOKEN.to_string(),

sdks/typescript/runner/src/mod.ts

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)