From 3b80efad8902e7d47d599a015b82e98cef6fd2f5 Mon Sep 17 00:00:00 2001 From: bvzrays <1372627285@qq.com> Date: Wed, 2 Dec 2026 13:59:02 +0800 Subject: [PATCH] fix(plugin): accept relative root paths in safety checks --- internal/plugin/package.go | 6 +++++- internal/plugin/path_test.go | 27 +++++++++++++++++++++++++++ internal/plugin/scanner.go | 20 ++++++++++---------- 3 files changed, 42 insertions(+), 11 deletions(-) create mode 100644 internal/plugin/path_test.go diff --git a/internal/plugin/package.go b/internal/plugin/package.go index c63b3fb..6be8ae3 100644 --- a/internal/plugin/package.go +++ b/internal/plugin/package.go @@ -1116,7 +1116,11 @@ func validateDiskContributions(m Manifest, rootDir string) error { } func safeJoin(root, rel string) (string, error) { - cleanRoot := filepath.Clean(root) + absRoot, err := filepath.Abs(root) + if err != nil { + return "", fmt.Errorf("resolve root path: %w", err) + } + cleanRoot := filepath.Clean(absRoot) joined := filepath.Clean(filepath.Join(cleanRoot, filepath.FromSlash(rel))) if !pathWithinRoot(cleanRoot, joined) { return "", fmt.Errorf("path traversal blocked: %q", rel) diff --git a/internal/plugin/path_test.go b/internal/plugin/path_test.go new file mode 100644 index 0000000..11f6fe4 --- /dev/null +++ b/internal/plugin/path_test.go @@ -0,0 +1,27 @@ +package plugin + +import ( + "path/filepath" + "testing" +) + +func TestSafeJoinRelativeRoot(t *testing.T) { + want, err := filepath.Abs("path_test.go") + if err != nil { + t.Fatalf("resolve expected path: %v", err) + } + + got, err := safeJoin(".", "path_test.go") + if err != nil { + t.Fatalf("safeJoin rejected a path inside a relative root: %v", err) + } + if filepath.Clean(got) != filepath.Clean(want) { + t.Fatalf("safeJoin path = %q, want %q", got, want) + } +} + +func TestSafeJoinRelativeRootRejectsTraversal(t *testing.T) { + if _, err := safeJoin(".", filepath.Join("..", "go.mod")); err == nil { + t.Fatal("safeJoin accepted a path outside a relative root") + } +} diff --git a/internal/plugin/scanner.go b/internal/plugin/scanner.go index ed80758..be55b18 100644 --- a/internal/plugin/scanner.go +++ b/internal/plugin/scanner.go @@ -123,16 +123,16 @@ var ( reSVGJavascriptURI = regexp.MustCompile(`(?i)javascript\s*:`) reSVGForeignObject = regexp.MustCompile(`(?i)<\s*foreignobject\b`) - reJSWailsGo = regexp.MustCompile(`(?i)\b(?:window|globalThis|self|parent|top)\s*(?:\.\s*go\b|\[\s*['"]go['"]\s*\])`) - reJSWailsRuntime = regexp.MustCompile(`(?i)\b(?:window|globalThis|self|parent|top)\s*(?:\.\s*runtime\b|\[\s*['"]runtime['"]\s*\])`) - reJSEval = regexp.MustCompile(`\beval\s*\(`) - reJSNewFunction = regexp.MustCompile(`\bnew\s+Function\s*\(`) - reJSFunctionCall = regexp.MustCompile(`(?:^|[^.\w$])Function\s*\(`) - reJSDocCookie = regexp.MustCompile(`\bdocument\s*\.\s*cookie\b`) - reJSDocWrite = regexp.MustCompile(`\bdocument\s*\.\s*write(?:ln)?\s*\(`) - reJSScriptTag = regexp.MustCompile(`(?i)<\s*script\b`) - reJSCreateScript = regexp.MustCompile(`(?i)\bcreateElement\s*\(\s*['"]script['"]\s*\)`) - reJSNetworkCall = regexp.MustCompile(`\b(?:fetch\s*\(|XMLHttpRequest\b|WebSocket\b|EventSource\b|navigator\s*\.\s*sendBeacon\s*\()`) + reJSWailsGo = regexp.MustCompile(`(?i)\b(?:window|globalThis|self|parent|top)\s*(?:\.\s*go\b|\[\s*['"]go['"]\s*\])`) + reJSWailsRuntime = regexp.MustCompile(`(?i)\b(?:window|globalThis|self|parent|top)\s*(?:\.\s*runtime\b|\[\s*['"]runtime['"]\s*\])`) + reJSEval = regexp.MustCompile(`\beval\s*\(`) + reJSNewFunction = regexp.MustCompile(`\bnew\s+Function\s*\(`) + reJSFunctionCall = regexp.MustCompile(`(?:^|[^.\w$])Function\s*\(`) + reJSDocCookie = regexp.MustCompile(`\bdocument\s*\.\s*cookie\b`) + reJSDocWrite = regexp.MustCompile(`\bdocument\s*\.\s*write(?:ln)?\s*\(`) + reJSScriptTag = regexp.MustCompile(`(?i)<\s*script\b`) + reJSCreateScript = regexp.MustCompile(`(?i)\bcreateElement\s*\(\s*['"]script['"]\s*\)`) + reJSNetworkCall = regexp.MustCompile(`\b(?:fetch\s*\(|XMLHttpRequest\b|WebSocket\b|EventSource\b|navigator\s*\.\s*sendBeacon\s*\()`) ) // ScanArchive extracts archivePath into a temporary directory under tmpParentDir