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:
@@ -8,6 +8,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -16,19 +17,29 @@ import (
|
||||
|
||||
// Step-up re-authentication (see internal/protocol/elevation.go). A web
|
||||
// session is enough to read, but the requests below act as the operator's
|
||||
// shell, so each connection must also confirm the sign-in password recently.
|
||||
// shell, so the operator must also confirm the sign-in password once.
|
||||
//
|
||||
// A confirmation is keyed by the web session the token-authenticated proxy
|
||||
// reports (trustedSession): every connection and reconnect of that session
|
||||
// stays elevated until the cap (DefaultElevationTTL) passes. The session ending
|
||||
// ends it too: the proxy closes the session's sockets and refuses its cookie,
|
||||
// and its derived id changes with every new sign-in and every password change
|
||||
// or revoke-all, so no later request can name the old elevation. A connection
|
||||
// without a trusted session id (no server token configured, a direct client)
|
||||
// is elevated by itself only, until it closes.
|
||||
//
|
||||
// The gate is on whenever the web sign-in password file exists; a server run
|
||||
// without the web proxy (tests, a bare loopback server) has nothing to verify
|
||||
// against and keeps its previous behaviour. A file that exists but cannot be
|
||||
// read safely keeps the gate on and every elevate fails (fail closed).
|
||||
|
||||
// DefaultElevationTTL is how long one confirmation lasts on a connection.
|
||||
const DefaultElevationTTL = 15 * time.Minute
|
||||
// DefaultElevationTTL caps how long one confirmation lasts.
|
||||
const DefaultElevationTTL = 24 * time.Hour
|
||||
|
||||
// Elevation attempt limits: a connection waits elevationBackoffBase doubled
|
||||
// for each consecutive wrong password (at most elevationBackoffMax), and the
|
||||
// whole server refuses attempts after elevationGlobalFailures wrong passwords
|
||||
// within elevationGlobalWindow.
|
||||
// Elevation attempt limits: a key waits elevationBackoffBase doubled for each
|
||||
// consecutive wrong password (at most elevationBackoffMax), and the whole
|
||||
// server refuses attempts after elevationGlobalFailures wrong passwords within
|
||||
// elevationGlobalWindow. At most elevationTokenLimit elevations live at once.
|
||||
const (
|
||||
elevationBackoffBase = time.Second
|
||||
elevationBackoffMax = time.Minute
|
||||
@@ -37,14 +48,34 @@ const (
|
||||
elevationTokenLimit = 256
|
||||
)
|
||||
|
||||
// Terminals are deliberately absent: terminal_open starts `su -l`, so the OS
|
||||
// account password is the barrier there.
|
||||
var elevationGated = map[string]bool{
|
||||
protocol.ChatShellRun: true,
|
||||
protocol.ChatShellInput: true,
|
||||
protocol.RenamePath: true,
|
||||
protocol.MovePath: true,
|
||||
protocol.DeletePath: true,
|
||||
protocol.TerminalOpen: true,
|
||||
protocol.TerminalInput: true,
|
||||
}
|
||||
|
||||
const (
|
||||
sessionKeyPrefix = "session:"
|
||||
connectionKeyPrefix = "conn:"
|
||||
)
|
||||
|
||||
// sessionElevationKey keys elevation by a trusted web session id.
|
||||
func sessionElevationKey(session string) string { return sessionKeyPrefix + session }
|
||||
|
||||
// connectionElevationKey keys elevation by one websocket connection.
|
||||
func connectionElevationKey(client string) string { return connectionKeyPrefix + client }
|
||||
|
||||
// elevationKeyFor is the key a connection elevates under: its web session
|
||||
// when the proxy reported one, else the connection itself.
|
||||
func elevationKeyFor(session, client string) string {
|
||||
if session != "" {
|
||||
return sessionElevationKey(session)
|
||||
}
|
||||
return connectionElevationKey(client)
|
||||
}
|
||||
|
||||
type elevationGate struct {
|
||||
@@ -55,7 +86,7 @@ type elevationGate struct {
|
||||
verify sync.Mutex
|
||||
|
||||
mu sync.Mutex
|
||||
until map[string]time.Time // client -> elevation end
|
||||
until map[string]time.Time // key -> elevation end
|
||||
tokens map[string]elevationToken
|
||||
failures map[string]elevationFailures
|
||||
recent []time.Time // wrong passwords, server-wide, oldest first
|
||||
@@ -63,8 +94,8 @@ type elevationGate struct {
|
||||
}
|
||||
|
||||
type elevationToken struct {
|
||||
client string
|
||||
until time.Time
|
||||
key string
|
||||
until time.Time
|
||||
}
|
||||
|
||||
type elevationFailures struct {
|
||||
@@ -106,43 +137,43 @@ func (g *elevationGate) enabled() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (g *elevationGate) elevated(client string) bool {
|
||||
func (g *elevationGate) elevated(key string) bool {
|
||||
g.mu.Lock()
|
||||
defer g.mu.Unlock()
|
||||
until, ok := g.until[client]
|
||||
until, ok := g.until[key]
|
||||
if ok && !g.now().Before(until) {
|
||||
g.dropLocked(client)
|
||||
g.dropLocked(key)
|
||||
return false
|
||||
}
|
||||
return ok
|
||||
}
|
||||
|
||||
// allows reports whether client may perform a gated request now.
|
||||
func (g *elevationGate) allows(client string) bool {
|
||||
return g.elevated(client) || !g.enabled()
|
||||
// allows reports whether key may perform a gated request now.
|
||||
func (g *elevationGate) allows(key string) bool {
|
||||
return g.elevated(key) || !g.enabled()
|
||||
}
|
||||
|
||||
// refuse answers a gated request on a connection that is not elevated with a
|
||||
// typed error and reports true; any other request passes (false).
|
||||
func (g *elevationGate) refuse(request protocol.Message, client string, write func(protocol.Message) error) bool {
|
||||
if !elevationGated[request.Type] || g.allows(client) {
|
||||
// refuse answers a gated request whose key is not elevated with a typed error
|
||||
// and reports true; any other request passes (false).
|
||||
func (g *elevationGate) refuse(request protocol.Message, key string, write func(protocol.Message) error) bool {
|
||||
if !elevationGated[request.Type] || g.allows(key) {
|
||||
return false
|
||||
}
|
||||
_ = write(protocol.Message{
|
||||
Type: protocol.Error,
|
||||
ID: request.ID,
|
||||
Code: protocol.ErrorElevationRequired,
|
||||
Error: "Confirm your password to run commands, change files or use a terminal on this machine.",
|
||||
Error: "Confirm your password to run commands or change files on this machine.",
|
||||
})
|
||||
return true
|
||||
}
|
||||
|
||||
// handle answers an elevate request and reports true; other requests pass.
|
||||
func (g *elevationGate) handle(request protocol.Message, client string, write func(protocol.Message) error) bool {
|
||||
func (g *elevationGate) handle(request protocol.Message, key string, write func(protocol.Message) error) bool {
|
||||
if request.Type != protocol.Elevate {
|
||||
return false
|
||||
}
|
||||
reply, err := g.elevate(client, request.Password)
|
||||
reply, err := g.elevate(key, request.Password)
|
||||
if err != nil {
|
||||
code := protocol.ErrorElevationFailed
|
||||
if errors.Is(err, errWebAuthMissing) {
|
||||
@@ -155,11 +186,11 @@ func (g *elevationGate) handle(request protocol.Message, client string, write fu
|
||||
return true
|
||||
}
|
||||
|
||||
func (g *elevationGate) elevate(client, password string) (*protocol.Elevation, error) {
|
||||
func (g *elevationGate) elevate(key, password string) (*protocol.Elevation, error) {
|
||||
if g == nil || g.authFile == "" {
|
||||
return nil, fmt.Errorf("this server has no web sign-in password to confirm: %w", errWebAuthMissing)
|
||||
}
|
||||
if wait := g.blocked(client); wait > 0 {
|
||||
if wait := g.blocked(key); wait > 0 {
|
||||
return nil, fmt.Errorf("too many wrong passwords; try again in %d seconds", int((wait+time.Second-1)/time.Second))
|
||||
}
|
||||
verifier, err := readWebAuth(g.authFile)
|
||||
@@ -178,13 +209,13 @@ func (g *elevationGate) elevate(client, password string) (*protocol.Elevation, e
|
||||
return nil, errors.New("the password could not be checked; see the server log")
|
||||
}
|
||||
if !ok {
|
||||
g.noteFailure(client)
|
||||
g.noteFailure(key)
|
||||
return nil, errors.New("wrong password")
|
||||
}
|
||||
return g.grant(client)
|
||||
return g.grant(key)
|
||||
}
|
||||
|
||||
func (g *elevationGate) blocked(client string) time.Duration {
|
||||
func (g *elevationGate) blocked(key string) time.Duration {
|
||||
g.mu.Lock()
|
||||
defer g.mu.Unlock()
|
||||
now := g.now()
|
||||
@@ -192,7 +223,7 @@ func (g *elevationGate) blocked(client string) time.Duration {
|
||||
if len(g.recent) >= elevationGlobalFailures {
|
||||
return g.recent[0].Add(elevationGlobalWindow).Sub(now)
|
||||
}
|
||||
if entry, ok := g.failures[client]; ok && now.Before(entry.until) {
|
||||
if entry, ok := g.failures[key]; ok && now.Before(entry.until) {
|
||||
return entry.until.Sub(now)
|
||||
}
|
||||
return 0
|
||||
@@ -206,19 +237,26 @@ func (g *elevationGate) pruneLocked(now time.Time) {
|
||||
g.recent = g.recent[keep:]
|
||||
}
|
||||
|
||||
func (g *elevationGate) noteFailure(client string) {
|
||||
func (g *elevationGate) noteFailure(key string) {
|
||||
g.mu.Lock()
|
||||
defer g.mu.Unlock()
|
||||
now := g.now()
|
||||
g.recent = append(g.recent, now)
|
||||
entry := g.failures[client]
|
||||
// A session's failures outlive its connections; drop entries whose wait
|
||||
// is long over so the map stays bounded by recent activity.
|
||||
for other, entry := range g.failures {
|
||||
if !now.Before(entry.until.Add(elevationGlobalWindow)) {
|
||||
delete(g.failures, other)
|
||||
}
|
||||
}
|
||||
entry := g.failures[key]
|
||||
entry.count++
|
||||
delay := elevationBackoffBase << min(entry.count-1, 6)
|
||||
entry.until = now.Add(min(delay, elevationBackoffMax))
|
||||
g.failures[client] = entry
|
||||
g.failures[key] = entry
|
||||
}
|
||||
|
||||
func (g *elevationGate) grant(client string) (*protocol.Elevation, error) {
|
||||
func (g *elevationGate) grant(key string) (*protocol.Elevation, error) {
|
||||
raw := make([]byte, 32)
|
||||
if _, err := rand.Read(raw); err != nil {
|
||||
return nil, err
|
||||
@@ -228,18 +266,18 @@ func (g *elevationGate) grant(client string) (*protocol.Elevation, error) {
|
||||
defer g.mu.Unlock()
|
||||
now := g.now()
|
||||
until := now.Add(g.ttl)
|
||||
g.dropLocked(client)
|
||||
delete(g.failures, client)
|
||||
for key, entry := range g.tokens {
|
||||
if !now.Before(entry.until) {
|
||||
delete(g.tokens, key)
|
||||
g.dropLocked(key)
|
||||
delete(g.failures, key)
|
||||
for other, end := range g.until {
|
||||
if !now.Before(end) {
|
||||
g.dropLocked(other)
|
||||
}
|
||||
}
|
||||
if len(g.tokens) >= elevationTokenLimit {
|
||||
return nil, errors.New("too many elevated connections on this server")
|
||||
if len(g.until) >= elevationTokenLimit {
|
||||
return nil, errors.New("too many elevated sessions on this server")
|
||||
}
|
||||
g.until[client] = until
|
||||
g.tokens[tokenKey(token)] = elevationToken{client: client, until: until}
|
||||
g.until[key] = until
|
||||
g.tokens[tokenKey(token)] = elevationToken{key: key, until: until}
|
||||
return &protocol.Elevation{
|
||||
ExpiresAt: until.UTC().Format(time.RFC3339),
|
||||
Seconds: int(g.ttl / time.Second),
|
||||
@@ -252,28 +290,30 @@ func tokenKey(token string) string {
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
|
||||
// dropLocked ends client's elevation and its upload token.
|
||||
func (g *elevationGate) dropLocked(client string) {
|
||||
delete(g.until, client)
|
||||
for key, entry := range g.tokens {
|
||||
if entry.client == client {
|
||||
delete(g.tokens, key)
|
||||
// dropLocked ends key's elevation and its upload token.
|
||||
func (g *elevationGate) dropLocked(key string) {
|
||||
delete(g.until, key)
|
||||
for hash, entry := range g.tokens {
|
||||
if entry.key == key {
|
||||
delete(g.tokens, hash)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// forget ends a closed connection's elevation and attempt history.
|
||||
func (g *elevationGate) forget(client string) {
|
||||
if g == nil {
|
||||
// closed is called when a connection that elevated under key ends: a
|
||||
// connection's own elevation and attempt history end with it, a web session's
|
||||
// outlive it for that session's next connection.
|
||||
func (g *elevationGate) closed(key string) {
|
||||
if g == nil || !strings.HasPrefix(key, connectionKeyPrefix) {
|
||||
return
|
||||
}
|
||||
g.mu.Lock()
|
||||
defer g.mu.Unlock()
|
||||
g.dropLocked(client)
|
||||
delete(g.failures, client)
|
||||
g.dropLocked(key)
|
||||
delete(g.failures, key)
|
||||
}
|
||||
|
||||
// tokenValid reports whether token belongs to a still elevated connection.
|
||||
// tokenValid reports whether token belongs to a still live elevation.
|
||||
func (g *elevationGate) tokenValid(token string) bool {
|
||||
if token == "" {
|
||||
return false
|
||||
@@ -285,7 +325,7 @@ func (g *elevationGate) tokenValid(token string) bool {
|
||||
return false
|
||||
}
|
||||
if !g.now().Before(entry.until) {
|
||||
g.dropLocked(entry.client)
|
||||
g.dropLocked(entry.key)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
|
||||
Reference in New Issue
Block a user