diff --git a/modules/mesh-delivery-sdk/buildstep/request.go b/modules/mesh-delivery-sdk/buildstep/request.go index 5ea272b0..e56ddb19 100644 --- a/modules/mesh-delivery-sdk/buildstep/request.go +++ b/modules/mesh-delivery-sdk/buildstep/request.go @@ -146,9 +146,15 @@ func (r FetchRequest) Check() error { if r.Dir != "" && (outside(r.Dir) || strings.HasPrefix(r.Dir, "/")) { return fmt.Errorf("the module directory %q leaves the repository", r.Dir) } + // A path is relative to the module's directory; one that leaves it but stays inside the repository (a sibling the + // artifact's go.mod requires, "..//") is accepted and narrowed from the repository's root, and one that + // leaves the repository is refused (design 47 H-A-198, H-A-199). for _, p := range r.Paths { - if p == "" || strings.HasPrefix(p, "/") || outside(p) || path.Clean(p) == "." { - return fmt.Errorf("the path %q leaves the module's directory or is not a path in it", p) + if p == "" || strings.HasPrefix(p, "/") || path.Clean(p) == "." { + return fmt.Errorf("the path %q is not a path relative to the module's directory", p) + } + if outside(path.Join(r.Dir, p)) { + return fmt.Errorf("the path %q leaves the repository", p) } } return nil diff --git a/modules/mesh-delivery-sdk/buildstep/request_test.go b/modules/mesh-delivery-sdk/buildstep/request_test.go index fbd79d55..c7360587 100644 --- a/modules/mesh-delivery-sdk/buildstep/request_test.go +++ b/modules/mesh-delivery-sdk/buildstep/request_test.go @@ -37,6 +37,11 @@ func TestAFetchRequestCarriesTheNamedPathsThroughTheToolCall(t *testing.T) { if dirOnly, err := ParseFetchRequest(map[string]any{"repository": "novox/x", "commit": strings.Repeat("b", 40), "dir": "modules/a"}); err != nil || dirOnly.Dir != "modules/a" || dirOnly.Paths != nil { t.Fatalf("a directory with no paths: %+v, %v", dirOnly, err) } + // A sibling of the same repository is a path that leaves the module's directory, not the repository (H-A-199). + if sib, err := ParseFetchRequest(map[string]any{"repository": "novox/x", "commit": strings.Repeat("b", 40), "dir": "modules/a", + "paths": []any{"pkg", "../b/client"}}); err != nil || len(sib.Paths) != 2 { + t.Fatalf("a sibling path inside the repository: %+v, %v", sib, err) + } for _, c := range []struct { set map[string]any says string @@ -46,7 +51,8 @@ func TestAFetchRequestCarriesTheNamedPathsThroughTheToolCall(t *testing.T) { {map[string]any{"commit": "abc"}, "is not a full commit"}, {map[string]any{"base": "abc"}, "is not a trunk commit"}, {map[string]any{"paths": []any{"src"}}, "names no module directory"}, - {map[string]any{"dir": "modules/a", "paths": []any{"../b"}}, "leaves the module's directory"}, + {map[string]any{"dir": "modules/a", "paths": []any{"../../../b"}}, "leaves the repository"}, + {map[string]any{"dir": "modules/a", "paths": []any{"/b"}}, "not a path relative to the module's directory"}, {map[string]any{"dir": "../a", "paths": []any{"src"}}, "leaves the repository"}, {map[string]any{"dir": "modules/a", "paths": []any{3}}, "paths is a list of strings"}, {map[string]any{"packages": map[string]any{"git.novox.be/novox/mesh-sdk/go": "main"}}, "neither a full commit nor a Go semantic version"}, diff --git a/modules/mesh-delivery-sdk/steprun/narrow.go b/modules/mesh-delivery-sdk/steprun/narrow.go index 7ced0ec1..c5be833c 100644 --- a/modules/mesh-delivery-sdk/steprun/narrow.go +++ b/modules/mesh-delivery-sdk/steprun/narrow.go @@ -25,9 +25,11 @@ func Narrow(tree string, r sdkstep.FetchRequest) error { } keep := map[string]bool{".git": true} for _, p := range r.Paths { + // Kept by its path from the repository's root: a path leaving the module's directory but not the repository is + // narrowed from the root like any other (design 47 H-A-199). rel := path.Join(r.Dir, p) if strings.HasPrefix(rel, "../") || rel == ".." || path.IsAbs(p) { - return fmt.Errorf("the named path %q leaves the module's directory", p) + return fmt.Errorf("the named path %q leaves the repository", p) } if _, err := os.Lstat(filepath.Join(tree, filepath.FromSlash(rel))); err != nil { return fmt.Errorf("the named path %q is not in the fetched tree: %w", p, err) diff --git a/modules/mesh-delivery-sdk/steprun/narrow_test.go b/modules/mesh-delivery-sdk/steprun/narrow_test.go index d27352ba..e62dd6de 100644 --- a/modules/mesh-delivery-sdk/steprun/narrow_test.go +++ b/modules/mesh-delivery-sdk/steprun/narrow_test.go @@ -68,6 +68,25 @@ func TestNarrowKeepsAModuleDirectorysPathsAndFailsOnAnAbsentOne(t *testing.T) { } } +// A path leaving the module's directory but not the repository is kept from the root; one leaving the repository is +// refused, naming the repository (design 47 H-A-198, H-A-199). +func TestNarrowKeepsASiblingOfTheRepositoryAndRefusesOneOutsideIt(t *testing.T) { + tree := t.TempDir() + for _, f := range []string{"modules/a/module.json", "modules/a/pkg/go.mod", "modules/a/notes.md", "modules/b/client/go.mod", + "modules/b/other.go", "root.txt"} { + writeTreeFile(t, tree, f, "x") + } + if err := Narrow(tree, sdkstep.FetchRequest{Dir: "modules/a", Paths: []string{"../../../x"}}); err == nil || !strings.Contains(err.Error(), "leaves the repository") { + t.Fatalf("a path leaving the repository gave %v", err) + } + if err := Narrow(tree, sdkstep.FetchRequest{Dir: "modules/a", Paths: []string{"pkg", "../b/client"}}); err != nil { + t.Fatal(err) + } + if got := strings.Join(filesUnder(t, tree), ","); got != "modules/a/module.json,modules/a/pkg/go.mod,modules/b/client/go.mod" { + t.Fatalf("the narrowed tree holds %s", got) + } +} + // A build that needs package.json or go.mod names it among its paths, and so keys on it (ADR 0499 clause 4). func TestNarrowKeepsAManifestOfTheLanguageOnlyWhenNamed(t *testing.T) { tree := t.TempDir()