From ae1578494a9afe36a022fe03daddfc5d4edc842b Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Sat, 5 Dec 2026 20:39:53 +0000 Subject: [PATCH] Resolve host import links in bounded and unbounded access modes --- docs/roadmap/implementation.md | 5 +++ internal/engine/filesystem_links.go | 52 ++++++++++++++++++++---- internal/engine/filesystem_links_test.go | 42 +++++++++++++++++++ 3 files changed, 91 insertions(+), 8 deletions(-) diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index af3b76b..ef69c10 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -214,6 +214,11 @@ weakening explicit-root access or retaining handles in published metadata. ## Stage 4 — Integrate host link inspection with both access modes +Completed: the shared iterative link resolver supports absolute host coordinates +and ordinary unbounded Lstat/Readlink operations. Focused link/access/regular-file +checks pass. Overlay integration remains pending in stage 10; the broader FIFO +test currently reports a missing overlay because access still uses import mode. + Read `filesystem_links.go`, `filesystem_links_test.go`, `directory_walk.go` and the regular-file tests. Update host import-entry inspection and link operations to use the selected bounded/unbounded mode. Retain the shared iterative link diff --git a/internal/engine/filesystem_links.go b/internal/engine/filesystem_links.go index 6730631..01a76ef 100644 --- a/internal/engine/filesystem_links.go +++ b/internal/engine/filesystem_links.go @@ -3,6 +3,7 @@ package engine import ( "errors" "io/fs" + "os" "path" "path/filepath" "strings" @@ -37,7 +38,12 @@ func (a *filesystemAccess) importEntryTarget(name string, entry fs.DirEntry, fol } relative = filepath.Join(filepath.FromSlash(resolvedParent), filepath.Base(relative)) } - info, err := a.root.Lstat(relative) + var info fs.FileInfo + if a.root == nil { + info, err = os.Lstat(relative) + } else { + info, err = a.root.Lstat(relative) + } if err != nil { return importEntryTarget{}, err } @@ -52,8 +58,11 @@ func (a *filesystemAccess) importEntryTarget(name string, entry fs.DirEntry, fol if err != nil { return importEntryTarget{}, err } - resolvedPath := filepath.Join(a.rootPath, filepath.FromSlash(resolved)) - info, err = a.root.Stat(filepath.FromSlash(resolved)) + resolvedPath := filepath.FromSlash(resolved) + if a.root != nil { + resolvedPath = filepath.Join(a.rootPath, resolvedPath) + } + info, err = a.stat(resolvedPath) if err != nil { return importEntryTarget{}, err } @@ -127,14 +136,22 @@ type importLinkFrame struct { } type importLinkOperations struct { + pathRoot func(string) string lstat func(string) (fs.FileInfo, error) readlink func(string) (string, error) normalizeTarget func(string, string) (string, error) } func resolveImportLinkPath(name string, operations importLinkOperations) (string, error) { - frames := []importLinkFrame{{pending: strings.Split(name, "/")}} - resolved := "" + split := func(name string) (string, []string) { + root := "" + if operations.pathRoot != nil { + root = operations.pathRoot(name) + } + return root, strings.Split(strings.TrimPrefix(name, root), "/") + } + resolved, pending := split(name) + frames := []importLinkFrame{{pending: pending}} active := make(map[string]struct{}) completed := make(map[string]string) for len(frames) > 0 { @@ -180,8 +197,9 @@ func resolveImportLinkPath(name string, operations importLinkOperations) (string return "", err } active[candidate] = struct{}{} - frames = append(frames, importLinkFrame{pending: strings.Split(targetPath, "/"), link: candidate}) - resolved = "" + root, pending := split(targetPath) + frames = append(frames, importLinkFrame{pending: pending, link: candidate}) + resolved = root } if resolved == "" { resolved = "." @@ -190,20 +208,38 @@ func resolveImportLinkPath(name string, operations importLinkOperations) (string } 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)) } func hostImportLinkOperations(access *filesystemAccess) importLinkOperations { return importLinkOperations{ + pathRoot: func(name string) string { + if access.root != nil { + 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)) + } return access.root.Lstat(filepath.FromSlash(candidate)) }, readlink: func(candidate string) (string, error) { + if access.root == nil { + return os.Readlink(filepath.FromSlash(candidate)) + } return access.root.Readlink(filepath.FromSlash(candidate)) }, 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 { return "", errImportPathOutsideRoot @@ -212,7 +248,7 @@ func hostImportLinkOperations(access *filesystemAccess) importLinkOperations { } else { targetPath = path.Clean(path.Join(path.Dir(candidate), filepath.ToSlash(target))) } - if !withinPortableRoot(targetPath) { + if access.root != nil && !withinPortableRoot(targetPath) { return "", errImportPathOutsideRoot } return targetPath, nil diff --git a/internal/engine/filesystem_links_test.go b/internal/engine/filesystem_links_test.go index a743af9..0e6a12e 100644 --- a/internal/engine/filesystem_links_test.go +++ b/internal/engine/filesystem_links_test.go @@ -313,3 +313,45 @@ func (f *countingReadLinkFS) ReadLink(name string) (string, error) { f.readLinks++ return f.links.ReadLink(name) } + +func TestUnboundedHostLinkInspection(t *testing.T) { + root := canonicalTestPath(t, t.TempDir()) + external := canonicalTestPath(t, t.TempDir()) + file := writeImportFile(t, external, "part.yml", "value: linked\n") + for name, target := range map[string]string{"external": external, "self": "self", "first": "second", "second": "first"} { + if err := os.Symlink(target, filepath.Join(root, name)); err != nil { + t.Skipf("symlinks unavailable: %v", err) + } + } + access, err := openHostAccess("") + if err != nil { + t.Fatal(err) + } + defer access.close() + selected := filepath.Join(root, "external") + target, err := access.importEntryTarget(selected, nil, false) + if err != nil || !target.symlink || target.dir { + t.Fatalf("unfollowed target=%#v, %v", target, err) + } + target, err = access.importEntryTarget(selected, nil, true) + if err != nil || !target.dir || target.identity != external { + t.Fatalf("followed target=%#v, %v", target, err) + } + got, err := resolveHostImportPath(access, filepath.Join(selected, "part.yml")) + if err != nil || filepath.FromSlash(got) != file { + t.Fatalf("intermediate absolute link=%q, %v", got, err) + } + for _, name := range []string{"self", "first"} { + if _, err := resolveHostImportPath(access, filepath.Join(root, name)); !errors.Is(err, errImportDirectoryLinkCycle) { + t.Fatalf("cycle %s=%v", name, err) + } + } + bounded, err := openHostAccess(root) + if err != nil { + t.Fatal(err) + } + defer bounded.close() + if _, err := bounded.importEntryTarget(selected, nil, true); !errors.Is(err, errImportPathOutsideRoot) { + t.Fatalf("bounded external target=%v", err) + } +}