Files
alan 1ad85de1f1 Keep step-up elevation for the signed-in session and stop gating terminals
The proxy forwards an HMAC-derived session id (X-Pipelines-Session) on
/connect and POST /files; the server trusts it only on token-authenticated
requests and keys elevation by it, so reconnects stay elevated up to a 24 h
cap. Terminals rely on their su -l login. The app no longer forgets the
elevation on disconnect and asks again only on a fresh refusal.
2026-11-10 03:00:45 +00:00

48 lines
1.7 KiB
Go

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
}