f11ff8650d
A new elevate request verifies the web sign-in password (scrypt hash, read-only, rate-limited, constant time) and elevates that connection for 15 minutes. chat_shell_run/input, rename/move/delete, terminal_open/input and POST /files (via a per-connection upload token) are refused with code elevation_required until then.
41 lines
1.3 KiB
Go
41 lines
1.3 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, so the elevated connection's token
|
|
// (protocol.ElevationHeader) authorises it; it is valid only while that
|
|
// connection stays open and elevated.
|
|
func (g *elevationGate) requireElevationHTTP(next http.HandlerFunc) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
if 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.",
|
|
})
|
|
}
|
|
}
|
|
|
|
// ElevationTTLFromEnvironment reads PIPELINES_ELEVATION_MINUTES (1 to 720);
|
|
// 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 > 720 {
|
|
return DefaultElevationTTL
|
|
}
|
|
return time.Duration(minutes) * time.Minute
|
|
}
|