mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-26 01:41:04 +08:00
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.
69 lines
1.8 KiB
Go
69 lines
1.8 KiB
Go
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)
|
|
}
|
|
}
|
|
}
|