diff --git a/internal/app/azldev/cmds/component/changed.go b/internal/app/azldev/cmds/component/changed.go index 31226191b..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" @@ -249,7 +248,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) } @@ -469,7 +468,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) } @@ -495,21 +494,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 ce8e19ebc..9d95b553f 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, diff --git a/internal/app/azldev/core/sources/synthistory.go b/internal/app/azldev/core/sources/synthistory.go index 593bd1a7e..0c957c81d 100644 --- a/internal/app/azldev/core/sources/synthistory.go +++ b/internal/app/azldev/core/sources/synthistory.go @@ -583,7 +583,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..ca29f6674 100644 --- a/internal/utils/git/gogit.go +++ b/internal/utils/git/gogit.go @@ -4,6 +4,13 @@ package git import ( + "errors" + "fmt" + "io/fs" + "os" + "path/filepath" + "strings" + gogit "github.com/go-git/go-git/v5" ) @@ -18,3 +25,87 @@ func OpenProjectRepo(path string) (*gogit.Repository, error) { EnableDotGitCommonDir: true, }) } + +const maxSymlinkHops = 255 + +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) { + 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 + } + + 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 := resolveSymlinksLimited(parent, remainingLinks) + if err != nil { + return "", err + } + + 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 + } + + 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) + } + + if !filepath.IsAbs(target) { + target = filepath.Join(resolvedParent, target) + } + + return resolveSymlinksLimited(target, remainingLinks) +} diff --git a/internal/utils/git/gogit_test.go b/internal/utils/git/gogit_test.go new file mode 100644 index 000000000..87136af79 --- /dev/null +++ b/internal/utils/git/gogit_test.go @@ -0,0 +1,125 @@ +// 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"))) + require.NoError(t, os.Symlink(filepath.Join("locks", "pending"), filepath.Join(realRepoDir, "pending"))) + + 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: "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, + 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"))) + 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) + 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") + } +}