Files
ARTEX/server/sharedbrowser_profile_unix.go
BaaRai 716e484e74 feat(server,web): opt-in shared browser with isolated CDP, per-task profiles, and configurable lock (#621)
Rebased onto main (5f5ec32) and hardened per security review:
- Opt-in only (-shared-browser / ARTEX_SHARED_BROWSER=true; default false); README and CHANGELOG accurately describe hardened behavior and document the residual risk of same-UID argv token visibility alongside per-message operator-hold/lease enforcement and immediate revocation.
- Isolates CDP via --remote-debugging-pipe on Unix (no Chromium DevTools TCP port or DevToolsActivePort) fronted by a per-run 256-bit token-authenticated server CDP proxy enforcing operator hold and active lease on every HTTP request and WebSocket message.
- Removes --ignore-certificate-errors from the capture path; trusts only ARTEX's capture CA SPKI pin (--ignore-certificate-errors-spki-list) and fails closed if unreadable.
- Removes --disable-blink-features=AutomationControlled and the HeadlessChrome user-agent rewrite (policy: hand challenges off via ask_operator).
- Isolates browser profiles per task or chat session under <data>/browser-profiles/<scope> using descriptor-relative openat(O_NOFOLLOW|O_DIRECTORY) + fchown + unlinkat with root-owned parent (no filepath.Walk over executor-owned trees), tested against root/scope/child symlinks.
- Makes operator lock / ask_operator hold timeout configurable via -shared-browser-lock-timeout / ARTEX_SHARED_BROWSER_LOCK_TIMEOUT (default 15m), auto-expiring idle locks.
Co-authored-by: Pranil Acharya <Pro-Nil@users.noreply.github.com>
2026-11-28 10:00:56 +01:00

87 lines
3.2 KiB
Go

//go:build unix
package server
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"syscall"
"golang.org/x/sys/unix"
)
// sharedBrowserStaleLockFiles are Chromium profile lock/port entries cleared
// inside the scope directory before relaunching.
var sharedBrowserStaleLockFiles = []string{
"SingletonLock",
"SingletonSocket",
"SingletonCookie",
"DevToolsActivePort",
}
// prepareSharedBrowserProfileDir creates and secures the isolated profile
// directory <root>/<scope> without walking children or following symlinks.
//
// Security (matches execpriv_unix.go):
// - <root> (<data>/browser-profiles) stays root:root 0755 when privilege drop
// is enabled, so the executor cannot rename or replace <scope> inside it.
// - Both <root> and <scope> are opened with O_RDONLY|O_DIRECTORY|O_NOFOLLOW
// (and <scope> relative to rootFD via openat), so a planted symlink at
// <root> or <scope> is rejected with ELOOP before any fchown or unlinkat.
// - Ownership is changed only via fchown on the verified directory descriptor
// — never via filepath.Walk over executor-owned children, which would allow
// an intermediate-component symlink swap race into root-only paths.
// - Stale Chromium lock files are removed via unlinkat on scopeFD.
func prepareSharedBrowserProfileDir(root, scope string, priv ExecPrivilege) (string, error) {
scope = sanitizeProfileScope(scope)
if err := os.MkdirAll(root, 0o755); err != nil {
return "", err
}
rootFD, err := unix.Open(root, unix.O_RDONLY|unix.O_DIRECTORY|unix.O_NOFOLLOW|unix.O_CLOEXEC, 0)
if err != nil {
return "", fmt.Errorf("open profile root %q: %w", root, err)
}
defer unix.Close(rootFD)
if priv.Enabled {
// Keep root owned by root:root 0755 so the executor can traverse to its
// scope directory but cannot swap <scope> for a symlink.
if err := unix.Fchown(rootFD, 0, 0); err != nil {
return "", fmt.Errorf("chown profile root %q: %w", root, err)
}
if err := unix.Fchmod(rootFD, 0o755); err != nil {
return "", fmt.Errorf("chmod profile root %q: %w", root, err)
}
}
if err := unix.Mkdirat(rootFD, scope, 0o700); err != nil && !errors.Is(err, syscall.EEXIST) && !errors.Is(err, fs.ErrExist) {
return "", fmt.Errorf("mkdir scope %q: %w", scope, err)
}
scopeFD, err := unix.Openat(rootFD, scope, unix.O_RDONLY|unix.O_DIRECTORY|unix.O_NOFOLLOW|unix.O_CLOEXEC, 0)
if err != nil {
return "", fmt.Errorf("open profile scope %q (symlink refused): %w", scope, err)
}
defer unix.Close(scopeFD)
if priv.Enabled {
// Fchown ONLY the verified scope directory descriptor; Chromium runs as
// the executor and creates all children under it with executor ownership.
if err := unix.Fchown(scopeFD, int(priv.UID), int(priv.GID)); err != nil {
return "", fmt.Errorf("chown profile scope %q: %w", scope, err)
}
if err := unix.Fchmod(scopeFD, 0o700); err != nil {
return "", fmt.Errorf("chmod profile scope %q: %w", scope, err)
}
}
// Remove stale lock files relative to the verified scopeFD (never via
// path-based os.Remove that could traverse an intermediate symlink).
for _, f := range sharedBrowserStaleLockFiles {
_ = unix.Unlinkat(scopeFD, f, 0)
}
return filepath.Join(root, scope), nil
}