Skip to content

Commit e0b50dd

Browse files
author
npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757
committed
feat(relay): gate Git web GUI separately
Always expose the bundled invite landing page in the production image, while requiring BUZZ_SERVE_GIT_WEB_GUI=true to serve root and repository browser routes. Co-authored-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@sprout-oss.stage.blox.sqprod.co> Signed-off-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@sprout-oss.stage.blox.sqprod.co>
1 parent a2fd5f8 commit e0b50dd

4 files changed

Lines changed: 103 additions & 45 deletions

File tree

‎Dockerfile‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,9 @@ COPY --from=builder /build/target/release/buzz-admin /usr/local/bin/buzz-admi
138138
COPY --from=builder /build/target/release/buzz-pair-relay /usr/local/bin/buzz-pair-relay
139139
COPY --from=web-builder /build/web/dist /srv/buzz/web
140140

141-
# The web UI is bundled but disabled by default. Set
142-
# BUZZ_WEB_DIR=/srv/buzz/web to opt in.
141+
# The invite landing page is always served from the bundled web UI. Repository
142+
# browser routes require the separate BUZZ_SERVE_GIT_WEB_GUI=true opt-in.
143+
ENV BUZZ_WEB_DIR=/srv/buzz/web
143144

144145
# 3000: app (WS + REST) · 8080: /_liveness, /_readiness · 9102: /metrics
145146
EXPOSE 3000 8080 9102

‎TESTING.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ out of the box with `just setup` or `just relay`. Common overrides:
272272
| `BUZZ_AUTO_MIGRATE` | `false` | Opt in with `true`/`1`/`yes`/`on` to run embedded SQLx migrations on relay startup |
273273
| `RELAY_OWNER_PUBKEY` | unset | Bootstrapped as `owner` in `relay_members` at first start |
274274
| `BUZZ_ALLOW_NIP_OA_AUTH` | `false` | Enable NIP-OA owner attestation for membership |
275-
| `BUZZ_WEB_DIR` | unset | Set to the bundled `/srv/buzz/web` in the container to opt in to the browser web UI |
275+
| `BUZZ_WEB_DIR` | unset (source), `/srv/buzz/web` (container) | Directory containing the invite landing bundle; the production container enables it so `/invite/{code}` always works |
276+
| `BUZZ_SERVE_GIT_WEB_GUI` | `false` | Set to `true` or `1` to expose the bundled Git repository browser at `/` and `/repos/...`; invite routes do not depend on this flag |
276277

277278
CLI-side, only two matter for testing:
278279

‎crates/buzz-relay/src/config.rs‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,12 @@ pub struct Config {
198198
pub push_gateway_timeout: Duration,
199199

200200
/// Optional path to the web UI `dist/` directory.
201-
/// When set, the relay serves the SPA from this directory for browser requests.
201+
/// When set, the relay serves the invite landing page and its static assets.
202202
/// When unset, no static file serving happens (relay behaves as before).
203203
pub web_dir: Option<std::path::PathBuf>,
204+
/// Whether the configured web bundle serves Git browser routes in addition
205+
/// to the public invite landing page. Defaults to false.
206+
pub serve_git_web_gui: bool,
204207
}
205208

206209
fn parse_bind_addr(raw: &str) -> Result<SocketAddr, ConfigError> {
@@ -605,6 +608,9 @@ impl Config {
605608
.map(|s| s.trim().to_string())
606609
.filter(|s| !s.is_empty())
607610
.map(std::path::PathBuf::from);
611+
let serve_git_web_gui = std::env::var("BUZZ_SERVE_GIT_WEB_GUI")
612+
.map(|value| value == "true" || value == "1")
613+
.unwrap_or(false);
608614

609615
if let Some(ref dir) = web_dir {
610616
if !dir.join("index.html").is_file() {
@@ -668,6 +674,7 @@ impl Config {
668674
push_gateway_delivery_url,
669675
push_gateway_timeout,
670676
web_dir,
677+
serve_git_web_gui,
671678
})
672679
}
673680
}
@@ -712,6 +719,10 @@ mod tests {
712719
!config.allow_nip_oa_auth,
713720
"allow_nip_oa_auth should default to false"
714721
);
722+
assert!(
723+
!config.serve_git_web_gui,
724+
"serve_git_web_gui should default to false"
725+
);
715726
assert!(
716727
config.huddle_audio_available,
717728
"huddle_audio_available should default to true so single-pod (N=1) keeps today's huddle behavior"

‎crates/buzz-relay/src/router.rs‎

Lines changed: 86 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use axum::{
1212
Router,
1313
};
1414
use serde_json::json;
15+
use tower::ServiceExt;
1516
use tower_http::cors::{AllowOrigin, CorsLayer};
1617
use tower_http::limit::RequestBodyLimitLayer;
1718
use tower_http::services::ServeDir;
@@ -107,44 +108,31 @@ pub fn build_router(state: Arc<AppState>) -> Router {
107108
.merge(git_router)
108109
.merge(git_policy_router);
109110

110-
// When BUZZ_WEB_DIR is set, serve the SPA as a fallback for unmatched routes.
111+
// When BUZZ_WEB_DIR is set, serve either the full SPA or its invite-only
112+
// surface. Invite-only mode deliberately exposes only /invite/{code} and
113+
// hashed build assets; root and repository browser routes remain absent.
111114
if let Some(ref web_dir) = state.config.web_dir {
112115
let index_path = web_dir.join("index.html");
113-
let spa_fallback = ServeDir::new(web_dir).not_found_service(tower::service_fn(
114-
move |req: axum::extract::Request| {
115-
let index = index_path.clone();
116-
async move {
117-
let path = req.uri().path();
118-
// Reserved API prefixes must 404 normally, not serve index.html.
119-
let reserved = path.starts_with("/api/")
120-
|| path.starts_with("/media/")
121-
|| path.starts_with("/operator/")
122-
|| path.starts_with("/git/")
123-
|| path.starts_with("/internal/")
124-
|| path.starts_with("/.well-known/")
125-
|| path.starts_with("/huddle/")
126-
|| path == "/health"
127-
|| path == "/_liveness"
128-
|| path == "/_readiness"
129-
|| path == "/_status"
130-
|| path == "/info";
131-
// Files with extensions (e.g. /assets/missing.js) should 404.
132-
// Exception: /invite/<code> — invite codes contain a "."
133-
// (payload.mac separator) but are SPA routes, not files.
134-
let has_ext = !path.starts_with("/invite/")
135-
&& path.rsplit('/').next().is_some_and(|seg| seg.contains('.'));
136-
if reserved || has_ext {
137-
Ok(StatusCode::NOT_FOUND.into_response())
138-
} else {
139-
// SPA client-side route → serve index.html
140-
match tokio::fs::read(&index).await {
141-
Ok(body) => Ok(axum::response::Html(body).into_response()),
142-
Err(_) => Ok(StatusCode::INTERNAL_SERVER_ERROR.into_response()),
143-
}
144-
}
116+
let static_files = ServeDir::new(web_dir);
117+
let serve_git_web_gui = state.config.serve_git_web_gui;
118+
let spa_fallback = tower::service_fn(move |req: axum::extract::Request| {
119+
let index = index_path.clone();
120+
let static_files = static_files.clone();
121+
async move {
122+
let path = req.uri().path();
123+
if path.starts_with("/assets/") {
124+
return static_files
125+
.oneshot(req)
126+
.await
127+
.map(IntoResponse::into_response);
128+
}
129+
130+
if should_serve_spa(path, serve_git_web_gui) {
131+
return Ok(read_spa_index(&index).await);
145132
}
146-
},
147-
));
133+
Ok(StatusCode::NOT_FOUND.into_response())
134+
}
135+
});
148136
merged = merged.fallback_service(spa_fallback);
149137
}
150138

@@ -154,6 +142,26 @@ pub fn build_router(state: Arc<AppState>) -> Router {
154142
.layer(build_cors_layer(&state.config.cors_origins))
155143
}
156144

145+
fn is_invite_landing_path(path: &str) -> bool {
146+
path.strip_prefix("/invite/")
147+
.is_some_and(|code| !code.is_empty() && !code.contains('/'))
148+
}
149+
150+
fn should_serve_spa(path: &str, serve_git_web_gui: bool) -> bool {
151+
is_invite_landing_path(path) || (serve_git_web_gui && is_git_web_gui_path(path))
152+
}
153+
154+
fn is_git_web_gui_path(path: &str) -> bool {
155+
path == "/" || path == "/repos" || path.starts_with("/repos/")
156+
}
157+
158+
async fn read_spa_index(index: &std::path::Path) -> axum::response::Response {
159+
match tokio::fs::read(index).await {
160+
Ok(body) => axum::response::Html(body).into_response(),
161+
Err(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
162+
}
163+
}
164+
157165
/// Build the health-only router for K8s probes (port 8080 in CAKE).
158166
///
159167
/// No metrics middleware, no auth, no CORS, no body limit.
@@ -218,12 +226,14 @@ async fn nip11_or_ws_handler(
218226
.on_upgrade(move |socket| handle_connection(socket, state, addr, tenant))
219227
.into_response(),
220228
Err(_) => {
221-
// Browser requesting HTML and web UI is configured → serve SPA.
222-
if let Some(ref dir) = state.config.web_dir {
223-
if accept.contains("text/html") {
224-
let index = dir.join("index.html");
225-
if let Ok(body) = tokio::fs::read(&index).await {
226-
return axum::response::Html(body).into_response();
229+
// Browser requesting HTML and Git web GUI is enabled → serve SPA.
230+
if state.config.serve_git_web_gui {
231+
if let Some(ref dir) = state.config.web_dir {
232+
if accept.contains("text/html") {
233+
let index = dir.join("index.html");
234+
if let Ok(body) = tokio::fs::read(&index).await {
235+
return axum::response::Html(body).into_response();
236+
}
227237
}
228238
}
229239
}
@@ -322,3 +332,38 @@ fn build_cors_layer(cors_origins: &[String]) -> CorsLayer {
322332
.allow_methods(tower_http::cors::Any)
323333
.allow_headers(tower_http::cors::Any)
324334
}
335+
336+
#[cfg(test)]
337+
mod tests {
338+
use super::{is_git_web_gui_path, is_invite_landing_path, should_serve_spa};
339+
340+
#[test]
341+
fn invite_landing_path_requires_exactly_one_nonempty_code_segment() {
342+
assert!(is_invite_landing_path("/invite/payload.mac"));
343+
assert!(!is_invite_landing_path("/invite/"));
344+
assert!(!is_invite_landing_path("/invite/code/extra"));
345+
assert!(!is_invite_landing_path("/repos"));
346+
assert!(!is_invite_landing_path("/"));
347+
}
348+
349+
#[test]
350+
fn git_web_gui_paths_are_explicit() {
351+
assert!(is_git_web_gui_path("/"));
352+
assert!(is_git_web_gui_path("/repos"));
353+
assert!(is_git_web_gui_path("/repos/example"));
354+
assert!(!is_git_web_gui_path("/repository"));
355+
assert!(!is_git_web_gui_path("/arbitrary"));
356+
assert!(!is_git_web_gui_path("/api/invites"));
357+
}
358+
359+
#[test]
360+
fn invite_is_always_served_but_git_gui_requires_opt_in() {
361+
assert!(should_serve_spa("/invite/payload.mac", false));
362+
assert!(should_serve_spa("/invite/payload.mac", true));
363+
assert!(!should_serve_spa("/", false));
364+
assert!(!should_serve_spa("/repos/example", false));
365+
assert!(should_serve_spa("/", true));
366+
assert!(should_serve_spa("/repos/example", true));
367+
assert!(!should_serve_spa("/arbitrary", true));
368+
}
369+
}

0 commit comments

Comments
 (0)