From f3180bf2ea0d4f437229c6d8e3834622d330926b Mon Sep 17 00:00:00 2001 From: IzanVil Date: Sat, 26 Sep 2026 23:14:30 +0200 Subject: [PATCH 1/5] fix(sources): resolve symlinks when computing repo-relative lock paths go-git reports the project worktree root with symlinks resolved, while paths derived from the project config keep the invocation directory. When the project is reached through a symlink, the repo-relative lock file path became "../../..", the committed lock was not found at HEAD, and 'component render' silently dropped the synthetic changelog history and regressed the release number. Add git.RepoRelPath, which resolves symlinks on both sides before relativizing and rejects paths that escape the repository root, and use it for the lock file and upstream-commit TOML paths in synthetic history. Fixes #188 Signed-off-by: IzanVil --- .../app/azldev/core/sources/synthistory.go | 2 +- .../core/sources/synthistory_internal_test.go | 104 ++++++++++++++++++ .../sources/synthistory_upstreamcommit.go | 4 +- internal/utils/git/gogit.go | 52 +++++++++ internal/utils/git/gogit_test.go | 90 +++++++++++++++ 5 files changed, 249 insertions(+), 3 deletions(-) create mode 100644 internal/app/azldev/core/sources/synthistory_internal_test.go create mode 100644 internal/utils/git/gogit_test.go diff --git a/internal/app/azldev/core/sources/synthistory.go b/internal/app/azldev/core/sources/synthistory.go index ae0467eef..556632ab8 100644 --- a/internal/app/azldev/core/sources/synthistory.go +++ b/internal/app/azldev/core/sources/synthistory.go @@ -486,7 +486,7 @@ func buildSyntheticCommits( return nil, "", fmt.Errorf("resolving lock file path for %#q:\n%w", componentName, err) } - lockFileRelPath, err := filepath.Rel(projectRepoDir, lockFileAbsPath) + lockFileRelPath, err := git.RepoRelPath(projectRepoDir, lockFileAbsPath) if err != nil { return nil, "", fmt.Errorf("failed to compute repo-relative lock path for %#q:\n%w", lockFileAbsPath, err) diff --git a/internal/app/azldev/core/sources/synthistory_internal_test.go b/internal/app/azldev/core/sources/synthistory_internal_test.go new file mode 100644 index 000000000..303c2de52 --- /dev/null +++ b/internal/app/azldev/core/sources/synthistory_internal_test.go @@ -0,0 +1,104 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +package sources + +import ( + "os" + "os/exec" + "path/filepath" + "strings" + "testing" + "time" + + gogit "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/plumbing/object" + "github.com/microsoft/azure-linux-dev-tools/internal/global/testctx" + "github.com/microsoft/azure-linux-dev-tools/internal/projectconfig" + "github.com/microsoft/azure-linux-dev-tools/internal/utils/fileperms" + "github.com/microsoft/azure-linux-dev-tools/internal/utils/fileutils" + "github.com/spf13/afero" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestBuildSyntheticCommits_SymlinkedProjectPath(t *testing.T) { + const ( + componentName = "curl" + importCommit = "1111111111111111111111111111111111111111" + upstreamCommit = "2222222222222222222222222222222222222222" + ) + + tempDir := t.TempDir() + realParentDir := filepath.Join(tempDir, "real") + realProjectDir := filepath.Join(realParentDir, "project") + linkedProjectDir := filepath.Join(tempDir, "link", "project") + + osFS := afero.NewOsFs() + + require.NoError(t, fileutils.MkdirAll(osFS, filepath.Join(realProjectDir, "locks"))) + require.NoError(t, os.Symlink(realParentDir, filepath.Join(tempDir, "link"))) + + require.NoError(t, fileutils.WriteFile(osFS, + filepath.Join(realProjectDir, projectconfig.DefaultConfigFileName), + []byte("[components."+componentName+"]\n"), fileperms.PublicFile)) + + require.NoError(t, fileutils.WriteFile(osFS, + filepath.Join(realProjectDir, "locks", componentName+".lock"), + []byte(strings.Join([]string{ + "version = 1", + `import-commit = "` + importCommit + `"`, + `upstream-commit = "` + upstreamCommit + `"`, + `input-fingerprint = "sha256:abc"`, + }, "\n")+"\n"), fileperms.PublicFile)) + + repo, err := gogit.PlainInit(realProjectDir, false) + require.NoError(t, err) + + worktree, err := repo.Worktree() + require.NoError(t, err) + require.NoError(t, worktree.AddWithOptions(&gogit.AddOptions{All: true})) + + commitHash, err := worktree.Commit("add curl lock", &gogit.CommitOptions{ + Author: &object.Signature{ + Name: "azldev", + Email: "azldev@local", + When: time.Date(2024, 6, 1, 0, 0, 0, 0, time.UTC), + }, + }) + require.NoError(t, err) + + _, config, err := projectconfig.LoadProjectConfig( + osFS, testctx.NewTestOSEnv(), linkedProjectDir, true, t.TempDir(), + nil, false, false, + ) + require.NoError(t, err) + require.Equal(t, filepath.Join(linkedProjectDir, "locks"), config.Project.LockDir, + "precondition: the loaded config keeps the unresolved path") + + component, ok := config.Components[componentName] + require.True(t, ok) + + var loggedPaths []string + + cmdFactory := testctx.NewTestCmdFactory() + cmdFactory.RunAndGetOutputHandler = func(cmd *exec.Cmd) (string, error) { + loggedPaths = append(loggedPaths, cmd.Args[len(cmd.Args)-1]) + + return strings.Join([]string{ + commitHash.String(), "azldev", "azldev@local", "1717200000", "add curl lock", + }, "\x00") + "\x01", nil + } + + changes, gotImportCommit, err := buildSyntheticCommits( + t.Context(), cmdFactory, &component, componentName, config.Project.LockDir, "", + ) + require.NoError(t, err) + + assert.Equal(t, []string{filepath.Join("locks", componentName+".lock")}, loggedPaths) + assert.Equal(t, importCommit, gotImportCommit) + + require.Len(t, changes, 1) + assert.Equal(t, commitHash.String(), changes[0].Hash) + assert.Equal(t, upstreamCommit, changes[0].UpstreamCommit) +} diff --git a/internal/app/azldev/core/sources/synthistory_upstreamcommit.go b/internal/app/azldev/core/sources/synthistory_upstreamcommit.go index bbad7f5fb..e0e9c3867 100644 --- a/internal/app/azldev/core/sources/synthistory_upstreamcommit.go +++ b/internal/app/azldev/core/sources/synthistory_upstreamcommit.go @@ -8,7 +8,6 @@ import ( "errors" "fmt" "log/slog" - "path/filepath" "slices" gogit "github.com/go-git/go-git/v5" @@ -16,6 +15,7 @@ import ( "github.com/go-git/go-git/v5/plumbing/object" "github.com/microsoft/azure-linux-dev-tools/internal/global/opctx" "github.com/microsoft/azure-linux-dev-tools/internal/projectconfig" + "github.com/microsoft/azure-linux-dev-tools/internal/utils/git" toml "github.com/pelletier/go-toml/v2" ) @@ -178,7 +178,7 @@ func buildUpstreamCommitSyntheticCommits( return nil, nil } - configFileRelPath, err := filepath.Rel(projectRepoDir, configFileAbsPath) + configFileRelPath, err := git.RepoRelPath(projectRepoDir, configFileAbsPath) if err != nil { return nil, fmt.Errorf("failed to compute repo-relative config path for %#q:\n%w", configFileAbsPath, err) diff --git a/internal/utils/git/gogit.go b/internal/utils/git/gogit.go index f7583a849..dd168f31b 100644 --- a/internal/utils/git/gogit.go +++ b/internal/utils/git/gogit.go @@ -4,6 +4,12 @@ package git import ( + "errors" + "fmt" + "io/fs" + "path/filepath" + "strings" + gogit "github.com/go-git/go-git/v5" ) @@ -18,3 +24,49 @@ func OpenProjectRepo(path string) (*gogit.Repository, error) { EnableDotGitCommonDir: true, }) } + +func RepoRelPath(repoRoot, absPath string) (string, error) { + resolvedRoot, err := resolveSymlinks(repoRoot) + if err != nil { + return "", fmt.Errorf("resolving repository root %#q:\n%w", repoRoot, err) + } + + resolvedPath, err := resolveSymlinks(absPath) + if err != nil { + return "", fmt.Errorf("resolving path %#q:\n%w", absPath, err) + } + + relPath, err := filepath.Rel(resolvedRoot, resolvedPath) + if err != nil { + return "", fmt.Errorf("computing relative path from %#q to %#q:\n%w", repoRoot, absPath, err) + } + + if relPath == ".." || strings.HasPrefix(relPath, ".."+string(filepath.Separator)) { + return "", fmt.Errorf("path %#q escapes repository root %#q", absPath, repoRoot) + } + + return relPath, nil +} + +func resolveSymlinks(path string) (string, error) { + resolved, err := filepath.EvalSymlinks(path) + if err == nil { + return resolved, nil + } + + if !errors.Is(err, fs.ErrNotExist) { + return "", fmt.Errorf("evaluating symlinks in %#q:\n%w", path, err) + } + + parent := filepath.Dir(path) + if parent == path { + return path, nil + } + + resolvedParent, err := resolveSymlinks(parent) + if err != nil { + return "", err + } + + return filepath.Join(resolvedParent, filepath.Base(path)), nil +} diff --git a/internal/utils/git/gogit_test.go b/internal/utils/git/gogit_test.go new file mode 100644 index 000000000..2ae6e18a8 --- /dev/null +++ b/internal/utils/git/gogit_test.go @@ -0,0 +1,90 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +package git_test + +import ( + "os" + "path/filepath" + "testing" + + "github.com/microsoft/azure-linux-dev-tools/internal/utils/fileperms" + "github.com/microsoft/azure-linux-dev-tools/internal/utils/git" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestRepoRelPath(t *testing.T) { + tempDir := t.TempDir() + + realRepoDir := filepath.Join(tempDir, "real", "repo") + linkedRepoDir := filepath.Join(tempDir, "link", "repo") + + require.NoError(t, os.MkdirAll(filepath.Join(realRepoDir, "locks"), fileperms.PrivateDir)) + require.NoError(t, os.WriteFile(filepath.Join(realRepoDir, "locks", "curl.lock"), nil, fileperms.PrivateFile)) + require.NoError(t, os.Symlink(filepath.Join(tempDir, "real"), filepath.Join(tempDir, "link"))) + + testCases := []struct { + name string + repoRoot string + absPath string + expected string + }{ + { + name: "no symlinks", + repoRoot: realRepoDir, + absPath: filepath.Join(realRepoDir, "locks", "curl.lock"), + expected: filepath.Join("locks", "curl.lock"), + }, + { + name: "path through symlink", + repoRoot: realRepoDir, + absPath: filepath.Join(linkedRepoDir, "locks", "curl.lock"), + expected: filepath.Join("locks", "curl.lock"), + }, + { + name: "root through symlink", + repoRoot: linkedRepoDir, + absPath: filepath.Join(realRepoDir, "locks", "curl.lock"), + expected: filepath.Join("locks", "curl.lock"), + }, + { + name: "missing file through symlink", + repoRoot: realRepoDir, + absPath: filepath.Join(linkedRepoDir, "locks", "new", "openssl.lock"), + expected: filepath.Join("locks", "new", "openssl.lock"), + }, + { + name: "repository root itself", + repoRoot: realRepoDir, + absPath: linkedRepoDir, + expected: ".", + }, + } + + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + relPath, err := git.RepoRelPath(testCase.repoRoot, testCase.absPath) + require.NoError(t, err) + assert.Equal(t, testCase.expected, relPath) + }) + } +} + +func TestRepoRelPath_EscapesRoot(t *testing.T) { + tempDir := t.TempDir() + + repoDir := filepath.Join(tempDir, "repo") + require.NoError(t, os.MkdirAll(repoDir, fileperms.PrivateDir)) + + require.NoError(t, os.Symlink(tempDir, filepath.Join(repoDir, "outside"))) + + for _, absPath := range []string{ + filepath.Join(tempDir, "other", "curl.lock"), + filepath.Join(repoDir, "outside", "other", "curl.lock"), + } { + _, err := git.RepoRelPath(repoDir, absPath) + require.Error(t, err, absPath) + assert.Contains(t, err.Error(), "escapes repository root") + } +} From cfe87cc22404220687873c5ff1a9beccef6127b2 Mon Sep 17 00:00:00 2001 From: IzanVil Date: Sat, 26 Sep 2026 23:14:30 +0200 Subject: [PATCH 2/5] fix(component): use symlink-aware repo paths in changed and history 'component changed' failed with "escapes repository root" and 'component history' silently dropped TOML and lock metrics when the project was reached through a symlinked path, for the same reason as #188. Replace the local repoRelPath helper with git.RepoRelPath. Signed-off-by: IzanVil --- internal/app/azldev/cmds/component/changed.go | 19 ++----------------- .../cmds/component/changed_upstreamcommit.go | 7 ++++--- .../cmds/component/history_gitmetrics.go | 6 +++--- 3 files changed, 9 insertions(+), 23 deletions(-) diff --git a/internal/app/azldev/cmds/component/changed.go b/internal/app/azldev/cmds/component/changed.go index 40e947df5..fe6d045be 100644 --- a/internal/app/azldev/cmds/component/changed.go +++ b/internal/app/azldev/cmds/component/changed.go @@ -278,7 +278,7 @@ func newChangedContext(env *azldev.Env) (*changedContext, error) { return nil, err } - lockRelDir, err := repoRelPath(repoRoot, env.Config().Project.LockDir) + lockRelDir, err := git.RepoRelPath(repoRoot, env.Config().Project.LockDir) if err != nil { return nil, fmt.Errorf("computing repo-relative lock dir:\n%w", err) } @@ -534,7 +534,7 @@ func compareSources( return false, fmt.Errorf("resolving rendered spec dir:\n%w", err) } - sourcesRelPath, err := repoRelPath(repoRoot, filepath.Join(renderedDir, "sources")) + sourcesRelPath, err := git.RepoRelPath(repoRoot, filepath.Join(renderedDir, "sources")) if err != nil { return false, fmt.Errorf("computing repo-relative sources path:\n%w", err) } @@ -560,21 +560,6 @@ func compareSources( } } -// repoRelPath computes a repo-relative path and rejects `..`-prefixed results -// that would escape the repository root. -func repoRelPath(repoRoot, absPath string) (string, error) { - relPath, err := filepath.Rel(repoRoot, absPath) - if err != nil { - return "", fmt.Errorf("computing relative path from %#q to %#q:\n%w", repoRoot, absPath, err) - } - - if relPath == ".." || strings.HasPrefix(relPath, ".."+string(filepath.Separator)) { - return "", fmt.Errorf("path %#q escapes repository root %#q", absPath, repoRoot) - } - - return relPath, nil -} - // isFileNotFound returns true if the error indicates a missing file or // directory in a git tree. func isFileNotFound(err error) bool { diff --git a/internal/app/azldev/cmds/component/changed_upstreamcommit.go b/internal/app/azldev/cmds/component/changed_upstreamcommit.go index e46e0e46a..00bee166d 100644 --- a/internal/app/azldev/cmds/component/changed_upstreamcommit.go +++ b/internal/app/azldev/cmds/component/changed_upstreamcommit.go @@ -22,6 +22,7 @@ import ( "github.com/microsoft/azure-linux-dev-tools/internal/providers/sourceproviders" "github.com/microsoft/azure-linux-dev-tools/internal/utils/fileperms" "github.com/microsoft/azure-linux-dev-tools/internal/utils/fileutils" + "github.com/microsoft/azure-linux-dev-tools/internal/utils/git" "github.com/spf13/afero" ) @@ -52,7 +53,7 @@ func changedComponentsFromProjectConfigs( return nil, fmt.Errorf("resolving --to ref %#q:\n%w", options.To, err) } - projectRelDir, err := repoRelPath(repoRoot, env.ProjectDir()) + projectRelDir, err := git.RepoRelPath(repoRoot, env.ProjectDir()) if err != nil { return nil, fmt.Errorf("resolving project directory within repository:\n%w", err) } @@ -185,7 +186,7 @@ func loadHistoricalProject( return nil, err } - result.renderedSpecsRelDir, err = repoRelPath(snapshotRepoRoot, config.Project.RenderedSpecsDir) + result.renderedSpecsRelDir, err = git.RepoRelPath(snapshotRepoRoot, config.Project.RenderedSpecsDir) if err != nil { return nil, fmt.Errorf("resolving rendered specs directory:\n%w", err) } @@ -509,7 +510,7 @@ func projectSpecRepoRelPath(env *azldev.Env, repoRoot, specPath string) (string, absolutePath = filepath.Join(env.ProjectDir(), absolutePath) } - relativePath, err := repoRelPath(repoRoot, absolutePath) + relativePath, err := git.RepoRelPath(repoRoot, absolutePath) if err != nil { return "", fmt.Errorf("resolving spec path %#q:\n%w", specPath, err) } diff --git a/internal/app/azldev/cmds/component/history_gitmetrics.go b/internal/app/azldev/cmds/component/history_gitmetrics.go index f89a6b5ca..65b629283 100644 --- a/internal/app/azldev/cmds/component/history_gitmetrics.go +++ b/internal/app/azldev/cmds/component/history_gitmetrics.go @@ -161,7 +161,7 @@ func collectUniqueTomlRelPathsFromStubs(repoRoot string, stubs []historyStub) [] continue } - relPath, err := repoRelPath(repoRoot, absPath) + relPath, err := git.RepoRelPath(repoRoot, absPath) if err != nil { continue } @@ -223,7 +223,7 @@ func populateTomlMetrics( tomlAbsPath := config.SourceConfigFile.SourcePath() result.SharedToml = tomlSharing[tomlAbsPath] > 1 - tomlRelPath, err := repoRelPath(ctx.repoRoot, tomlAbsPath) + tomlRelPath, err := git.RepoRelPath(ctx.repoRoot, tomlAbsPath) if err != nil { // A TOML file outside the repo isn't a hard error -- record a // warning and leave path/commit counts empty. @@ -327,7 +327,7 @@ func populateLockMetrics( return } - lockRelPath, err := repoRelPath(ctx.repoRoot, lockAbsPath) + lockRelPath, err := git.RepoRelPath(ctx.repoRoot, lockAbsPath) if err != nil { // Lock dir lives outside the repo: nothing to count. result.Warnings = append(result.Warnings, From d3b16cffc6079715f0d81df2db304e3896ca5559 Mon Sep 17 00:00:00 2001 From: IzanVil Date: Sun, 27 Sep 2026 00:13:16 +0200 Subject: [PATCH 3/5] fix(git): follow dangling symlinks in RepoRelPath A symlink whose target does not exist made EvalSymlinks fail, and the fallback appended the link name to its resolved parent without following it. An in-repo link pointing outside the repository was then reported as inside it. Read the link target and keep resolving it instead. Signed-off-by: IzanVil --- internal/utils/git/gogit.go | 27 ++++++++++++++++++++++++++- internal/utils/git/gogit_test.go | 14 ++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/internal/utils/git/gogit.go b/internal/utils/git/gogit.go index dd168f31b..22bc27b04 100644 --- a/internal/utils/git/gogit.go +++ b/internal/utils/git/gogit.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "io/fs" + "os" "path/filepath" "strings" @@ -68,5 +69,29 @@ func resolveSymlinks(path string) (string, error) { return "", err } - return filepath.Join(resolvedParent, filepath.Base(path)), nil + joined := filepath.Join(resolvedParent, filepath.Base(path)) + + info, err := os.Lstat(joined) + if errors.Is(err, fs.ErrNotExist) { + return joined, nil + } + + if err != nil { + return "", fmt.Errorf("inspecting %#q:\n%w", joined, err) + } + + if info.Mode()&fs.ModeSymlink == 0 { + return joined, nil + } + + target, err := os.Readlink(joined) + if err != nil { + return "", fmt.Errorf("reading symlink %#q:\n%w", joined, err) + } + + if !filepath.IsAbs(target) { + target = filepath.Join(resolvedParent, target) + } + + return resolveSymlinks(target) } diff --git a/internal/utils/git/gogit_test.go b/internal/utils/git/gogit_test.go index 2ae6e18a8..8693ba2b3 100644 --- a/internal/utils/git/gogit_test.go +++ b/internal/utils/git/gogit_test.go @@ -23,6 +23,7 @@ func TestRepoRelPath(t *testing.T) { require.NoError(t, os.MkdirAll(filepath.Join(realRepoDir, "locks"), fileperms.PrivateDir)) require.NoError(t, os.WriteFile(filepath.Join(realRepoDir, "locks", "curl.lock"), nil, fileperms.PrivateFile)) require.NoError(t, os.Symlink(filepath.Join(tempDir, "real"), filepath.Join(tempDir, "link"))) + require.NoError(t, os.Symlink(filepath.Join("locks", "pending"), filepath.Join(realRepoDir, "pending"))) testCases := []struct { name string @@ -54,6 +55,12 @@ func TestRepoRelPath(t *testing.T) { absPath: filepath.Join(linkedRepoDir, "locks", "new", "openssl.lock"), expected: filepath.Join("locks", "new", "openssl.lock"), }, + { + name: "dangling symlink inside repository", + repoRoot: realRepoDir, + absPath: filepath.Join(linkedRepoDir, "pending", "openssl.lock"), + expected: filepath.Join("locks", "pending", "openssl.lock"), + }, { name: "repository root itself", repoRoot: realRepoDir, @@ -78,10 +85,17 @@ func TestRepoRelPath_EscapesRoot(t *testing.T) { require.NoError(t, os.MkdirAll(repoDir, fileperms.PrivateDir)) require.NoError(t, os.Symlink(tempDir, filepath.Join(repoDir, "outside"))) + require.NoError(t, os.Symlink(filepath.Join(tempDir, "missing"), filepath.Join(repoDir, "dangling"))) + require.NoError(t, os.Symlink(filepath.Join("..", "missing"), filepath.Join(repoDir, "dangling-relative"))) + require.NoError(t, os.Symlink("dangling", filepath.Join(repoDir, "dangling-chain"))) for _, absPath := range []string{ filepath.Join(tempDir, "other", "curl.lock"), filepath.Join(repoDir, "outside", "other", "curl.lock"), + filepath.Join(repoDir, "dangling"), + filepath.Join(repoDir, "dangling", "curl.lock"), + filepath.Join(repoDir, "dangling-relative", "curl.lock"), + filepath.Join(repoDir, "dangling-chain", "curl.lock"), } { _, err := git.RepoRelPath(repoDir, absPath) require.Error(t, err, absPath) From 6ddb23d266c94e82951e09d71542b77a67c9e501 Mon Sep 17 00:00:00 2001 From: IzanVil Date: Wed, 11 Nov 2026 17:28:46 +0100 Subject: [PATCH 4/5] fix(component): drop unused strings import after merging main Signed-off-by: IzanVil --- internal/app/azldev/cmds/component/changed.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/app/azldev/cmds/component/changed.go b/internal/app/azldev/cmds/component/changed.go index d383d2a7e..64da4051d 100644 --- a/internal/app/azldev/cmds/component/changed.go +++ b/internal/app/azldev/cmds/component/changed.go @@ -9,7 +9,6 @@ import ( "fmt" "path/filepath" "sort" - "strings" gogit "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing" From 39948c423c9a087cabd712419a488442ee4059dc Mon Sep 17 00:00:00 2001 From: IzanVil Date: Wed, 11 Nov 2026 17:34:32 +0100 Subject: [PATCH 5/5] fix(git): bound symlink resolution to avoid infinite recursion on cyclic links Signed-off-by: IzanVil --- internal/utils/git/gogit.go | 18 ++++++++++++++++-- internal/utils/git/gogit_test.go | 21 +++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/internal/utils/git/gogit.go b/internal/utils/git/gogit.go index 22bc27b04..ca29f6674 100644 --- a/internal/utils/git/gogit.go +++ b/internal/utils/git/gogit.go @@ -26,6 +26,8 @@ func OpenProjectRepo(path string) (*gogit.Repository, error) { }) } +const maxSymlinkHops = 255 + func RepoRelPath(repoRoot, absPath string) (string, error) { resolvedRoot, err := resolveSymlinks(repoRoot) if err != nil { @@ -50,6 +52,12 @@ func RepoRelPath(repoRoot, absPath string) (string, error) { } func resolveSymlinks(path string) (string, error) { + remainingLinks := maxSymlinkHops + + return resolveSymlinksLimited(path, &remainingLinks) +} + +func resolveSymlinksLimited(path string, remainingLinks *int) (string, error) { resolved, err := filepath.EvalSymlinks(path) if err == nil { return resolved, nil @@ -64,7 +72,7 @@ func resolveSymlinks(path string) (string, error) { return path, nil } - resolvedParent, err := resolveSymlinks(parent) + resolvedParent, err := resolveSymlinksLimited(parent, remainingLinks) if err != nil { return "", err } @@ -84,6 +92,12 @@ func resolveSymlinks(path string) (string, error) { return joined, nil } + if *remainingLinks == 0 { + return "", fmt.Errorf("too many levels of symbolic links resolving %#q", joined) + } + + *remainingLinks-- + target, err := os.Readlink(joined) if err != nil { return "", fmt.Errorf("reading symlink %#q:\n%w", joined, err) @@ -93,5 +107,5 @@ func resolveSymlinks(path string) (string, error) { target = filepath.Join(resolvedParent, target) } - return resolveSymlinks(target) + return resolveSymlinksLimited(target, remainingLinks) } diff --git a/internal/utils/git/gogit_test.go b/internal/utils/git/gogit_test.go index 8693ba2b3..87136af79 100644 --- a/internal/utils/git/gogit_test.go +++ b/internal/utils/git/gogit_test.go @@ -102,3 +102,24 @@ func TestRepoRelPath_EscapesRoot(t *testing.T) { assert.Contains(t, err.Error(), "escapes repository root") } } + +func TestRepoRelPath_SymlinkCycle(t *testing.T) { + tempDir := t.TempDir() + + repoDir := filepath.Join(tempDir, "repo") + require.NoError(t, os.MkdirAll(repoDir, fileperms.PrivateDir)) + + require.NoError(t, os.Symlink("missing/../self", filepath.Join(repoDir, "self"))) + require.NoError(t, os.Symlink("missing/../second", filepath.Join(repoDir, "first"))) + require.NoError(t, os.Symlink("missing/../first", filepath.Join(repoDir, "second"))) + + for _, absPath := range []string{ + filepath.Join(repoDir, "self"), + filepath.Join(repoDir, "self", "curl.lock"), + filepath.Join(repoDir, "first", "curl.lock"), + } { + _, err := git.RepoRelPath(repoDir, absPath) + require.Error(t, err, absPath) + assert.Contains(t, err.Error(), "too many levels of symbolic links") + } +}