Skip to content

Commit adfde63

Browse files
authored
feat(misconf): add support of buildkit instructions when building dockerfile from image config (aquasecurity#5990)
1 parent e2eb70e commit adfde63

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

pkg/fanal/analyzer/imgconf/dockerfile/dockerfile.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ func (a *historyAnalyzer) Analyze(ctx context.Context, input analyzer.ConfigAnal
5252
case strings.HasPrefix(h.CreatedBy, "/bin/sh -c"):
5353
// RUN instruction
5454
createdBy = strings.ReplaceAll(h.CreatedBy, "/bin/sh -c", "RUN")
55+
case strings.HasSuffix(h.CreatedBy, "# buildkit"):
56+
// buildkit instructions
57+
// COPY ./foo /foo # buildkit
58+
// ADD ./foo.txt /foo.txt # buildkit
59+
// RUN /bin/sh -c ls -hl /foo # buildkit
60+
createdBy = strings.TrimSuffix(h.CreatedBy, "# buildkit")
61+
if strings.HasPrefix(h.CreatedBy, "RUN /bin/sh -c") {
62+
createdBy = strings.ReplaceAll(createdBy, "RUN /bin/sh -c", "RUN")
63+
}
5564
case strings.HasPrefix(h.CreatedBy, "USER"):
5665
// USER instruction
5766
createdBy = h.CreatedBy

pkg/fanal/analyzer/imgconf/dockerfile/dockerfile_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,92 @@ func Test_historyAnalyzer_Analyze(t *testing.T) {
9494
},
9595
},
9696
},
97+
{
98+
name: "happy path with buildkit instructions",
99+
input: analyzer.ConfigAnalysisInput{
100+
Config: &v1.ConfigFile{
101+
Config: v1.Config{
102+
Healthcheck: &v1.HealthConfig{
103+
Test: []string{"CMD-SHELL", "curl --fail http://localhost:3000 || exit 1"},
104+
Interval: time.Second * 10,
105+
Timeout: time.Second * 3,
106+
},
107+
},
108+
History: []v1.History{
109+
{
110+
CreatedBy: "/bin/sh -c #(nop) ADD file:289c2fac17119508ced527225d445747cd177111b4a0018a6b04948ecb3b5e29 in / ",
111+
EmptyLayer: false,
112+
},
113+
{
114+
CreatedBy: "/bin/sh -c #(nop) CMD [\"/bin/sh\"]",
115+
EmptyLayer: true,
116+
},
117+
{
118+
CreatedBy: "ADD ./foo.txt /foo.txt # buildkit",
119+
EmptyLayer: false,
120+
},
121+
{
122+
CreatedBy: "COPY ./foo /foo # buildkit",
123+
EmptyLayer: false,
124+
},
125+
{
126+
CreatedBy: "RUN /bin/sh -c ls -hl /foo # buildkit",
127+
EmptyLayer: false,
128+
},
129+
{
130+
CreatedBy: "USER foo",
131+
EmptyLayer: true,
132+
},
133+
{
134+
CreatedBy: `HEALTHCHECK &{["CMD-SHELL" "curl -sS 127.0.0.1 || exit 1"] "10s" "3s" "0s" '\x00'}`,
135+
EmptyLayer: true,
136+
},
137+
},
138+
},
139+
},
140+
want: &analyzer.ConfigAnalysisResult{
141+
Misconfiguration: &types.Misconfiguration{
142+
FileType: "dockerfile",
143+
FilePath: "Dockerfile",
144+
Failures: types.MisconfResults{
145+
types.MisconfResult{
146+
Namespace: "builtin.dockerfile.DS005",
147+
Query: "data.builtin.dockerfile.DS005.deny",
148+
Message: "Consider using 'COPY ./foo.txt /foo.txt' command instead of 'ADD ./foo.txt /foo.txt'",
149+
PolicyMetadata: types.PolicyMetadata{
150+
ID: "DS005",
151+
AVDID: "AVD-DS-0005",
152+
Type: "Dockerfile Security Check",
153+
Title: "ADD instead of COPY",
154+
Description: "You should use COPY instead of ADD unless you want to extract a tar file. Note that an ADD command will extract a tar file, which adds the risk of Zip-based vulnerabilities. Accordingly, it is advised to use a COPY command, which does not extract tar files.",
155+
Severity: "LOW",
156+
RecommendedActions: "Use COPY instead of ADD",
157+
References: []string{"https://docs.docker.com/engine/reference/builder/#add"},
158+
},
159+
CauseMetadata: types.CauseMetadata{
160+
Provider: "Dockerfile",
161+
Service: "general",
162+
StartLine: 1,
163+
EndLine: 1,
164+
Code: types.Code{
165+
Lines: []types.Line{
166+
{
167+
Number: 1,
168+
Content: "ADD ./foo.txt /foo.txt",
169+
IsCause: true,
170+
Truncated: false,
171+
Highlighted: "\x1b[38;5;64mADD\x1b[0m ./foo.txt /foo.txt",
172+
FirstCause: true,
173+
LastCause: true,
174+
},
175+
},
176+
},
177+
},
178+
},
179+
},
180+
},
181+
},
182+
},
97183
{
98184
name: "happy path. Base layer is found",
99185
input: analyzer.ConfigAnalysisInput{

0 commit comments

Comments
 (0)