Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contain RepoSpec path in repo #4885

Merged
merged 1 commit into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions api/internal/git/repospec.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ func NewRepoSpecFromURL(n string) (*RepoSpec, error) {
if host == "" {
return nil, fmt.Errorf("url lacks host: %s", n)
}
cleanedPath := filepath.Clean(strings.TrimPrefix(path, string(filepath.Separator)))
if pathElements := strings.Split(cleanedPath, string(filepath.Separator)); len(pathElements) > 0 &&
pathElements[0] == filesys.ParentDir {
return nil, fmt.Errorf("url path exits repo: %s", n)
}
return &RepoSpec{
raw: n, Host: host, OrgRepo: orgRepo,
Dir: notCloned, Path: path, Ref: gitRef, GitSuffix: suffix,
Expand Down
4 changes: 4 additions & 0 deletions api/internal/git/repospec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ func TestNewRepoSpecFromUrlErrors(t *testing.T) {
"https://host?ref=group/version/minor_version",
"url lacks orgRepo",
},
"path_exits_repo": {
"https://github.com/org/repo.git//path/../../exits/repo",
"url path exits repo",
},
}

for name, testCase := range badData {
Expand Down
7 changes: 7 additions & 0 deletions api/loader/fileloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ func newLoaderAtGitClone(
"'%s' refers to file '%s'; expecting directory",
repoSpec.AbsPath(), f)
}
// Path in repo can contain symlinks that exit repo. We can only
// check for this after cloning repo.
if !root.HasPrefix(repoSpec.CloneDir()) {
_ = cleaner()
return nil, fmt.Errorf("%q refers to directory outside of repo %q", repoSpec.AbsPath(),
repoSpec.CloneDir())
}
return &fileLoader{
// Clones never allowed to escape root.
loadRestrictor: RestrictionRootOnly,
Expand Down
22 changes: 22 additions & 0 deletions api/loader/fileloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package loader

import (
"bytes"
"fmt"
"io"
"net/http"
"os"
Expand Down Expand Up @@ -444,6 +445,27 @@ func TestLoaderDisallowsLocalBaseFromRemoteOverlay(t *testing.T) {
"base '/whatever/highBase' is outside '/whatever/someClone'")
}

func TestLoaderDisallowsRemoteBaseExitRepo(t *testing.T) {
fSys := filesys.MakeFsOnDisk()
dir, err := filesys.NewTmpConfirmedDir()
require.NoError(t, err)
t.Cleanup(func() {
_ = fSys.RemoveAll(dir.String())
})
repo := dir.Join("repo")
require.NoError(t, fSys.Mkdir(repo))

base := filepath.Join(repo, "base")
require.NoError(t, os.Symlink(dir.String(), base))

repoSpec, err := git.NewRepoSpecFromURL("https://github.com/org/repo/base")
require.NoError(t, err)

_, err = newLoaderAtGitClone(repoSpec, fSys, nil, git.DoNothingCloner(filesys.ConfirmedDir(repo)))
require.Error(t, err)
require.Contains(t, err.Error(), fmt.Sprintf("%q refers to directory outside of repo %q", base, repo))
}

func TestLocalLoaderReferencingGitBase(t *testing.T) {
require := require.New(t)

Expand Down