From 3f3f30371a283704649e653c0bbb0e3c40c4c743 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Sun, 8 Nov 2026 23:05:25 -0600 Subject: [PATCH 1/9] fix(provider): keep local provider binaries on checksum mismatch --- pkg/provider/download.go | 107 +++++++++-- pkg/provider/local_source_safety_test.go | 235 +++++++++++++++++++++++ pkg/provider/resolve.go | 66 ++++++- 3 files changed, 386 insertions(+), 22 deletions(-) create mode 100644 pkg/provider/local_source_safety_test.go diff --git a/pkg/provider/download.go b/pkg/provider/download.go index f7add5319..559ff0ec2 100644 --- a/pkg/provider/download.go +++ b/pkg/provider/download.go @@ -132,26 +132,105 @@ func downloadBinaryForPlatform( continue } - // check if binary is correct - binaryTargetFolder := filepath.Join(targetFolder, strings.ToLower(binaryName)) - binaryPath := getBinaryPath(binary, binaryTargetFolder) - if verifyOrRemoveBinary(binaryPath, binary.Checksum) || - fromCache(binary, binaryTargetFolder) { - return binaryPath, nil - } - - // try to download the binary - binaryPath, err := downloadWithRetry(ctx, binaryName, binary, binaryTargetFolder) - if err != nil { - return "", err - } - return binaryPath, nil + return preparePlatformBinary(ctx, binaryName, binary, targetFolder) } return "", fmt.Errorf( "cannot download provider binary %s, because no binary location matched OS %s and ARCH %s", binaryName, runtime.GOOS, runtime.GOARCH) } +func preparePlatformBinary( + ctx context.Context, + binaryName string, + binary *ProviderBinary, + targetFolder string, +) (string, error) { + binaryTargetFolder := filepath.Join(targetFolder, strings.ToLower(binaryName)) + binaryPath := getBinaryPath(binary, binaryTargetFolder) + if err := verifyAvailableLocalSource(binary); err != nil { + return "", err + } + if isUnownedLocalBinary(binary.Path, binaryPath) { + return prepareLocalSource(binary, binaryPath, binaryTargetFolder) + } + if verifyOrRemoveBinary(binaryPath, binary.Checksum) || fromCache(binary, binaryTargetFolder) { + return binaryPath, nil + } + return downloadWithRetry(ctx, binaryName, binary, binaryTargetFolder) +} + +func verifyAvailableLocalSource(binary *ProviderBinary) error { + if isRemotePath(binary.Path) || filepath.IsAbs(binary.Path) { + return nil + } + _, err := verifyLocalBinary(binary.Path, binary.Checksum) + if errors.Is(err, os.ErrNotExist) { + return nil + } + return err +} + +func isUnownedLocalBinary(source, target string) bool { + return filepath.IsAbs(source) || sameLocalFile(source, target) +} + +func prepareLocalSource(binary *ProviderBinary, source, targetFolder string) (string, error) { + result, err := verifyLocalBinary(source, binary.Checksum) + if !filepath.IsAbs(binary.Path) || !errors.Is(err, os.ErrNotExist) { + return result, err + } + target := localTargetPath(binary, targetFolder) + absTarget, absErr := filepath.Abs(target) + if absErr != nil || filepath.Clean(absTarget) == filepath.Clean(source) { + return "", err + } + if _, targetErr := verifyLocalBinary(target, binary.Checksum); targetErr == nil { + return target, nil + } + return "", err +} + +func verifyLocalBinary(binaryPath, checksum string) (string, error) { + info, err := os.Stat(binaryPath) + if err != nil { + return "", fmt.Errorf("stat provider binary %s: %w", binaryPath, err) + } + if !info.Mode().IsRegular() { + return "", fmt.Errorf("provider binary %s is not a regular file", binaryPath) + } + if checksum == "" { + return binaryPath, nil + } + actual, err := hash.File(binaryPath) + if err != nil { + return "", fmt.Errorf("hash provider binary %s: %w", binaryPath, err) + } + if !strings.EqualFold(actual, checksum) { + return "", fmt.Errorf( + "provider binary %s SHA-256 mismatch: expected %s, actual %s; regenerate the manifest", + binaryPath, checksum, actual, + ) + } + return binaryPath, nil +} + +func sameLocalFile(source, target string) bool { + if isRemotePath(source) { + return false + } + sourcePath, sourceErr := filepath.Abs(source) + targetPath, targetErr := filepath.Abs(target) + if sourceErr == nil && targetErr == nil && sourcePath == targetPath { + return true + } + sourceInfo, err := os.Stat(source) + if err != nil { + return false + } + targetInfo, err := os.Stat(target) + return err == nil && os.SameFile(sourceInfo, targetInfo) +} + func downloadWithRetry( ctx context.Context, binaryName string, diff --git a/pkg/provider/local_source_safety_test.go b/pkg/provider/local_source_safety_test.go new file mode 100644 index 000000000..3a7db7223 --- /dev/null +++ b/pkg/provider/local_source_safety_test.go @@ -0,0 +1,235 @@ +package provider + +import ( + "context" + "crypto/sha256" + "fmt" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "runtime" + "strings" + "sync/atomic" + "testing" + + "github.com/stretchr/testify/suite" +) + +type localSourceSafetySuite struct{ suite.Suite } + +func TestLocalSourceSafety(t *testing.T) { suite.Run(t, new(localSourceSafetySuite)) } + +func (s *localSourceSafetySuite) TestChecksumMismatch() { + for _, tc := range []struct { + name string + remote bool + }{{name: "absolute source"}, {name: "downloaded source", remote: true}} { + s.Run(tc.name, func() { + dir := s.T().TempDir() + source := filepath.Join(dir, "source") + contents := []byte("actual binary") + s.Require().NoError(os.WriteFile(source, contents, 0o600)) + path := source + var requests atomic.Int32 + if tc.remote { + server := httptest.NewServer( + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + requests.Add(1) + _, _ = w.Write(contents) + }), + ) + defer server.Close() + path = server.URL + "/binary" + } + expected := strings.Repeat("0", 64) + actual := fmt.Sprintf("%x", sha256.Sum256(contents)) + binary := &ProviderBinary{ + OS: runtime.GOOS, + Arch: runtime.GOARCH, + Path: path, + Checksum: expected, + } + _, err := downloadBinaryForPlatform( + context.Background(), + "TEST", + []*ProviderBinary{binary}, + filepath.Join(dir, "storage"), + ) + s.Require().Error(err) + if tc.remote { + s.Positive(requests.Load()) + s.ErrorIs(err, errChecksumVerificationFailed) + s.NoFileExists(getBinaryPath(binary, filepath.Join(dir, "storage", "test"))) + } else { + s.FileExists(source) + s.ErrorContains(err, source) + s.ErrorContains(err, expected) + s.ErrorContains(err, actual) + s.ErrorContains(err, "regenerate") + } + }) + } +} + +func (s *localSourceSafetySuite) TestManifestPaths() { + dir := s.T().TempDir() + existing := filepath.Join(dir, "manifest") + s.Require().NoError(os.WriteFile(existing, []byte("name: test\n"), 0o600)) + for _, tc := range []struct { + name, path string + missing bool + }{ + {name: "missing yaml", path: filepath.Join(dir, "missing.yaml"), missing: true}, + {name: "missing yml", path: filepath.Join(dir, "missing.yml"), missing: true}, + {name: "missing absolute path", path: filepath.Join(dir, "missing"), missing: true}, + {name: "existing without extension", path: existing}, + } { + s.Run(tc.name, func() { + data, source, err := ResolveProvider(context.Background(), tc.path) + if tc.missing { + s.Require().Error(err) + s.ErrorContains(err, "provider manifest file not found: "+tc.path) + return + } + s.Require().NoError(err) + s.Equal("name: test\n", string(data)) + s.Equal(tc.path, source.File) + }) + } +} + +func (s *localSourceSafetySuite) TestRelativeSourceAtDestination() { + dir := s.T().TempDir() + s.Require().NoError(os.Mkdir(filepath.Join(dir, "test"), 0o750)) + source := filepath.Join(dir, "test", "binary") + s.Require().NoError(os.WriteFile(source, []byte("local"), 0o600)) + workingDir, err := os.Getwd() + s.Require().NoError(err) + relative, err := filepath.Rel(workingDir, source) + s.Require().NoError(err) + binary := &ProviderBinary{ + OS: runtime.GOOS, + Arch: runtime.GOARCH, + Path: relative, + Checksum: strings.Repeat("0", 64), + } + _, err = downloadBinaryForPlatform(context.Background(), "TEST", []*ProviderBinary{binary}, dir) + s.Require().ErrorContains(err, "regenerate") + s.FileExists(source) +} + +func (s *localSourceSafetySuite) TestInvalidCachedBinaryIsRemoved() { + file := filepath.Join(s.T().TempDir(), "cached") + s.Require().NoError(os.WriteFile(file, []byte("stale"), 0o600)) + s.False(verifyOrRemoveBinary(file, strings.Repeat("0", 64))) + s.NoFileExists(file) +} + +func (s *localSourceSafetySuite) TestGitHubSourcesDoNotResolveAsFiles() { + for _, source := range []string{"owner/repo", "owner/repo.yaml", "github.com/owner/repo.yml"} { + s.Run(source, func() { + _, ok, err := resolveFileProvider(source, &ProviderSource{}) + s.NoError(err) + s.False(ok) + }) + } +} + +func (s *localSourceSafetySuite) TestMissingAbsoluteSourceUsesStoredCopy() { + dir := s.T().TempDir() + target := filepath.Join(dir, "test", "binary") + s.Require().NoError(os.MkdirAll(filepath.Dir(target), 0o750)) + content := []byte("installed binary") + s.Require().NoError(os.WriteFile(target, content, 0o600)) + checksum := fmt.Sprintf("%x", sha256.Sum256(content)) + binary := &ProviderBinary{ + OS: runtime.GOOS, + Arch: runtime.GOARCH, + Path: filepath.Join(dir, "missing"), + Name: "binary", + Checksum: checksum, + } + got, err := downloadBinaryForPlatform( + context.Background(), + "TEST", + []*ProviderBinary{binary}, + dir, + ) + s.Require().NoError(err) + s.Equal(target, got) + s.FileExists(target) +} + +func (s *localSourceSafetySuite) TestMissingExplicitManifestForms() { + name := filepath.Base(s.T().TempDir()) + ".yaml" + home, err := os.UserHomeDir() + s.Require().NoError(err) + cases := []struct { + name, source, want string + local bool + }{ + { + name: "dot", + source: "./" + name, + want: filepath.Join(mustWorkingDir(s), name), + local: true, + }, + { + name: "parent", + source: "../" + name, + want: filepath.Join(filepath.Dir(mustWorkingDir(s)), name), + local: true, + }, + { + name: "absolute", + source: filepath.Join(s.T().TempDir(), name), + want: filepath.Join(s.T().TempDir(), name), + local: true, + }, + {name: "home", source: "~/" + name, want: filepath.Join(home, name), local: true}, + {name: "bare repository", source: "owner/repo.yaml"}, + {name: "bare yaml", source: name}, + } + cases[2].want = cases[2].source + if runtime.GOOS == windowsOS { + drivePath := filepath.Join(filepath.VolumeName(mustWorkingDir(s))+`\`, name) + cases = append(cases, struct { + name, source, want string + local bool + }{name: "backslash", source: `.\` + name, want: filepath.Join(mustWorkingDir(s), name), local: true}) + cases = append(cases, struct { + name, source, want string + local bool + }{name: "drive", source: drivePath, want: drivePath, local: true}) + } + for _, tc := range cases { + s.Run(tc.name, func() { + _, ok, err := resolveFileProvider(tc.source, &ProviderSource{}) + if !tc.local { + s.NoError(err) + s.False(ok) + return + } + s.True(ok) + s.Require().Error(err) + s.ErrorContains(err, "provider manifest file not found: "+tc.want) + }) + } +} + +func mustWorkingDir(s *localSourceSafetySuite) string { + dir, err := os.Getwd() + s.Require().NoError(err) + return dir +} + +func (s *localSourceSafetySuite) TestExplicitNonFileAndHomePrefix() { + dir := s.T().TempDir() + _, ok, err := resolveFileProvider(dir, &ProviderSource{}) + s.True(ok) + s.ErrorContains(err, "not a regular file") + path, err := expandManifestPath("~providers/provider.yaml") + s.NoError(err) + s.Equal("~providers/provider.yaml", path) +} diff --git a/pkg/provider/resolve.go b/pkg/provider/resolve.go index a740db405..5afc323ad 100644 --- a/pkg/provider/resolve.go +++ b/pkg/provider/resolve.go @@ -195,24 +195,32 @@ func resolveFileProvider( providerSource string, retSource *ProviderSource, ) ([]byte, bool, error) { - if !strings.HasSuffix(providerSource, ".yaml") && !strings.HasSuffix(providerSource, ".yml") { + explicit := isExplicitManifestPath(providerSource) + if skipManifestSource(providerSource, explicit) { return nil, false, nil } - - if _, err := os.Stat(providerSource); err != nil { - if os.IsNotExist(err) { - return nil, false, nil + filePath, err := expandManifestPath(providerSource) + if err != nil { + return nil, true, err + } + info, err := os.Stat(filePath) + if err != nil { + return resolveFileStatError(filePath, explicit, err) + } + if !info.Mode().IsRegular() { + if explicit { + return nil, true, fmt.Errorf("provider manifest is not a regular file: %s", filePath) } - return nil, true, fmt.Errorf("stat provider file %q: %w", providerSource, err) + return nil, false, nil } // #nosec G304 - providerSource is user-provided path for loading provider config - out, err := os.ReadFile(providerSource) + out, err := os.ReadFile(filePath) if err != nil { return nil, true, fmt.Errorf("read provider file %q: %w", providerSource, err) } - absPath, err := filepath.Abs(providerSource) + absPath, err := filepath.Abs(filePath) if err != nil { return nil, true, fmt.Errorf("resolve absolute path for %q: %w", providerSource, err) } @@ -220,6 +228,48 @@ func resolveFileProvider( return out, true, nil } +func skipManifestSource(source string, explicit bool) bool { + return !explicit && !hasYAMLSuffix(source) +} + +func isExplicitManifestPath(source string) bool { + return filepath.IsAbs(source) || strings.HasPrefix(source, "/") || + strings.HasPrefix(source, "./") || + strings.HasPrefix(source, "../") || + strings.HasPrefix(source, "~") || + strings.Contains(source, `\`) || + (len(source) >= 2 && source[1] == ':') +} + +func expandManifestPath(source string) (string, error) { + if source != "~" && !strings.HasPrefix(source, "~/") && !strings.HasPrefix(source, `~\`) { + return source, nil + } + home, err := os.UserHomeDir() + if err != nil { + return "", fmt.Errorf("resolve home directory: %w", err) + } + return filepath.Join(home, strings.TrimLeft(source[1:], `/\`)), nil +} + +func hasYAMLSuffix(source string) bool { + return strings.HasSuffix(source, ".yaml") || strings.HasSuffix(source, ".yml") +} + +func resolveFileStatError(source string, manifestPath bool, err error) ([]byte, bool, error) { + if !os.IsNotExist(err) { + return nil, true, fmt.Errorf("stat provider file %q: %w", source, err) + } + if !manifestPath { + return nil, false, nil + } + absPath, absErr := filepath.Abs(source) + if absErr != nil { + return nil, true, fmt.Errorf("resolve absolute path for %q: %w", source, absErr) + } + return nil, true, fmt.Errorf("provider manifest file not found: %s", absPath) +} + func downloadProvider(ctx context.Context, url string) ([]byte, error) { req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) if err != nil { From 9b9e0f7eaca10c3423103a72d8ae5b258906876a Mon Sep 17 00:00:00 2001 From: Samuel K Date: Sun, 8 Nov 2026 23:11:24 -0600 Subject: [PATCH 2/9] fix(provider): reuse verified local provider copies --- pkg/provider/download.go | 6 +++--- pkg/provider/local_source_safety_test.go | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/pkg/provider/download.go b/pkg/provider/download.go index 559ff0ec2..1cf80c53c 100644 --- a/pkg/provider/download.go +++ b/pkg/provider/download.go @@ -147,15 +147,15 @@ func preparePlatformBinary( ) (string, error) { binaryTargetFolder := filepath.Join(targetFolder, strings.ToLower(binaryName)) binaryPath := getBinaryPath(binary, binaryTargetFolder) - if err := verifyAvailableLocalSource(binary); err != nil { - return "", err - } if isUnownedLocalBinary(binary.Path, binaryPath) { return prepareLocalSource(binary, binaryPath, binaryTargetFolder) } if verifyOrRemoveBinary(binaryPath, binary.Checksum) || fromCache(binary, binaryTargetFolder) { return binaryPath, nil } + if err := verifyAvailableLocalSource(binary); err != nil { + return "", err + } return downloadWithRetry(ctx, binaryName, binary, binaryTargetFolder) } diff --git a/pkg/provider/local_source_safety_test.go b/pkg/provider/local_source_safety_test.go index 3a7db7223..4a6b01984 100644 --- a/pkg/provider/local_source_safety_test.go +++ b/pkg/provider/local_source_safety_test.go @@ -233,3 +233,22 @@ func (s *localSourceSafetySuite) TestExplicitNonFileAndHomePrefix() { s.NoError(err) s.Equal("~providers/provider.yaml", path) } + +func (s *localSourceSafetySuite) TestValidStoredCopySurvivesChangedRelativeSource() { + dir := s.T().TempDir() + source := filepath.Join(dir, "source") + stored := filepath.Join(dir, "storage", "test", "binary") + s.Require().NoError(os.MkdirAll(filepath.Dir(stored), 0o750)) + s.Require().NoError(os.WriteFile(source, []byte("changed"), 0o600)) + content := []byte("installed") + s.Require().NoError(os.WriteFile(stored, content, 0o600)) + cwd, err := os.Getwd() + s.Require().NoError(err) + relative, err := filepath.Rel(cwd, source) + s.Require().NoError(err) + binary := &ProviderBinary{OS: runtime.GOOS, Arch: runtime.GOARCH, Path: relative, Name: "binary", Checksum: fmt.Sprintf("%x", sha256.Sum256(content))} + got, err := downloadBinaryForPlatform(context.Background(), "TEST", []*ProviderBinary{binary}, filepath.Join(dir, "storage")) + s.Require().NoError(err) + s.Equal(stored, got) + s.FileExists(source) +} From 5ca99a6ecde198f157382ccb43c3653c68e66dd5 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Sun, 8 Nov 2026 23:11:52 -0600 Subject: [PATCH 3/9] test(provider): format stored copy regression --- pkg/provider/local_source_safety_test.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/pkg/provider/local_source_safety_test.go b/pkg/provider/local_source_safety_test.go index 4a6b01984..5906191ce 100644 --- a/pkg/provider/local_source_safety_test.go +++ b/pkg/provider/local_source_safety_test.go @@ -246,8 +246,19 @@ func (s *localSourceSafetySuite) TestValidStoredCopySurvivesChangedRelativeSourc s.Require().NoError(err) relative, err := filepath.Rel(cwd, source) s.Require().NoError(err) - binary := &ProviderBinary{OS: runtime.GOOS, Arch: runtime.GOARCH, Path: relative, Name: "binary", Checksum: fmt.Sprintf("%x", sha256.Sum256(content))} - got, err := downloadBinaryForPlatform(context.Background(), "TEST", []*ProviderBinary{binary}, filepath.Join(dir, "storage")) + binary := &ProviderBinary{ + OS: runtime.GOOS, + Arch: runtime.GOARCH, + Path: relative, + Name: "binary", + Checksum: fmt.Sprintf("%x", sha256.Sum256(content)), + } + got, err := downloadBinaryForPlatform( + context.Background(), + "TEST", + []*ProviderBinary{binary}, + filepath.Join(dir, "storage"), + ) s.Require().NoError(err) s.Equal(stored, got) s.FileExists(source) From 6cb5406fdc6f0bec889902dee617120f04c154b5 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Sun, 8 Nov 2026 23:27:09 -0600 Subject: [PATCH 4/9] fix(provider): reuse verified stored binaries in lookup --- pkg/provider/download.go | 13 +++++++++++-- pkg/provider/local_source_safety_test.go | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/pkg/provider/download.go b/pkg/provider/download.go index 1cf80c53c..b28467e1f 100644 --- a/pkg/provider/download.go +++ b/pkg/provider/download.go @@ -73,8 +73,8 @@ func GetBinariesFrom(config *ProviderConfig, binariesDir string) (map[string]str } targetFolder := filepath.Join(binariesDir, strings.ToLower(binaryName)) - binaryPath := getBinaryPath(binary, targetFolder) - if _, err := os.Stat(binaryPath); err != nil { + binaryPath, err := findBinaryPath(binary, targetFolder) + if err != nil { return nil, fmt.Errorf("error trying to find binary %s: %w", binaryName, err) } @@ -95,6 +95,15 @@ func GetBinariesFrom(config *ProviderConfig, binariesDir string) (map[string]str return retBinaries, nil } +func findBinaryPath(binary *ProviderBinary, targetFolder string) (string, error) { + binaryPath := getBinaryPath(binary, targetFolder) + _, err := os.Stat(binaryPath) + if filepath.IsAbs(binary.Path) && errors.Is(err, os.ErrNotExist) { + return prepareLocalSource(binary, binaryPath, targetFolder) + } + return binaryPath, err +} + func GetBinaries(context string, config *ProviderConfig) (map[string]string, error) { binariesDir, err := GetProviderBinariesDir(context, config.Name) if err != nil { diff --git a/pkg/provider/local_source_safety_test.go b/pkg/provider/local_source_safety_test.go index 5906191ce..b959de9f5 100644 --- a/pkg/provider/local_source_safety_test.go +++ b/pkg/provider/local_source_safety_test.go @@ -13,6 +13,7 @@ import ( "sync/atomic" "testing" + "github.com/devsy-org/devsy/pkg/hash" "github.com/stretchr/testify/suite" ) @@ -63,6 +64,9 @@ func (s *localSourceSafetySuite) TestChecksumMismatch() { s.NoFileExists(getBinaryPath(binary, filepath.Join(dir, "storage", "test"))) } else { s.FileExists(source) + got, readErr := hash.File(source) + s.Require().NoError(readErr) + s.Equal(actual, got) s.ErrorContains(err, source) s.ErrorContains(err, expected) s.ErrorContains(err, actual) @@ -117,6 +121,9 @@ func (s *localSourceSafetySuite) TestRelativeSourceAtDestination() { _, err = downloadBinaryForPlatform(context.Background(), "TEST", []*ProviderBinary{binary}, dir) s.Require().ErrorContains(err, "regenerate") s.FileExists(source) + got, readErr := hash.File(source) + s.Require().NoError(readErr) + s.Equal(fmt.Sprintf("%x", sha256.Sum256([]byte("local"))), got) } func (s *localSourceSafetySuite) TestInvalidCachedBinaryIsRemoved() { @@ -159,6 +166,14 @@ func (s *localSourceSafetySuite) TestMissingAbsoluteSourceUsesStoredCopy() { s.Require().NoError(err) s.Equal(target, got) s.FileExists(target) + config := &ProviderConfig{Binaries: map[string][]*ProviderBinary{"TEST": {binary}}} + binaries, err := GetBinariesFrom(config, dir) + s.Require().NoError(err) + s.Equal(target, binaries["TEST"]) + s.Require().NoError(os.WriteFile(target, []byte("changed"), 0o600)) + _, err = GetBinariesFrom(config, dir) + s.Require().Error(err) + s.FileExists(target) } func (s *localSourceSafetySuite) TestMissingExplicitManifestForms() { From 5782fcb48efdba7bcc8ad341cfc1462b70645619 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Sun, 8 Nov 2026 23:50:14 -0600 Subject: [PATCH 5/9] fix(provider): distinguish drive paths from bare sources --- pkg/provider/local_source_safety_test.go | 15 +++++++++++++++ pkg/provider/resolve.go | 12 +++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/pkg/provider/local_source_safety_test.go b/pkg/provider/local_source_safety_test.go index b959de9f5..17c54082a 100644 --- a/pkg/provider/local_source_safety_test.go +++ b/pkg/provider/local_source_safety_test.go @@ -278,3 +278,18 @@ func (s *localSourceSafetySuite) TestValidStoredCopySurvivesChangedRelativeSourc s.Equal(stored, got) s.FileExists(source) } + +func (s *localSourceSafetySuite) TestDrivePathClassification() { + cases := []struct { + path string + want bool + }{ + {path: "x:foo", want: runtime.GOOS == windowsOS}, + {path: "C:/missing.yaml", want: true}, + {path: `C:\missing.yaml`, want: true}, + {path: "1:foo", want: false}, + } + for _, tc := range cases { + s.Run(tc.path, func() { s.Equal(tc.want, isExplicitManifestPath(tc.path)) }) + } +} diff --git a/pkg/provider/resolve.go b/pkg/provider/resolve.go index 5afc323ad..9f2e5f419 100644 --- a/pkg/provider/resolve.go +++ b/pkg/provider/resolve.go @@ -8,6 +8,7 @@ import ( "net/url" "os" "path/filepath" + "runtime" "strings" "github.com/devsy-org/devsy/pkg/config" @@ -238,7 +239,16 @@ func isExplicitManifestPath(source string) bool { strings.HasPrefix(source, "../") || strings.HasPrefix(source, "~") || strings.Contains(source, `\`) || - (len(source) >= 2 && source[1] == ':') + isWindowsDrivePath(source) +} + +func isWindowsDrivePath(source string) bool { + if len(source) < 2 || source[1] != ':' { + return false + } + letter := source[0] | 0x20 + return letter >= 'a' && letter <= 'z' && + (runtime.GOOS == windowsOS || (len(source) > 2 && source[2] == '/')) } func expandManifestPath(source string) (string, error) { From 52ccb93cbb59298795bda3fa74ef2071b923c0a6 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Mon, 9 Nov 2026 00:28:24 -0600 Subject: [PATCH 6/9] fix(provider): verify local sources during lookup --- pkg/provider/download.go | 6 +-- pkg/provider/local_source_safety_test.go | 61 ++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/pkg/provider/download.go b/pkg/provider/download.go index b28467e1f..7b3c60bae 100644 --- a/pkg/provider/download.go +++ b/pkg/provider/download.go @@ -97,10 +97,10 @@ func GetBinariesFrom(config *ProviderConfig, binariesDir string) (map[string]str func findBinaryPath(binary *ProviderBinary, targetFolder string) (string, error) { binaryPath := getBinaryPath(binary, targetFolder) - _, err := os.Stat(binaryPath) - if filepath.IsAbs(binary.Path) && errors.Is(err, os.ErrNotExist) { + if filepath.IsAbs(binary.Path) { return prepareLocalSource(binary, binaryPath, targetFolder) } + _, err := os.Stat(binaryPath) return binaryPath, err } @@ -717,7 +717,7 @@ func downloadToTempFile(reader io.Reader) (string, error) { func localTargetPath(binary *ProviderBinary, targetFolder string) string { name := binary.Name if name == "" { - name = path.Base(binary.Path) + name = filepath.Base(binary.Path) } return filepath.Join(targetFolder, name) } diff --git a/pkg/provider/local_source_safety_test.go b/pkg/provider/local_source_safety_test.go index 17c54082a..0ee832b1e 100644 --- a/pkg/provider/local_source_safety_test.go +++ b/pkg/provider/local_source_safety_test.go @@ -170,6 +170,11 @@ func (s *localSourceSafetySuite) TestMissingAbsoluteSourceUsesStoredCopy() { binaries, err := GetBinariesFrom(config, dir) s.Require().NoError(err) s.Equal(target, binaries["TEST"]) + s.Require().NoError(os.WriteFile(binary.Path, []byte("changed source"), 0o600)) + _, err = GetBinariesFrom(config, dir) + s.Require().ErrorContains(err, "SHA-256 mismatch") + s.FileExists(binary.Path) + s.Require().NoError(os.Remove(binary.Path)) s.Require().NoError(os.WriteFile(target, []byte("changed"), 0o600)) _, err = GetBinariesFrom(config, dir) s.Require().Error(err) @@ -293,3 +298,59 @@ func (s *localSourceSafetySuite) TestDrivePathClassification() { s.Run(tc.path, func() { s.Equal(tc.want, isExplicitManifestPath(tc.path)) }) } } + +func (s *localSourceSafetySuite) TestMissingSourceFallbackUsesLocalFilename() { + dir := s.T().TempDir() + source := filepath.Join(dir, "source", "binary") + stored := filepath.Join(dir, "test", "binary") + s.Require().NoError(os.MkdirAll(filepath.Dir(stored), 0o750)) + contents := []byte("installed") + s.Require().NoError(os.WriteFile(stored, contents, 0o600)) + binary := &ProviderBinary{ + OS: runtime.GOOS, + Arch: runtime.GOARCH, + Path: source, + Checksum: fmt.Sprintf("%x", sha256.Sum256(contents)), + } + got, err := downloadBinaryForPlatform( + context.Background(), + "TEST", + []*ProviderBinary{binary}, + dir, + ) + s.Require().NoError(err) + s.Equal(stored, got) +} + +func (s *localSourceSafetySuite) TestValidLocalSources() { + for _, relative := range []bool{false, true} { + s.Run(fmt.Sprintf("relative=%t", relative), func() { + dir := s.T().TempDir() + source := filepath.Join(dir, "test", "binary") + s.Require().NoError(os.MkdirAll(filepath.Dir(source), 0o750)) + contents := []byte("valid binary") + s.Require().NoError(os.WriteFile(source, contents, 0o600)) + path := source + if relative { + cwd, err := os.Getwd() + s.Require().NoError(err) + path, err = filepath.Rel(cwd, source) + s.Require().NoError(err) + } + binary := &ProviderBinary{ + OS: runtime.GOOS, + Arch: runtime.GOARCH, + Path: path, + Checksum: fmt.Sprintf("%x", sha256.Sum256(contents)), + } + got, err := downloadBinaryForPlatform( + context.Background(), + "TEST", + []*ProviderBinary{binary}, + dir, + ) + s.Require().NoError(err) + s.Equal(source, got) + }) + } +} From 6a84527bafadcf5fd8f3a945189c7bffa132c2d4 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Mon, 9 Nov 2026 00:53:01 -0600 Subject: [PATCH 7/9] fix(provider): preserve local path verification semantics --- pkg/provider/download.go | 2 +- pkg/provider/local_source_safety_test.go | 28 ++++++++++++++++++++++++ pkg/provider/resolve.go | 19 ++++++++++++++-- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/pkg/provider/download.go b/pkg/provider/download.go index 7b3c60bae..82143d50c 100644 --- a/pkg/provider/download.go +++ b/pkg/provider/download.go @@ -97,7 +97,7 @@ func GetBinariesFrom(config *ProviderConfig, binariesDir string) (map[string]str func findBinaryPath(binary *ProviderBinary, targetFolder string) (string, error) { binaryPath := getBinaryPath(binary, targetFolder) - if filepath.IsAbs(binary.Path) { + if isUnownedLocalBinary(binary.Path, binaryPath) { return prepareLocalSource(binary, binaryPath, targetFolder) } _, err := os.Stat(binaryPath) diff --git a/pkg/provider/local_source_safety_test.go b/pkg/provider/local_source_safety_test.go index 0ee832b1e..adfbe57a5 100644 --- a/pkg/provider/local_source_safety_test.go +++ b/pkg/provider/local_source_safety_test.go @@ -124,6 +124,11 @@ func (s *localSourceSafetySuite) TestRelativeSourceAtDestination() { got, readErr := hash.File(source) s.Require().NoError(readErr) s.Equal(fmt.Sprintf("%x", sha256.Sum256([]byte("local"))), got) + binary.Checksum = fmt.Sprintf("%x", sha256.Sum256([]byte("expected"))) + config := &ProviderConfig{Binaries: map[string][]*ProviderBinary{"TEST": {binary}}} + _, err = GetBinariesFrom(config, dir) + s.Require().ErrorContains(err, "SHA-256 mismatch") + s.FileExists(source) } func (s *localSourceSafetySuite) TestInvalidCachedBinaryIsRemoved() { @@ -354,3 +359,26 @@ func (s *localSourceSafetySuite) TestValidLocalSources() { }) } } + +func (s *localSourceSafetySuite) TestHomeManifestSymlinkTraversal() { + if runtime.GOOS == windowsOS { + s.T().Skip("symlinks may require elevated privileges on Windows") + } + home := s.T().TempDir() + s.T().Setenv("HOME", home) + outer := s.T().TempDir() + s.Require().NoError(os.Mkdir(filepath.Join(outer, "nested"), 0o750)) + s.Require().NoError(os.Symlink(filepath.Join(outer, "nested"), filepath.Join(home, "alias"))) + expected := []byte("name: from-outer\n") + s.Require().NoError(os.WriteFile(filepath.Join(outer, "provider.yaml"), expected, 0o600)) + s.Require(). + NoError(os.WriteFile(filepath.Join(home, "provider.yaml"), []byte("name: from-home\n"), 0o600)) + source := &ProviderSource{} + data, ok, err := resolveFileProvider("~/alias/../provider.yaml", source) + s.Require().NoError(err) + s.True(ok) + s.Equal(expected, data) + reloaded, err := hash.File(source.File) + s.Require().NoError(err) + s.Equal(fmt.Sprintf("%x", sha256.Sum256(expected)), reloaded) +} diff --git a/pkg/provider/resolve.go b/pkg/provider/resolve.go index 9f2e5f419..52f834ab5 100644 --- a/pkg/provider/resolve.go +++ b/pkg/provider/resolve.go @@ -221,7 +221,7 @@ func resolveFileProvider( return nil, true, fmt.Errorf("read provider file %q: %w", providerSource, err) } - absPath, err := filepath.Abs(filePath) + absPath, err := absoluteManifestPath(filePath) if err != nil { return nil, true, fmt.Errorf("resolve absolute path for %q: %w", providerSource, err) } @@ -229,6 +229,13 @@ func resolveFileProvider( return out, true, nil } +func absoluteManifestPath(filePath string) (string, error) { + if filepath.IsAbs(filePath) { + return filePath, nil + } + return filepath.Abs(filePath) +} + func skipManifestSource(source string, explicit bool) bool { return !explicit && !hasYAMLSuffix(source) } @@ -259,7 +266,15 @@ func expandManifestPath(source string) (string, error) { if err != nil { return "", fmt.Errorf("resolve home directory: %w", err) } - return filepath.Join(home, strings.TrimLeft(source[1:], `/\`)), nil + return strings.TrimRight( + home, + `/\`, + ) + string( + filepath.Separator, + ) + strings.TrimLeft( + source[1:], + `/\`, + ), nil } func hasYAMLSuffix(source string) bool { From f716ab62b22f070cac16b1f5e2458959305ebe41 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Mon, 9 Nov 2026 00:57:42 -0600 Subject: [PATCH 8/9] fix(provider): resolve parent traversal for manifest assets --- pkg/provider/local_source_safety_test.go | 3 +++ pkg/provider/resolve.go | 10 ++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/pkg/provider/local_source_safety_test.go b/pkg/provider/local_source_safety_test.go index adfbe57a5..be8909f4d 100644 --- a/pkg/provider/local_source_safety_test.go +++ b/pkg/provider/local_source_safety_test.go @@ -381,4 +381,7 @@ func (s *localSourceSafetySuite) TestHomeManifestSymlinkTraversal() { reloaded, err := hash.File(source.File) s.Require().NoError(err) s.Equal(fmt.Sprintf("%x", sha256.Sum256(expected)), reloaded) + resolvedOuter, err := filepath.EvalSymlinks(outer) + s.Require().NoError(err) + s.Equal(resolvedOuter, filepath.Dir(source.File)) } diff --git a/pkg/provider/resolve.go b/pkg/provider/resolve.go index 52f834ab5..b9362738d 100644 --- a/pkg/provider/resolve.go +++ b/pkg/provider/resolve.go @@ -230,8 +230,14 @@ func resolveFileProvider( } func absoluteManifestPath(filePath string) (string, error) { - if filepath.IsAbs(filePath) { - return filePath, nil + for part := range strings.SplitSeq(filepath.ToSlash(filePath), "/") { + if part == ".." { + resolved, err := filepath.EvalSymlinks(filePath) + if err != nil { + return "", err + } + return filepath.Abs(resolved) + } } return filepath.Abs(filePath) } From be0ec10f045f3e90ace1b723246ce16870a03ab9 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Mon, 9 Nov 2026 02:55:32 -0600 Subject: [PATCH 9/9] fix(provider): resolve verified stored runtimes safely --- pkg/provider/download.go | 11 ++-- pkg/provider/local_source_safety_test.go | 75 +++++++++++++++++++++--- 2 files changed, 73 insertions(+), 13 deletions(-) diff --git a/pkg/provider/download.go b/pkg/provider/download.go index 82143d50c..6ace9ea4b 100644 --- a/pkg/provider/download.go +++ b/pkg/provider/download.go @@ -227,11 +227,6 @@ func sameLocalFile(source, target string) bool { if isRemotePath(source) { return false } - sourcePath, sourceErr := filepath.Abs(source) - targetPath, targetErr := filepath.Abs(target) - if sourceErr == nil && targetErr == nil && sourcePath == targetPath { - return true - } sourceInfo, err := os.Stat(source) if err != nil { return false @@ -438,6 +433,12 @@ func getBinaryFileName(binary *ProviderBinary) string { func getBinaryPath(binary *ProviderBinary, targetFolder string) string { if filepath.IsAbs(binary.Path) { + if _, err := os.Stat(binary.Path); errors.Is(err, os.ErrNotExist) { + target := localTargetPath(binary, targetFolder) + if _, targetErr := verifyLocalBinary(target, binary.Checksum); targetErr == nil { + return target + } + } return binary.Path } diff --git a/pkg/provider/local_source_safety_test.go b/pkg/provider/local_source_safety_test.go index be8909f4d..aa304279f 100644 --- a/pkg/provider/local_source_safety_test.go +++ b/pkg/provider/local_source_safety_test.go @@ -17,6 +17,8 @@ import ( "github.com/stretchr/testify/suite" ) +const fixtureBinaryName = "binary" + type localSourceSafetySuite struct{ suite.Suite } func TestLocalSourceSafety(t *testing.T) { suite.Run(t, new(localSourceSafetySuite)) } @@ -106,7 +108,7 @@ func (s *localSourceSafetySuite) TestManifestPaths() { func (s *localSourceSafetySuite) TestRelativeSourceAtDestination() { dir := s.T().TempDir() s.Require().NoError(os.Mkdir(filepath.Join(dir, "test"), 0o750)) - source := filepath.Join(dir, "test", "binary") + source := filepath.Join(dir, "test", fixtureBinaryName) s.Require().NoError(os.WriteFile(source, []byte("local"), 0o600)) workingDir, err := os.Getwd() s.Require().NoError(err) @@ -150,7 +152,7 @@ func (s *localSourceSafetySuite) TestGitHubSourcesDoNotResolveAsFiles() { func (s *localSourceSafetySuite) TestMissingAbsoluteSourceUsesStoredCopy() { dir := s.T().TempDir() - target := filepath.Join(dir, "test", "binary") + target := filepath.Join(dir, "test", fixtureBinaryName) s.Require().NoError(os.MkdirAll(filepath.Dir(target), 0o750)) content := []byte("installed binary") s.Require().NoError(os.WriteFile(target, content, 0o600)) @@ -159,7 +161,7 @@ func (s *localSourceSafetySuite) TestMissingAbsoluteSourceUsesStoredCopy() { OS: runtime.GOOS, Arch: runtime.GOARCH, Path: filepath.Join(dir, "missing"), - Name: "binary", + Name: fixtureBinaryName, Checksum: checksum, } got, err := downloadBinaryForPlatform( @@ -262,7 +264,7 @@ func (s *localSourceSafetySuite) TestExplicitNonFileAndHomePrefix() { func (s *localSourceSafetySuite) TestValidStoredCopySurvivesChangedRelativeSource() { dir := s.T().TempDir() source := filepath.Join(dir, "source") - stored := filepath.Join(dir, "storage", "test", "binary") + stored := filepath.Join(dir, "storage", "test", fixtureBinaryName) s.Require().NoError(os.MkdirAll(filepath.Dir(stored), 0o750)) s.Require().NoError(os.WriteFile(source, []byte("changed"), 0o600)) content := []byte("installed") @@ -275,7 +277,7 @@ func (s *localSourceSafetySuite) TestValidStoredCopySurvivesChangedRelativeSourc OS: runtime.GOOS, Arch: runtime.GOARCH, Path: relative, - Name: "binary", + Name: fixtureBinaryName, Checksum: fmt.Sprintf("%x", sha256.Sum256(content)), } got, err := downloadBinaryForPlatform( @@ -306,8 +308,8 @@ func (s *localSourceSafetySuite) TestDrivePathClassification() { func (s *localSourceSafetySuite) TestMissingSourceFallbackUsesLocalFilename() { dir := s.T().TempDir() - source := filepath.Join(dir, "source", "binary") - stored := filepath.Join(dir, "test", "binary") + source := filepath.Join(dir, "source", fixtureBinaryName) + stored := filepath.Join(dir, "test", fixtureBinaryName) s.Require().NoError(os.MkdirAll(filepath.Dir(stored), 0o750)) contents := []byte("installed") s.Require().NoError(os.WriteFile(stored, contents, 0o600)) @@ -331,7 +333,7 @@ func (s *localSourceSafetySuite) TestValidLocalSources() { for _, relative := range []bool{false, true} { s.Run(fmt.Sprintf("relative=%t", relative), func() { dir := s.T().TempDir() - source := filepath.Join(dir, "test", "binary") + source := filepath.Join(dir, "test", fixtureBinaryName) s.Require().NoError(os.MkdirAll(filepath.Dir(source), 0o750)) contents := []byte("valid binary") s.Require().NoError(os.WriteFile(source, contents, 0o600)) @@ -385,3 +387,60 @@ func (s *localSourceSafetySuite) TestHomeManifestSymlinkTraversal() { s.Require().NoError(err) s.Equal(resolvedOuter, filepath.Dir(source.File)) } + +func (s *localSourceSafetySuite) TestRelativeParentTraversalDoesNotAliasTarget() { + if runtime.GOOS == windowsOS { + s.T().Skip("symlinks may require elevated privileges on Windows") + } + dir := s.T().TempDir() + folder := filepath.Join(dir, "test") + outside := s.T().TempDir() + s.Require().NoError(os.MkdirAll(filepath.Join(outside, "nested"), 0o750)) + s.Require().NoError(os.MkdirAll(folder, 0o750)) + s.Require(). + NoError(os.Symlink(filepath.Join(outside, "nested"), filepath.Join(folder, "alias"))) + content := []byte("correct source") + s.Require().NoError(os.WriteFile(filepath.Join(outside, fixtureBinaryName), content, 0o600)) + target := filepath.Join(folder, fixtureBinaryName) + s.Require().NoError(os.WriteFile(target, []byte("stale target"), 0o600)) + cwd, err := os.Getwd() + s.Require().NoError(err) + alias, err := filepath.Rel(cwd, filepath.Join(folder, "alias")) + s.Require().NoError(err) + binary := &ProviderBinary{ + OS: runtime.GOOS, + Arch: runtime.GOARCH, + Path: alias + "/../binary", + Name: fixtureBinaryName, + Checksum: fmt.Sprintf("%x", sha256.Sum256(content)), + } + got, err := downloadBinaryForPlatform( + context.Background(), + "TEST", + []*ProviderBinary{binary}, + dir, + ) + s.Require().NoError(err) + s.Equal(target, got) + s.FileExists(filepath.Join(outside, fixtureBinaryName)) +} + +func (s *localSourceSafetySuite) TestMissingExternalRuntimeUsesVerifiedStoredCopy() { + dir := s.T().TempDir() + agent := externalAgentFixture() + binary := agent.Binaries["RUNTIME"][0] + binary.Path = filepath.Join(dir, "missing") + binary.Name = "runtime" + stored := filepath.Join(dir, "runtime", "runtime") + s.Require().NoError(os.MkdirAll(filepath.Dir(stored), 0o750)) + fixture := &externalConfigSuite{} + fixture.SetT(s.T()) + fixture.writeExecutable(stored, externalFixturePayload) + resolved, err := ResolveExternalRuntimeBinary(agent, dir) + s.Require().NoError(err) + s.Equal(stored, resolved) + s.Require().NoError(os.WriteFile(stored, []byte("tampered"), 0o600)) + _, err = ResolveExternalRuntimeBinary(agent, dir) + s.Require().Error(err) + s.FileExists(stored) +}