Files
magpie/internal/gateway/claude_subscription_root_test.go
T
yetone c112d2a448 gateway: the Claude subscription answers when magpie runs as root (FrierenF)
Run as root (a server, a Docker container), every Claude subscription
request failed with "Claude Code: exited before it answered magpie's
initialize request (exit status 1): --dangerously-skip-permissions cannot
be used with root/sudo privileges for security reasons".

Claude Code 2.1.296's setup refuses bypassPermissions/
--dangerously-skip-permissions when process.getuid() === 0, IS_SANDBOX is
not "1" and CLAUDE_CODE_BUBBLEWRAP is unset (read from the installed
binary; --permission-mode bypassPermissions is refused the same way). The
bridge's run has no tool of Claude Code's own that touches the machine,
only magpie's MCP tools, answered by the client, and Anthropic's web
search, so the bridge run as root is now given IS_SANDBOX=1
(claudeCLI.asRoot). Its args are unchanged, and a run as anyone else has
its env exactly as before. A WSL distro whose default user is root is the
same case: the WSL probe now prints `id -u` (wslrun.Tool.Root), and
IS_SANDBOX goes into the distro (WSLENV) only then. The warm-up and /usage
runs don't pass the flag and are untouched.

Run:
- Docker node:22-bookworm-slim as root, @anthropic-ai/claude-code@2.1.296,
  fake API: the bridge's exact argv refuses with the message above without
  IS_SANDBOX, and with IS_SANDBOX=1 answers from the fake API
  (permissionMode bypassPermissions).
- The gateway's serveClaudeSubscription as root in that container (linux
  test binary, real claude, no credentials): before, 502 "Claude Code
  ended without an answer"; after, it gets past the check to Claude
  Code's own "Not logged in".
- TestClaudeRunsAsRoot (a fake that refuses as root does) goes red with
  asRoot removed from the launch; TestClaudeAsRootEnv; TestParseProbe.
- go test -tags nogui ./internal/gateway ./internal/wslrun ./internal/agent,
  gofmt -l, go vet (darwin, GOOS=linux, GOOS=windows). WSL root path is
  unit-tested only, not tried on the Windows box.
2026-10-10 19:39:19 +08:00

154 lines
5.6 KiB
Go

package gateway
import (
"encoding/json"
"net/http/httptest"
"os"
"path/filepath"
"runtime"
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/testenv"
"github.com/yetone/magpie/internal/wslrun"
)
// claudeRootRefusal is what Claude Code 2.1.296 prints, and exits 1 on,
// when it is run as root with --dangerously-skip-permissions, IS_SANDBOX
// not "1" and CLAUDE_CODE_BUBBLEWRAP unset (FrierenF on Discord, magpie
// on a server as root).
const claudeRootRefusal = "--dangerously-skip-permissions cannot be used with root/sudo privileges for security reasons"
// fakeRootClaude refuses as Claude Code run as root does while the file
// it returns is there, and otherwise answers with the IS_SANDBOX it was
// given.
func fakeRootClaude(t *testing.T) (root string) {
t.Helper()
if runtime.GOOS == "windows" {
t.Skip("a shell script stands in for Claude Code")
}
dir := t.TempDir()
root = filepath.Join(dir, "root")
script := `#!/bin/sh
[ -e '` + root + `' ] && case " $* " in *" --dangerously-skip-permissions "*)
if [ "$IS_SANDBOX" != 1 ] && [ -z "$CLAUDE_CODE_BUBBLEWRAP" ]; then
echo '` + claudeRootRefusal + `' >&2; exit 1
fi;;
esac
while read -r line; do
echo '{"type":"stream_event","event":{"type":"message_start","message":{"id":"m","model":"claude-sonnet-5","usage":{"input_tokens":1}}}}'
echo '{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"IS_SANDBOX='"${IS_SANDBOX-unset}"'"}}}'
echo '{"type":"stream_event","event":{"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":3}}}'
echo '{"type":"stream_event","event":{"type":"message_stop"}}'
echo '{"type":"result","subtype":"success","is_error":false,"result":""}'
done
`
testenv.Program(t, filepath.Join(dir, "claude"), script)
t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH"))
runsEndWithTest(t, dir)
return root
}
// Run as root, the Claude subscription answers: Claude Code is told
// IS_SANDBOX=1, which its root check lets --dangerously-skip-permissions
// through on. Run as anyone else, its env is what it was.
func TestClaudeRunsAsRoot(t *testing.T) {
root := fakeRootClaude(t)
// unset for the test; t.Setenv puts back what was there
t.Setenv("IS_SANDBOX", "")
os.Unsetenv("IS_SANDBOX")
t.Setenv("CLAUDE_CODE_BUBBLEWRAP", "")
os.Unsetenv("CLAUDE_CODE_BUBBLEWRAP")
s := New()
t.Cleanup(s.subscription.abortAll)
p := provider.Provider{ID: "claude", Account: &provider.Account{Agent: "claude", User: "u"}}
ask := func(say string) (int, string) {
t.Helper()
body := `{"model":"claude-sonnet-5","max_tokens":100,"messages":[{"role":"user","content":"` + say + `"}]}`
rec := httptest.NewRecorder()
var u Usage
code, msg := s.serveClaudeSubscription(rec, httptest.NewRequest("POST", "/v1/messages", strings.NewReader(body)), provider.Anthropic, p, "claude-sonnet-5", []byte(body), &u)
if code != 200 {
return code, msg
}
var res struct {
Content []struct{ Text string } `json:"content"`
}
json.Unmarshal(rec.Body.Bytes(), &res)
if len(res.Content) == 0 {
t.Fatalf("no answer: %s", rec.Body)
}
return code, res.Content[0].Text
}
euid := claudeEuid
t.Cleanup(func() { claudeEuid = euid })
claudeEuid = func() int { return 0 }
if err := os.WriteFile(root, nil, 0o600); err != nil {
t.Fatal(err)
}
if code, got := ask("as root"); code != 200 || got != "IS_SANDBOX=1" {
t.Fatalf("as root: %d %q", code, got)
}
claudeEuid = func() int { return 1000 }
if err := os.Remove(root); err != nil {
t.Fatal(err)
}
if code, got := ask("as a user"); code != 200 || got != "IS_SANDBOX=unset" {
t.Fatalf("as a user: %d %q", code, got)
}
}
func TestClaudeAsRootEnv(t *testing.T) {
euid := claudeEuid
t.Cleanup(func() { claudeEuid = euid })
local := claudeCLI{path: "/usr/bin/claude"}
base := []string{"PATH=/bin", "HOME=/root"}
claudeEuid = func() int { return 1000 }
for _, env := range [][]string{base, append(slices.Clone(base), "IS_SANDBOX=yes")} {
if got := local.asRoot(slices.Clone(env)); !slices.Equal(got, env) {
t.Fatalf("not root: %q, want %q as it was", got, env)
}
}
claudeEuid = func() int { return 0 }
for _, c := range []struct{ env, want []string }{
{base, append(slices.Clone(base), "IS_SANDBOX=1")},
// a value Claude Code doesn't take is put right
{[]string{"IS_SANDBOX=true", "PATH=/bin"}, []string{"PATH=/bin", "IS_SANDBOX=1"}},
// already let through: kept as it is
{[]string{"PATH=/bin", "IS_SANDBOX=1"}, []string{"PATH=/bin", "IS_SANDBOX=1"}},
{[]string{"PATH=/bin", "CLAUDE_CODE_BUBBLEWRAP=1"}, []string{"PATH=/bin", "CLAUDE_CODE_BUBBLEWRAP=1"}},
} {
if got := local.asRoot(slices.Clone(c.env)); !slices.Equal(got, c.want) {
t.Errorf("root %q: %q, want %q", c.env, got, c.want)
}
}
// a WSL distro's user decides, not magpie's on Windows; and IS_SANDBOX
// goes into the distro only for root
claudeEuid = func() int { return 1000 }
wslenv := func(env []string) string {
for _, e := range env {
if v, ok := strings.CutPrefix(e, "WSLENV="); ok {
return v
}
}
return ""
}
root := claudeCLI{wsl: &wslrun.Tool{Path: "/usr/bin/claude", Root: true}}
env := root.env(root.asRoot(slices.Clone(base)), "")
if !slices.Contains(env, "IS_SANDBOX=1") || !slices.Contains(strings.Split(wslenv(env), ":"), "IS_SANDBOX") {
t.Fatalf("WSL as root: %q", env)
}
user := claudeCLI{wsl: &wslrun.Tool{Path: "/usr/bin/claude"}}
env = user.env(user.asRoot(append(slices.Clone(base), "IS_SANDBOX=x")), "")
if slices.Contains(env, "IS_SANDBOX=1") || slices.Contains(strings.Split(wslenv(env), ":"), "IS_SANDBOX") {
t.Fatalf("WSL as a user: %q", env)
}
}