Wrap the /user/{username} route in RequireAuth middleware #60
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Part of the road to 1.0 (see #33).
internal/server/routes.gosetupUserRoutes()appliesCSRF()to/user/{username}but notRequireAuth(). Authentication is enforced only insideHandleProfile(internal/handlers/profile.go), which is defense-by-implementation rather than defense-by-design. Every other authenticated route group (/sources,/source/{sourceID}) is wrapped ins.mw.RequireAuth(); this one should be too.Definition of done:
/user/{username}route group usess.mw.RequireAuth()/pages/loginat the middleware layer (not only by the handler)Implementation instructions
Scope is exactly the definition of done above — one small, isolated change. Do not expand scope.
Change 1 —
internal/server/routes.go,setupUserRoutes()The
/user/{username}route group currently has onlyr.Use(s.mw.CSRF()). Addr.Use(s.mw.RequireAuth())immediately after the CSRF line, so auth is enforced at the middleware layer — matching/sourcesand/source/{sourceID}, which apply CSRF then RequireAuth. Keep the relative order: CSRF first, then RequireAuth.Change 2 —
internal/handlers/profile.go,HandleProfileWith
RequireAuthin front of the route, the handler's ownsession.Get+IsAuthenticated+ redirect-to-login branch is now dead code (the middleware guarantees an authenticated session before the handler runs). Simplify it:requestedUsername != sessionUsername) and the template data/pages/login" branchTests
internal/handlersprofile tests: own profile returns 200, another user's profile returns 403/user/{username}is redirected to/pages/loginby the middleware and never reaches the handler (a routing- or middleware-level test is fine)Validation gates
make fmtbefore committinggo.modrequires 1.26, so validate with the authoritative CI gate:docker build .must pass (it runs fmt-check, lint, test, and build). Do not skip lint; no new lint findings.Process
main, namedissue-60-user-requireauth(closes #60)main, then post a comment on the PR describing exactly what changed and pasting thedocker build .result