diff --git a/docs/roadmap/remediation.md b/docs/roadmap/remediation.md index 94472a2..c6083c8 100644 --- a/docs/roadmap/remediation.md +++ b/docs/roadmap/remediation.md @@ -112,7 +112,14 @@ named-file rejection or a stronger filesystem guarantee. ## 3. Preserve symlink-target components before resolving host directory identity -Priority: P2. Status: open. +Priority: P2. Status: complete. + +Completed: host link targets retain unresolved components for the shared frame +evaluator. Bounded absolute target resolution keeps canonical containment and +uses rooted access for contained names. A regression compares all three import +selectors for relative/absolute targets and bounded/unbounded access; it fails +before the fix and passes afterward. Focused link/directory/glob tests, +`./scripts/check.sh`, and `go test -race ./internal/engine` pass. Relevant code: `hostImportLinkOperations.normalizeTarget` at lines 237–249 of [filesystem_links.go](../../internal/engine/filesystem_links.go), and the diff --git a/internal/engine/filesystem_links.go b/internal/engine/filesystem_links.go index 01a76ef..3b3ef47 100644 --- a/internal/engine/filesystem_links.go +++ b/internal/engine/filesystem_links.go @@ -211,45 +211,75 @@ func resolveHostImportPath(access *filesystemAccess, name string) (string, error if access.root == nil && !filepath.IsAbs(name) { return "", fs.ErrInvalid } - return resolveImportLinkPath(filepath.ToSlash(name), hostImportLinkOperations(access)) + resolved, err := resolveImportLinkPath(filepath.ToSlash(name), hostImportLinkOperations(access)) + if err != nil || access.root == nil { + return resolved, err + } + if filepath.IsAbs(filepath.FromSlash(resolved)) { + relative, err := access.relative(filepath.FromSlash(resolved)) + if err != nil { + return "", errImportPathOutsideRoot + } + return filepath.ToSlash(relative), nil + } + if !withinPortableRoot(resolved) { + return "", errImportPathOutsideRoot + } + return resolved, nil } func hostImportLinkOperations(access *filesystemAccess) importLinkOperations { + // Absolute target components may include resolution prefixes outside Root. + // Use the live root for contained names and ordinary host inspection for + // those prefixes; target admission remains bounded by canonical identity. + rootedName := func(candidate string) (string, bool) { + name := filepath.FromSlash(candidate) + if access.root == nil { + return name, false + } + if !filepath.IsAbs(name) { + return name, true + } + if relative, err := access.relative(name); err == nil { + return relative, true + } + return name, false + } return importLinkOperations{ pathRoot: func(name string) string { - if access.root != nil { + if !filepath.IsAbs(filepath.FromSlash(name)) { return "" } return filepath.ToSlash(filepath.VolumeName(filepath.FromSlash(name))) + "/" }, lstat: func(candidate string) (fs.FileInfo, error) { - if access.root == nil { - return os.Lstat(filepath.FromSlash(candidate)) + name, rooted := rootedName(candidate) + if rooted { + return access.root.Lstat(name) } - return access.root.Lstat(filepath.FromSlash(candidate)) + return os.Lstat(name) }, readlink: func(candidate string) (string, error) { - if access.root == nil { - return os.Readlink(filepath.FromSlash(candidate)) + name, rooted := rootedName(candidate) + if rooted { + return access.root.Readlink(name) } - return access.root.Readlink(filepath.FromSlash(candidate)) + return os.Readlink(name) }, normalizeTarget: func(candidate, target string) (string, error) { - var targetPath string - if filepath.IsAbs(target) { - if access.root == nil { - return filepath.ToSlash(filepath.Clean(target)), nil - } - relative, err := filepath.Rel(access.rootPath, filepath.Clean(target)) - if err != nil { + targetPath := filepath.ToSlash(target) + if !filepath.IsAbs(target) { + // Do not Join/Clean here: a link before .. must resolve first. + targetPath = path.Dir(candidate) + "/" + targetPath + } + if access.root != nil { + if filepath.IsAbs(filepath.FromSlash(targetPath)) { + if !containedPath(access.rootPath, filepath.Clean(filepath.FromSlash(targetPath))) { + return "", errImportPathOutsideRoot + } + } else if !withinPortableRoot(path.Clean(targetPath)) { return "", errImportPathOutsideRoot } - targetPath = path.Clean(filepath.ToSlash(relative)) - } else { - targetPath = path.Clean(path.Join(path.Dir(candidate), filepath.ToSlash(target))) - } - if access.root != nil && !withinPortableRoot(targetPath) { - return "", errImportPathOutsideRoot } return targetPath, nil }, diff --git a/internal/engine/import_link_parent_test.go b/internal/engine/import_link_parent_test.go new file mode 100644 index 0000000..8ec902a --- /dev/null +++ b/internal/engine/import_link_parent_test.go @@ -0,0 +1,48 @@ +package engine + +import ( + "os" + "path/filepath" + "strconv" + "testing" +) + +func TestHostImportsResolveParentsInsideLinkTargets(t *testing.T) { + root := canonicalTestPath(t, t.TempDir()) + writeImportFile(t, root, "external/b/unit/marker", "") + writeImportFile(t, root, "external/a/shared/part.yml", "value: wrong\n") + want := writeImportFile(t, root, "external/b/shared/part.yml", "value: intended\n") + if err := os.Symlink("../b/unit", filepath.Join(root, "external/a/link")); err != nil { + t.Skipf("symlinks unavailable: %v", err) + } + alias := filepath.Join(root, "external/alias") + for _, absolute := range []bool{false, true} { + target := "a/link/../shared" + if absolute { + target = filepath.Join(root, "external/a/link") + "/../shared" + } + if err := os.Symlink(target, alias); err != nil { + t.Fatal(err) + } + for _, bounded := range []bool{false, true} { + var context *FilesystemContext + if bounded { + context = &FilesystemContext{Root: root} + } + for _, selector := range []string{"dir: " + strconv.Quote(alias) + ", pattern: '*.yml'", "glob: " + strconv.Quote(alias+"/*.yml"), "path: " + strconv.Quote(alias+"/part.yml")} { + main := writeImportFile(t, root, "app/main.yml", "config: {imports: [{"+selector+", required: true}]}\n") + result, err := testLoad[map[string]string](File(main), Options[map[string]string]{Imports: true, Filesystem: context}) + if err != nil { + t.Fatalf("absolute=%v bounded=%v selector=%s: %v", absolute, bounded, selector, err) + } + sources := result.Inspection.Sources() + if result.Config["value"] != "intended" || len(sources) != 2 || sources[1].Path != want { + t.Fatalf("absolute=%v bounded=%v selector=%s: value=%v sources=%#v", absolute, bounded, selector, result.Config, sources) + } + } + } + if err := os.Remove(alias); err != nil { + t.Fatal(err) + } + } +}