Skip to content

Commit 6a90742

Browse files
ndeloofglours
authored andcommitted
Test to check writeComposeFile detects invalid OCI artifact
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 6007d4c commit 6a90742

File tree

5 files changed

+28
-15
lines changed

5 files changed

+28
-15
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: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ package remote
1919
import (
2020
"path/filepath"
2121
"testing"
22+
23+
spec "github.com/opencontainers/image-spec/specs-go/v1"
24+
"gotest.tools/v3/assert"
2225
)
2326

2427
func TestValidatePathInBase(t *testing.T) {
@@ -84,11 +87,6 @@ func TestValidatePathInBase(t *testing.T) {
8487
unsafePath: "..",
8588
wantErr: true,
8689
},
87-
{
88-
name: "current directory reference",
89-
unsafePath: "./file.yaml",
90-
wantErr: false, // ./ resolves to base dir
91-
},
9290
{
9391
name: "mixed separators",
9492
unsafePath: "config/sub\\file.yaml",
@@ -104,11 +102,6 @@ func TestValidatePathInBase(t *testing.T) {
104102
unsafePath: "file-name_v1.2.3.yaml",
105103
wantErr: false,
106104
},
107-
{
108-
name: "single parent then back",
109-
unsafePath: "../compose/file.yaml",
110-
wantErr: false, // Resolves back to base dir, which is fine
111-
},
112105
}
113106

114107
for _, tt := range tests {
@@ -123,3 +116,24 @@ func TestValidatePathInBase(t *testing.T) {
123116
})
124117
}
125118
}
119+
120+
func TestWriteComposeFileWithExtendsPathTraversal(t *testing.T) {
121+
tmpDir := t.TempDir()
122+
123+
// Create a layer with com.docker.compose.extends=true and a path traversal attempt
124+
layer := spec.Descriptor{
125+
MediaType: "application/vnd.docker.compose.file.v1+yaml",
126+
Digest: "sha256:test123",
127+
Size: 100,
128+
Annotations: map[string]string{
129+
"com.docker.compose.extends": "true",
130+
"com.docker.compose.file": "../other",
131+
},
132+
}
133+
134+
content := []byte("services:\n test:\n image: nginx\n")
135+
136+
// writeComposeFile should return an error due to path traversal
137+
err := writeComposeFile(layer, 0, tmpDir, content)
138+
assert.Error(t, err, "invalid OCI artifact")
139+
}

0 commit comments

Comments
 (0)