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.
This commit is contained in:
2026-11-10 03:00:45 +00:00
parent 60b4c495f1
commit 1ad85de1f1
19 changed files with 876 additions and 165 deletions
+34
View File
@@ -1,10 +1,12 @@
package server
import (
"context"
"net"
"net/http"
"strings"
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/servertoken"
)
@@ -23,6 +25,11 @@ import (
// request must carry it in servertoken.Header, except the chat-tools MCP
// endpoint (its own per-chat token, loopback and no-Origin checks) and
// /healthz (a bare liveness boolean).
// - Session: protocol.SessionHeader names the web session a proxied request
// belongs to (an opaque id the proxy derives from the verified cookie). It
// is trusted only on a request that carried the configured token; it is
// always removed from the request and, when trusted, reaches handlers
// through the context (trustedSession).
type requestBoundary struct {
next http.Handler
token string
@@ -47,9 +54,36 @@ func (b requestBoundary) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
// The secret never reaches a handler, a log line or a forwarded request.
r.Header.Del(servertoken.Header)
session := r.Header.Get(protocol.SessionHeader)
r.Header.Del(protocol.SessionHeader)
if b.token != "" && validSessionID(session) {
r = r.WithContext(context.WithValue(r.Context(), sessionContextKey{}, session))
}
b.next.ServeHTTP(w, r)
}
type sessionContextKey struct{}
// trustedSession is the web session id the token-authenticated proxy reported
// for r, or "" (no server token configured, a direct client, or no header).
func trustedSession(r *http.Request) string {
session, _ := r.Context().Value(sessionContextKey{}).(string)
return session
}
// validSessionID bounds the proxy's opaque id: 16 to 128 URL-safe characters.
func validSessionID(value string) bool {
if len(value) < 16 || len(value) > 128 {
return false
}
for _, c := range value {
if !(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '-' || c == '_') {
return false
}
}
return true
}
func tokenExempt(path string) bool {
return path == "/healthz" || strings.HasPrefix(path, chatToolPath)
}