package engine import ( "io/fs" "os" "path/filepath" "testing" "testing/fstest" ) func TestSelectGlobFiltersOnlyWildcardDiscoveredEntries(t *testing.T) { t.Parallel() root := t.TempDir() for name, content := range map[string]string{ ".hidden/secret.yaml": "value: hidden\n", "visible/item.yaml": "value: visible\n", "visible/source.txt": "value: source\n", } { filename := filepath.Join(root, filepath.FromSlash(name)) if err := os.MkdirAll(filepath.Dir(filename), 0o700); err != nil { t.Fatal(err) } if err := os.WriteFile(filename, []byte(content), 0o600); err != nil { t.Fatal(err) } } for alias, target := range map[string]string{ "visible-alias": "visible", "selected.yaml": "visible/source.txt", "unselected.txt": "visible/item.yaml", ".secret.yaml": "visible/source.txt", } { if err := os.Symlink(target, filepath.Join(root, filepath.FromSlash(alias))); err != nil { t.Skipf("symlinks are unavailable: %v", err) } } access, err := openHostAccess(root) if err != nil { t.Fatal(err) } defer access.close() selection := selectGlobForTest(t, access, root, ` - glob: '*/*.yaml' follow: false hidden: false `) if got, want := selectedPathsRelativeTo(selection, root), []string{"visible/item.yaml"}; !equalStrings(got, want) { t.Fatalf("filtered glob candidates = %#v, want %#v", got, want) } selection = selectGlobForTest(t, access, root, ` - glob: '.hidden/*.yaml' follow: false hidden: false `) if got, want := selectedPathsRelativeTo(selection, root), []string{".hidden/secret.yaml"}; !equalStrings(got, want) { t.Fatalf("literal hidden component candidates = %#v, want %#v", got, want) } selection = selectGlobForTest(t, access, root, ` - glob: 'visible-alias/*.yaml' follow: false hidden: false `) if got, want := selectedPathsRelativeTo(selection, root), []string{"visible-alias/item.yaml"}; !equalStrings(got, want) { t.Fatalf("literal symlink component candidates = %#v, want %#v", got, want) } selection = selectGlobForTest(t, access, root, ` - glob: '*.yaml' follow: false hidden: true `) if len(selection.candidates) != 0 { t.Fatalf("follow:false selected a discovered file symlink: %#v", selection.candidates) } selection = selectGlobForTest(t, access, root, ` - glob: '*.yaml' follow: true hidden: false `) if got, want := selectedPathsRelativeTo(selection, root), []string{"selected.yaml"}; !equalStrings(got, want) { t.Fatalf("filename aliases were not filtered by candidate spelling: %#v, want %#v", got, want) } } func TestSelectGlobAppliesExclusionsBeforeSourceAdmissionWithoutOpeningFiles(t *testing.T) { t.Parallel() filesystem := &observedGlobFilesystem{files: fstest.MapFS{ "parts": &fstest.MapFile{Mode: fs.ModeDir | 0o700}, "parts/keep.yaml": &fstest.MapFile{Data: []byte("not parsed: [\n")}, "parts/skip-one.yaml": &fstest.MapFile{Data: []byte("not parsed: [\n")}, "parts/skip-two.yaml": &fstest.MapFile{Data: []byte("not parsed: [\n")}, "parts/skip-three.yaml": &fstest.MapFile{Data: []byte("not parsed: [\n")}, }} access := newNativeAccess(filesystem, ".", &namespaceToken{}) specs, diagnostics := parseImportSpecsYAML(t, ` - glob: 'parts/*.yaml' exclude: 'skip-*.yaml' `) if len(diagnostics) != 0 { t.Fatal(diagnostics) } policy, diagnostics := resolveImportPolicy(specs[0], ImportDefaults{}) if len(diagnostics) != 0 { t.Fatal(diagnostics) } limits := defaultImportLimits() limits.sources = 2 accounting := &importAccounting{sources: 1} selection, diagnostics := selectGlobImportCandidates(access, ".", specs[0], policy, limits, accounting) if len(diagnostics) != 0 || !equalStrings(selectedPaths(selection), []string{"parts/keep.yaml"}) { t.Fatalf("post-exclusion selection, diagnostics = %#v, %#v", selection, diagnostics) } if len(filesystem.opened) != 1 || filesystem.opened[0] != "parts" { t.Fatalf("glob opened paths = %#v, want only the parts directory", filesystem.opened) } if accounting.sources+len(selection.candidates) > limits.sources { t.Fatalf("final candidate set exceeds the private source limit: accounting=%#v selection=%#v", accounting, selection.candidates) } } func TestSelectGlobRejectsEscapingWildcardLinks(t *testing.T) { t.Parallel() root := t.TempDir() outside := t.TempDir() if err := os.WriteFile(filepath.Join(outside, "file.yaml"), []byte("value: outside\n"), 0o600); err != nil { t.Fatal(err) } if err := os.Symlink(outside, filepath.Join(root, "escape")); err != nil { t.Skipf("symlinks are unavailable: %v", err) } access, err := openHostAccess(root) if err != nil { t.Fatal(err) } defer access.close() _, diagnostics := selectGlobForTestWithDiagnostics(t, access, root, ` - glob: '*/file.yaml' follow: true `) if len(diagnostics) != 1 || diagnostics[0].Code != CodeImportOutsideRoot { t.Fatalf("escaping glob diagnostics = %#v, want import.outside_root", diagnostics) } selection := selectGlobForTest(t, access, root, ` - glob: '*/file.yaml' follow: false `) if len(selection.candidates) != 0 { t.Fatalf("follow:false selected an escaping alias: %#v", selection.candidates) } } func TestSelectGlobPreservesMatchedDirectoryValidityBeforeExclusion(t *testing.T) { t.Parallel() filesystem := fstest.MapFS{ "parts": &fstest.MapFile{Mode: fs.ModeDir | 0o700}, "parts/matched.yaml": &fstest.MapFile{Mode: fs.ModeDir | 0o700}, "parts/matched.yaml/x": &fstest.MapFile{Data: []byte("value: x\n")}, } access := newNativeAccess(filesystem, ".", &namespaceToken{}) _, diagnostics := selectGlobForTestWithDiagnostics(t, access, ".", ` - glob: 'parts/*.yaml' exclude: '*.yaml' `) if len(diagnostics) != 1 || diagnostics[0].Code != CodeSourceNotRegular { t.Fatalf("excluded directory diagnostics = %#v, want source.not_regular", diagnostics) } } func selectGlobForTest(t *testing.T, access *filesystemAccess, base, declaration string) importSelection { t.Helper() selection, diagnostics := selectGlobForTestWithDiagnostics(t, access, base, declaration) if len(diagnostics) != 0 { t.Fatalf("glob selection diagnostics = %#v", diagnostics) } return selection } func selectGlobForTestWithDiagnostics(t *testing.T, access *filesystemAccess, base, declaration string) (importSelection, []Diagnostic) { t.Helper() specs, diagnostics := parseImportSpecsYAML(t, declaration) if len(diagnostics) != 0 { t.Fatalf("parse diagnostics = %#v", diagnostics) } policy, diagnostics := resolveImportPolicy(specs[0], ImportDefaults{}) if len(diagnostics) != 0 { t.Fatalf("policy diagnostics = %#v", diagnostics) } return selectGlobImportCandidates(access, base, specs[0], policy, defaultImportLimits(), &importAccounting{}) } type observedGlobFilesystem struct { files fstest.MapFS opened []string failStat string } func (filesystem *observedGlobFilesystem) Open(name string) (fs.File, error) { filesystem.opened = append(filesystem.opened, name) return filesystem.files.Open(name) } func (filesystem *observedGlobFilesystem) Stat(name string) (fs.FileInfo, error) { if name == filesystem.failStat { return nil, fs.ErrPermission } return fs.Stat(filesystem.files, name) } func TestSelectGlobInspectsWildcardBeforeParent(t *testing.T) { t.Parallel() filesystem := &observedGlobFilesystem{files: fstest.MapFS{ "services/unit/marker": &fstest.MapFile{}, "services/shared/one.yml": &fstest.MapFile{Data: []byte("value: one\n")}, "services/irrelevant": &fstest.MapFile{}, }} access := newNativeAccess(filesystem, ".", &namespaceToken{}) declaration := " - glob: services/*/../shared/*.yml\n required: true\n" selection, diagnostics := selectGlobForTestWithDiagnostics(t, access, ".", declaration) if len(diagnostics) != 0 || !equalStrings(selectedPaths(selection), []string{"services/shared/one.yml"}) { t.Fatalf("selection=%#v, diagnostics=%#v", selection, diagnostics) } filesystem.failStat = "services/unit" _, diagnostics = selectGlobForTestWithDiagnostics(t, access, ".", declaration+" exclude: '*.yml'\n") if len(diagnostics) == 0 || diagnostics[0].Code != CodeImportPermission { t.Fatalf("matched directory failure=%#v", diagnostics) } missing := selectGlobForTest(t, access, ".", " - glob: services/absent*/../shared/*.yml\n") if len(missing.candidates) != 0 { t.Fatalf("unmatched wildcard was cleaned away: %#v", missing) } } func TestSelectGlobParentUsesMatchedLinkTarget(t *testing.T) { root := canonicalTestPath(t, t.TempDir()) writeImportFile(t, root, "targets/unit/marker", "marker") file := writeImportFile(t, root, "targets/shared/one.yml", "value: one\n") if err := os.MkdirAll(filepath.Join(root, "services"), 0700); err != nil { t.Fatal(err) } if err := os.Symlink("../targets/unit", filepath.Join(root, "services", "alias")); err != nil { t.Skipf("symlinks unavailable: %v", err) } host, err := openHostAccess(root) if err != nil { t.Fatal(err) } defer host.close() native := newNativeAccess(os.DirFS(root), ".", &namespaceToken{}) for _, mode := range []struct { name string access *filesystemAccess base, want string }{ {"host", host, root, file}, {"native", native, ".", "targets/shared/one.yml"}, } { t.Run(mode.name, func(t *testing.T) { for _, pattern := range []string{"services/*/../shared/*.yml", "services/alias/../shared/*.yml"} { selection, diagnostics := selectGlobForTestWithDiagnostics(t, mode.access, mode.base, " - glob: "+pattern+"\n required: true\n") if len(diagnostics) != 0 || len(selection.candidates) != 1 || selection.candidates[0].accessPath != mode.want { t.Fatalf("pattern %q: selection=%#v, diagnostics=%#v", pattern, selection, diagnostics) } } selection := selectGlobForTest(t, mode.access, mode.base, " - glob: services/*/../shared/*.yml\n follow: false\n") if len(selection.candidates) != 0 { t.Fatalf("unfollowed link selected=%#v", selection) } }) } } func TestSelectGlobRetainsDistinctPhysicalParents(t *testing.T) { root := canonicalTestPath(t, t.TempDir()) for _, branch := range []string{"one", "two"} { writeImportFile(t, root, branch+"/unit/marker", "marker") writeImportFile(t, root, branch+"/shared/"+branch+".yml", "value: "+branch+"\n") } if err := os.Mkdir(filepath.Join(root, "services"), 0700); err != nil { t.Fatal(err) } for alias, target := range map[string]string{"a": "../one/unit", "b": "../two/unit"} { if err := os.Symlink(target, filepath.Join(root, "services", alias)); err != nil { t.Skipf("symlinks unavailable: %v", err) } } access, err := openHostAccess(root) if err != nil { t.Fatal(err) } defer access.close() for _, mode := range []struct { name string access *filesystemAccess base string }{ {"host", access, root}, {"native", newNativeAccess(os.DirFS(root), ".", &namespaceToken{}), "."}, } { t.Run(mode.name, func(t *testing.T) { selection := selectGlobForTest(t, mode.access, mode.base, " - glob: services/*/../shared/*.yml\n required: true\n") if len(selection.candidates) != 2 { t.Fatalf("distinct physical parents coalesced: %#v", selection) } }) } }