package server import ( "encoding/json" "net/http" "strconv" "time" "git.a-wan.net/alan/pipelines/internal/protocol" ) // requireElevationHTTP guards an HTTP route that changes the operator's files. // The upload travels outside the websocket. It is allowed when the web session // the proxy reports is elevated, or when it carries a live elevation's token // (protocol.ElevationHeader; a connection-only elevation's token ends with its // connection). func (g *elevationGate) requireElevationHTTP(next http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { session := trustedSession(r) if (session != "" && g.elevated(sessionElevationKey(session))) || g.tokenValid(r.Header.Get(protocol.ElevationHeader)) || !g.enabled() { next(w, r) return } w.Header().Set("Content-Type", "application/json") w.Header().Set("Cache-Control", "no-store") w.WriteHeader(http.StatusForbidden) _ = json.NewEncoder(w).Encode(map[string]string{ "code": protocol.ErrorElevationRequired, "error": "Confirm your password to upload files to this machine.", }) } } // maxElevationMinutes is the longest PIPELINES_ELEVATION_MINUTES accepted: // 30 days, the longest a remembered web session lasts. const maxElevationMinutes = 30 * 24 * 60 // ElevationTTLFromEnvironment reads PIPELINES_ELEVATION_MINUTES (1 to 43200); // anything else keeps DefaultElevationTTL. func ElevationTTLFromEnvironment(getenv func(string) string) time.Duration { minutes, err := strconv.Atoi(getenv("PIPELINES_ELEVATION_MINUTES")) if err != nil || minutes < 1 || minutes > maxElevationMinutes { return DefaultElevationTTL } return time.Duration(minutes) * time.Minute }