Skip to content

test(utils): cover path verification, sanitization, and unique naming#9978

Merged
mudler merged 1 commit into
mudler:masterfrom
shihyunhuang:test/path-utils
May 29, 2026
Merged

test(utils): cover path verification, sanitization, and unique naming#9978
mudler merged 1 commit into
mudler:masterfrom
shihyunhuang:test/path-utils

Conversation

@TLoE419

@TLoE419 TLoE419 commented May 25, 2026

Copy link
Copy Markdown
Contributor

Summary

pkg/utils/path.go provides the security primitives for download paths (VerifyPath, InTrustedRoot) plus the file-naming helpers used by every import flow (SanitizeFileName, GenerateUniqueFileName). None of them had any test coverage, so a future regression in the traversal check or in the .. stripping inside SanitizeFileName would land unnoticed.

This PR adds pkg/utils/path_test.go covering all four helpers. No production code changes.

What the new specs pin

  • VerifyPath — accepts strict descendants and inner traversal that stays inside the base; rejects .., compound traversal, and the base path itself. An explicit spec documents that the check is purely lexical (filepath.Clean, not EvalSymlinks), so any future caller that needs symlink-aware defence knows to EvalSymlinks first.
  • InTrustedRoot — rejects the trusted root itself and sibling directories; accepts deeply nested descendants.
  • SanitizeFileName — covers the leading-directory and absolute-prefix paths plus the embedded .. case (foo..barfoobar) that the Clean+Base layer alone would leave intact.
  • GenerateUniqueFileName — covers the no-collision, single-collision, walk-the-counter, and empty-extension cases. All file-touching specs use GinkgoT().TempDir() so the suite stays hermetic.

Test plan

  • go test ./pkg/utils/... — 47/47 specs pass (24 new, 23 pre-existing)
  • go vet ./pkg/utils/... clean
  • Tests follow the project's Ginkgo/Gomega-only convention (package utils_test, Describe/It, no stdlib testing calls)

@TLoE419 TLoE419 closed this May 25, 2026
@TLoE419 TLoE419 deleted the test/path-utils branch May 25, 2026 02:29
@TLoE419 TLoE419 restored the test/path-utils branch May 25, 2026 03:42
@TLoE419 TLoE419 reopened this May 26, 2026
pkg/utils/path.go provides the security primitives for download paths
(VerifyPath, InTrustedRoot) and the file-naming helpers used by every
import flow (SanitizeFileName, GenerateUniqueFileName). None of them had
test coverage, so a future regression in the traversal check or in the
".." stripping inside SanitizeFileName would land unnoticed.

The new specs pin the lexical contract for each helper:

- VerifyPath accepts strict descendants and inner traversal that stays
  inside the base, rejects "..", compound traversal, and the base path
  itself. An explicit spec documents that the check is purely lexical
  (filepath.Clean, not EvalSymlinks) so any future caller that needs
  symlink-aware defence knows to EvalSymlinks first.
- InTrustedRoot rejects the trusted root and sibling directories,
  accepts deeply nested descendants.
- SanitizeFileName covers the leading-directory and absolute-prefix
  paths plus the embedded ".." case ("foo..bar" -> "foobar") that the
  Clean+Base layer alone would leave intact.
- GenerateUniqueFileName covers the no-collision, single-collision,
  walk-the-counter, and empty-extension cases using GinkgoT().TempDir()
  so the suite stays hermetic.

Assisted-by: Claude:claude-opus-4-7 [Claude Code]
Signed-off-by: TLoE419 <tloemizuchizu@gmail.com>
@TLoE419 TLoE419 force-pushed the test/path-utils branch from 9453ee3 to aa6e8b9 Compare May 26, 2026 20:47
@mudler mudler enabled auto-merge (squash) May 29, 2026 10:21
@mudler mudler merged commit fc2bd09 into mudler:master May 29, 2026
54 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants