Files
sam db346b159d fix(webui): mark cookies Secure behind a TLS proxy; run gosec in the gate (#1919)
Cookies took Secure from r.TLS, which is nil behind a proxy that terminates
TLS, so sign-in cookies went out without it. Every dashboard cookie now goes
through one builder that reads the scheme the way the origin check does.

The worktree reset and the agent worker's chown walk use os.Root, so a
symlink swapped in mid-walk can't point them outside the worktree.

gosec runs in golangci-lint, tests included. G104, G115, G204 and G304 are
excluded in config; each remaining finding is fixed or suppressed at the site
with its reason.
2026-11-13 03:16:15 +11:00

117 lines
3.6 KiB
Go

package webui
import (
"encoding/json"
"net/http"
"github.com/samcharles93/archie-core/internal/domain/identity"
)
func (s *Server) registerPasskeyRoutes(public, protected *http.ServeMux) {
public.HandleFunc("POST /api/auth/passkey/begin", func(w http.ResponseWriter, r *http.Request) { s.handlePasskeyBegin(w, r, false) })
public.HandleFunc("POST /api/auth/passkey/finish", func(w http.ResponseWriter, r *http.Request) { s.handlePasskeyFinish(w, r, false) })
protected.HandleFunc("POST /api/account/passkeys/begin", func(w http.ResponseWriter, r *http.Request) { s.handlePasskeyBegin(w, r, true) })
protected.HandleFunc("POST /api/account/passkeys/finish", func(w http.ResponseWriter, r *http.Request) { s.handlePasskeyFinish(w, r, true) })
protected.HandleFunc("GET /api/account/passkeys", s.handlePasskeyList)
protected.HandleFunc("DELETE /api/account/passkeys/{id}", s.handlePasskeyDelete)
}
func (s *Server) passkeyCookie(registration bool) string {
if registration {
return s.cookies().passkeyRegistration
}
return s.cookies().passkeyLogin
}
func (s *Server) handlePasskeyBegin(w http.ResponseWriter, r *http.Request, registration bool) {
if !s.authorizeAuthenticationMutation(w, r) {
return
}
if registration && !s.requireFreshAccount(w, r) {
return
}
store, ok := s.Accounts.(identity.Passkeys)
if !ok {
http.Error(w, "passkeys unavailable", http.StatusServiceUnavailable)
return
}
value, err := store.BeginPasskey(r.Context(), registration)
if err != nil {
s.writeAccountError(w, err)
return
}
s.setCookie(w, r, cookie{name: s.passkeyCookie(registration), value: value.Token, path: "/api/", maxAge: 300})
w.Header().Set("Cache-Control", "no-store")
writeJSON(w, json.RawMessage(value.Options))
}
func (s *Server) handlePasskeyFinish(w http.ResponseWriter, r *http.Request, registration bool) {
if !s.authorizeAuthenticationMutation(w, r) {
return
}
if registration && !s.requireFreshAccount(w, r) {
return
}
store, ok := s.Accounts.(identity.Passkeys)
if !ok {
http.Error(w, "passkeys unavailable", http.StatusServiceUnavailable)
return
}
cookie, err := r.Cookie(s.passkeyCookie(registration))
if err != nil {
http.Error(w, "passkey request expired", http.StatusUnauthorized)
return
}
s.clearCookie(w, r, s.passkeyCookie(registration), "/api/")
var request struct {
Credential json.RawMessage `json:"credential"`
Name string `json:"name"`
}
if !decodeAccountRequest(w, r, &request) {
return
}
value, err := store.FinishPasskey(r.Context(), cookie.Value, request.Credential, registration, request.Name)
if err != nil {
s.writeAccountError(w, err)
return
}
if !registration {
s.clearCookie(w, r, s.cookies().providerToken, "/")
s.setAccountCookie(w, r, value)
}
writeJSON(w, map[string]any{"user": value.Identity})
}
func (s *Server) handlePasskeyList(w http.ResponseWriter, r *http.Request) {
store, ok := s.Accounts.(identity.Passkeys)
if !ok {
http.Error(w, "passkeys unavailable", http.StatusServiceUnavailable)
return
}
values, err := store.ListPasskeys(r.Context())
if err != nil {
s.writeAccountError(w, err)
return
}
writeJSON(w, map[string]any{"passkeys": values})
}
func (s *Server) handlePasskeyDelete(w http.ResponseWriter, r *http.Request) {
if !s.requireFreshAccount(w, r) {
return
}
if !s.authorizeAuthenticationMutation(w, r) {
return
}
store, ok := s.Accounts.(identity.Passkeys)
if !ok {
http.Error(w, "passkeys unavailable", http.StatusServiceUnavailable)
return
}
if err := store.DeletePasskey(r.Context(), r.PathValue("id")); err != nil {
s.writeAccountError(w, err)
return
}
w.WriteHeader(http.StatusNoContent)
}