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

add/copy: make sure we handle relative path names correctly #5050

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions add.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,9 +456,11 @@ func (b *Builder) Add(destination string, extract bool, options AddAndCopyOption
// Iterate through every item that matched the glob.
itemsCopied := 0
for _, glob := range localSourceStat.Globbed {
rel, err := filepath.Rel(contextDir, glob)
if err != nil {
return fmt.Errorf("computing path of %q relative to %q: %w", glob, contextDir, err)
rel := glob
if filepath.IsAbs(glob) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct? We only append contextDir, if the glob is a Absolute path?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't appended - we're conditionalizing whether or not we want to convert the matched value to a relative path. We don't need to do that when it's already a relative path.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, now I understand.

if rel, err = filepath.Rel(contextDir, glob); err != nil {
return fmt.Errorf("computing path of %q relative to %q: %w", glob, contextDir, err)
}
}
if strings.HasPrefix(rel, ".."+string(os.PathSeparator)) {
return fmt.Errorf("possible escaping context directory error: %q is outside of %q", glob, contextDir)
Expand Down
43 changes: 43 additions & 0 deletions tests/copy.bats
Original file line number Diff line number Diff line change
Expand Up @@ -494,3 +494,46 @@ stuff/mystuff"
expect_line_count 1
assert "$output" = "test_file" "only contents of copied directory"
}

@test "copy-file-relative-context-dir" {
image=busybox
_prefetch $image
mkdir -p ${TEST_SCRATCH_DIR}/context
createrandom ${TEST_SCRATCH_DIR}/context/test_file
run_buildah from --quiet $WITH_POLICY_JSON $image
ctr="$output"
run_buildah copy --contextdir ${TEST_SCRATCH_DIR}/context $ctr test_file /opt/
run_buildah run $ctr ls -1 /opt/
expect_line_count 1
assert "$output" = "test_file" "only the one file"
}

@test "copy-file-absolute-context-dir" {
image=busybox
_prefetch $image
mkdir -p ${TEST_SCRATCH_DIR}/context/subdir
createrandom ${TEST_SCRATCH_DIR}/context/subdir/test_file
run_buildah from --quiet $WITH_POLICY_JSON $image
ctr="$output"
run_buildah copy --contextdir ${TEST_SCRATCH_DIR}/context $ctr /subdir/test_file /opt/
run_buildah run $ctr ls -1 /opt/
expect_line_count 1
assert "$output" = "test_file" "only the one file"
}

@test "copy-file-relative-no-context-dir" {
image=busybox
_prefetch $image
mkdir -p ${TEST_SCRATCH_DIR}/context
createrandom ${TEST_SCRATCH_DIR}/context/test_file
run_buildah from --quiet $WITH_POLICY_JSON $image
ctr="$output"
# we're not in that directory currently
run_buildah 125 copy $ctr test_file /opt/
# now we are
cd ${TEST_SCRATCH_DIR}/context
run_buildah copy $ctr test_file /opt/
run_buildah run $ctr ls -1 /opt/
expect_line_count 1
assert "$output" = "test_file" "only the one file"
}