Skip to content

Commit 243739b

Browse files
committed
Test to check writeComposeFile detects invalid OCI artifact
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 6007d4c commit 243739b

File tree

5 files changed

+34
-5
lines changed

5 files changed

+34
-5
lines changed

cmd/formatter/shortcut_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ package formatter
2222
func handleCtrlZ() {
2323
// Windows doesn't support SIGSTOP/SIGCONT signals
2424
// Ctrl+Z behavior is handled differently by the Windows terminal
25-
}
25+
}

pkg/compose/publish_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ services:
7777
MediaType: "application/vnd.docker.compose.file+yaml",
7878
Annotations: map[string]string{
7979
"com.docker.compose.file": "compose.yaml",
80-
"com.docker.compose.version": internal.Version},
80+
"com.docker.compose.version": internal.Version,
81+
},
8182
},
8283
{
8384
MediaType: "application/vnd.docker.compose.file+yaml",
@@ -98,5 +99,4 @@ services:
9899
assert.DeepEqual(t, expectedLayers, layers, cmp.FilterPath(func(path cmp.Path) bool {
99100
return !slices.Contains([]string{".Data", ".Digest", ".Size"}, path.String())
100101
}, cmp.Ignore()))
101-
102102
}

pkg/compose/transform/replace.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ func ReplaceEnvFile(in []byte, service string, i int, value string) ([]byte, err
104104
} else {
105105
return replace(in, envFile.Line, envFile.Column, value), nil
106106
}
107-
108107
}
109108

110109
func getMapping(root *yaml.Node, key string) (*yaml.Node, error) {

pkg/remote/oci.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func (g ociRemoteLoader) Dir(path string) string {
179179
return g.known[path]
180180
}
181181

182-
func (g ociRemoteLoader) pullComposeFiles(ctx context.Context, local string, manifest spec.Manifest, ref reference.Named, resolver remotes.Resolver) error { //nolint:gocyclo
182+
func (g ociRemoteLoader) pullComposeFiles(ctx context.Context, local string, manifest spec.Manifest, ref reference.Named, resolver remotes.Resolver) error {
183183
err := os.MkdirAll(local, 0o700)
184184
if err != nil {
185185
return err

pkg/remote/oci_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@
1717
package remote
1818

1919
import (
20+
"os"
2021
"path/filepath"
2122
"testing"
23+
24+
spec "github.com/opencontainers/image-spec/specs-go/v1"
25+
"gotest.tools/v3/assert"
2226
)
2327

2428
func TestValidatePathInBase(t *testing.T) {
@@ -123,3 +127,29 @@ func TestValidatePathInBase(t *testing.T) {
123127
})
124128
}
125129
}
130+
131+
func TestWriteComposeFileWithExtendsPathTraversal(t *testing.T) {
132+
// Create a temporary directory for testing
133+
tmpDir, err := os.MkdirTemp("", "compose-test-*")
134+
if err != nil {
135+
t.Fatalf("failed to create temp dir: %v", err)
136+
}
137+
defer os.RemoveAll(tmpDir) //nolint:errcheck
138+
139+
// Create a layer with com.docker.compose.extends=true and a path traversal attempt
140+
layer := spec.Descriptor{
141+
MediaType: "application/vnd.docker.compose.file.v1+yaml",
142+
Digest: "sha256:test123",
143+
Size: 100,
144+
Annotations: map[string]string{
145+
"com.docker.compose.extends": "true",
146+
"com.docker.compose.file": "../other",
147+
},
148+
}
149+
150+
content := []byte("services:\n test:\n image: nginx\n")
151+
152+
// writeComposeFile should return an error due to path traversal
153+
err = writeComposeFile(layer, 0, tmpDir, content)
154+
assert.Error(t, err, "invalid OCI artifact")
155+
}

0 commit comments

Comments
 (0)