mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-25 17:30:56 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
73ab430c6a |
@@ -31,9 +31,44 @@ func (s *Server) wsResolveConfined(rel string) (confinedPath, bool) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return confinedPath{}, false
|
return confinedPath{}, false
|
||||||
}
|
}
|
||||||
|
if workspacePathDenied(cp.realRoot, cp.realPath) {
|
||||||
|
return confinedPath{}, false
|
||||||
|
}
|
||||||
return cp, true
|
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
|
// 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 (including via symlinks).
|
// dir. It returns ok=false if the path would escape the root (including via symlinks).
|
||||||
func (s *Server) wsResolve(rel string) (string, bool) {
|
func (s *Server) wsResolve(rel string) (string, bool) {
|
||||||
|
|||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user