Require step-up password confirmation for shells, terminals and file changes
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.
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"crypto/subtle"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// The web sign-in password file (web-auth.json) is written by the installer,
|
||||
// ./password and the web proxy. This server only reads it, to verify step-up
|
||||
// re-authentication against the same password the operator signs in with.
|
||||
//
|
||||
// password_hash is "scrypt$N$r$p$<salt>$<key>" with unpadded base64url salt and
|
||||
// key; a file not yet migrated by the proxy still holds a plaintext
|
||||
// "password", which is compared in constant time through SHA-256 digests.
|
||||
|
||||
// webAuthMaxBytes bounds the file read; real files are a few hundred bytes.
|
||||
const webAuthMaxBytes = 64 << 10
|
||||
|
||||
// DefaultWebAuthFile is where the installer keeps the web sign-in password.
|
||||
func DefaultWebAuthFile() string {
|
||||
if explicit := os.Getenv("PIPELINES_WEB_AUTH_FILE"); explicit != "" {
|
||||
return explicit
|
||||
}
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return filepath.Join(home, ".config", "pipelines", "web-auth.json")
|
||||
}
|
||||
|
||||
type webAuthVerifier struct {
|
||||
hash string
|
||||
password string
|
||||
}
|
||||
|
||||
var errWebAuthMissing = errors.New("no web sign-in password file")
|
||||
|
||||
// readWebAuth loads the verifier from path. A missing file is
|
||||
// errWebAuthMissing; anything unsafe or malformed is another error, and the
|
||||
// caller then fails closed.
|
||||
func readWebAuth(path string) (webAuthVerifier, error) {
|
||||
info, err := os.Lstat(path)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return webAuthVerifier{}, errWebAuthMissing
|
||||
}
|
||||
if err != nil {
|
||||
return webAuthVerifier{}, err
|
||||
}
|
||||
if !info.Mode().IsRegular() {
|
||||
return webAuthVerifier{}, errors.New("web sign-in password file is not a regular file")
|
||||
}
|
||||
if info.Mode().Perm()&0o077 != 0 {
|
||||
return webAuthVerifier{}, errors.New("web sign-in password file is not private (chmod 600)")
|
||||
}
|
||||
if !ownedByCurrentUser(info) {
|
||||
return webAuthVerifier{}, errors.New("web sign-in password file belongs to another user")
|
||||
}
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return webAuthVerifier{}, err
|
||||
}
|
||||
defer file.Close()
|
||||
body, err := io.ReadAll(io.LimitReader(file, webAuthMaxBytes+1))
|
||||
if err != nil {
|
||||
return webAuthVerifier{}, err
|
||||
}
|
||||
if len(body) > webAuthMaxBytes {
|
||||
return webAuthVerifier{}, errors.New("web sign-in password file is too large")
|
||||
}
|
||||
var parsed struct {
|
||||
PasswordHash string `json:"password_hash"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &parsed); err != nil {
|
||||
return webAuthVerifier{}, errors.New("web sign-in password file is malformed")
|
||||
}
|
||||
if parsed.PasswordHash != "" {
|
||||
if _, err := parseScryptHash(parsed.PasswordHash); err != nil {
|
||||
return webAuthVerifier{}, err
|
||||
}
|
||||
return webAuthVerifier{hash: parsed.PasswordHash}, nil
|
||||
}
|
||||
if len(parsed.Password) >= 8 {
|
||||
return webAuthVerifier{password: parsed.Password}, nil
|
||||
}
|
||||
return webAuthVerifier{}, errors.New("web sign-in password file has no password")
|
||||
}
|
||||
|
||||
type scryptHash struct {
|
||||
n, r, p int
|
||||
salt, key []byte
|
||||
}
|
||||
|
||||
func parseScryptHash(value string) (scryptHash, error) {
|
||||
invalid := errors.New("web sign-in password hash is malformed")
|
||||
parts := strings.Split(value, "$")
|
||||
if len(parts) != 6 || parts[0] != "scrypt" {
|
||||
return scryptHash{}, invalid
|
||||
}
|
||||
var numbers [3]int
|
||||
for i, limit := range [3]int{1 << 20, 32, 16} {
|
||||
number, err := strconv.Atoi(parts[i+1])
|
||||
if err != nil || number < 1 || number > limit {
|
||||
return scryptHash{}, invalid
|
||||
}
|
||||
numbers[i] = number
|
||||
}
|
||||
salt, err := base64.RawURLEncoding.DecodeString(parts[4])
|
||||
if err != nil || len(salt) < 8 {
|
||||
return scryptHash{}, invalid
|
||||
}
|
||||
key, err := base64.RawURLEncoding.DecodeString(parts[5])
|
||||
if err != nil || len(key) < 16 || len(key) > 64 {
|
||||
return scryptHash{}, invalid
|
||||
}
|
||||
if numbers[0] < 2 || numbers[0]&(numbers[0]-1) != 0 {
|
||||
return scryptHash{}, invalid
|
||||
}
|
||||
return scryptHash{n: numbers[0], r: numbers[1], p: numbers[2], salt: salt, key: key}, nil
|
||||
}
|
||||
|
||||
// verify reports whether candidate is the stored password, in constant time
|
||||
// with respect to the candidate's content.
|
||||
func (v webAuthVerifier) verify(candidate string) (bool, error) {
|
||||
if v.hash == "" {
|
||||
given := sha256.Sum256([]byte(candidate))
|
||||
want := sha256.Sum256([]byte(v.password))
|
||||
return subtle.ConstantTimeCompare(given[:], want[:]) == 1 && v.password != "", nil
|
||||
}
|
||||
parsed, err := parseScryptHash(v.hash)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
derived, err := scryptKey([]byte(candidate), parsed.salt, parsed.n, parsed.r, parsed.p, len(parsed.key))
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("verify password: %w", err)
|
||||
}
|
||||
return subtle.ConstantTimeCompare(derived, parsed.key) == 1, nil
|
||||
}
|
||||
Reference in New Issue
Block a user