Skip to content

Commit 18cd2d5

Browse files
Merge pull request containers#4181 from nalind/scrub-usernames
Scrub user and group names from layer diffs
2 parents 5c081c0 + da4647b commit 18cd2d5

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

image.go

+10
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,16 @@ func (i *containerImageRef) NewImageSource(ctx context.Context, sc *types.System
430430
return nil, fmt.Errorf("error compressing %s: %w", what, err)
431431
}
432432
writer := io.MultiWriter(writeCloser, srcHasher.Hash())
433+
// Scrub any local user names that might correspond to UIDs or GIDs of
434+
// files in this layer.
435+
{
436+
nestedWriteCloser := ioutils.NewWriteCloserWrapper(writer, writeCloser.Close)
437+
writeCloser = newTarFilterer(nestedWriteCloser, func(hdr *tar.Header) (bool, bool, io.Reader) {
438+
hdr.Uname, hdr.Gname = "", ""
439+
return false, false, nil
440+
})
441+
writer = io.Writer(writeCloser)
442+
}
433443
// Use specified timestamps in the layer, if we're doing that for
434444
// history entries.
435445
if i.created != nil {

tests/commit.bats

+21
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,24 @@ load helpers
305305
run_buildah commit --authfile ${TEST_SCRATCH_DIR}/test.auth $WITH_POLICY_JSON --tls-verify=false $cid docker://localhost:${REGISTRY_PORT}/buildah/my-busybox
306306
expect_output --substring "Writing manifest to image destination"
307307
}
308+
309+
@test "commit-without-names" {
310+
_prefetch busybox
311+
run_buildah from --quiet --pull=false $WITH_POLICY_JSON busybox
312+
cid=$output
313+
run_buildah run $cid touch /testfile
314+
run_buildah run $cid chown $(id -u):$(id -g) /testfile
315+
run_buildah commit $cid dir:${TEST_SCRATCH_DIR}/new-image
316+
config=$(jq -r .config.digest ${TEST_SCRATCH_DIR}/new-image/manifest.json)
317+
echo "config blob is $config"
318+
diffid=$(jq -r '.rootfs.diff_ids[-1]' ${TEST_SCRATCH_DIR}/new-image/${config##*:})
319+
echo "new layer is $diffid"
320+
run_buildah copy $cid ${TEST_SCRATCH_DIR}/new-image/${diffid##*:} /testdiff.tar
321+
# use in-container version of tar to avoid worrying about differences in
322+
# output formats between tar implementations
323+
run_buildah run $cid tar tvf /testdiff.tar testfile
324+
echo "new file looks like [$output]"
325+
# ownership information should be forced to be in number/number format
326+
# instead of name/name because the names are gone
327+
assert "$output" =~ $(id -u)/$(id -g)
328+
}

0 commit comments

Comments
 (0)