Compare commits

...
Author SHA1 Message Date
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 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
13 changed files with 3192 additions and 93 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
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)
}
}
+13 -5
View File
@@ -765,12 +765,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 {
+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
+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
})
+110 -40
View File
@@ -1156,7 +1156,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 +1167,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 +1275,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 +1322,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 +1357,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 +1400,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 +1416,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 +1618,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 +1730,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 +1845,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 +1881,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 +1920,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 +1963,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 +1994,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})
File diff suppressed because it is too large Load Diff
+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)
}
}
}