@@ -12,6 +12,7 @@ use axum::{
1212 Router ,
1313} ;
1414use serde_json:: json;
15+ use tower:: ServiceExt ;
1516use tower_http:: cors:: { AllowOrigin , CorsLayer } ;
1617use tower_http:: limit:: RequestBodyLimitLayer ;
1718use 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