Compare commits

...
Author SHA1 Message Date
BaaRai b27ac66009 security(server): block SSRF in POST /api/llm/models (GHSA-7w8h) 2026-11-20 02:35:39 +01:00
BaaRai 0a22c9583f Merge pull request #389 from BaaRai/security/intercept-default-tools
fix(intercept): gate ExecuteExtraTool and Monitor by default
2026-11-20 02:01:35 +01:00
BaaRai 29edd24285 Merge pull request #388 from BaaRai/security/asset-dsl-or-scope
fix(db): parenthesise a top-level OR in asset DSL queries
2026-11-20 01:53:07 +01:00
BaaRai 46751cfb9e fix(intercept): gate ExecuteExtraTool and Monitor by default
The default intercept-enabled set left out ExecuteExtraTool (the dispatcher for MCP and deferred tools) and Monitor (background commands), so calls through them bypassed every rule unless the operator had saved their own enabled-tools list.

A malformed intercept_enabled_tools value also produced an empty set, which turned the rule system off; it now falls back to the defaults, and GetEnabledTools reports the parse error instead of pretending nothing is gated.
2026-11-20 01:48:50 +01:00
BaaRai 4677c1b6f6 Merge pull request #387 from BaaRai/security/workspace-platform-deny
fix(server): keep the capture CA and index out of the workspace file API
2026-11-20 01:37:49 +01:00
BaaRai 4d4b42586e Merge pull request #386 from BaaRai/security/mcp-sse-origin
fix(mcp): keep the legacy SSE message endpoint on the configured origin
2026-11-20 01:37:43 +01:00
BaaRai 84a623bf2d fix(db): parenthesise a top-level OR in asset DSL queries
QueryDSLInScope builds the DSL clause and then appends the task-scope clause with AND. An OR node joined its children without wrapping the whole group, so '(a) OR (b) AND id IN (scope)' let the first branch return assets outside the task scope. Wrap the whole disjunction; the children are already parenthesised.

Tests: the pure builder shape, and a PostgreSQL case where the bare-text branch matches both an in-scope and an out-of-scope asset (fails before the fix).
2026-11-20 01:34:28 +01:00
BaaRai 43f1c893c4 Merge pull request #385 from BaaRai/security/denylist-git-cmake
fix(intercept): deny always-allow for git, cmake, ctest and cpack
2026-11-20 01:34:21 +01:00
BaaRai 4a29dbfd6d Merge pull request #384 from BaaRai/fix/llm-rotate-context
fix(server): attach the rotated LLM profile pin after execContextFor
2026-11-20 01:28:50 +01:00
BaaRai 73ab430c6a fix(server): keep the capture CA and index out of the workspace file API
The workspace file manager roots every path at the data directory, so the
capture proxy CA private key under traffic/_ca and the SQLite exchange index
under traffic/_index were listable, downloadable, overwritable and deletable
through /api/workspace/*. Refuse those two subtrees after resolving the path,
so a symlink alias or a Windows spelling a trailing dot cannot reach them.
2026-11-20 01:23:50 +01:00
BaaRai 7cd9faf1f9 fix(mcp): keep the legacy SSE message endpoint on the configured origin
readSSEEndpoint accepted any absolute URL, and legacyRoundTrip copied every
configured header (Authorization included) to it. Resolve the announced
endpoint against the configured SSE URL, refuse a different scheme, host or
effective port, refuse userinfo, and apply the same origin check to every
redirect hop for both the legacy SSE and Streamable HTTP clients.
2026-11-20 01:23:48 +01:00
BaaRai 02a67bf9c5 fix(intercept): deny always-allow for git, cmake, ctest and cpack
The argv allow denylist is the only gate that stops an always-allow rule
from naming a program whose flags can run arbitrary commands. git (aliases,
hooks, -c), cmake (-P), ctest and cpack run commands from user-controlled
files, so an allow prefix for them is never safe. Also key the arity table
by the normalised program name so a path-qualified binary derives the same
prefix length as the bare name.
2026-11-20 01:23:45 +01:00
BaaRai b3c9708bf3 fix(server): attach rotated LLM profile pin after execContextFor (#376) 2026-11-20 01:01:08 +01:00
BaaRai d41da1b2bb Merge pull request #383 from BaaRai/chore/export-ignore-offensive-skills
chore: exclude the offensive research skills from generated source archives
2026-11-20 00:50:22 +01:00
BaaRai 4c7dace5f2 chore: exclude the offensive research skills from generated source archives 2026-11-20 00:50:05 +01:00
BaaRai 0255f6af2c Merge pull request #377 from BaaRai/fix/argv-compound-matching
intercept: match deny/ask argv rules across compound commands
2026-11-19 20:33:57 +01:00
BaaRai 5c95f13403 Merge pull request #378 from BaaRai/chore/export-ignore-pocs
chore: exclude the php2shell PoCs from generated source archives
2026-11-19 20:31:35 +01:00
BaaRai 6c121845d7 chore: exclude the php2shell PoCs from generated source archives 2026-11-19 20:31:17 +01:00
BaaRai bbdb064afc fix(intercept): over-approximate unknown options on known wrappers and expand parallel flags 2026-11-19 18:56:42 +01:00
BaaRai 0258a0570b fix(intercept): parse command lines after parallel ::: when no command precedes it 2026-11-19 18:18:18 +01:00
BaaRai da00bf7c8a fix(intercept): close shell-option, env -S, xargs -l and find -exec argv bypasses 2026-11-19 17:57:59 +01:00
BaaRai 8f0b73f514 fix(intercept): match argv deny and ask rules across compound and wrapped Bash commands 2026-11-19 17:15:53 +01:00
BaaRai 8e6acb0638 Merge pull request #371 from mathkartel/docs/openai-compatible-gateways
docs: recipe for OpenAI-compatible gateways
2026-11-19 16:31:32 +01:00
BaaRai 35fbda765a Merge pull request #372 from BaaRai/fix/workspace-symlink-confinement
fix(server): confine workspace and skill paths against symlink escapes
2026-11-19 16:28:24 +01:00
BaaRai 39990fe7e4 docs(changelog): note the workspace and skill symlink confinement 2026-11-19 16:10:12 +01:00
BaaRai b1ccac4c9a fix(server): confine workspace and skill file paths against symlink escapes (#367) 2026-11-19 16:10:02 +01:00
BaaRai 90ca982c5e Merge pull request #370 from BaaRai/fix/target-gate-proxy-name-only
fix(intercept): make target pins name-only behind any proxy
2026-11-19 16:04:16 +01:00
mathkartel 95dd5e9711 docs: recipe for OpenAI-compatible gateways (NotHoly example) 2026-11-19 11:58:37 -03:00
33 changed files with 5105 additions and 168 deletions
+3
View File
@@ -1,2 +1,5 @@
*.bat text eol=crlf
*.sh text eol=lf
scripts/poc/ export-ignore
skills/edr-bypass-re/ export-ignore
skills/php2shell/ export-ignore
+1
View File
@@ -49,6 +49,7 @@ All notable changes to this project are documented in this file. The format is b
### Fixed
- **Workspace and skill symlink confinement**: workspace file operations, the skill file APIs and the skill loader resolve symlinks in every path component and refuse a target outside their root, so a link planted in the workspace or a skill directory cannot expose another file.
- **Target gate behind proxies**: a target approval is name-only whenever a proxy is in the path, including the recording proxy, the global egress proxy and a custom tool's own proxy, so the gate never claims an IP binding the proxy does not enforce.
- **Remote MCP response bound**: Streamable HTTP JSON-RPC bodies and SSE data payloads are refused above 8 MiB, so a remote MCP server cannot force an unbounded decode.
- **LLM response capture bound**: each recorded provider attempt keeps at most 1 MiB, and a call keeps at most 8 attempts, so a large or retrying stream cannot grow the recorder without bound. The live stream is unchanged.
+8
View File
@@ -428,6 +428,14 @@ runtime supports it.
`/chat/completions` (or `/responses` for `openai-responses`), so their base **does** end in `/v1`
(for example `https://api.openai.com/v1`).
**OpenAI-compatible gateways**: any provider that serves an OpenAI-style
`/v1/chat/completions` endpoint works through the **OpenAI** format — no dedicated format
needed. Create a profile with format **OpenAI**, set **Base URL** to the gateway's `/v1`
root, paste the key, then click the **↻** button next to *Model* to load the provider's
catalog (`GET /v1/models`) and pick a model; models the provider adds later appear on the
next refresh. For example, [NotHoly](https://notholy.com) (offensive-security models) uses
base URL `https://api.notholy.com/v1` with an API key from its console.
**Concurrency**: work agents per task, in "System Settings" (default 3).
**Untrusted-output boundary (on by default)**: tool results for the worker, planner, chat and main
+6
View File
@@ -43,6 +43,12 @@ The enforced trust boundary for asset insertion is `insert_assets`, which runs e
Accepted gap: when a task has no allow-list, there is no allow rule to apply (block rules still run), and any host carried on an in-scope event that is not blocked enters the graph as an asset. Operators who rely on the scanner's scope rules alone should configure an allow-list per task, or review `output.json` before feeding it to ARTEX. Cross-checking ingested events against ARTEX's own rule set would change what existing scanner configurations mean for those operators, so it is treated as a deliberate design question (see #317), not an accepted vulnerability.
## Outbound Model Listing (`POST /api/llm/models`)
`POST /api/llm/models` fetches model lists from the provider endpoint and optional HTTP proxy supplied in the request. By default, ARTEX allows only `http` and `https` URLs whose hosts resolve exclusively to public IP addresses (blocking loopback, RFC1918 private networks, IPv6 unique-local `fc00::/7`, link-local/cloud metadata such as `169.254.169.254`, unspecified, and multicast addresses), and enforces the same checks on redirects and at dial time. The built-in local Ollama default (`http://127.0.0.1:11434/v1`) is permitted only when the request leaves `base_url` empty for the `ollama` provider.
Operators running self-hosted LLM gateways or Ollama on a LAN or Docker bridge can allow specific endpoints with `ARTEX_LLM_PRIVATE_ENDPOINTS`, a comma-separated list of exact `host:port` entries (for example, `127.0.0.1:11434,host.docker.internal:11434,192.168.1.10:11434`). A URL whose `host:port` matches an entry may resolve to a loopback or private address while scheme restrictions, link-local/cloud-metadata blocks, and redirect validation still apply; everything else stays refused. Leaving `ARTEX_LLM_PRIVATE_ENDPOINTS` unset or empty keeps the strict public-only policy.
## What to Expect
This is an open-source project maintained on a best-effort basis. We aim to acknowledge a report within about a week and keep you updated, but we cannot guarantee specific response or fix timeframes.
+5 -1
View File
@@ -302,7 +302,11 @@ func (b *whereBuilder) build(node *astNode) (string, error) {
}
parts = append(parts, "("+clause+")")
}
return strings.Join(parts, " OR "), nil
// The whole disjunction must be parenthesised: callers append their own
// clauses with AND (the task scope in QueryDSLInScope, the type filter),
// and AND binds tighter than OR, so "(a) OR (b) AND scope" would let the
// first branch escape the scope.
return "(" + strings.Join(parts, " OR ") + ")", nil
case "leaf":
return b.buildLeaf(*node.expr)
}
+86
View File
@@ -0,0 +1,86 @@
package db
import (
"fmt"
"strings"
"testing"
"time"
)
// A top-level OR must be parenthesised as a whole. QueryDSLInScope appends the
// task-scope clause with AND, and AND binds tighter than OR, so an unparenthesised
// disjunction lets the first branch return rows from outside the task scope.
func TestBuildDSLWhereWrapsOr(t *testing.T) {
node, err := ParseDSL("domain=alpha OR domain=beta")
if err != nil {
t.Fatal(err)
}
where, _, err := buildDSLWhereBase(node, 0)
if err != nil {
t.Fatal(err)
}
if !strings.HasPrefix(where, "((") || !strings.HasSuffix(where, "))") {
t.Fatalf("compiled to %q: the whole OR group must be parenthesised", where)
}
}
// TestQueryDSLInScopeOrDoesNotEscape is the behavioural form: an OR query whose
// first branch matches every row must not return an asset outside the task scope.
func TestQueryDSLInScopeOrDoesNotEscape(t *testing.T) {
d, assets, _ := testSetup(t)
t.Cleanup(func() { d.Close() })
task, err := d.CreateTask("or scope", "goal", nil, 0, 0)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { cleanupErr(t, "task", d.DeleteTask(task.ID)) })
stamp := time.Now().UnixNano()
root := fmt.Sprintf("in%d.invalid", stamp)
out := fmt.Sprintf("x%d.invalid", stamp)
marker := fmt.Sprintf("or%d", stamp)
if err := assets.upsertTaskScope(TaskScope{TaskID: task.ID, Kind: "root_domain", Domain: root}); err != nil {
t.Fatalf("seed scope: %v", err)
}
var inID, outID int64
for _, r := range []struct {
domain, root string
id *int64
}{
{"a." + root, root, &inID},
{out, out, &outID},
} {
if err := d.QueryRow(`INSERT INTO assets(type,domain,root_domain,page_title,last_seen)
VALUES ('subdomain',$1,$2,$3,now()) RETURNING id`, r.domain, r.root, marker).Scan(r.id); err != nil {
t.Fatalf("insert %s: %v", r.domain, err)
}
}
t.Cleanup(func() {
cleanupExec(t, d, `DELETE FROM assets WHERE id=ANY($1::bigint[])`, []int64{inID, outID})
})
// The bare marker matches both rows; the second branch names the out-of-scope
// host. Only the in-scope row may come back.
got, err := assets.QueryDSLInScope(marker+" OR domain="+out, "", task.ID, 50, 0)
if err != nil {
t.Fatal(err)
}
var sawIn, sawOut bool
for _, a := range got {
switch a.ID {
case inID:
sawIn = true
case outID:
sawOut = true
}
}
if !sawIn {
t.Fatalf("in-scope asset %d missing from %+v", inID, got)
}
if sawOut {
t.Fatalf("out-of-scope asset %d leaked through an OR query", outID)
}
}
+4 -4
View File
@@ -15,12 +15,12 @@ import (
func TestArgvAllowKeepsDenyAndTargetGate(t *testing.T) {
ic := intercept.New(nil)
ic.SetRulesForTest([]db.InterceptRule{
{ID: 1, Name: "no force push", Enabled: true, Priority: 50, MatchTarget: "tool_input", MatchType: "string",
{ID: 1, Name: "no force flag", Enabled: true, Priority: 50, MatchTarget: "tool_input", MatchType: "string",
Pattern: "--force", Action: "deny", Message: "force push is forbidden"},
{ID: 2, Name: "argv allow curl", Enabled: true, Priority: -1000, MatchTarget: "tool_input", MatchType: "argv",
Pattern: `["curl"]`, Action: "allow"},
{ID: 3, Name: "argv allow git push", Enabled: true, Priority: -1000, MatchTarget: "tool_input", MatchType: "argv",
Pattern: `["git","push"]`, Action: "allow"},
{ID: 3, Name: "argv allow nmap", Enabled: true, Priority: -1000, MatchTarget: "tool_input", MatchType: "argv",
Pattern: `["nmap"]`, Action: "allow"},
})
g := NewWithInterceptor(ic)
gate := &fakeGate{t: t, check: unpinnedOnce, request: func(int, []intercept.TargetRequest, *intercept.Decision) intercept.TargetResult {
@@ -29,7 +29,7 @@ func TestArgvAllowKeepsDenyAndTargetGate(t *testing.T) {
g.gate = gate
ctx := gateOn(context.Background(), "7", time.Minute)
if block, msg := pre(g, ctx, "Bash", `{"command":"git push --force origin"}`); !block || !strings.Contains(msg, "force push is forbidden") {
if block, msg := pre(g, ctx, "Bash", `{"command":"nmap --force 10.0.0.1"}`); !block || !strings.Contains(msg, "force push is forbidden") {
t.Fatalf("deny over argv allow: block=%v msg=%q", block, msg)
}
if gate.checks != 0 {
+3 -1
View File
@@ -530,7 +530,9 @@ func TestGateOffLeavesEveryBranchUnchanged(t *testing.T) {
for name, ctx := range off {
before := len(g.rows())
// 1. A tool outside the enabled set: passes, nothing is written.
for _, c := range []struct{ tool, input string }{{"Monitor", curl(deny)}, {"mcp__scan__run", `{"target":"203.0.113.7","note":"` + deny + `"}`}} {
// Monitor and ExecuteExtraTool are in the default set now, so use read-only
// tools and an MCP name (inner names are not in the set).
for _, c := range []struct{ tool, input string }{{"list_assets", curl(deny)}, {"mcp__scan__run", `{"target":"203.0.113.7","note":"` + deny + `"}`}} {
if block, msg := pre(guard, ctx, c.tool, c.input); block {
t.Fatalf("%s: %s blocked: %s", name, c.tool, msg)
}
+2
View File
@@ -27,6 +27,8 @@ var alwaysAllowDenylist = func() map[string]bool {
// package managers and build tools: run package scripts and hooks
"npm npx pnpm yarn bunx corepack pip pipx uv uvx poetry conda composer gem bundle bundler rake cargo go " +
"mvn gradle dotnet apt apt-get dpkg dnf yum rpm pacman apk brew snap flatpak",
// version control and build generators: run hooks, aliases and script files
"git cmake ctest cpack",
// orchestration: run images, playbooks and plans
"docker docker-compose podman nerdctl kubectl helm ansible ansible-playbook terraform pulumi vagrant",
// destructive
+27 -7
View File
@@ -13,12 +13,13 @@ func TestArgvAllowDenylist(t *testing.T) {
"python", "python3", "python3.11", "/usr/bin/python3.11", "php8.2", "ruby3.2", "perl5", "pip3",
"env", "/usr/bin/env", "bash", "/bin/sh", "find", "rm", "mkfs", "mkfs.ext4", "/sbin/mkfs.xfs",
"docker", "kubectl", "npm", "sudo", "xargs", "Rscript", ".",
"git", "/usr/bin/git", "cmake", "cmake3", "/usr/bin/cmake", "ctest", "cpack",
} {
if !ArgvAllowDenied(prog) {
t.Errorf("%q is not denied", prog)
}
}
for _, prog := range []string{"nmap", "git", "/usr/bin/git", "curl", "ls", "gh", "x", "7z", "pythonic"} {
for _, prog := range []string{"nmap", "curl", "ls", "gh", "x", "7z", "pythonic"} {
if ArgvAllowDenied(prog) {
t.Errorf("%q is denied, want allowable", prog)
}
@@ -30,8 +31,8 @@ func TestAlwaysAllowPrefix(t *testing.T) {
b, _ := json.Marshal(map[string]string{"command": cmd})
return b
}
if got, err := AlwaysAllowPrefix("Bash", bash("git push --force origin main")); err != nil || !slices.Equal(got, []string{"git", "push"}) {
t.Fatalf("git push: %q %v", got, err)
if got, err := AlwaysAllowPrefix("Bash", bash("gh pr list --limit 5")); err != nil || !slices.Equal(got, []string{"gh", "pr", "list"}) {
t.Fatalf("gh pr list: %q %v", got, err)
}
if got, err := AlwaysAllowPrefix("Bash", bash("nmap -p 80 10.0.0.1")); err != nil || !slices.Equal(got, []string{"nmap"}) {
t.Fatalf("nmap: %q %v", got, err)
@@ -40,10 +41,10 @@ func TestAlwaysAllowPrefix(t *testing.T) {
tool string
input []byte
}{
"not Bash": {"shell_open", bash("git push")},
"compound": {"Bash", bash("git push; id")},
"substitution": {"Bash", bash("git push $(id)")},
"redirection": {"Bash", bash("git push > out")},
"not Bash": {"shell_open", bash("gh pr list")},
"compound": {"Bash", bash("gh pr list; id")},
"substitution": {"Bash", bash("gh pr list $(id)")},
"redirection": {"Bash", bash("gh pr list > out")},
"empty": {"Bash", bash("")},
"no command": {"Bash", []byte(`{}`)},
"not json": {"Bash", []byte(`not json`)},
@@ -52,6 +53,11 @@ func TestAlwaysAllowPrefix(t *testing.T) {
"find": {"Bash", bash("find . -exec sh -c id ;")},
"rm": {"Bash", bash("rm -rf /")},
"docker": {"Bash", bash("docker run --privileged x")},
"git": {"Bash", bash("git status")},
"path to git": {"Bash", bash("/usr/bin/git status")},
"git config": {"Bash", bash("git config user.name x")},
"cmake": {"Bash", bash("cmake --version")},
"ctest": {"Bash", bash("ctest -N")},
} {
if got, err := AlwaysAllowPrefix(c.tool, c.input); err == nil {
t.Errorf("%s: prefix %q, want a refusal", name, got)
@@ -59,6 +65,20 @@ func TestAlwaysAllowPrefix(t *testing.T) {
}
}
// The arity table is keyed by program name, so a path-qualified binary derives
// the same prefix length as the bare name.
func TestArgvPrefixLenUsesBasename(t *testing.T) {
if got := argvPrefixLen([]string{"/usr/bin/gh", "pr", "list"}); got != 3 {
t.Errorf("path-qualified gh prefix length = %d, want 3", got)
}
if got := argvPrefixLen([]string{"/usr/bin/git", "status"}); got != 2 {
t.Errorf("path-qualified git prefix length = %d, want 2", got)
}
if got, err := ArgvPrefix("/usr/bin/gh pr list"); err != nil || !slices.Equal(got, []string{"/usr/bin/gh", "pr", "list"}) {
t.Errorf("path-qualified gh prefix = %q, %v", got, err)
}
}
// The denylist neutralises argv allow rules at matching time (a rule created
// before the denylist, or written straight into the database), and only them:
// an argv deny or ask rule for the same program still matches.
+9 -1
View File
@@ -25,9 +25,17 @@ var argvArity = map[string]int{
}
// argvPrefixLen returns the number of leading tokens that form the prefix.
// The arity table is keyed by program name, so a path-qualified binary
// (/usr/bin/git) uses the same arity as the bare name.
func argvPrefixLen(tokens []string) int {
if len(tokens) == 0 {
return 0
}
key := make([]string, len(tokens))
copy(key, tokens)
key[0] = argvProgramName(tokens[0])
for n := len(tokens); n > 0; n-- {
if a, ok := argvArity[strings.Join(tokens[:n], " ")]; ok {
if a, ok := argvArity[strings.Join(key[:n], " ")]; ok {
return min(a, len(tokens))
}
}
File diff suppressed because it is too large Load Diff
+291
View File
@@ -0,0 +1,291 @@
package intercept
import (
"encoding/json"
"testing"
"github.com/Autumn-27/artex/db"
)
func bashInput(cmd string) []byte {
b, _ := json.Marshal(map[string]string{"command": cmd})
return b
}
func matchSingleArgvRule(action, pattern, toolName string, input []byte) (Decision, bool) {
ic := New(nil)
ic.SetRulesForTest([]db.InterceptRule{{
ID: 1,
Name: action + " " + pattern,
Enabled: true,
Priority: 100,
MatchTarget: "tool_input",
MatchType: "argv",
Pattern: pattern,
Action: action,
}})
return ic.Match(toolName, input)
}
func TestArgvDenyAndAskCompoundAndWrapped(t *testing.T) {
nmapShouldMatch := []struct {
name string
cmd string
}{
{"plain", "nmap -p 80 x"},
{"command wrapper", "command nmap -p 80 x"},
{"command double dash", "command -- nmap -p 80 x"},
{"command -p", "command -p nmap -p 80 x"},
{"path qualified", "/usr/bin/nmap -p 80 x"},
{"sudo", "sudo nmap -p 80 x"},
{"sudo with flags and assign", "sudo -u root -n FOO=1 nmap -p 80 x"},
{"doas", "doas -u root nmap -p 80 x"},
{"env with assign", "env FOO=1 nmap -p 80 x"},
{"env with flags", "env -i -u HOME -C /tmp -S arg FOO=1 nmap -p 80 x"},
{"env with expansion in assign", "env FOO=$(id) nmap -p 80 x"},
{"env -S separate", "env -S 'nmap -p 80 x'"},
{"env -S attached", "env -S'nmap -p 80 x'"},
{"env --split-string equals", "env --split-string='nmap -p 80 x'"},
{"env --split-string separate", "env --split-string 'nmap -p 80 x'"},
{"env -S with env flags", "env -S '-i FOO=1 nmap -p 80 x'"},
{"timeout", "timeout 30 nmap -p 80 x"},
{"timeout with flags", "timeout -k 5 -s KILL 30 nmap -p 80 x"},
{"nohup", "nohup nmap -p 80 x"},
{"setsid", "setsid -f nmap -p 80 x"},
{"stdbuf", "stdbuf -oL -eL nmap -p 80 x"},
{"nice -n", "nice -n 10 nmap -p 80 x"},
{"nice numeric flag", "nice -10 nmap -p 80 x"},
{"ionice", "ionice -c 3 nmap -p 80 x"},
{"taskset", "taskset -c 0-1 nmap -p 80 x"},
{"chrt", "chrt -f 50 nmap -p 80 x"},
{"watch", "watch -n 5 nmap -p 80 x"},
{"watch -d permanent", "watch -d permanent nmap -p 80 x"},
{"flock", "flock -n /tmp/lock nmap -p 80 x"},
{"flock -c after lockfile", "flock /tmp/lock -c 'nmap -p 80 x'"},
{"flock -c before lockfile", "flock -n -c 'nmap -p 80 x' /tmp/lock"},
{"strace", "strace -f -o /tmp/trace nmap -p 80 x"},
{"ltrace", "ltrace -f -o /tmp/ltrace nmap -p 80 x"},
{"xargs", "echo 10.0.0.1 | xargs -n 1 nmap -p 80"},
{"xargs -I", "xargs -I{} nmap -p 80 {}"},
{"xargs -l bare", "xargs -l nmap"},
{"xargs -l5 attached", "xargs -l5 nmap"},
{"xargs -l 5 separate", "xargs -l 5 nmap"},
{"find -exec", `find . -maxdepth 0 -exec nmap -p 80 x \;`},
{"find -execdir", `find . -execdir nmap -p 80 x \;`},
{"find -ok", `find . -ok nmap -p 80 x \;`},
{"find -okdir plus", `find . -okdir nmap -p 80 x +`},
{"find multiple -exec clauses", `find . -exec echo {} \; -exec nmap -p 80 {} \;`},
{"find -exec shell wrapper", `find . -exec sh -ec 'nmap -p 80 x' \;`},
{"parallel bare", "parallel nmap"},
{"parallel with jobs and :::", "parallel -j 4 nmap -p 80 {} ::: 10.0.0.1"},
{"parallel quoted shell string", "parallel -j 4 'nmap -p 80 {}' ::: 10.0.0.1"},
{"parallel no command ::: bare", "parallel ::: nmap"},
{"parallel no command ::: quoted line", `parallel ::: "nmap -p 80 x"`},
{"parallel no command -j2 ::: bare", "parallel -j2 ::: nmap"},
{"parallel no command ::: multiple lines", "parallel ::: echo nmap"},
{"parallel --link ::: nmap", "parallel --link ::: nmap"},
{"parallel --xargs ::: nmap", "parallel --xargs ::: nmap"},
{"parallel unknown no-arg long opt", "parallel --future-flag ::: nmap"},
{"parallel unknown value long opt", "parallel --future-val 42 nmap -p 80 x"},
{"sudo unknown flag over-approximated", "sudo --unknown-flag nmap -p 80 x"},
{"env unknown flag over-approximated", "env -Z nmap -p 80 x"},
{"busybox", "busybox nmap -p 80 x"},
{"busybox sh -c", "busybox sh -c 'nmap -p 80 x'"},
{"chroot", "chroot / nmap -p 80 x"},
{"chroot with userspec", "chroot --userspec=0:0 /mnt nmap -p 80 x"},
{"exec", "exec -- nmap -p 80 x"},
{"exec -a", "exec -a fake nmap -p 80 x"},
{"semicolon list", "nmap -p 80 x; true"},
{"and list", "true && nmap -p 80 x"},
{"or list", "false || nmap -p 80 x"},
{"pipeline", "nmap -p 80 x | tee out"},
{"subshell", "(nmap -p 80 x)"},
{"redirect", "nmap -p 80 x > out"},
{"background", "nmap -p 80 x &"},
{"prefix assign", "FOO=1 nmap -p 80 x"},
{"expansion in later arg", "nmap $(id)"},
{"param expansion in later arg", "nmap $TARGET"},
{"command subst inside echo", "echo $(nmap -p 80 x)"},
{"bash -c single quoted", "bash -c 'nmap -p 80 x'"},
{"bash -x -c", "bash -x -c 'nmap -p 80 x'"},
{"bash -lc", "bash -lc 'nmap -p 80 x'"},
{"bash -ec", "bash -ec 'nmap -p 80 x'"},
{"bash -o pipefail -c", "bash -o pipefail -c 'nmap -p 80 x'"},
{"bash +o pipefail -c", "bash +o pipefail -c 'nmap -p 80 x'"},
{"bash -O extglob -c", "bash -O extglob -c 'nmap -p 80 x'"},
{"bash --norc -c", "bash --norc -c 'nmap -p 80 x'"},
{"bash --noprofile --rcfile /dev/null -c --", "bash --noprofile --rcfile /dev/null -c -- 'nmap -p 80 x'"},
{"sh -c double quoted", `sh -c "nmap -p 80 x"`},
{"zsh -c", "zsh -c 'nmap -p 80 x'"},
{"dash -c", "dash -c 'nmap -p 80 x'"},
{"ksh -c", "ksh -c 'nmap -p 80 x'"},
{"fish -c", "fish -c 'nmap -p 80 x'"},
{"fish --command", "fish --command='nmap -p 80 x'"},
{"path qualified bash -c", "/bin/bash -c 'nmap -p 80 x'"},
{"eval quoted", "eval 'nmap -p 80 x'"},
{"eval separate args", "eval nmap -p 80 x"},
{"eval with trailing subst", "eval nmap $(id)"},
{"stacked wrappers", "sudo env FOO=1 timeout 30 nohup command /usr/bin/nmap -p 80 x"},
{"nested payload depth 2", `bash -c "sh -c 'nmap -p 80 x'"`},
{"nested payload depth 3", `bash -c "sh -c \"eval 'nmap -p 80 x'\""`},
{"bad nested payload does not block later call", `bash -c "if then"; nmap -p 80 x`},
}
for _, action := range []string{"deny", "ask"} {
for _, tc := range nmapShouldMatch {
t.Run(action+"/match/"+tc.name, func(t *testing.T) {
dec, ok := matchSingleArgvRule(action, `["nmap"]`, "Bash", bashInput(tc.cmd))
if !ok || dec.Action != action {
t.Errorf("%s [\"nmap\"] on %q: got (%+v, %v), want match with action %q", action, tc.cmd, dec, ok, action)
}
})
}
}
nmapShouldNotMatch := []struct {
name string
cmd string
}{
{"argument to echo", "echo nmap"},
{"prefix of other binary", "nmapx"},
{"argument in pipeline", "echo x | grep nmap"},
{"command -v lookup", "command -v nmap"},
{"command -V lookup", "command -V nmap"},
{"sudo -l list", "sudo -l nmap"},
{"sudo -e edit", "sudo -e nmap"},
{"env no command", "env FOO=1"},
{"ionice pid mode", "ionice -p 1234"},
{"taskset pid mode", "taskset -p 1234"},
{"chrt pid mode", "chrt -p 1234"},
{"xargs -l echo nmap does not match nmap", "xargs -l echo nmap"},
{"shell unknown option before -c", "bash --unknown-opt -c 'nmap -p 80 x'"},
{"shell opaque option before -c", "bash $OPT -c 'nmap -p 80 x'"},
{"shell script mode with -x", "bash -x nmap"},
{"shell script file", "bash nmap"},
{"shell -- before -c treats -c as script", "bash -- -c 'nmap -p 80 x'"},
{"find -exec echo nmap", `find . -exec echo nmap \;`},
{"parallel echo with nmap input", "parallel echo ::: nmap"},
{"parallel no command ::: echo nmap quoted", `parallel ::: "echo nmap"`},
{"parallel echo nmap ::: x", "parallel echo nmap ::: x"},
{"parallel --link echo nmap ::: x", "parallel --link echo nmap ::: x"},
{"parallel :::: file input", "parallel :::: nmap"},
{"parallel :::+ separator", "parallel :::+ nmap"},
{"chroot without command", "chroot /"},
{"busybox list mode", "busybox --list"},
{"eval opaque leading arg", "eval $CMD nmap"},
{"opaque command token", "$CMD -p 80 x"},
{"nested payload depth 4 exceeds limit", `bash -c "sh -c \"bash -c 'eval \\\"nmap -p 80 x\\\"'\""`},
{"unparsable single quote", "nmap 'unterminated"},
{"unparsable subst", "nmap $("},
{"empty command", ""},
}
for _, action := range []string{"deny", "ask"} {
for _, tc := range nmapShouldNotMatch {
t.Run(action+"/nomatch/"+tc.name, func(t *testing.T) {
if dec, ok := matchSingleArgvRule(action, `["nmap"]`, "Bash", bashInput(tc.cmd)); ok {
t.Errorf("%s [\"nmap\"] on %q unexpectedly matched: %+v", action, tc.cmd, dec)
}
})
}
}
// Non-Bash tool never matches argv rules.
if dec, ok := matchSingleArgvRule("deny", `["nmap"]`, "shell_open", bashInput("nmap -p 80 x")); ok {
t.Errorf("non-Bash tool matched deny rule: %+v", dec)
}
}
func TestArgvDenyMultiTokenAndPathRules(t *testing.T) {
cases := []struct {
name string
pattern string
cmd string
want bool
}{
{"git config path qualified with flag", `["git","config"]`, "/usr/bin/git config --list", true},
{"git config via command", `["git","config"]`, "command git config --list", true},
{"git config via sudo", `["git","config"]`, "sudo git config user.name foo", true},
{"git config opaque subcommand", `["git","config"]`, "git $SUB --list", false},
{"git status does not match git config", `["git","config"]`, "git status", false},
{"exact path rule matches exact path", `["/usr/bin/nmap"]`, "/usr/bin/nmap -p 80 x", true},
{"exact path rule does not match bare name", `["/usr/bin/nmap"]`, "nmap -p 80 x", false},
{"exact path rule does not match other path", `["/usr/bin/nmap"]`, "/opt/bin/nmap -p 80 x", false},
{"builtin wrapper matches echo", `["echo"]`, "builtin -- echo hi", true},
{"xargs with no command does not invent echo", `["echo"]`, "xargs -n 1", false},
{"wrapper itself can still be denied", `["sudo"]`, "sudo nmap -p 80 x", true},
{"shell itself can still be denied", `["bash"]`, "bash -c 'echo hi'", true},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
_, got := matchSingleArgvRule("deny", tc.pattern, "Bash", bashInput(tc.cmd))
if got != tc.want {
t.Errorf("deny %s on %q = %v, want %v", tc.pattern, tc.cmd, got, tc.want)
}
})
}
}
func TestArgvAllowRemainsStrict(t *testing.T) {
cases := []struct {
cmd string
want bool
}{
{"nmap -p 80 x", true},
{"command nmap x", false},
{"nmap x; true", false},
{"bash -c 'nmap x'", false},
{"/usr/bin/nmap x", false},
{"sudo nmap x", false},
{"env FOO=1 nmap x", false},
{"nmap $(id)", false},
}
for _, tc := range cases {
t.Run(tc.cmd, func(t *testing.T) {
_, got := matchSingleArgvRule("allow", `["nmap"]`, "Bash", bashInput(tc.cmd))
if got != tc.want {
t.Errorf("allow [\"nmap\"] on %q = %v, want %v", tc.cmd, got, tc.want)
}
})
}
}
func TestArgvCompoundMalformedInputDoesNotPanic(t *testing.T) {
inputs := [][]byte{
nil,
[]byte(``),
[]byte(`not json`),
[]byte(`{}`),
bashInput(`nmap 'unterminated`),
bashInput(`nmap $(`),
bashInput(`if then fi; (( (`),
bashInput(`bash -c "unterminated`),
bashInput(`eval "((("`),
}
for _, action := range []string{"deny", "ask", "allow"} {
for _, in := range inputs {
if dec, ok := matchSingleArgvRule(action, `["nmap"]`, "Bash", in); ok {
t.Errorf("%s matched malformed input %q: %+v", action, string(in), dec)
}
}
}
}
func TestArgvPriorityPreservedWithCompoundDeny(t *testing.T) {
ic := New(nil)
ic.SetRulesForTest([]db.InterceptRule{
{ID: 1, Name: "allow simple nmap", Enabled: true, Priority: 100, MatchTarget: "tool_input", MatchType: "argv", Pattern: `["nmap"]`, Action: "allow"},
{ID: 2, Name: "deny compound nmap", Enabled: true, Priority: 50, MatchTarget: "tool_input", MatchType: "argv", Pattern: `["nmap"]`, Action: "deny"},
})
// Simple command hits the higher-priority allow rule.
dec, ok := ic.Match("Bash", bashInput("nmap -p 80 x"))
if !ok || dec.Action != "allow" || dec.RuleID != 1 {
t.Fatalf("simple nmap: got (%+v, %v), want allow rule 1", dec, ok)
}
// Compound command fails the strict allow rule and falls through to the deny rule.
dec, ok = ic.Match("Bash", bashInput("nmap -p 80 x; true"))
if !ok || dec.Action != "deny" || dec.RuleID != 2 {
t.Fatalf("compound nmap: got (%+v, %v), want deny rule 2", dec, ok)
}
}
+8 -8
View File
@@ -214,20 +214,20 @@ func TestParseArgvPattern(t *testing.T) {
func TestArgvAllowBelowDeny(t *testing.T) {
ic := New(nil)
ic.SetRulesForTest([]db.InterceptRule{
{ID: 1, Name: "no force push", Enabled: true, Priority: 50, MatchTarget: "tool_input", MatchType: "regex",
{ID: 1, Name: "no force flag", Enabled: true, Priority: 50, MatchTarget: "tool_input", MatchType: "regex",
Pattern: `--force`, Action: "deny"},
{ID: 2, Name: "argv allow git push", Enabled: true, Priority: -1000, MatchTarget: "tool_input", MatchType: "argv",
Pattern: `["git","push"]`, Action: "allow"},
{ID: 2, Name: "argv allow nmap", Enabled: true, Priority: -1000, MatchTarget: "tool_input", MatchType: "argv",
Pattern: `["nmap"]`, Action: "allow"},
})
dec, ok := ic.Match("Bash", []byte(`{"command":"git push --force"}`))
dec, ok := ic.Match("Bash", []byte(`{"command":"nmap --force 10.0.0.1"}`))
if !ok || dec.Action != "deny" || dec.RuleID != 1 {
t.Fatalf("force push: %+v %v, want the deny rule", dec, ok)
t.Fatalf("force flag: %+v %v, want the deny rule", dec, ok)
}
dec, ok = ic.Match("Bash", []byte(`{"command":"git push origin"}`))
dec, ok = ic.Match("Bash", []byte(`{"command":"nmap 10.0.0.1"}`))
if !ok || dec.Action != "allow" || dec.RuleID != 2 {
t.Fatalf("push: %+v %v, want the argv allow", dec, ok)
t.Fatalf("nmap: %+v %v, want the argv allow", dec, ok)
}
if _, ok := ic.Match("Bash", []byte(`{"command":"git push; id"}`)); ok {
if _, ok := ic.Match("Bash", []byte(`{"command":"nmap 10.0.0.1; id"}`)); ok {
t.Fatal("a compound command matched the argv rule")
}
if _, ok := compileRule(db.InterceptRule{MatchType: "argv", Pattern: `["-x"]`}); ok {
+48
View File
@@ -0,0 +1,48 @@
package intercept
import "testing"
// TestDefaultEnabledToolsCoverExecutors pins the tools that must be gated when no
// intercept_enabled_tools setting has been saved. ExecuteExtraTool and Monitor
// dispatch MCP / deferred tools and background commands, so leaving them out let
// those calls bypass every rule.
func TestDefaultEnabledToolsCoverExecutors(t *testing.T) {
got := map[string]bool{}
for _, name := range defaultEnabledTools {
got[name] = true
}
for _, want := range []string{"Bash", "Write", "Edit", "MultiEdit", "ExecuteExtraTool", "Monitor"} {
if !got[want] {
t.Errorf("defaultEnabledTools must include %q", want)
}
}
for _, readOnly := range []string{"list_assets", "traffic_search", "Read"} {
if got[readOnly] {
t.Errorf("defaultEnabledTools must not gate the read-only tool %q by default", readOnly)
}
}
}
// TestEnabledToolsFromSettingNeverDisablesTheGate: an unset or malformed setting
// must keep the defaults, not turn the rule system off.
func TestEnabledToolsFromSettingNeverDisablesTheGate(t *testing.T) {
for _, tc := range []struct {
name string
val string
ok bool
}{
{"unset", "", false},
{"malformed", "{not json", true},
{"wrong shape", `"Bash"`, true},
} {
got := enabledToolsFromSetting(tc.val, tc.ok)
if !got["Bash"] || !got["ExecuteExtraTool"] || !got["Monitor"] {
t.Errorf("%s: enabled tools %v, want the defaults", tc.name, got)
}
}
// A valid explicit list replaces the defaults.
got := enabledToolsFromSetting(`["Bash"]`, true)
if !got["Bash"] || got["WebFetch"] {
t.Fatalf("explicit list: %v, want only Bash", got)
}
}
+41 -24
View File
@@ -266,6 +266,10 @@ var defaultEnabledTools = []string{
"Bash", "WebFetch", "web_search",
"shell_open", "shell_send",
"Write", "Edit", "MultiEdit",
// ExecuteExtraTool dispatches MCP and deferred tools, and Monitor starts a
// background command; both execute third-party or shell commands, so they
// belong in the default gate with the rest.
"ExecuteExtraTool", "Monitor",
}
// New creates an Interceptor backed by d. The rule cache is lazy-loaded on
@@ -305,27 +309,32 @@ func (i *Interceptor) loadLocked() error {
// Load enabled-tools set from settings, falling back to hard-coded defaults.
val, ok, _ := i.db.GetSetting("intercept_enabled_tools")
if !ok {
m := make(map[string]bool, len(defaultEnabledTools))
for _, n := range defaultEnabledTools {
m[n] = true
}
i.enabledTools = m
} else {
var names []string
if json.Unmarshal([]byte(val), &names) != nil {
i.enabledTools = map[string]bool{}
} else {
m := make(map[string]bool, len(names))
for _, n := range names {
m[n] = true
}
i.enabledTools = m
}
}
i.enabledTools = enabledToolsFromSetting(val, ok)
return nil
}
// enabledToolsFromSetting maps the stored intercept_enabled_tools value to the
// gate's set. A malformed value must not silently disable the gate, so it falls
// back to the same defaults as an unset setting.
func enabledToolsFromSetting(val string, ok bool) map[string]bool {
m := make(map[string]bool, len(defaultEnabledTools))
for _, n := range defaultEnabledTools {
m[n] = true
}
if !ok {
return m
}
var names []string
if json.Unmarshal([]byte(val), &names) != nil {
return m
}
m = make(map[string]bool, len(names))
for _, n := range names {
m[n] = true
}
return m
}
func (i *Interceptor) rules() ([]compiledRule, error) {
i.mu.RLock()
if i.cached != nil {
@@ -381,7 +390,7 @@ func (i *Interceptor) GetEnabledTools() ([]string, error) {
}
var names []string
if err := json.Unmarshal([]byte(val), &names); err != nil {
return []string{}, nil
return nil, fmt.Errorf("intercept_enabled_tools is not valid JSON: %w", err)
}
return names, nil
}
@@ -765,12 +774,20 @@ func (i *Interceptor) SetRulesForTest(rules []db.InterceptRule) {
func ruleMatches(r compiledRule, toolName string, input []byte) bool {
if r.MatchType == "argv" {
// Bash only, on the parsed command; match_target is always tool_input.
// The denylist neutralises allow rules only: an argv deny or ask rule
// for a denylisted program still matches.
if r.Action == "allow" && CheckArgvAllow(r.argv) != nil {
return false
// Allow rules stay strictly limited to a single simple command with
// exact derived prefix and denylist enforcement. Deny and ask rules
// walk every call in a compound Bash command, stripping known wrappers
// and recursing into eval / shell -c payloads.
if r.Action == "allow" {
if CheckArgvAllow(r.argv) != nil {
return false
}
return r.MatchTarget == "tool_input" && argvRuleMatches(r.argv, toolName, input)
}
return r.MatchTarget == "tool_input" && argvRuleMatches(r.argv, toolName, input)
if r.Action == "deny" || r.Action == "ask" {
return r.MatchTarget == "tool_input" && argvCompoundMatches(r.argv, toolName, input)
}
return false
}
var subject string
switch r.MatchTarget {
+13 -2
View File
@@ -165,6 +165,18 @@ func (i *Interceptor) loadExemptions() (exemptions, error) {
return ex, nil
}
// IsNonPublicAddr reports whether addr is private (RFC1918, fc00::/7 unique
// local), loopback, link-local (including 169.254.169.254 and fe80::/10),
// unspecified, multicast, or a known instance-metadata address.
// Invalid addresses are treated as non-public (fail closed).
func IsNonPublicAddr(addr netip.Addr) bool {
if !addr.IsValid() {
return true
}
addr = addr.Unmap().WithZone("")
return addr.IsPrivate() || ValidateExemptionHost(addr.String()) != nil
}
// voidsExemption reports whether one resolved address voids a stored
// exemption on a name: a private address (10/8, 172.16/12, 192.168/16, the
// unique local range fc00::/7), or one that is never exempt as a literal
@@ -175,6 +187,5 @@ func voidsExemption(ip string) bool {
if err != nil {
return true
}
a = a.Unmap().WithZone("")
return a.IsPrivate() || ValidateExemptionHost(a.String()) != nil
return IsNonPublicAddr(a)
}
+87 -11
View File
@@ -17,6 +17,7 @@ import (
"errors"
"fmt"
"io"
"net"
"net/http"
"net/url"
"strings"
@@ -101,6 +102,66 @@ func normalizeHeaders(headers map[string]string) (map[string]string, error) {
return out, nil
}
// originKey returns the scheme, host and effective port of u, in a form two
// URLs can be compared with. Empty means u is not an absolute http(s) URL with
// a host.
func originKey(u *url.URL) string {
if u == nil {
return ""
}
scheme := strings.ToLower(u.Scheme)
var port string
switch scheme {
case "http":
port = "80"
case "https":
port = "443"
default:
return ""
}
host := strings.ToLower(u.Hostname())
if host == "" {
return ""
}
if p := u.Port(); p != "" {
port = p
}
return scheme + "://" + net.JoinHostPort(host, port)
}
// sameOrigin reports whether u has the same scheme, host and effective port as
// base.
func sameOrigin(base, u *url.URL) bool {
key := originKey(base)
return key != "" && key == originKey(u)
}
// redirectPolicy refuses a redirect that leaves the origin of the configured
// endpoint, so a remote MCP server cannot retarget authenticated requests.
func redirectPolicy(base *url.URL) func(*http.Request, []*http.Request) error {
return func(req *http.Request, via []*http.Request) error {
if len(via) >= 10 {
return errors.New("mcp: stopped after 10 redirects")
}
if !sameOrigin(base, req.URL) {
return fmt.Errorf("mcp: refusing a redirect to another origin: %s", req.URL.Redacted())
}
return nil
}
}
// parseMCPURL parses and validates a configured MCP endpoint URL.
func parseMCPURL(raw string) (*url.URL, error) {
u, err := url.Parse(raw)
if err != nil {
return nil, fmt.Errorf("mcp url: %w", err)
}
if originKey(u) == "" {
return nil, fmt.Errorf("mcp url must be an absolute http or https URL with a host: %q", raw)
}
return u, nil
}
// New connects to a remote MCP endpoint and performs the initialize handshake.
// headers are sent on every request (Authorization, custom API keys, …).
// When insecure is true, TLS certificate verification is skipped so servers that
@@ -117,7 +178,11 @@ func New(ctx context.Context, server, url string, headers map[string]string, ins
if timeoutSeconds > 0 {
timeout = time.Duration(timeoutSeconds) * time.Second
}
hc := &http.Client{Timeout: timeout}
base, err := parseMCPURL(url)
if err != nil {
return nil, err
}
hc := &http.Client{Timeout: timeout, CheckRedirect: redirectPolicy(base)}
if insecure {
hc.Transport = &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}
}
@@ -142,7 +207,11 @@ func NewSSE(ctx context.Context, server, sseURL string, headers map[string]strin
if err != nil {
return nil, err
}
hc := &http.Client{} // the SSE stream is intentionally long-lived.
base, err := parseMCPURL(sseURL)
if err != nil {
return nil, err
}
hc := &http.Client{CheckRedirect: redirectPolicy(base)} // the SSE stream is intentionally long-lived.
if insecure {
hc.Transport = &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}
}
@@ -167,7 +236,7 @@ func NewSSE(ctx context.Context, server, sseURL string, headers map[string]strin
return nil, fmt.Errorf("mcp sse http %d: %s", resp.StatusCode, readSnippet(resp.Body))
}
reader := bufio.NewReader(resp.Body)
endpoint, err := readSSEEndpoint(reader, sseURL)
endpoint, err := readSSEEndpoint(reader, base)
if err != nil {
resp.Body.Close()
cancel()
@@ -195,7 +264,11 @@ func NewSSE(ctx context.Context, server, sseURL string, headers map[string]strin
return c, nil
}
func readSSEEndpoint(r *bufio.Reader, base string) (string, error) {
// readSSEEndpoint reads the message endpoint announced by the SSE stream. The
// endpoint must stay on the origin of the configured SSE URL: the POSTs that
// follow carry the operator's headers, and a remote server must not be able to
// retarget them.
func readSSEEndpoint(r *bufio.Reader, base *url.URL) (string, error) {
for {
line, err := r.ReadString('\n')
if err != nil {
@@ -213,14 +286,17 @@ func readSSEEndpoint(r *bufio.Reader, base string) (string, error) {
if err != nil {
return "", fmt.Errorf("mcp sse endpoint URL: %w", err)
}
if !u.IsAbs() {
b, err := url.Parse(base)
if err != nil {
return "", err
}
candidate = b.ResolveReference(u).String()
if u.User != nil {
return "", errors.New("mcp sse endpoint must not carry userinfo")
}
return candidate, nil
abs := u
if !u.IsAbs() {
abs = base.ResolveReference(u)
}
if !sameOrigin(base, abs) {
return "", fmt.Errorf("mcp sse endpoint %q is not on the configured origin", abs.Redacted())
}
return abs.String(), nil
}
}
+133
View File
@@ -1,11 +1,13 @@
package mcphttp
import (
"bufio"
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"sync"
"testing"
@@ -105,3 +107,134 @@ func TestLegacySSEClientDiscoverAndCall(t *testing.T) {
t.Fatalf("Call() = %q, %v", got, err)
}
}
func TestReadSSEEndpointSameOrigin(t *testing.T) {
base, err := url.Parse("https://mcp.example.com:8443/sse")
if err != nil {
t.Fatal(err)
}
read := func(line string) (string, error) {
stream := "event: endpoint\ndata: " + line + "\n\n"
return readSSEEndpoint(bufio.NewReader(strings.NewReader(stream)), base)
}
for _, line := range []string{
"/message?sessionId=1",
"https://mcp.example.com:8443/message",
"HTTPS://MCP.EXAMPLE.COM:8443/message",
"//mcp.example.com:8443/message",
} {
got, err := read(line)
if err != nil {
t.Errorf("%q: %v", line, err)
continue
}
u, err := url.Parse(got)
if err != nil || !sameOrigin(base, u) {
t.Errorf("%q: resolved to %q, not the configured origin", line, got)
}
}
for _, line := range []string{
"https://evil.example/message",
"http://mcp.example.com:8443/message",
"https://mcp.example.com:444/message",
"//evil.example/message",
"http://user:pass@mcp.example.com:8443/message",
"ftp://mcp.example.com/message",
} {
if got, err := read(line); err == nil {
t.Errorf("%q: accepted as %q, want a refusal", line, got)
}
}
}
func TestReadSSEEndpointDefaultPort(t *testing.T) {
base, err := url.Parse("https://mcp.example.com/sse")
if err != nil {
t.Fatal(err)
}
stream := "data: https://mcp.example.com:443/message\n\n"
if _, err := readSSEEndpoint(bufio.NewReader(strings.NewReader(stream)), base); err != nil {
t.Fatalf("explicit default port refused: %v", err)
}
}
func TestParseMCPURL(t *testing.T) {
for _, raw := range []string{"ftp://x/y", "not-a-url", "http:///nohost", ""} {
if _, err := parseMCPURL(raw); err == nil {
t.Errorf("%q accepted, want a refusal", raw)
}
}
for _, raw := range []string{"http://x/y", "https://x:8443/y"} {
if _, err := parseMCPURL(raw); err != nil {
t.Errorf("%q refused: %v", raw, err)
}
}
}
func TestLegacySSERefusesCrossOriginEndpoint(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")
fmt.Fprint(w, "event: endpoint\ndata: http://evil.invalid/message\n\n")
w.(http.Flusher).Flush()
<-r.Context().Done()
}))
defer srv.Close()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if _, err := NewSSE(ctx, "test", srv.URL+"/sse", nil, false); err == nil {
t.Fatal("cross-origin SSE endpoint accepted")
}
}
func TestMCPClientRefusesCrossOriginRedirect(t *testing.T) {
var hits int
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
hits++
}))
defer target.Close()
src := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, target.URL+"/steal", http.StatusFound)
}))
defer src.Close()
base, err := url.Parse(src.URL + "/sse")
if err != nil {
t.Fatal(err)
}
hc := &http.Client{CheckRedirect: redirectPolicy(base)}
resp, err := hc.Get(src.URL + "/sse")
if err == nil {
resp.Body.Close()
t.Fatal("cross-origin redirect accepted")
}
if hits != 0 {
t.Fatalf("redirect target was reached %d times", hits)
}
}
func TestMCPClientAllowsSameOriginRedirect(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/sse", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/sse2", http.StatusFound)
})
mux.HandleFunc("/sse2", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
srv := httptest.NewServer(mux)
defer srv.Close()
base, err := url.Parse(srv.URL + "/sse")
if err != nil {
t.Fatal(err)
}
hc := &http.Client{CheckRedirect: redirectPolicy(base)}
resp, err := hc.Get(srv.URL + "/sse")
if err != nil {
t.Fatalf("same-origin redirect refused: %v", err)
}
resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("status %d", resp.StatusCode)
}
}
+1 -1
View File
@@ -44,7 +44,7 @@ func wireAgentAugment(pg *db.DB, skillDir string, hostTools func() ([]actool.Cor
for _, n := range names {
nameSet[n] = true
}
if allReg, err := skill.LoadDir(skillDir); err == nil && allReg != nil {
if allReg, err := loadSkillsConfined(skillDir); err == nil && allReg != nil {
reg = skill.NewRegistry()
for _, s := range allReg.List() {
// match by directory name (Base of Dir), not by skill display Name
+23 -10
View File
@@ -1182,6 +1182,27 @@ func (e *Engine) runWorkerStep(ctx context.Context, t *Task, name string, worker
return e.runIntent(ctx, t, name, worker, intent, token, "", "")
}
// intentWorkContext resolves the cached per-task exec context and returns a
// per-intent child context with the rotated LLM profile pin attached (#85,
// #376). Attaching the pin after execContextFor ensures a cached e.execCtx
// does not drop it; attaching it after the paused/deleting check ensures a
// rolled-back claim never advances the rotation counter.
func (e *Engine) intentWorkContext(ctx context.Context, t *Task) (ectx, workCtx context.Context, workCancel context.CancelCauseFunc, ok bool) {
ectx = e.clockCtx(e.execContextFor(ctx, t.ID), t, false) // cancellable by Pause; carries the task deadline
if ectx.Err() != nil || e.IsDeleting(t.ID) {
return ectx, nil, nil, false
}
// per-work child context so the planner's kill_work can stop just this work.
workCtx, workCancel = context.WithCancelCause(ectx)
// Per-intent LLM rotation (#85): each claimed worker intent runs on the
// next chain head. The pin travels through the run context into the
// provider resolver; non-worker roles and failover keep the active head.
if head, pinned := t.nextRotatedProfile(); pinned {
workCtx = withLLMProfilePin(workCtx, head)
}
return ectx, workCtx, workCancel, true
}
// runIntent executes and fully settles one already-claimed (state=running) intent.
// Both the pool worker loop (via runWorkerStep) and the human-message handler (via
// runDetachedIntent, a dedicated goroutine outside the worker pool) call it, so the
@@ -1194,25 +1215,17 @@ func (e *Engine) runWorkerStep(ctx context.Context, t *Task, name string, worker
// while the worker is busy, and every intent state write below is fenced on it,
// so a worker whose claim was taken over cannot overwrite the new claimer.
func (e *Engine) runIntent(ctx context.Context, t *Task, name string, worker *agent.Worker, intent *db.Node, token, requestID, message string) bool {
// Per-intent LLM rotation (#85): each claimed worker intent runs on the
// next chain head. The pin travels through the run context into the
// provider resolver; non-worker roles and failover keep the active head.
if head, ok := t.nextRotatedProfile(); ok {
ctx = withLLMProfilePin(ctx, head)
}
hasChatMessage := message != ""
e.stampFirstRun(t) // first real execution → stamp first_run_at + compute the deadline (only for tasks with a timeout)
e.touch(t.ID)
emit := func(r db.Activity) { e.emitActivity(t, r) }
ectx := e.clockCtx(e.execContextFor(ctx, t.ID), t, false) // cancellable by Pause; carries the task deadline
if ectx.Err() != nil || e.IsDeleting(t.ID) {
ectx, workCtx, workCancel, ok := e.intentWorkContext(ctx, t)
if !ok {
if err := t.Store.SettleIntentClaim(intent.ID, token, "open"); err != nil {
log.Printf("[worker %s] task %s intent #%d claim rollback failed: %v", name, t.ID, intent.ID, err)
}
return true
}
// per-work child context so the planner's kill_work can stop just this work.
workCtx, workCancel := context.WithCancelCause(ectx)
e.registerWork(intent.ID, workCancel)
stopHeartbeat := startClaimHeartbeat(t.Store, db.IntentClaimHeartbeatInterval, name, intent.ID, token, workCancel)
// wrap the guard hooks so steer_work can inject a mid-run course-correction
+4 -4
View File
@@ -7,7 +7,7 @@ import (
)
func TestValidateArgvRuleReq(t *testing.T) {
base := interceptRuleReq{Name: "r", MatchTarget: "tool_input", MatchType: "argv", Pattern: `["git","push"]`, Action: "allow"}
base := interceptRuleReq{Name: "r", MatchTarget: "tool_input", MatchType: "argv", Pattern: `["nmap"]`, Action: "allow"}
if err := validateInterceptRuleReq(base); err != nil {
t.Fatalf("valid argv rule refused: %v", err)
}
@@ -15,7 +15,7 @@ func TestValidateArgvRuleReq(t *testing.T) {
"tool_name target": func(r *interceptRuleReq) { r.MatchTarget = "tool_name" },
"empty pattern": func(r *interceptRuleReq) { r.Pattern = "" },
"empty token": func(r *interceptRuleReq) { r.Pattern = `["git",""]` },
"flag token": func(r *interceptRuleReq) { r.Pattern = `["git","-C"]` },
"flag token": func(r *interceptRuleReq) { r.Pattern = `["nmap","-C"]` },
"metacharacter": func(r *interceptRuleReq) { r.Pattern = `["nmap;id"]` },
"nine tokens": func(r *interceptRuleReq) { r.Pattern = `["a","b","c","d","e","f","g","h","i"]` },
"not an array": func(r *interceptRuleReq) { r.Pattern = `git push` },
@@ -31,7 +31,7 @@ func TestValidateArgvRuleReq(t *testing.T) {
// The manual rule form refuses an argv allow rule for a denylisted program,
// with the message of the "always allow" endpoint; deny and ask stay creatable.
func TestValidateArgvAllowDenylist(t *testing.T) {
for _, pattern := range []string{`["python"]`, `["python3.11"]`, `["/usr/bin/env"]`, `["find"]`, `["docker","run"]`} {
for _, pattern := range []string{`["python"]`, `["python3.11"]`, `["/usr/bin/env"]`, `["find"]`, `["docker","run"]`, `["git","push"]`, `["cmake"]`} {
req := interceptRuleReq{Name: "r", MatchTarget: "tool_input", MatchType: "argv", Pattern: pattern, Action: "allow"}
tokens, _ := intercept.ParseArgvPattern(pattern)
want := intercept.CheckArgvAllow(tokens)
@@ -45,7 +45,7 @@ func TestValidateArgvAllowDenylist(t *testing.T) {
}
}
}
for _, pattern := range []string{`["nmap"]`, `["git","push"]`} {
for _, pattern := range []string{`["nmap"]`, `["curl"]`} {
req := interceptRuleReq{Name: "r", MatchTarget: "tool_input", MatchType: "argv", Pattern: pattern, Action: "allow"}
if err := validateInterceptRuleReq(req); err != nil {
t.Errorf("allow %s refused: %v", pattern, err)
+1
View File
@@ -130,6 +130,7 @@ func TestStoredProfileKeyOnlyForStoredEndpoint(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
s := New(ctx, m, t.TempDir(), t.TempDir(), t.TempDir())
s.allowLoopbackModels = true
s.cfgMu.Lock()
s.llmCfg = agent.Config{} // no global key to fall back on
s.cfgMu.Unlock()
+920
View File
@@ -0,0 +1,920 @@
package server
import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"math/big"
"net"
"net/http"
"net/http/httptest"
"net/netip"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/Autumn-27/artex/agent"
)
func dialTestServerForHosts(routes map[string]string, fallback func(ctx context.Context, network, addr string) (net.Conn, error)) func(ctx context.Context, network, addr string) (net.Conn, error) {
return func(ctx context.Context, network, addr string) (net.Conn, error) {
if target, ok := routes[strings.ToLower(addr)]; ok {
return (&net.Dialer{}).DialContext(ctx, network, target)
}
host, _, err := net.SplitHostPort(addr)
if err == nil {
if target, ok := routes[strings.ToLower(host)]; ok {
return (&net.Dialer{}).DialContext(ctx, network, target)
}
}
return fallback(ctx, network, addr)
}
}
// newTestTLSServer starts an HTTPS httptest server with a leaf certificate
// valid for dnsNames and returns the server and a CertPool trusting it.
func newTestTLSServer(t *testing.T, dnsNames []string, h http.Handler) (*httptest.Server, *x509.CertPool) {
t.Helper()
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
t.Fatal(err)
}
tmpl := &x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{CommonName: dnsNames[0]},
NotBefore: time.Now().Add(-time.Hour),
NotAfter: time.Now().Add(time.Hour),
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
DNSNames: dnsNames,
}
der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key)
if err != nil {
t.Fatal(err)
}
cert, err := x509.ParseCertificate(der)
if err != nil {
t.Fatal(err)
}
pool := x509.NewCertPool()
pool.AddCert(cert)
srv := httptest.NewUnstartedServer(h)
srv.TLS = &tls.Config{
Certificates: []tls.Certificate{{
Certificate: [][]byte{der},
PrivateKey: key,
}},
}
srv.StartTLS()
t.Cleanup(srv.Close)
return srv, pool
}
func TestLLMModelsRefusesLoopbackBeforeRequest(t *testing.T) {
t.Setenv(LLMPrivateEndpointsEnv, "")
var hits atomic.Int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
hits.Add(1)
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte("internal secret body"))
}))
t.Cleanup(srv.Close)
// Strict production default: no test seams set.
s := &Server{}
for _, tc := range []struct {
name string
provider string
apiKey string
}{
{"openai", "openai", "sk-test"},
{"anthropic", "anthropic", "sk-test"},
{"ollama_keyless", "ollama", ""},
} {
t.Run(tc.name, func(t *testing.T) {
resp := postJSON(t, s.pgListModels, "/api/llm/models", map[string]any{
"provider": tc.provider,
"base_url": srv.URL,
"api_key": tc.apiKey,
})
if hits.Load() != 0 {
t.Fatalf("loopback server was contacted (%d hits)", hits.Load())
}
if resp["ok"] != false {
t.Fatalf("expected ok=false, got %v", resp)
}
errMsg, _ := resp["error"].(string)
if errMsg == "" {
t.Fatalf("expected non-empty refusal error, got %v", resp)
}
if strings.Contains(errMsg, "127.0.0.1") || strings.Contains(errMsg, "internal secret") {
t.Fatalf("error leaked address or body: %q", errMsg)
}
})
}
}
func TestLLMModelsDefaultOllamaAllowedWithoutSeam(t *testing.T) {
t.Setenv(LLMPrivateEndpointsEnv, "")
// 1. Caller-supplied DefaultOllamaBaseURL ("http://127.0.0.1:11434/v1") must
// still be refused with no test seams and no allow-list entry.
bare := &Server{}
supplied := postJSON(t, bare.pgListModels, "/api/llm/models", map[string]any{
"provider": "ollama",
"base_url": agent.DefaultOllamaBaseURL,
"api_key": "",
})
if supplied["ok"] != false {
t.Fatalf("caller-supplied %s must be refused, got %v", agent.DefaultOllamaBaseURL, supplied)
}
if errMsg, _ := supplied["error"].(string); !strings.Contains(errMsg, "not allowed") || strings.Contains(errMsg, "127.0.0.1") {
t.Fatalf("unexpected refusal for caller-supplied ollama URL: %q", errMsg)
}
// 2. Empty base_url with provider="ollama" uses the code-owned default and
// must NOT be refused by URL validation or dial control even with no test seam.
var ollamaHits atomic.Int32
fakeOllama := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ollamaHits.Add(1)
if r.URL.Path != "/v1/models" {
w.WriteHeader(http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"data":[{"id":"llama3.2"}]}`))
}))
t.Cleanup(fakeOllama.Close)
// Verify the production guard allows dialing only 127.0.0.1:11434 (not other loopback ports).
s := &Server{}
s.modelDialContext = dialTestServerForHosts(
map[string]string{"127.0.0.1:11434": fakeOllama.Listener.Addr().String()},
(&net.Dialer{Control: newModelURLGuard(s).blockDial}).DialContext,
)
resp := postJSON(t, s.pgListModels, "/api/llm/models", map[string]any{
"provider": "ollama",
"base_url": "",
"api_key": "",
})
if ollamaHits.Load() != 1 {
t.Fatalf("expected default ollama endpoint to be contacted once, got %d", ollamaHits.Load())
}
if resp["ok"] != true {
t.Fatalf("expected ok=true for default ollama without allowLoopbackModels, got %v", resp)
}
rawModels, _ := resp["models"].([]any)
if len(rawModels) != 1 || rawModels[0] != "llama3.2" {
t.Fatalf("unexpected models: %v", rawModels)
}
// Also verify a completely bare Server (zero seams at all) does not refuse
// {"provider":"ollama","base_url":""} at validation or dial Control.
bareResp := postJSON(t, bare.pgListModels, "/api/llm/models", map[string]any{
"provider": "ollama",
"base_url": "",
"api_key": "",
})
if bareResp["ok"] == false {
errMsg, _ := bareResp["error"].(string)
if strings.Contains(errMsg, "not allowed") {
t.Fatalf("bare Server refused code-owned default Ollama URL: %q", errMsg)
}
}
}
func TestLLMModelsPrivateEndpointsAllowList(t *testing.T) {
var allowedHits atomic.Int32
allowedSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
allowedHits.Add(1)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"data":[{"id":"local-llama"}]}`))
}))
t.Cleanup(allowedSrv.Close)
var otherPortHits atomic.Int32
otherPortSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
otherPortHits.Add(1)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"data":[{"id":"wrong-port"}]}`))
}))
t.Cleanup(otherPortSrv.Close)
// 1. No allow-list entry -> refused with bare Server (zero test seams).
t.Setenv(LLMPrivateEndpointsEnv, "")
bare := &Server{}
noEntry := postJSON(t, bare.pgListModels, "/api/llm/models", map[string]any{
"provider": "ollama",
"base_url": allowedSrv.URL,
"api_key": "",
})
if allowedHits.Load() != 0 || noEntry["ok"] != false {
t.Fatalf("expected refusal with empty %s, hits=%d resp=%v", LLMPrivateEndpointsEnv, allowedHits.Load(), noEntry)
}
// 2. Allow-listed exact host:port works end-to-end with bare Server (real production dialer + guard).
t.Setenv(LLMPrivateEndpointsEnv, allowedSrv.Listener.Addr().String()+",host.docker.internal:11434")
okResp := postJSON(t, bare.pgListModels, "/api/llm/models", map[string]any{
"provider": "ollama",
"base_url": allowedSrv.URL,
"api_key": "",
})
if allowedHits.Load() != 1 || okResp["ok"] != true {
t.Fatalf("allow-listed endpoint should work, hits=%d resp=%v", allowedHits.Load(), okResp)
}
rawModels, _ := okResp["models"].([]any)
if len(rawModels) != 1 || rawModels[0] != "local-llama" {
t.Fatalf("unexpected models from allow-listed endpoint: %v", rawModels)
}
// 3. Same host on a different port stays refused.
diffPort := postJSON(t, bare.pgListModels, "/api/llm/models", map[string]any{
"provider": "ollama",
"base_url": otherPortSrv.URL,
"api_key": "",
})
if otherPortHits.Load() != 0 || diffPort["ok"] != false {
t.Fatalf("same host on another port must be refused, hits=%d resp=%v", otherPortHits.Load(), diffPort)
}
// 4. Allow-listed hostname resolving to RFC1918 works on its exact port,
// stays refused on another port, and still validates scheme & redirects.
var dockerHits atomic.Int32
dockerSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
dockerHits.Add(1)
if r.URL.Path == "/redirect/v1/models" {
http.Redirect(w, r, otherPortSrv.URL+"/secret", http.StatusFound)
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"data":[{"id":"docker-qwen"}]}`))
}))
t.Cleanup(dockerSrv.Close)
s := &Server{
modelLookupIP: func(_ context.Context, host string) ([]netip.Addr, error) {
if host == "host.docker.internal" {
return []netip.Addr{netip.MustParseAddr("192.168.65.254")}, nil
}
return nil, &net.DNSError{Err: "no such host", Name: host}
},
}
s.modelDialContext = dialTestServerForHosts(
map[string]string{"host.docker.internal:11434": dockerSrv.Listener.Addr().String()},
(&net.Dialer{Control: newModelURLGuard(s).blockDial}).DialContext,
)
dockerOK := postJSON(t, s.pgListModels, "/api/llm/models", map[string]any{
"provider": "ollama",
"base_url": "http://host.docker.internal:11434",
"api_key": "",
})
if dockerHits.Load() != 1 || dockerOK["ok"] != true {
t.Fatalf("allow-listed docker host should work, hits=%d resp=%v", dockerHits.Load(), dockerOK)
}
dockerWrongPort := postJSON(t, s.pgListModels, "/api/llm/models", map[string]any{
"provider": "ollama",
"base_url": "http://host.docker.internal:8080",
"api_key": "",
})
if dockerWrongPort["ok"] != false {
t.Fatalf("docker host on wrong port must be refused: %v", dockerWrongPort)
}
dockerBadScheme := postJSON(t, s.pgListModels, "/api/llm/models", map[string]any{
"provider": "ollama",
"base_url": "ftp://host.docker.internal:11434",
"api_key": "",
})
if dockerBadScheme["ok"] != false {
t.Fatalf("disallowed scheme on allow-listed host must be refused: %v", dockerBadScheme)
}
dockerBadRedirect := postJSON(t, s.pgListModels, "/api/llm/models", map[string]any{
"provider": "ollama",
"base_url": "http://host.docker.internal:11434/redirect",
"api_key": "",
})
if otherPortHits.Load() != 0 || dockerBadRedirect["ok"] != false {
t.Fatalf("redirect from allow-listed host to non-listed loopback port must be refused, hits=%d resp=%v", otherPortHits.Load(), dockerBadRedirect)
}
// 5. Cloud metadata remains refused even if mistakenly added to the env var.
t.Setenv(LLMPrivateEndpointsEnv, "169.254.169.254:80,[fd00:ec2::254]:80")
for _, metaURL := range []string{"http://169.254.169.254/latest/meta-data/", "http://[fd00:ec2::254]/latest/meta-data/"} {
metaResp := postJSON(t, bare.pgListModels, "/api/llm/models", map[string]any{
"provider": "openai",
"base_url": metaURL,
"api_key": "sk-test",
})
if metaResp["ok"] != false {
t.Fatalf("metadata URL %s must stay refused even if listed: %v", metaURL, metaResp)
}
}
}
func TestLLMModelsRefusesMetadataAndRestrictedAddresses(t *testing.T) {
t.Setenv(LLMPrivateEndpointsEnv, "")
s := &Server{}
for _, tc := range []struct {
name string
baseURL string
noLeak string
}{
{"aws_metadata", "http://169.254.169.254/latest/meta-data/", "169.254.169.254"},
{"rfc1918_10", "http://10.0.0.1/v1", "10.0.0.1"},
{"rfc1918_172", "http://172.16.0.1/v1", "172.16.0.1"},
{"rfc1918_192", "http://192.168.1.1/v1", "192.168.1.1"},
{"unique_local_v6", "http://[fc00::1]/v1", "fc00::1"},
{"unique_local_fd", "http://[fd12:3456::1]/v1", "fd12:3456::1"},
{"unspecified_v4", "http://0.0.0.0/v1", "0.0.0.0"},
{"unspecified_v6", "http://[::]/v1", "::"},
{"loopback_v6", "http://[::1]:8080/v1", "::1"},
{"link_local_v6", "http://[fe80::1]/v1", "fe80::1"},
{"v4_mapped_loopback", "http://[::ffff:127.0.0.1]/v1", "127.0.0.1"},
{"v4_mapped_metadata", "http://[::ffff:169.254.169.254]/v1", "169.254.169.254"},
{"aws_imds_v6", "http://[fd00:ec2::254]/latest/meta-data/", "fd00:ec2::254"},
{"alibaba_metadata", "http://100.100.100.200/latest/meta-data/", "100.100.100.200"},
{"decimal_loopback", "http://2130706433/v1", "127.0.0.1"},
{"hex_metadata", "http://0xa9.254.0xa9fe/latest/meta-data/", "169.254.169.254"},
{"localhost", "http://localhost:8080/v1", "127.0.0.1"},
} {
t.Run(tc.name, func(t *testing.T) {
resp := postJSON(t, s.pgListModels, "/api/llm/models", map[string]any{
"provider": "openai",
"base_url": tc.baseURL,
"api_key": "sk-test",
})
if resp["ok"] != false {
t.Fatalf("expected ok=false for %s, got %v", tc.baseURL, resp)
}
errMsg, _ := resp["error"].(string)
if errMsg == "" {
t.Fatalf("expected error message for %s", tc.baseURL)
}
if tc.noLeak != "" && strings.Contains(errMsg, tc.noLeak) {
t.Fatalf("error leaked address %q: %q", tc.noLeak, errMsg)
}
})
}
// Resolved DNS addresses must be checked and must never leak into the error.
for _, tc := range []struct {
name string
addrs []netip.Addr
noLeak string
}{
{"dns_private", []netip.Addr{netip.MustParseAddr("10.23.45.67")}, "10.23.45.67"},
{"dns_metadata", []netip.Addr{netip.MustParseAddr("169.254.169.254")}, "169.254.169.254"},
{"dns_unique_local", []netip.Addr{netip.MustParseAddr("fd00::42")}, "fd00::42"},
{"dns_mixed_public_and_loopback", []netip.Addr{netip.MustParseAddr("93.184.216.34"), netip.MustParseAddr("127.0.0.1")}, "127.0.0.1"},
} {
t.Run(tc.name, func(t *testing.T) {
rs := &Server{
modelLookupIP: func(_ context.Context, _ string) ([]netip.Addr, error) {
return tc.addrs, nil
},
}
resp := postJSON(t, rs.pgListModels, "/api/llm/models", map[string]any{
"provider": "openai",
"base_url": "http://internal.example/v1",
"api_key": "sk-test",
})
if resp["ok"] != false {
t.Fatalf("expected ok=false, got %v", resp)
}
errMsg, _ := resp["error"].(string)
if errMsg == "" || strings.Contains(errMsg, tc.noLeak) {
t.Fatalf("unexpected error %q (must be non-empty and not contain %q)", errMsg, tc.noLeak)
}
})
}
}
func TestLLMModelsRefusesDisallowedSchemes(t *testing.T) {
t.Setenv(LLMPrivateEndpointsEnv, "")
s := &Server{}
for _, raw := range []string{
"file:///etc/passwd",
"ftp://api.example.com/v1",
"gopher://api.example.com:70/1",
"javascript:alert(1)",
} {
resp := postJSON(t, s.pgListModels, "/api/llm/models", map[string]any{
"provider": "openai",
"base_url": raw,
"api_key": "sk-test",
})
if resp["ok"] != false {
t.Fatalf("scheme %q was not refused: %v", raw, resp)
}
if errMsg, _ := resp["error"].(string); !strings.Contains(errMsg, "http or https") {
t.Fatalf("scheme %q: expected http/https refusal, got %q", raw, errMsg)
}
}
}
func TestLLMModelsRefusesRestrictedProxyAndTargetThroughProxy(t *testing.T) {
t.Setenv(LLMPrivateEndpointsEnv, "")
var proxyHits atomic.Int32
proxySrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
proxyHits.Add(1)
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"data":[{"id":"proxied"}]}`))
}))
t.Cleanup(proxySrv.Close)
// 1. Loopback proxy is refused before any request is sent.
s := &Server{
modelLookupIP: func(_ context.Context, host string) ([]netip.Addr, error) {
if host == "api.public.example" {
return []netip.Addr{netip.MustParseAddr("93.184.216.34")}, nil
}
if host == "private-proxy.example" {
return []netip.Addr{netip.MustParseAddr("10.99.0.1")}, nil
}
return nil, &net.DNSError{Err: "no such host", Name: host}
},
}
for _, tc := range []struct {
name string
proxy string
noLeak string
}{
{"loopback_proxy", proxySrv.URL, "127.0.0.1"},
{"metadata_proxy", "http://169.254.169.254:80", "169.254.169.254"},
{"socks5_proxy", "socks5://api.public.example:1080", ""},
{"dns_private_proxy", "http://private-proxy.example:3128", "10.99.0.1"},
} {
t.Run(tc.name, func(t *testing.T) {
resp := postJSON(t, s.pgListModels, "/api/llm/models", map[string]any{
"provider": "openai",
"base_url": "http://api.public.example/v1",
"proxy": tc.proxy,
"api_key": "sk-test",
})
if proxyHits.Load() != 0 {
t.Fatalf("proxy was contacted (%d hits)", proxyHits.Load())
}
if resp["ok"] != false {
t.Fatalf("expected ok=false, got %v", resp)
}
errMsg, _ := resp["error"].(string)
if errMsg == "" || (tc.noLeak != "" && strings.Contains(errMsg, tc.noLeak)) {
t.Fatalf("unexpected error %q (must not leak %q)", errMsg, tc.noLeak)
}
})
}
// 2. Valid public proxy + refused target: target must be rejected before the proxy is contacted.
s2 := &Server{
modelLookupIP: func(_ context.Context, host string) ([]netip.Addr, error) {
if host == "proxy.public.example" {
return []netip.Addr{netip.MustParseAddr("93.184.216.35")}, nil
}
return nil, &net.DNSError{Err: "no such host", Name: host}
},
}
s2.modelDialContext = dialTestServerForHosts(
map[string]string{"proxy.public.example": proxySrv.Listener.Addr().String()},
(&net.Dialer{Control: newModelURLGuard(s2).blockDial}).DialContext,
)
resp := postJSON(t, s2.pgListModels, "/api/llm/models", map[string]any{
"provider": "openai",
"base_url": "http://169.254.169.254/latest/meta-data/",
"proxy": "http://proxy.public.example:8080",
"api_key": "sk-test",
})
if proxyHits.Load() != 0 {
t.Fatalf("proxy was contacted for refused target (%d hits)", proxyHits.Load())
}
if resp["ok"] != false {
t.Fatalf("expected ok=false, got %v", resp)
}
if errMsg, _ := resp["error"].(string); strings.Contains(errMsg, "169.254.169.254") {
t.Fatalf("error leaked metadata address: %q", errMsg)
}
// 3. Valid public proxy + public target that returns 302 to 127.0.0.1:
// the proxy must see the first request only, never the redirect target.
var proxyReqs []string
redirProxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
proxyReqs = append(proxyReqs, r.URL.String())
http.Redirect(w, r, "http://127.0.0.1:9999/secret", http.StatusFound)
}))
t.Cleanup(redirProxy.Close)
s3 := &Server{
modelLookupIP: func(_ context.Context, host string) ([]netip.Addr, error) {
switch host {
case "proxy.public.example":
return []netip.Addr{netip.MustParseAddr("93.184.216.35")}, nil
case "api.public.example":
return []netip.Addr{netip.MustParseAddr("93.184.216.34")}, nil
default:
return nil, &net.DNSError{Err: "no such host", Name: host}
}
},
}
s3.modelDialContext = dialTestServerForHosts(
map[string]string{"proxy.public.example": redirProxy.Listener.Addr().String()},
(&net.Dialer{Control: newModelURLGuard(s3).blockDial}).DialContext,
)
resp3 := postJSON(t, s3.pgListModels, "/api/llm/models", map[string]any{
"provider": "openai",
"base_url": "http://api.public.example/v1",
"proxy": "http://proxy.public.example:8080",
"api_key": "sk-test",
})
if len(proxyReqs) != 1 || proxyReqs[0] != "http://api.public.example/v1/models" {
t.Fatalf("proxy received unexpected requests: %v", proxyReqs)
}
if resp3["ok"] != false {
t.Fatalf("expected ok=false for proxied redirect to loopback, got %v", resp3)
}
if errMsg, _ := resp3["error"].(string); errMsg == "" || strings.Contains(errMsg, "127.0.0.1") {
t.Fatalf("unexpected error %q (must not leak 127.0.0.1)", errMsg)
}
}
func TestLLMModelsRefusesRedirectToLoopbackAndRestrictedHost(t *testing.T) {
t.Setenv(LLMPrivateEndpointsEnv, "")
var targetHits atomic.Int32
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
targetHits.Add(1)
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte("loopback secret"))
}))
t.Cleanup(target.Close)
for _, tc := range []struct {
name string
location string
noLeak string
}{
{"redirect_to_loopback", target.URL + "/secret", "127.0.0.1"},
{"redirect_to_metadata", "http://169.254.169.254/latest/meta-data/", "169.254.169.254"},
{"redirect_to_private_dns", "http://internal.example/secret", "10.44.55.66"},
} {
t.Run(tc.name, func(t *testing.T) {
targetHits.Store(0)
var redirHits atomic.Int32
redirector := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
redirHits.Add(1)
http.Redirect(w, r, tc.location, http.StatusFound)
}))
t.Cleanup(redirector.Close)
s := &Server{
modelLookupIP: func(_ context.Context, host string) ([]netip.Addr, error) {
switch host {
case "public.example":
return []netip.Addr{netip.MustParseAddr("93.184.216.34")}, nil
case "internal.example":
return []netip.Addr{netip.MustParseAddr("10.44.55.66")}, nil
default:
return nil, &net.DNSError{Err: "no such host", Name: host}
}
},
}
s.modelDialContext = dialTestServerForHosts(
map[string]string{"public.example": redirector.Listener.Addr().String()},
(&net.Dialer{Control: newModelURLGuard(s).blockDial}).DialContext,
)
resp := postJSON(t, s.pgListModels, "/api/llm/models", map[string]any{
"provider": "openai",
"base_url": "http://public.example/v1",
"api_key": "sk-test",
})
if redirHits.Load() != 1 {
t.Fatalf("expected redirector to be hit once, got %d", redirHits.Load())
}
if targetHits.Load() != 0 {
t.Fatalf("redirect target on loopback was contacted (%d hits)", targetHits.Load())
}
if resp["ok"] != false {
t.Fatalf("expected ok=false, got %v", resp)
}
errMsg, _ := resp["error"].(string)
if errMsg == "" {
t.Fatalf("expected refusal error, got %v", resp)
}
if strings.Contains(errMsg, tc.noLeak) || strings.Contains(errMsg, "loopback secret") {
t.Fatalf("error leaked address or body: %q", errMsg)
}
})
}
}
func TestLLMModelsRedirectSchemePolicyAndCap(t *testing.T) {
t.Setenv(LLMPrivateEndpointsEnv, "")
t.Run("http_to_https_same_host_allowed", func(t *testing.T) {
httpsSrv, pool := newTestTLSServer(t, []string{"public.example"}, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"data":[{"id":"upgraded-model"}]}`))
}))
httpSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "https://public.example/v1/models", http.StatusFound)
}))
t.Cleanup(httpSrv.Close)
s := &Server{
modelRootCAs: pool,
modelLookupIP: func(_ context.Context, host string) ([]netip.Addr, error) {
if host == "public.example" {
return []netip.Addr{netip.MustParseAddr("93.184.216.34")}, nil
}
return nil, &net.DNSError{Err: "no such host", Name: host}
},
}
s.modelDialContext = dialTestServerForHosts(
map[string]string{
"public.example:80": httpSrv.Listener.Addr().String(),
"public.example:443": httpsSrv.Listener.Addr().String(),
},
(&net.Dialer{Control: newModelURLGuard(s).blockDial}).DialContext,
)
resp := postJSON(t, s.pgListModels, "/api/llm/models", map[string]any{
"provider": "openai",
"base_url": "http://public.example/v1",
"api_key": "sk-test",
})
if resp["ok"] != true {
t.Fatalf("expected same-host http->https upgrade to succeed, got %v", resp)
}
rawModels, _ := resp["models"].([]any)
if len(rawModels) != 1 || rawModels[0] != "upgraded-model" {
t.Fatalf("unexpected models after upgrade: %v", rawModels)
}
})
t.Run("https_to_http_downgrade_refused", func(t *testing.T) {
var httpHits atomic.Int32
httpSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
httpHits.Add(1)
_, _ = w.Write([]byte(`{"data":[{"id":"downgraded"}]}`))
}))
t.Cleanup(httpSrv.Close)
httpsSrv, pool := newTestTLSServer(t, []string{"public.example"}, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "http://public.example/v1/models", http.StatusFound)
}))
s := &Server{
modelRootCAs: pool,
modelLookupIP: func(_ context.Context, host string) ([]netip.Addr, error) {
if host == "public.example" {
return []netip.Addr{netip.MustParseAddr("93.184.216.34")}, nil
}
return nil, &net.DNSError{Err: "no such host", Name: host}
},
}
s.modelDialContext = dialTestServerForHosts(
map[string]string{
"public.example:80": httpSrv.Listener.Addr().String(),
"public.example:443": httpsSrv.Listener.Addr().String(),
},
(&net.Dialer{Control: newModelURLGuard(s).blockDial}).DialContext,
)
resp := postJSON(t, s.pgListModels, "/api/llm/models", map[string]any{
"provider": "openai",
"base_url": "https://public.example/v1",
"api_key": "sk-test",
})
if httpHits.Load() != 0 {
t.Fatalf("downgraded HTTP endpoint was contacted (%d hits)", httpHits.Load())
}
if resp["ok"] != false {
t.Fatalf("expected ok=false on https->http downgrade, got %v", resp)
}
if errMsg, _ := resp["error"].(string); !strings.Contains(errMsg, "scheme") {
t.Fatalf("expected scheme refusal error, got %q", errMsg)
}
})
t.Run("http_to_https_cross_host_refused", func(t *testing.T) {
var otherHits atomic.Int32
httpsOther, pool := newTestTLSServer(t, []string{"other.public.example"}, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
otherHits.Add(1)
_, _ = w.Write([]byte(`{"data":[{"id":"other"}]}`))
}))
httpSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "https://other.public.example/v1/models", http.StatusFound)
}))
t.Cleanup(httpSrv.Close)
s := &Server{
modelRootCAs: pool,
modelLookupIP: func(_ context.Context, host string) ([]netip.Addr, error) {
switch host {
case "public.example":
return []netip.Addr{netip.MustParseAddr("93.184.216.34")}, nil
case "other.public.example":
return []netip.Addr{netip.MustParseAddr("93.184.216.35")}, nil
default:
return nil, &net.DNSError{Err: "no such host", Name: host}
}
},
}
s.modelDialContext = dialTestServerForHosts(
map[string]string{
"public.example:80": httpSrv.Listener.Addr().String(),
"other.public.example:443": httpsOther.Listener.Addr().String(),
},
(&net.Dialer{Control: newModelURLGuard(s).blockDial}).DialContext,
)
resp := postJSON(t, s.pgListModels, "/api/llm/models", map[string]any{
"provider": "openai",
"base_url": "http://public.example/v1",
"api_key": "sk-test",
})
if otherHits.Load() != 0 {
t.Fatalf("cross-host HTTPS redirect target was contacted (%d hits)", otherHits.Load())
}
if resp["ok"] != false {
t.Fatalf("expected ok=false on cross-host scheme change, got %v", resp)
}
if errMsg, _ := resp["error"].(string); !strings.Contains(errMsg, "scheme") {
t.Fatalf("expected scheme refusal error, got %q", errMsg)
}
})
t.Run("http_to_file_refused", func(t *testing.T) {
httpSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "file:///etc/passwd", http.StatusFound)
}))
t.Cleanup(httpSrv.Close)
s := &Server{
modelLookupIP: func(_ context.Context, host string) ([]netip.Addr, error) {
if host == "public.example" {
return []netip.Addr{netip.MustParseAddr("93.184.216.34")}, nil
}
return nil, &net.DNSError{Err: "no such host", Name: host}
},
}
s.modelDialContext = dialTestServerForHosts(
map[string]string{"public.example": httpSrv.Listener.Addr().String()},
(&net.Dialer{Control: newModelURLGuard(s).blockDial}).DialContext,
)
resp := postJSON(t, s.pgListModels, "/api/llm/models", map[string]any{
"provider": "openai",
"base_url": "http://public.example/v1",
"api_key": "sk-test",
})
if resp["ok"] != false {
t.Fatalf("expected ok=false on file:// redirect, got %v", resp)
}
if errMsg, _ := resp["error"].(string); !strings.Contains(errMsg, "scheme") {
t.Fatalf("expected scheme refusal error, got %q", errMsg)
}
})
t.Run("too_many_redirects", func(t *testing.T) {
var hops atomic.Int32
looper := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
n := hops.Add(1)
http.Redirect(w, r, "/v1/models?hop="+string(rune('0'+n)), http.StatusFound)
}))
t.Cleanup(looper.Close)
s := &Server{
modelLookupIP: func(_ context.Context, host string) ([]netip.Addr, error) {
if host == "public.example" {
return []netip.Addr{netip.MustParseAddr("93.184.216.34")}, nil
}
return nil, &net.DNSError{Err: "no such host", Name: host}
},
}
s.modelDialContext = dialTestServerForHosts(
map[string]string{"public.example": looper.Listener.Addr().String()},
(&net.Dialer{Control: newModelURLGuard(s).blockDial}).DialContext,
)
resp := postJSON(t, s.pgListModels, "/api/llm/models", map[string]any{
"provider": "openai",
"base_url": "http://public.example/v1",
"api_key": "sk-test",
})
if resp["ok"] != false {
t.Fatalf("expected ok=false on redirect loop, got %v", resp)
}
if errMsg, _ := resp["error"].(string); !strings.Contains(errMsg, "too many redirects") {
t.Fatalf("expected too many redirects error, got %q", errMsg)
}
if hops.Load() > int32(maxModelRedirects) {
t.Fatalf("followed %d redirects, cap is %d", hops.Load(), maxModelRedirects)
}
})
}
func TestLLMModelsChecksEveryCandidateURL(t *testing.T) {
t.Setenv(LLMPrivateEndpointsEnv, "")
var flip atomic.Bool
var seenPaths []string
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
seenPaths = append(seenPaths, r.URL.Path)
if r.URL.Path == "/anthropic/v1/models" {
// Flip DNS to a private address before candidate 2 is tried.
flip.Store(true)
w.WriteHeader(http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"data":[{"id":"should-not-reach"}]}`))
}))
t.Cleanup(upstream.Close)
s := &Server{
modelLookupIP: func(_ context.Context, host string) ([]netip.Addr, error) {
if host != "rebind.example" {
return nil, &net.DNSError{Err: "no such host", Name: host}
}
if flip.Load() {
return []netip.Addr{netip.MustParseAddr("10.77.88.99")}, nil
}
return []netip.Addr{netip.MustParseAddr("93.184.216.34")}, nil
},
}
s.modelDialContext = dialTestServerForHosts(
map[string]string{"rebind.example": upstream.Listener.Addr().String()},
(&net.Dialer{Control: newModelURLGuard(s).blockDial}).DialContext,
)
resp := postJSON(t, s.pgListModels, "/api/llm/models", map[string]any{
"provider": "anthropic",
"base_url": "http://rebind.example/anthropic",
"api_key": "sk-test",
})
if resp["ok"] != false {
t.Fatalf("expected ok=false when second candidate resolves to private IP, got %v", resp)
}
if len(seenPaths) != 1 || seenPaths[0] != "/anthropic/v1/models" {
t.Fatalf("expected only first candidate to be sent, got %v", seenPaths)
}
if errMsg, _ := resp["error"].(string); errMsg == "" || strings.Contains(errMsg, "10.77.88.99") {
t.Fatalf("unexpected error %q (must not leak resolved IP)", errMsg)
}
}
func TestLLMModelsPublicEndpointWorks(t *testing.T) {
t.Setenv(LLMPrivateEndpointsEnv, "")
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/v1/models" {
// Also verify a safe same-scheme redirect to a public host succeeds.
http.Redirect(w, r, "http://api.public.example/v2/models", http.StatusFound)
return
}
if r.URL.Path != "/v2/models" {
w.WriteHeader(http.StatusNotFound)
return
}
if r.Header.Get("Authorization") != "Bearer sk-public" {
w.WriteHeader(http.StatusUnauthorized)
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"data":[{"id":"gpt-4o"},{"id":"o3"}]}`))
}))
t.Cleanup(upstream.Close)
s := &Server{
modelLookupIP: func(_ context.Context, host string) ([]netip.Addr, error) {
if host == "api.public.example" {
return []netip.Addr{netip.MustParseAddr("93.184.216.34")}, nil
}
return nil, &net.DNSError{Err: "no such host", Name: host}
},
}
s.modelDialContext = dialTestServerForHosts(
map[string]string{"api.public.example": upstream.Listener.Addr().String()},
(&net.Dialer{Control: newModelURLGuard(s).blockDial}).DialContext,
)
resp := postJSON(t, s.pgListModels, "/api/llm/models", map[string]any{
"provider": "openai",
"base_url": "http://api.public.example/v1",
"api_key": "sk-public",
})
if resp["ok"] != true {
t.Fatalf("expected ok=true for normal public endpoint, got %v", resp)
}
rawModels, _ := resp["models"].([]any)
if len(rawModels) != 2 || rawModels[0] != "gpt-4o" || rawModels[1] != "o3" {
t.Fatalf("unexpected models: %v", rawModels)
}
}
+1
View File
@@ -202,6 +202,7 @@ func TestOllamaListModels(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
s := New(ctx, m, t.TempDir(), t.TempDir(), t.TempDir())
s.allowLoopbackModels = true
call := func(baseURL string) (bool, []string) {
body, _ := json.Marshal(map[string]any{"provider": "ollama", "base_url": baseURL, "api_key": ""})
+360
View File
@@ -0,0 +1,360 @@
package server
import (
"errors"
"os"
"path/filepath"
"strings"
"syscall"
"github.com/Autumn-27/norma/skill"
)
var errPathEscape = errors.New("invalid path")
// confinedPath holds the canonicalized result of resolving a user-supplied
// relative path inside a root directory.
type confinedPath struct {
realRoot string // canonical root directory after symlink evaluation
realPath string // resolved target path (deepest existing ancestor resolved + non-existent remainder)
entryPath string // resolved parent + final segment (differs from realPath when final segment is a symlink)
exists bool // whether the target itself exists on disk (and, if a symlink, its target exists)
}
// isWithinRoot reports whether target is equal to or strictly beneath realRoot.
// Both paths must be cleaned and canonicalized in the same coordinate space.
func isWithinRoot(realRoot, target string) bool {
realRoot = filepath.Clean(realRoot)
target = filepath.Clean(target)
if target == realRoot {
return true
}
rel, err := filepath.Rel(realRoot, target)
if err != nil {
return false
}
if rel == "." {
return true
}
if rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) || filepath.IsAbs(rel) {
return false
}
return true
}
// resolveRealRoot returns the cleaned, symlink-evaluated absolute path of root.
func resolveRealRoot(root string) (string, error) {
absRoot, err := filepath.Abs(root)
if err != nil {
return "", err
}
realRoot, err := filepath.EvalSymlinks(absRoot)
if err != nil {
return "", err
}
return filepath.Clean(realRoot), nil
}
// splitRelSegments validates and splits a relative path into individual safe
// path segments. It rejects NUL bytes, volume prefixes, ".." segments, and
// (unless trimLeadingSlash is true) leading separators.
func splitRelSegments(rel string, trimLeadingSlash bool) ([]string, error) {
if strings.ContainsRune(rel, 0) {
return nil, errPathEscape
}
rel = strings.TrimSpace(rel)
// Reject Windows drive-letter prefixes (e.g. "C:foo" or "C:/foo").
if filepath.VolumeName(rel) != "" {
return nil, errPathEscape
}
// Normalize backslashes so "..\foo" is caught uniformly across platforms.
slashed := strings.ReplaceAll(rel, "\\", "/")
if trimLeadingSlash {
slashed = strings.TrimLeft(slashed, "/")
} else if strings.HasPrefix(slashed, "/") {
return nil, errPathEscape
}
if slashed == "" || slashed == "." {
return nil, nil
}
rawSegs := strings.Split(slashed, "/")
segs := make([]string, 0, len(rawSegs))
for _, seg := range rawSegs {
if seg == "" || seg == "." {
continue
}
if seg == ".." {
return nil, errPathEscape
}
segs = append(segs, seg)
}
return segs, nil
}
func isNotDirErr(err error) bool {
return errors.Is(err, syscall.ENOTDIR)
}
// resolveWithinRoot resolves rel beneath root, walking each path component so
// that symlinks in any component are evaluated and verified to stay within root.
//
// For paths that do not exist yet (write, upload, mkdir), the deepest existing
// ancestor is resolved via Lstat + EvalSymlinks, then the remaining non-existent
// components are re-joined and checked against realRoot.
//
// Broken symlinks, symlink loops, ".." segments, and any symlink whose target
// escapes root return errPathEscape. Symlinks whose resolved target stays inside
// root are permitted.
func resolveWithinRoot(root, rel string, trimLeadingSlash bool) (confinedPath, error) {
realRoot, err := resolveRealRoot(root)
if err != nil {
return confinedPath{}, err
}
segs, err := splitRelSegments(rel, trimLeadingSlash)
if err != nil {
return confinedPath{}, err
}
if len(segs) == 0 {
return confinedPath{
realRoot: realRoot,
realPath: realRoot,
entryPath: realRoot,
exists: true,
}, nil
}
cur := realRoot
for i, seg := range segs {
candidate := filepath.Join(cur, seg)
fi, err := os.Lstat(candidate)
if err != nil {
if errors.Is(err, os.ErrNotExist) || isNotDirErr(err) {
// cur is the deepest existing ancestor (already verified within realRoot).
// Re-join the remaining non-existent segments and verify confinement.
rem := make([]string, 0, 1+len(segs)-i)
rem = append(rem, cur)
rem = append(rem, segs[i:]...)
full := filepath.Clean(filepath.Join(rem...))
if !isWithinRoot(realRoot, full) {
return confinedPath{}, errPathEscape
}
return confinedPath{
realRoot: realRoot,
realPath: full,
entryPath: full,
exists: false,
}, nil
}
return confinedPath{}, errPathEscape
}
if fi.Mode()&os.ModeSymlink != 0 {
target, evalErr := filepath.EvalSymlinks(candidate)
if evalErr != nil {
// Broken symlink, symlink loop, or unreadable symlink target.
return confinedPath{}, errPathEscape
}
target = filepath.Clean(target)
if !isWithinRoot(realRoot, target) {
return confinedPath{}, errPathEscape
}
if i == len(segs)-1 {
return confinedPath{
realRoot: realRoot,
realPath: target,
entryPath: candidate,
exists: true,
}, nil
}
cur = target
continue
}
cur = candidate
}
if !isWithinRoot(realRoot, cur) {
return confinedPath{}, errPathEscape
}
return confinedPath{
realRoot: realRoot,
realPath: cur,
entryPath: cur,
exists: true,
}, nil
}
// resolveSkillDir resolves <skillDir>/<name> and ensures it does not escape skillDir.
// If skillDir itself does not exist yet, it returns (confinedPath{exists: false}, nil)
// so read/delete handlers can return 404 rather than 400.
func resolveSkillDir(skillDir, name string) (confinedPath, error) {
cp, err := resolveWithinRoot(skillDir, name, false)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return confinedPath{exists: false}, nil
}
return confinedPath{}, errPathEscape
}
return cp, nil
}
// resolveSkillFile resolves <skillDir>/<name>/<relFile>, confining both the
// skill directory under skillDir and the file under that skill's directory.
func resolveSkillFile(skillDir, name, relFile string) (skillExists bool, cp confinedPath, err error) {
dirCP, err := resolveSkillDir(skillDir, name)
if err != nil {
return false, confinedPath{}, err
}
if !dirCP.exists {
return false, confinedPath{}, nil
}
fi, statErr := os.Stat(dirCP.realPath)
if statErr != nil || !fi.IsDir() {
return false, confinedPath{}, nil
}
fileCP, err := resolveWithinRoot(dirCP.realPath, relFile, false)
if err != nil {
return true, confinedPath{}, errPathEscape
}
return true, fileCP, nil
}
// loadSkillsConfined loads skills from dir using skill.LoadDir while refusing
// to follow symlinks whose real target leaves dir (or leaves its skill
// subdirectory for <dir>/<name>/SKILL.md).
func loadSkillsConfined(dir string) (*skill.Registry, error) {
realRoot, err := resolveRealRoot(dir)
if err != nil {
return nil, err
}
entries, err := os.ReadDir(realRoot)
if err != nil {
return nil, err
}
type safeEntry struct {
isDir bool
entryName string
sourcePath string
}
hasUnsafe := false
var safe []safeEntry
for _, e := range entries {
name := e.Name()
switch {
case e.IsDir():
// os.ReadDir reports e.IsDir() == false for symlinks, so <realRoot>/<name>
// is a real directory under realRoot. Verify its SKILL.md stays inside it.
skillRoot := filepath.Join(realRoot, name)
cp, rerr := resolveWithinRoot(skillRoot, "SKILL.md", false)
if rerr != nil {
hasUnsafe = true
continue
}
if !cp.exists {
continue
}
if fi, serr := os.Stat(cp.realPath); serr != nil || fi.IsDir() {
continue
}
safe = append(safe, safeEntry{
isDir: true,
entryName: name,
sourcePath: cp.realPath,
})
case strings.HasSuffix(name, ".md"):
cp, rerr := resolveWithinRoot(realRoot, name, false)
if rerr != nil {
hasUnsafe = true
continue
}
if !cp.exists {
continue
}
if fi, serr := os.Stat(cp.realPath); serr != nil || fi.IsDir() {
continue
}
safe = append(safe, safeEntry{
isDir: false,
entryName: name,
sourcePath: cp.realPath,
})
}
}
// Fast path: every skill file candidate in dir is safely confined.
if !hasUnsafe {
return skill.LoadDir(dir)
}
// Safe fallback: stage only verified files into a temporary directory so
// skill.LoadDir never opens or reads escaping/broken/looping symlinks, then
// restore each Skill.Dir to match dir.
if len(safe) == 0 {
return skill.NewRegistry(), nil
}
tmp, err := os.MkdirTemp("", "artex-skills-*")
if err != nil {
return nil, err
}
defer os.RemoveAll(tmp)
for _, item := range safe {
data, rerr := os.ReadFile(item.sourcePath)
if rerr != nil {
continue
}
if item.isDir {
sub := filepath.Join(tmp, item.entryName)
if err := os.MkdirAll(sub, 0o755); err != nil {
continue
}
_ = os.WriteFile(filepath.Join(sub, "SKILL.md"), data, 0o600)
} else {
_ = os.WriteFile(filepath.Join(tmp, item.entryName), data, 0o600)
}
}
loaded, err := skill.LoadDir(tmp)
if err != nil {
return nil, err
}
out := skill.NewRegistry()
for _, sk := range loaded.List() {
if sk.Dir == tmp {
sk.Dir = dir
} else if rel, relErr := filepath.Rel(tmp, sk.Dir); relErr == nil && rel != "." {
sk.Dir = filepath.Join(dir, rel)
}
out.Add(sk)
}
return out, nil
}
// safeFSErr formats a filesystem error without leaking absolute paths.
func safeFSErr(err error) string {
if err == nil {
return ""
}
var pe *os.PathError
if errors.As(err, &pe) && pe.Err != nil {
if pe.Op != "" {
return pe.Op + ": " + pe.Err.Error()
}
return pe.Err.Error()
}
var le *os.LinkError
if errors.As(err, &le) && le.Err != nil {
if le.Op != "" {
return le.Op + ": " + le.Err.Error()
}
return le.Err.Error()
}
var se *os.SyscallError
if errors.As(err, &se) && se.Err != nil {
return se.Err.Error()
}
return "filesystem error"
}
+22 -13
View File
@@ -98,12 +98,16 @@ func (s *Server) toolCreateSkill() actool.CoreTool {
if strings.TrimSpace(a.Description) == "" {
return actool.Errorf("description is required"), nil
}
path := filepath.Join(s.skillDir, a.Name)
if _, err := os.Stat(path); err == nil {
dirCP, err := resolveSkillDir(s.skillDir, a.Name)
if err != nil {
return actool.Errorf("invalid skill path"), nil
}
if dirCP.exists {
return actool.Errorf("skill already exists: " + a.Name), nil
}
path := filepath.Join(s.skillDir, a.Name)
if err := os.MkdirAll(path, 0o755); err != nil {
return actool.Errorf(err.Error()), nil
return actool.Errorf(safeFSErr(err)), nil
}
var b strings.Builder
b.WriteString("---\n")
@@ -117,7 +121,7 @@ func (s *Server) toolCreateSkill() actool.CoreTool {
}
if err := os.WriteFile(filepath.Join(path, "SKILL.md"), []byte(b.String()), 0o644); err != nil {
_ = os.RemoveAll(path)
return actool.Errorf(err.Error()), nil
return actool.Errorf(safeFSErr(err)), nil
}
return actool.Text("skill created: " + a.Name), nil
})
@@ -137,10 +141,6 @@ func (s *Server) toolUpdateSkillFile() actool.CoreTool {
if !validSkillName(a.Name) {
return actool.Errorf("invalid skill name"), nil
}
skillPath := filepath.Join(s.skillDir, a.Name)
if _, err := os.Stat(skillPath); os.IsNotExist(err) {
return actool.Errorf("skill does not exist: " + a.Name), nil
}
rel := strings.TrimSpace(a.File)
if rel == "" {
rel = "SKILL.md"
@@ -149,12 +149,21 @@ func (s *Server) toolUpdateSkillFile() actool.CoreTool {
if msg != "" {
return actool.Errorf("illegal path: " + msg), nil
}
full := filepath.Join(skillPath, clean)
if err := os.MkdirAll(filepath.Dir(full), 0o755); err != nil {
return actool.Errorf(err.Error()), nil
skillExists, cp, err := resolveSkillFile(s.skillDir, a.Name, clean)
if err != nil {
return actool.Errorf("illegal path: invalid path"), nil
}
if err := os.WriteFile(full, []byte(a.Content), 0o644); err != nil {
return actool.Errorf(err.Error()), nil
if !skillExists {
return actool.Errorf("skill does not exist: " + a.Name), nil
}
if cp.realPath == cp.realRoot {
return actool.Errorf("illegal path: invalid path"), nil
}
if err := os.MkdirAll(filepath.Dir(cp.realPath), 0o755); err != nil {
return actool.Errorf(safeFSErr(err)), nil
}
if err := os.WriteFile(cp.realPath, []byte(a.Content), 0o644); err != nil {
return actool.Errorf(safeFSErr(err)), nil
}
return actool.Text("skill file written: " + a.Name + "/" + clean), nil
})
+9
View File
@@ -2,6 +2,7 @@ package server
import (
"context"
"crypto/x509"
"encoding/base64"
"encoding/json"
"errors"
@@ -65,6 +66,14 @@ type Server struct {
authHashKey string
authVersionKey string
// Test-only seams for pgListModels SSRF checks. All remain zero/nil in
// production (strict public-only default; only exact host:port entries in
// ARTEX_LLM_PRIVATE_ENDPOINTS may resolve to private addresses).
allowLoopbackModels bool
modelLookupIP func(ctx context.Context, host string) ([]netip.Addr, error)
modelDialContext func(ctx context.Context, network, addr string) (net.Conn, error)
modelRootCAs *x509.CertPool
loginLimit *loginLimiter // per-IP attempt limit on POST /api/auth/login
pwdLimit *loginLimiter // attempt limit on POST /api/auth/change-password (IP + session token)
trustedProxies []netip.Prefix // ARTEX_TRUSTED_PROXIES; see clientIP
+427 -47
View File
@@ -4,13 +4,16 @@ import (
"archive/zip"
"bytes"
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"mime"
"net"
"net/http"
"net/netip"
"net/url"
"os"
"path"
@@ -18,6 +21,8 @@ import (
"regexp"
"strconv"
"strings"
"sync"
"syscall"
"text/template"
"text/template/parse"
"time"
@@ -26,6 +31,7 @@ import (
"github.com/Autumn-27/artex/agent"
"github.com/Autumn-27/artex/db"
"github.com/Autumn-27/artex/intercept"
"github.com/Autumn-27/artex/notify"
"github.com/Autumn-27/norma/llm"
"github.com/Autumn-27/norma/skill"
@@ -1156,7 +1162,7 @@ type skillFileNode struct {
func (s *Server) fsListSkills(w http.ResponseWriter, r *http.Request) {
_ = os.MkdirAll(s.skillDir, 0o755)
allReg, _ := skill.LoadDir(s.skillDir)
allReg, _ := loadSkillsConfined(s.skillDir)
metaByDir := map[string]skill.Skill{}
if allReg != nil {
for _, sk := range allReg.List() {
@@ -1167,7 +1173,7 @@ func (s *Server) fsListSkills(w http.ResponseWriter, r *http.Request) {
}
entries, err := os.ReadDir(s.skillDir)
if err != nil {
writeErr(w, 500, err.Error())
writeErr(w, 500, safeFSErr(err))
return
}
// Usage is best-effort decoration: a ledger read failure leaves the counts at
@@ -1275,13 +1281,18 @@ func (s *Server) fsCreateSkill(w http.ResponseWriter, r *http.Request) {
writeErr(w, 400, "description is required")
return
}
skillPath := filepath.Join(s.skillDir, body.Name)
if _, err := os.Stat(skillPath); err == nil {
dirCP, err := resolveSkillDir(s.skillDir, body.Name)
if err != nil {
writeErr(w, 400, "invalid path")
return
}
if dirCP.exists {
writeErr(w, 409, "skill already exists")
return
}
skillPath := filepath.Join(s.skillDir, body.Name)
if err := os.MkdirAll(skillPath, 0o755); err != nil {
writeErr(w, 500, err.Error())
writeErr(w, 500, safeFSErr(err))
return
}
// Build a spec-compliant SKILL.md (agentskills.io format):
@@ -1317,7 +1328,7 @@ func (s *Server) fsCreateSkill(w http.ResponseWriter, r *http.Request) {
}
if err := os.WriteFile(filepath.Join(skillPath, "SKILL.md"), []byte(sb.String()), 0o644); err != nil {
_ = os.RemoveAll(skillPath)
writeErr(w, 500, err.Error())
writeErr(w, 500, safeFSErr(err))
return
}
// Verify with the real parser: a description containing ": " (or any value that
@@ -1352,19 +1363,31 @@ func (s *Server) fsUpdateSkillMeta(w http.ResponseWriter, r *http.Request) {
writeErr(w, 400, err.Error())
return
}
skillMD := filepath.Join(s.skillDir, name, "SKILL.md")
raw, err := os.ReadFile(skillMD)
skillExists, cp, err := resolveSkillFile(s.skillDir, name, "SKILL.md")
if err != nil {
writeErr(w, 400, "invalid path")
return
}
if !skillExists || !cp.exists {
writeErr(w, 404, "skill not found")
return
}
raw, err := os.ReadFile(cp.realPath)
if err != nil {
if os.IsNotExist(err) {
writeErr(w, 404, "skill not found")
return
}
writeErr(w, 500, safeFSErr(err))
return
}
updated, err := rewriteSkillFrontmatter(raw, body.MCPs, body.Description, body.License, body.Compatibility)
if err != nil {
writeErr(w, 400, err.Error())
return
}
if err := os.WriteFile(skillMD, updated, 0o644); err != nil {
writeErr(w, 500, err.Error())
if err := os.WriteFile(cp.realPath, updated, 0o644); err != nil {
writeErr(w, 500, safeFSErr(err))
return
}
writeJSON(w, 200, map[string]any{"ok": true})
@@ -1383,9 +1406,9 @@ func yamlScalar(s string) string {
// verifySkillFile re-loads a written SKILL.md with the real skill parser and
// asserts the name and the mcps list survived the round trip.
func verifySkillFile(skillPath, wantName string, wantMCPs []string) error {
reg, err := skill.LoadDir(skillPath)
reg, err := loadSkillsConfined(skillPath)
if err != nil {
return fmt.Errorf("load: %w", err)
return fmt.Errorf("load: %s", safeFSErr(err))
}
for _, s := range reg.List() {
if s.Dir == "" {
@@ -1399,14 +1422,14 @@ func verifySkillFile(skillPath, wantName string, wantMCPs []string) error {
}
return nil
}
return fmt.Errorf("no skill loaded from %s", skillPath)
return fmt.Errorf("no skill loaded from %s", filepath.Base(skillPath))
}
// warnBrokenSkills logs skills whose parsed name does not match their directory
// name: the signature of frontmatter that failed to parse and silently fell back
// (fields empty, mcps gate lost). Best-effort, startup only.
func warnBrokenSkills(dir string) {
reg, err := skill.LoadDir(dir)
reg, err := loadSkillsConfined(dir)
if err != nil {
return
}
@@ -1601,9 +1624,14 @@ func (s *Server) fsUploadSkill(w http.ResponseWriter, r *http.Request) {
return
}
dirCP, err := resolveSkillDir(s.skillDir, name)
if err != nil {
writeErr(w, 400, "invalid path")
return
}
skillPath := filepath.Join(s.skillDir, name)
overwrite := r.URL.Query().Get("overwrite") == "true"
if _, err := os.Stat(skillPath); err == nil && !overwrite {
if dirCP.exists && !overwrite {
writeErr(w, 409, "skill already exists: "+name+" (retry after confirming if you want to overwrite)")
return
}
@@ -1708,13 +1736,20 @@ func (s *Server) fsDeleteSkill(w http.ResponseWriter, r *http.Request) {
writeErr(w, 400, "invalid skill name")
return
}
dirCP, err := resolveSkillDir(s.skillDir, name)
if err != nil {
writeErr(w, 400, "invalid path")
return
}
if err := pg.DeleteSkillVisibility(name); err != nil {
writeErr(w, 500, err.Error())
return
}
if err := os.RemoveAll(filepath.Join(s.skillDir, name)); err != nil {
writeErr(w, 500, err.Error())
return
if dirCP.exists {
if err := os.RemoveAll(dirCP.entryPath); err != nil {
writeErr(w, 500, safeFSErr(err))
return
}
}
writeJSON(w, 200, map[string]any{"deleted": name})
}
@@ -1816,14 +1851,23 @@ func (s *Server) fsListFiles(w http.ResponseWriter, r *http.Request) {
writeErr(w, 400, "invalid skill name")
return
}
dirPath := filepath.Join(s.skillDir, name)
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
dirCP, err := resolveSkillDir(s.skillDir, name)
if err != nil {
writeErr(w, 400, "invalid path")
return
}
if !dirCP.exists {
writeErr(w, 404, "skill not found")
return
}
files, err := walkSkillFiles(dirPath)
fi, err := os.Stat(dirCP.realPath)
if err != nil || !fi.IsDir() {
writeErr(w, 404, "skill not found")
return
}
files, err := walkSkillFiles(dirCP.realPath)
if err != nil {
writeErr(w, 500, err.Error())
writeErr(w, 500, safeFSErr(err))
return
}
if files == nil {
@@ -1843,13 +1887,22 @@ func (s *Server) fsReadFile(w http.ResponseWriter, r *http.Request) {
writeErr(w, 400, errMsg)
return
}
data, err := os.ReadFile(filepath.Join(s.skillDir, name, file))
skillExists, cp, err := resolveSkillFile(s.skillDir, name, file)
if err != nil {
writeErr(w, 400, "invalid path")
return
}
if !skillExists || !cp.exists {
writeErr(w, 404, "file not found")
return
}
data, err := os.ReadFile(cp.realPath)
if os.IsNotExist(err) {
writeErr(w, 404, "file not found")
return
}
if err != nil {
writeErr(w, 500, err.Error())
writeErr(w, 500, safeFSErr(err))
return
}
writeJSON(w, 200, map[string]any{"content": string(data), "file": file})
@@ -1873,19 +1926,26 @@ func (s *Server) fsWriteFile(w http.ResponseWriter, r *http.Request) {
writeErr(w, 400, err.Error())
return
}
skillPath := filepath.Join(s.skillDir, name)
if _, err := os.Stat(skillPath); os.IsNotExist(err) {
skillExists, cp, err := resolveSkillFile(s.skillDir, name, file)
if err != nil {
writeErr(w, 400, "invalid path")
return
}
if !skillExists {
writeErr(w, 404, "skill not found")
return
}
fullPath := filepath.Join(skillPath, file)
// create parent subdirectory if needed (e.g. scripts/, references/, assets/)
if err := os.MkdirAll(filepath.Dir(fullPath), 0o755); err != nil {
writeErr(w, 500, err.Error())
if cp.realPath == cp.realRoot {
writeErr(w, 400, "invalid path")
return
}
if err := os.WriteFile(fullPath, []byte(body.Content), 0o644); err != nil {
writeErr(w, 500, err.Error())
// create parent subdirectory if needed (e.g. scripts/, references/, assets/)
if err := os.MkdirAll(filepath.Dir(cp.realPath), 0o755); err != nil {
writeErr(w, 500, safeFSErr(err))
return
}
if err := os.WriteFile(cp.realPath, []byte(body.Content), 0o644); err != nil {
writeErr(w, 500, safeFSErr(err))
return
}
writeJSON(w, 200, map[string]any{"ok": true})
@@ -1909,13 +1969,21 @@ func (s *Server) fsCreateDir(w http.ResponseWriter, r *http.Request) {
writeErr(w, 400, errMsg)
return
}
skillPath := filepath.Join(s.skillDir, name)
if _, err := os.Stat(skillPath); os.IsNotExist(err) {
skillExists, cp, err := resolveSkillFile(s.skillDir, name, dir)
if err != nil {
writeErr(w, 400, "invalid path")
return
}
if !skillExists {
writeErr(w, 404, "skill not found")
return
}
if err := os.MkdirAll(filepath.Join(skillPath, dir), 0o755); err != nil {
writeErr(w, 500, err.Error())
if cp.realPath == cp.realRoot {
writeErr(w, 400, "invalid path")
return
}
if err := os.MkdirAll(cp.realPath, 0o755); err != nil {
writeErr(w, 500, safeFSErr(err))
return
}
writeJSON(w, 201, map[string]any{"dir": dir})
@@ -1932,13 +2000,21 @@ func (s *Server) fsDeletePath(w http.ResponseWriter, r *http.Request) {
writeErr(w, 400, errMsg)
return
}
fullPath := filepath.Join(s.skillDir, name, file)
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
skillExists, cp, err := resolveSkillFile(s.skillDir, name, file)
if err != nil {
writeErr(w, 400, "invalid path")
return
}
if !skillExists || !cp.exists {
writeErr(w, 404, "not found")
return
}
if err := os.RemoveAll(fullPath); err != nil {
writeErr(w, 500, err.Error())
if cp.realPath == cp.realRoot {
writeErr(w, 400, "invalid path")
return
}
if err := os.RemoveAll(cp.entryPath); err != nil {
writeErr(w, 500, safeFSErr(err))
return
}
writeJSON(w, 200, map[string]any{"deleted": file})
@@ -2236,6 +2312,224 @@ func (s *Server) pgLLMPoolReset(w http.ResponseWriter, r *http.Request) {
writeJSON(w, 200, s.llmPoolStatus())
}
const maxModelRedirects = 5
// LLMPrivateEndpointsEnv is an opt-in comma-separated allow-list of exact
// host:port entries (for example "127.0.0.1:11434,host.docker.internal:11434")
// that POST /api/llm/models may contact on loopback or private addresses.
// Unset or empty enforces the strict public-only policy.
const LLMPrivateEndpointsEnv = "ARTEX_LLM_PRIVATE_ENDPOINTS"
type modelURLRefusalError struct {
msg string
}
func (e *modelURLRefusalError) Error() string { return e.msg }
func normalizeModelHost(host string) (kind, normalized string, ok bool) {
host = strings.TrimSpace(host)
if host == "" {
return "", "", false
}
if addr, err := netip.ParseAddr(host); err == nil {
return intercept.TargetIP, addr.Unmap().WithZone("").String(), true
}
t, ok := intercept.NormalizeTarget(host)
if !ok || t.Kind == intercept.TargetCIDR || t.Host == "" {
return "", "", false
}
return t.Kind, t.Host, true
}
func urlEffectivePort(u *url.URL) (int, bool) {
if p := u.Port(); p != "" {
n, err := strconv.Atoi(p)
if err != nil || n < 1 || n > 65535 {
return 0, false
}
return n, true
}
switch strings.ToLower(u.Scheme) {
case "http":
return 80, true
case "https":
return 443, true
default:
return 0, false
}
}
func parsePrivateModelEndpoints(raw string) map[string]bool {
out := map[string]bool{}
for _, entry := range strings.Split(raw, ",") {
entry = strings.TrimSpace(entry)
if entry == "" {
continue
}
host, portStr, err := net.SplitHostPort(entry)
if err != nil {
continue
}
port, err := strconv.Atoi(portStr)
if err != nil || port < 1 || port > 65535 {
continue
}
_, normHost, ok := normalizeModelHost(host)
if !ok {
continue
}
out[net.JoinHostPort(normHost, strconv.Itoa(port))] = true
}
return out
}
func sameRedirectHost(a, b *url.URL) bool {
ha := strings.TrimSuffix(strings.ToLower(strings.TrimSpace(a.Hostname())), ".")
hb := strings.TrimSuffix(strings.ToLower(strings.TrimSpace(b.Hostname())), ".")
return ha != "" && ha == hb
}
func (s *Server) isBlockedModelAddr(addr netip.Addr, allowPrivate bool) bool {
if !addr.IsValid() {
return true
}
addr = addr.Unmap().WithZone("")
if s != nil && s.allowLoopbackModels && addr.IsLoopback() {
return false
}
if !intercept.IsNonPublicAddr(addr) {
return false
}
if allowPrivate {
// Allow-listed self-hosted endpoints may resolve to loopback or RFC1918 /
// unique-local private addresses, but never link-local, unspecified,
// multicast, or instance-metadata addresses (such as 169.254.169.254 or
// fd00:ec2::254).
if addr.IsLoopback() {
return false
}
if addr.IsPrivate() && intercept.ValidateExemptionHost(addr.String()) == nil {
return false
}
}
return true
}
func (s *Server) lookupModelIPs(ctx context.Context, host string) ([]netip.Addr, error) {
if s != nil && s.modelLookupIP != nil {
return s.modelLookupIP(ctx, host)
}
return net.DefaultResolver.LookupNetIP(ctx, "ip", host)
}
type modelURLGuard struct {
s *Server
privateEndpoints map[string]bool
mu sync.Mutex
allowedDialAddrs map[string]bool
}
func newModelURLGuard(s *Server) *modelURLGuard {
return &modelURLGuard{
s: s,
privateEndpoints: parsePrivateModelEndpoints(os.Getenv(LLMPrivateEndpointsEnv)),
}
}
func (g *modelURLGuard) allowDialAddr(addr netip.Addr, port int) {
if !addr.IsValid() || port < 1 || port > 65535 {
return
}
key := net.JoinHostPort(addr.Unmap().WithZone("").String(), strconv.Itoa(port))
g.mu.Lock()
if g.allowedDialAddrs == nil {
g.allowedDialAddrs = map[string]bool{}
}
g.allowedDialAddrs[key] = true
g.mu.Unlock()
}
func (g *modelURLGuard) isAllowedDialAddr(addr netip.Addr, port string) bool {
key := net.JoinHostPort(addr.Unmap().WithZone("").String(), port)
g.mu.Lock()
ok := g.allowedDialAddrs[key]
g.mu.Unlock()
return ok
}
func (g *modelURLGuard) blockDial(_, address string, _ syscall.RawConn) error {
host, port, err := net.SplitHostPort(address)
if err != nil {
return &modelURLRefusalError{msg: "target address is not allowed"}
}
addr, err := netip.ParseAddr(host)
if err != nil {
return &modelURLRefusalError{msg: "target address is not allowed"}
}
allowPrivate := g.isAllowedDialAddr(addr, port)
if g.s.isBlockedModelAddr(addr, allowPrivate) {
return &modelURLRefusalError{msg: "target address is not allowed"}
}
return nil
}
// validateURL validates a base_url candidate, proxy URL, or redirect target for
// POST /api/llm/models. Only http and https URLs are permitted; hosts must
// resolve strictly to public addresses unless their exact host:port is listed
// in ARTEX_LLM_PRIVATE_ENDPOINTS. Errors never include resolved IP addresses.
func (g *modelURLGuard) validateURL(ctx context.Context, rawURL, role string) (*url.URL, error) {
u, err := url.Parse(strings.TrimSpace(rawURL))
if err != nil {
return nil, &modelURLRefusalError{msg: fmt.Sprintf("invalid %s URL", role)}
}
scheme := strings.ToLower(u.Scheme)
if scheme != "http" && scheme != "https" {
return nil, &modelURLRefusalError{msg: fmt.Sprintf("%s URL must use http or https", role)}
}
if u.Opaque != "" {
return nil, &modelURLRefusalError{msg: fmt.Sprintf("invalid %s URL", role)}
}
port, ok := urlEffectivePort(u)
if !ok {
return nil, &modelURLRefusalError{msg: fmt.Sprintf("invalid %s URL", role)}
}
kind, normHost, ok := normalizeModelHost(u.Hostname())
if !ok {
return nil, &modelURLRefusalError{msg: fmt.Sprintf("invalid %s URL host", role)}
}
hostPort := net.JoinHostPort(normHost, strconv.Itoa(port))
allowPrivate := g.privateEndpoints[hostPort]
if kind == intercept.TargetIP {
addr, err := netip.ParseAddr(normHost)
if err != nil || g.s.isBlockedModelAddr(addr, allowPrivate) {
return nil, &modelURLRefusalError{msg: fmt.Sprintf("%s URL host is not allowed", role)}
}
if allowPrivate {
g.allowDialAddr(addr, port)
}
return u, nil
}
if !allowPrivate && !(g.s != nil && g.s.allowLoopbackModels) && (normHost == "localhost" || strings.HasSuffix(normHost, ".localhost")) {
return nil, &modelURLRefusalError{msg: fmt.Sprintf("%s URL host is not allowed", role)}
}
addrs, err := g.s.lookupModelIPs(ctx, normHost)
if err != nil || len(addrs) == 0 {
return nil, &modelURLRefusalError{msg: fmt.Sprintf("failed to resolve %s host", role)}
}
for _, addr := range addrs {
if g.s.isBlockedModelAddr(addr, allowPrivate) {
return nil, &modelURLRefusalError{msg: fmt.Sprintf("%s URL host is not allowed", role)}
}
}
if allowPrivate {
for _, addr := range addrs {
g.allowDialAddr(addr, port)
}
}
return u, nil
}
// pgListModels fetches available models from the provider's API endpoint.
// Supports OpenAI-format (GET /models), Anthropic-format (GET /v1/models) and
// Ollama (keyless OpenAI-compatible GET /v1/models on the local server); for
@@ -2285,7 +2579,9 @@ func (s *Server) pgListModels(w http.ResponseWriter, r *http.Request) {
return
}
baseURL := strings.TrimRight(strings.TrimSpace(req.BaseURL), "/")
rawBaseURL := strings.TrimSpace(req.BaseURL)
baseURL := strings.TrimRight(rawBaseURL, "/")
defaultOllama := provider == "ollama" && rawBaseURL == ""
// Candidate endpoints to try in order. Some Anthropic-compatible providers
// (e.g. DeepSeek) implement /v1/messages under an /anthropic path but expose
@@ -2349,19 +2645,98 @@ func (s *Server) pgListModels(w http.ResponseWriter, r *http.Request) {
}
}
// Build HTTP client with optional proxy.
transport := &http.Transport{}
// Validate proxy and every candidate URL before sending any request.
// Pre-request validation is required in addition to the dial-time guard
// because a caller-supplied HTTP proxy fetches the target URL itself.
guard := newModelURLGuard(s)
var proxyURL *url.URL
if p := strings.TrimSpace(req.Proxy); p != "" {
if pu, err := url.Parse(p); err == nil {
transport.Proxy = http.ProxyURL(pu)
var err error
proxyURL, err = guard.validateURL(r.Context(), p, "proxy")
if err != nil {
writeJSON(w, 200, map[string]any{"ok": false, "error": err.Error()})
return
}
}
client := &http.Client{Transport: transport, Timeout: 30 * time.Second}
if defaultOllama {
// The built-in Ollama default is code-owned, not caller input: allow
// dialing its exact host:port without relaxing caller-supplied URLs.
if du, err := url.Parse(agent.DefaultOllamaBaseURL); err == nil {
if addr, err := netip.ParseAddr(du.Hostname()); err == nil {
if port, ok := urlEffectivePort(du); ok {
guard.allowDialAddr(addr, port)
}
}
}
} else {
for _, c := range candidates {
if _, err := guard.validateURL(r.Context(), c.url, "endpoint"); err != nil {
writeJSON(w, 200, map[string]any{"ok": false, "error": err.Error()})
return
}
}
}
// Build HTTP client with strict dial guard, redirect validation, and optional proxy.
var transport *http.Transport
if base, ok := http.DefaultTransport.(*http.Transport); ok {
transport = base.Clone()
} else {
transport = &http.Transport{}
}
transport.Proxy = nil
if proxyURL != nil {
transport.Proxy = http.ProxyURL(proxyURL)
}
if s.modelRootCAs != nil {
if transport.TLSClientConfig == nil {
transport.TLSClientConfig = &tls.Config{}
} else {
transport.TLSClientConfig = transport.TLSClientConfig.Clone()
}
transport.TLSClientConfig.RootCAs = s.modelRootCAs
}
dialCtx := (&net.Dialer{Timeout: 10 * time.Second, Control: guard.blockDial}).DialContext
if s.modelDialContext != nil {
dialCtx = s.modelDialContext
}
transport.DialContext = dialCtx
defer transport.CloseIdleConnections()
client := &http.Client{
Transport: transport,
Timeout: 30 * time.Second,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
if len(via) >= maxModelRedirects {
return &modelURLRefusalError{msg: "too many redirects"}
}
if len(via) > 0 {
prev := via[len(via)-1].URL
prevScheme := strings.ToLower(prev.Scheme)
nextScheme := strings.ToLower(req.URL.Scheme)
if nextScheme != prevScheme {
if !(prevScheme == "http" && nextScheme == "https" && sameRedirectHost(prev, req.URL)) {
return &modelURLRefusalError{msg: "redirect scheme change is not allowed"}
}
}
}
if _, err := guard.validateURL(req.Context(), req.URL.String(), "redirect"); err != nil {
return err
}
return nil
},
}
// Try each candidate; return the first that yields a non-empty model list.
var lastErr string
emptyOK := false
for _, c := range candidates {
for i, c := range candidates {
if i > 0 && !defaultOllama {
if _, err := guard.validateURL(r.Context(), c.url, "endpoint"); err != nil {
writeJSON(w, 200, map[string]any{"ok": false, "error": err.Error()})
return
}
}
httpReq, err := http.NewRequestWithContext(r.Context(), http.MethodGet, c.url, nil)
if err != nil {
lastErr = "failed to build request: " + err.Error()
@@ -2370,6 +2745,11 @@ func (s *Server) pgListModels(w http.ResponseWriter, r *http.Request) {
httpReq.Header = c.hdr
resp, err := client.Do(httpReq)
if err != nil {
var refusal *modelURLRefusalError
if errors.As(err, &refusal) {
writeJSON(w, 200, map[string]any{"ok": false, "error": refusal.Error()})
return
}
lastErr = "request failed: " + err.Error()
continue
}
File diff suppressed because it is too large Load Diff
+179
View File
@@ -3,12 +3,14 @@ package server
import (
"context"
"fmt"
"iter"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"
"github.com/Autumn-27/artex/agent"
"github.com/Autumn-27/artex/db"
"github.com/Autumn-27/norma/llm"
)
@@ -211,3 +213,180 @@ func TestNextRotatedProfileRoundRobin(t *testing.T) {
t.Fatal("disabled rotation must not assign a pin")
}
}
type pinCapture struct {
id int64
ok bool
}
type pinCaptureProvider struct {
mu sync.Mutex
seen []pinCapture
}
func (p *pinCaptureProvider) record(ctx context.Context) {
id, ok := llmProfilePinFrom(ctx)
p.mu.Lock()
p.seen = append(p.seen, pinCapture{id: id, ok: ok})
p.mu.Unlock()
}
func (p *pinCaptureProvider) Stream(ctx context.Context, _ llm.CompletionRequest) iter.Seq2[llm.StreamEvent, error] {
p.record(ctx)
return func(yield func(llm.StreamEvent, error) bool) {
if !yield(llm.StreamEvent{Type: llm.SETextDelta, Text: "done"}, nil) {
return
}
if !yield(llm.StreamEvent{Type: llm.SEMessageDelta, StopReason: "end_turn"}, nil) {
return
}
yield(llm.StreamEvent{Type: llm.SEMessageStop}, nil)
}
}
func (p *pinCaptureProvider) Complete(ctx context.Context, _ llm.CompletionRequest) (llm.Message, string, llm.Usage, error) {
p.record(ctx)
return llm.Message{Role: llm.RoleAssistant, Content: []llm.ContentBlock{llm.TextBlock("done")}}, "end_turn", llm.Usage{}, nil
}
// Regression test for #376 (DB-free): warming e.execContextFor first (as the
// planner does) must not drop the per-intent LLM profile pin on subsequent
// worker intents, and a paused/deleting rollback must not advance rotation.
func TestIntentWorkContextRotatesWithCachedExecCtx(t *testing.T) {
const (
taskID = "77"
profileA = int64(101)
profileB = int64(202)
)
e := NewEngine(nil)
tk := &Task{ID: taskID, LLMProfileIDs: []int64{profileA, profileB}, LLMRotate: true}
// Warm the per-task cached execCtx, simulating the planner running first.
warm := e.execContextFor(context.Background(), taskID)
if warm == nil || warm.Err() != nil {
t.Fatalf("warm execContextFor returned invalid ctx: %v", warm)
}
// A paused task must roll back without advancing the rotation counter.
e.Pause(taskID, agent.AbortPausedByUser)
if _, _, _, ok := e.intentWorkContext(context.Background(), tk); ok {
t.Fatal("paused task unexpectedly yielded a work context")
}
e.Resume(tk)
// Re-warm cached execCtx after resume (again simulating planner before worker).
_ = e.execContextFor(context.Background(), taskID)
// A deleting task must also roll back without advancing the rotation counter.
e.BeginDelete(taskID)
if _, _, _, ok := e.intentWorkContext(context.Background(), tk); ok {
t.Fatal("deleting task unexpectedly yielded a work context")
}
e.AbortDelete(taskID, false)
// Two consecutive claimed intents must carry distinct rotated pins (A then B).
_, workA, cancelA, ok := e.intentWorkContext(context.Background(), tk)
if !ok {
t.Fatal("first intentWorkContext failed")
}
defer cancelA(nil)
gotA, okA := llmProfilePinFrom(workA)
if !okA || gotA != profileA {
t.Fatalf("first workCtx pin = (%d, %v), want (%d, true)", gotA, okA, profileA)
}
_, workB, cancelB, ok := e.intentWorkContext(context.Background(), tk)
if !ok {
t.Fatal("second intentWorkContext failed")
}
defer cancelB(nil)
gotB, okB := llmProfilePinFrom(workB)
if !okB || gotB != profileB {
t.Fatalf("second workCtx pin = (%d, %v), want (%d, true)", gotB, okB, profileB)
}
if gotA == gotB {
t.Fatalf("expected distinct pinned profile IDs, both were %d", gotA)
}
}
// Regression test for #376 (end-to-end through runIntent): warm execContextFor
// first (simulating the planner running before workers), then run two intents
// through runIntent and assert the two work contexts carry distinct pinned
// profile IDs (A then B).
func TestRunIntentRotatesPinnedProfileWithCachedExecCtx(t *testing.T) {
m, err := NewManager(t.TempDir(), "")
if err != nil {
t.Skipf("postgres unavailable: %v", err)
}
defer m.Close()
ids := make([]int64, 0, 2)
for i := 0; i < 2; i++ {
id, err := m.pg.SaveProfile(&db.LLMProfile{
Name: fmt.Sprintf("runintent-rotate-%d-%d", time.Now().UnixNano(), i),
Format: "openai",
BaseURL: "http://127.0.0.1:1/v1",
Model: "test",
APIKey: "test-key",
})
if err != nil {
t.Fatal(err)
}
ids = append(ids, id)
defer m.pg.DeleteProfile(id)
}
on := true
task, err := m.CreateTaskWithOptions("runIntent rotation", "verify pin survives cached execCtx", db.TaskCreateOptions{
LLMProfileIDs: ids,
LLMRotate: &on,
})
if err != nil {
t.Fatal(err)
}
defer m.DeleteTask(task.ID, DeleteTaskOptions{})
e := NewEngine(m)
// Warm the per-task cached execCtx before any worker intent runs.
_ = e.execContextFor(context.Background(), task.ID)
// Verify paused rollback through runIntent does not advance rotation.
if _, err := task.Store.AddIntent(map[string]any{"summary": "rolled back while paused"}, 1, nil, "planner"); err != nil {
t.Fatal(err)
}
pausedIntent, pausedToken := e.claimNext(task, "work#0")
if pausedIntent == nil {
t.Fatal("failed to claim intent for paused rollback check")
}
e.Pause(task.ID, agent.AbortPausedByUser)
probe := &pinCaptureProvider{}
worker := agent.NewWorker(probe, "test-model", m.dir, nil, 0, 2)
e.runIntent(context.Background(), task, "work#0", worker, pausedIntent, pausedToken, "", "")
if len(probe.seen) != 0 {
t.Fatalf("paused runIntent called provider %d times, want 0", len(probe.seen))
}
e.Resume(task)
// Re-warm cached execCtx after resume (simulating planner before workers).
_ = e.execContextFor(context.Background(), task.ID)
// Run two intents through runIntent and assert distinct pins: ids[0] then ids[1].
if _, err := task.Store.AddIntent(map[string]any{"summary": "rotate intent 2"}, 1, nil, "planner"); err != nil {
t.Fatal(err)
}
for i, wantID := range ids {
name := fmt.Sprintf("work#%d", i+1)
intent, token := e.claimNext(task, name)
if intent == nil {
t.Fatalf("intent %d not claimed", i+1)
}
p := &pinCaptureProvider{}
w := agent.NewWorker(p, "test-model", m.dir, nil, 0, 2)
e.runIntent(context.Background(), task, name, w, intent, token, "", "")
if len(p.seen) == 0 {
t.Fatalf("intent %d: provider was not called", i+1)
}
got := p.seen[0]
if !got.ok || got.id != wantID {
t.Fatalf("intent %d: workCtx pin = (%d, %v), want (%d, true)", i+1, got.id, got.ok, wantID)
}
}
}
+90 -34
View File
@@ -23,23 +23,68 @@ const (
maxWorkspaceUpload = 512 << 20 // 512 MiB per upload request
)
// wsResolveConfined maps a user-supplied relative path to a canonicalized path
// inside the work dir, resolving symlinks in every component and rejecting any
// traversal, broken symlink, symlink loop, or target that escapes s.m.dir.
func (s *Server) wsResolveConfined(rel string) (confinedPath, bool) {
cp, err := resolveWithinRoot(s.m.dir, rel, true)
if err != nil {
return confinedPath{}, false
}
if workspacePathDenied(cp.realRoot, cp.realPath) {
return confinedPath{}, false
}
return cp, true
}
// workspaceDeniedPrefixes are platform-internal subtrees under the data dir
// that the workspace file manager must not expose. traffic/_ca holds the
// capture proxy CA private key; traffic/_index holds the SQLite exchange index.
var workspaceDeniedPrefixes = []string{"traffic/_ca", "traffic/_index"}
// workspacePathDenied reports whether realPath lands in a platform-internal
// subtree. It compares the canonicalized path relative to the real root, so a
// symlink alias cannot reach a denied directory. Segments are lower-cased and
// trailing dots/spaces are trimmed because Windows resolves those spellings to
// the same directory.
func workspacePathDenied(realRoot, realPath string) bool {
rel, err := filepath.Rel(realRoot, realPath)
if err != nil {
return true // fail closed
}
rel = filepath.ToSlash(rel)
if rel == ".." || strings.HasPrefix(rel, "../") || strings.HasPrefix(rel, "/") {
return true // outside the root: fail closed
}
segs := strings.Split(rel, "/")
for i, seg := range segs {
segs[i] = strings.ToLower(strings.TrimRight(seg, ". "))
}
rel = strings.Join(segs, "/")
for _, prefix := range workspaceDeniedPrefixes {
if rel == prefix || strings.HasPrefix(rel, prefix+"/") {
return true
}
}
return false
}
// wsResolve maps a user-supplied relative path to an absolute path INSIDE the work
// dir. It returns ok=false if the path would escape the root. filepath.Clean on a
// rooted copy collapses any ".." so nothing can climb above the root.
// dir. It returns ok=false if the path would escape the root (including via symlinks).
func (s *Server) wsResolve(rel string) (string, bool) {
base := filepath.Clean(s.m.dir)
rel = strings.TrimPrefix(strings.TrimSpace(rel), "/")
clean := filepath.Clean("/" + rel) // e.g. "/a/../../etc" → "/etc" (still rooted at "/")
abs := filepath.Clean(filepath.Join(base, clean))
if abs != base && !strings.HasPrefix(abs, base+string(os.PathSeparator)) {
cp, ok := s.wsResolveConfined(rel)
if !ok {
return "", false
}
return abs, true
return cp.realPath, true
}
// wsRel renders an absolute path back as a workspace-relative path (forward slashes).
func (s *Server) wsRel(abs string) string {
base := filepath.Clean(s.m.dir)
base, err := resolveRealRoot(s.m.dir)
if err != nil {
base = filepath.Clean(s.m.dir)
}
rel, err := filepath.Rel(base, abs)
if err != nil || rel == "." {
return ""
@@ -57,11 +102,12 @@ type wsEntry struct {
// GET /api/workspace/list?path=<rel>
func (s *Server) wsList(w http.ResponseWriter, r *http.Request) {
abs, ok := s.wsResolve(r.URL.Query().Get("path"))
cp, ok := s.wsResolveConfined(r.URL.Query().Get("path"))
if !ok {
writeErr(w, 400, "invalid path")
return
}
abs := cp.realPath
fi, err := os.Stat(abs)
if err != nil {
writeErr(w, 404, "path does not exist")
@@ -73,7 +119,7 @@ func (s *Server) wsList(w http.ResponseWriter, r *http.Request) {
}
ents, err := os.ReadDir(abs)
if err != nil {
writeErr(w, 500, err.Error())
writeErr(w, 500, safeFSErr(err))
return
}
out := make([]wsEntry, 0, len(ents))
@@ -103,11 +149,12 @@ func (s *Server) wsList(w http.ResponseWriter, r *http.Request) {
// GET /api/workspace/read?path=<rel> — inline text for view/edit. Binary or oversize
// files return {binary:true}/{too_large:true} with no content (use download instead).
func (s *Server) wsRead(w http.ResponseWriter, r *http.Request) {
abs, ok := s.wsResolve(r.URL.Query().Get("path"))
cp, ok := s.wsResolveConfined(r.URL.Query().Get("path"))
if !ok {
writeErr(w, 400, "invalid path")
return
}
abs := cp.realPath
fi, err := os.Stat(abs)
if err != nil {
writeErr(w, 404, "file does not exist")
@@ -123,7 +170,7 @@ func (s *Server) wsRead(w http.ResponseWriter, r *http.Request) {
}
data, err := os.ReadFile(abs)
if err != nil {
writeErr(w, 500, err.Error())
writeErr(w, 500, safeFSErr(err))
return
}
if bytes.IndexByte(data, 0) >= 0 || !utf8.Valid(data) {
@@ -143,21 +190,22 @@ func (s *Server) wsWrite(w http.ResponseWriter, r *http.Request) {
writeErr(w, 400, err.Error())
return
}
abs, ok := s.wsResolve(req.Path)
if !ok || abs == filepath.Clean(s.m.dir) {
cp, ok := s.wsResolveConfined(req.Path)
if !ok || cp.realPath == cp.realRoot {
writeErr(w, 400, "invalid path")
return
}
abs := cp.realPath
if fi, err := os.Stat(abs); err == nil && fi.IsDir() {
writeErr(w, 400, "target is a directory")
return
}
if err := os.MkdirAll(filepath.Dir(abs), 0o755); err != nil {
writeErr(w, 500, err.Error())
writeErr(w, 500, safeFSErr(err))
return
}
if err := os.WriteFile(abs, []byte(req.Content), 0o644); err != nil {
writeErr(w, 500, err.Error())
writeErr(w, 500, safeFSErr(err))
return
}
writeJSON(w, 200, map[string]any{"ok": true, "path": s.wsRel(abs)})
@@ -172,13 +220,14 @@ func (s *Server) wsMkdir(w http.ResponseWriter, r *http.Request) {
writeErr(w, 400, err.Error())
return
}
abs, ok := s.wsResolve(req.Path)
if !ok || abs == filepath.Clean(s.m.dir) {
cp, ok := s.wsResolveConfined(req.Path)
if !ok || cp.realPath == cp.realRoot {
writeErr(w, 400, "invalid path")
return
}
abs := cp.realPath
if err := os.MkdirAll(abs, 0o755); err != nil {
writeErr(w, 500, err.Error())
writeErr(w, 500, safeFSErr(err))
return
}
writeJSON(w, 200, map[string]any{"ok": true, "path": s.wsRel(abs)})
@@ -187,21 +236,21 @@ func (s *Server) wsMkdir(w http.ResponseWriter, r *http.Request) {
// DELETE /api/workspace/delete?path=<rel> — removes a file or a directory tree
// (confined to the work dir; the root itself can't be deleted).
func (s *Server) wsDelete(w http.ResponseWriter, r *http.Request) {
abs, ok := s.wsResolve(r.URL.Query().Get("path"))
cp, ok := s.wsResolveConfined(r.URL.Query().Get("path"))
if !ok {
writeErr(w, 400, "invalid path")
return
}
if abs == filepath.Clean(s.m.dir) {
if cp.realPath == cp.realRoot {
writeErr(w, 400, "cannot delete the workspace root directory")
return
}
if _, err := os.Stat(abs); err != nil {
if !cp.exists {
writeErr(w, 404, "path does not exist")
return
}
if err := os.RemoveAll(abs); err != nil {
writeErr(w, 500, err.Error())
if err := os.RemoveAll(cp.entryPath); err != nil {
writeErr(w, 500, safeFSErr(err))
return
}
writeJSON(w, 200, map[string]any{"ok": true})
@@ -209,17 +258,18 @@ func (s *Server) wsDelete(w http.ResponseWriter, r *http.Request) {
// GET /api/workspace/download?path=<rel> — stream a file as an attachment.
func (s *Server) wsDownload(w http.ResponseWriter, r *http.Request) {
abs, ok := s.wsResolve(r.URL.Query().Get("path"))
cp, ok := s.wsResolveConfined(r.URL.Query().Get("path"))
if !ok {
writeErr(w, 400, "invalid path")
return
}
abs := cp.realPath
fi, err := os.Stat(abs)
if err != nil || fi.IsDir() {
writeErr(w, 404, "file does not exist")
return
}
name := filepath.Base(abs)
name := filepath.Base(cp.entryPath)
// RFC 5987 filename* keeps non-ASCII names intact; plain filename is the fallback.
w.Header().Set("Content-Disposition", "attachment; filename=\""+sanitizeFilename(name)+"\"; filename*=UTF-8''"+url.PathEscape(name))
http.ServeFile(w, r, abs)
@@ -227,12 +277,12 @@ func (s *Server) wsDownload(w http.ResponseWriter, r *http.Request) {
// POST /api/workspace/upload?path=<dir> — multipart form field "file" (one or more).
func (s *Server) wsUpload(w http.ResponseWriter, r *http.Request) {
dirAbs, ok := s.wsResolve(r.URL.Query().Get("path"))
dir, ok := s.wsResolveConfined(r.URL.Query().Get("path"))
if !ok {
writeErr(w, 400, "invalid path")
return
}
if fi, err := os.Stat(dirAbs); err != nil || !fi.IsDir() {
if fi, err := os.Stat(dir.realPath); err != nil || !fi.IsDir() {
writeErr(w, 400, "target directory does not exist")
return
}
@@ -246,18 +296,24 @@ func (s *Server) wsUpload(w http.ResponseWriter, r *http.Request) {
writeErr(w, 400, "missing upload file (form field file)")
return
}
dirRel := s.wsRel(dir.realPath)
saved := 0
for _, hdr := range files {
name := filepath.Base(hdr.Filename) // strip any path component
if name == "" || name == "." || name == ".." {
continue
}
destAbs, okd := s.wsResolve(filepath.Join(s.wsRel(dirAbs), name))
if !okd {
continue
destRel := name
if dirRel != "" {
destRel = dirRel + "/" + name
}
if err := saveUpload(hdr, destAbs); err != nil {
writeErr(w, 500, err.Error())
dest, okd := s.wsResolveConfined(destRel)
if !okd || dest.realPath == dest.realRoot {
writeErr(w, 400, "invalid path")
return
}
if err := saveUpload(hdr, dest.realPath); err != nil {
writeErr(w, 500, safeFSErr(err))
return
}
saved++
+68
View File
@@ -0,0 +1,68 @@
package server
import (
"os"
"path/filepath"
"testing"
)
func TestWorkspaceDeniesPlatformPaths(t *testing.T) {
dir := t.TempDir()
for _, p := range []string{"traffic/_ca", "traffic/_index", "traffic/host", "other"} {
if err := os.MkdirAll(filepath.Join(dir, p), 0o755); err != nil {
t.Fatal(err)
}
}
key := filepath.Join(dir, "traffic", "_ca", "mitmproxy-ca.pem")
if err := os.WriteFile(key, []byte("key"), 0o600); err != nil {
t.Fatal(err)
}
s := &Server{m: &Manager{dir: dir}}
for _, rel := range []string{
"traffic/_ca",
"traffic/_ca/mitmproxy-ca.pem",
"traffic/_index",
"traffic/_index/index.sqlite",
"TRAFFIC/_CA/mitmproxy-ca.pem",
"traffic/_ca.",
"traffic/_ca./mitmproxy-ca.pem",
} {
if _, ok := s.wsResolveConfined(rel); ok {
t.Errorf("%q resolved, want a refusal", rel)
}
}
for _, rel := range []string{"", "traffic", "traffic/host", "traffic/_caX", "other"} {
if _, ok := s.wsResolveConfined(rel); !ok {
t.Errorf("%q refused, want a resolution", rel)
}
}
link := filepath.Join(dir, "alias")
if err := os.Symlink(filepath.Join(dir, "traffic", "_ca"), link); err == nil {
if _, ok := s.wsResolveConfined("alias/mitmproxy-ca.pem"); ok {
t.Error("symlink alias to traffic/_ca resolved")
}
}
}
func TestWorkspacePathDenied(t *testing.T) {
root := filepath.Clean("/data")
for _, tc := range []struct {
path string
want bool
}{
{"/data/traffic/_ca", true},
{"/data/traffic/_ca/x", true},
{"/data/traffic/_index/index.sqlite", true},
{"/data/traffic/_caX", false},
{"/data/traffic", false},
{"/data/other", false},
{"/elsewhere/traffic/_ca", true}, // outside the root: fail closed
} {
if got := workspacePathDenied(root, filepath.FromSlash(tc.path)); got != tc.want {
t.Errorf("workspacePathDenied(%q) = %v, want %v", tc.path, got, tc.want)
}
}
}