Skip to content

Commit

Permalink
KICS Docker queries multi-staged aware (Checkmarx#4877)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaela-soares authored Mar 2, 2022
1 parent 7366eb7 commit 26ee35a
Show file tree
Hide file tree
Showing 34 changed files with 289 additions and 23 deletions.
10 changes: 10 additions & 0 deletions assets/libraries/dockerfile.rego
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,13 @@ withVersion(pack) {
arrayContains(array, list) {
contains(array[_], list[_])
}

check_multi_stage(imageName, images) {
unsortedIndex := {x |
images[name][i].Cmd == "from"
x := {"Name": name, "Line": images[name][i].EndLine}
}

sortedIndex := sort(unsortedIndex)
imageName == sortedIndex[minus(count(sortedIndex), 1)].Name
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package Cx

import data.generic.dockerfile as dockerLib

CxPolicy[result] {
resource := input.document[i].command[name]
dockerLib.check_multi_stage(name, input.document[i].command)

not contains(resource, "healthcheck")

result := {
"documentId": input.document[i].id,
"searchKey": sprintf("FROM={{%s}}", [name]),
"issueType": "MissingAttribute", #"MissingAttribute" / "RedundantAttribute"
"issueType": "MissingAttribute",
"keyExpectedValue": "Dockerfile contains instruction 'HEALTHCHECK'",
"keyActualValue": "Dockerfile doesn't contain instruction 'HEALTHCHECK'",
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM golang:1.16 AS builder
WORKDIR /go/src/github.com/alexellis/href-counter/
RUN go get -d -v golang.org/x/net/html
COPY app.go ./
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .

FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=builder /go/src/github.com/alexellis/href-counter/app ./
CMD ["./app"]
RUN useradd -ms /bin/bash patrick

USER patrick
HEALTHCHECK CMD curl --fail http://localhost:3000 || exit 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM golang:1.16 AS builder
WORKDIR /go/src/github.com/alexellis/href-counter/
RUN go get -d -v golang.org/x/net/html
COPY app.go ./
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .

FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=builder /go/src/github.com/alexellis/href-counter/app ./
CMD ["./app"]
RUN useradd -ms /bin/bash patrick

USER patrick
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
{
"queryName": "Healthcheck Instruction Missing",
"severity": "LOW",
"line": 1
"line": 1,
"fileName": "positive.dockerfile"
},
{
"queryName": "Healthcheck Instruction Missing",
"severity": "LOW",
"line": 7,
"fileName": "positive2.dockerfile"
}
]
4 changes: 4 additions & 0 deletions assets/queries/dockerfile/last_user_is_root/query.rego
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package Cx

import data.generic.dockerfile as dockerLib

CxPolicy[result] {
resource := input.document[i].command[name]
dockerLib.check_multi_stage(name, input.document[i].command)

userCmd := [x | resource[j].Cmd == "user"; x := resource[j]]
userCmd[minus(count(userCmd), 1)].Value[0] == "root"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM golang:1.16 AS builder
WORKDIR /go/src/github.com/alexellis/href-counter/
RUN go get -d -v golang.org/x/net/html
COPY app.go ./
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
USER root

FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=builder /go/src/github.com/alexellis/href-counter/app ./
CMD ["./app"]
RUN useradd -ms /bin/bash patrick

USER patrick
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ CxPolicy[result] {
result := {
"documentId": input.document[i].id,
"searchKey": sprintf("FROM={{%s}}.MAINTAINER={{%s}}", [name, resource.Value[0]]),
"issueType": "IncorrectValue", #"MissingAttribute" / "RedundantAttribute"
"issueType": "IncorrectValue",
"keyExpectedValue": sprintf("Maintainer instruction being used in Label 'LABEL maintainer=%s'", [resource.Value[0]]),
"keyActualValue": sprintf("Maintainer instruction not being used in Label 'MAINTAINER %s'", [resource.Value[0]]),
}
Expand Down
4 changes: 4 additions & 0 deletions assets/queries/dockerfile/missing_dnf_clean_all/query.rego
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package Cx

import data.generic.dockerfile as dockerLib

CxPolicy[result] {
resource := input.document[i].command[name][_]
dockerLib.check_multi_stage(name, input.document[i].command)

resource.Cmd == "run"
command := resource.Value[0]

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM golang:1.16 AS builder
WORKDIR /go/src/github.com/alexellis/href-counter/
RUN go get -d -v golang.org/x/net/html
COPY app.go ./
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
RUN set -uex && \
dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo && \
sed -i 's/\$releasever/26/g' /etc/yum.repos.d/docker-ce.repo && \
dnf install -vy docker-ce

FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=builder /go/src/github.com/alexellis/href-counter/app ./
CMD ["./app"]
RUN useradd -ms /bin/bash patrick

USER patrick
10 changes: 6 additions & 4 deletions assets/queries/dockerfile/missing_user_instruction/query.rego
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package Cx

import data.generic.dockerfile as dockerLib

CxPolicy[result] {
resource := input.document[i].command[name]
dockerLib.check_multi_stage(name, input.document[i].command)

not name == "scratch"
not hasUserInstruction(resource)
not has_user_instruction(resource)

result := {
"documentId": input.document[i].id,
Expand All @@ -15,7 +18,6 @@ CxPolicy[result] {
}
}

hasUserInstruction(resource) {
some j
resource[j].Cmd == "user"
has_user_instruction(resource) {
resource[_].Cmd == "user"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM golang:1.16 AS builder
WORKDIR /go/src/github.com/alexellis/href-counter/
RUN go get -d -v golang.org/x/net/html
COPY app.go ./
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .

FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=builder /go/src/github.com/alexellis/href-counter/app ./
CMD ["./app"]
RUN useradd -ms /bin/bash patrick

USER patrick
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM python:2.7
RUN pip install Flask==0.11.1
RUN useradd -ms /bin/bash patrick
COPY --chown=patrick:patrick app /app
WORKDIR /app
USER patrick
CMD ["python", "app.py"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM golang:1.16 AS builder
WORKDIR /go/src/github.com/alexellis/href-counter/
RUN go get -d -v golang.org/x/net/html
COPY app.go ./
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .

FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=builder /go/src/github.com/alexellis/href-counter/app ./
CMD ["./app"]
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
{
"queryName": "Missing User Instruction",
"severity": "HIGH",
"line": 1
"line": 1,
"fileName": "positive.dockerfile"
},
{
"queryName": "Missing User Instruction",
"severity": "HIGH",
"line": 7,
"fileName": "positive2.dockerfile"
}
]
7 changes: 4 additions & 3 deletions assets/queries/dockerfile/missing_zypper_clean/query.rego
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package Cx

import data.generic.dockerfile as dockerLib

CxPolicy[result] {
document := input.document[i]
commands = document.command
some img
some c

commands[img][c].Cmd == "run"
dockerLib.check_multi_stage(img, commands)

some j
command := commands[img][c].Value[j]

commandHasZypperUsage(command)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM golang:1.16 AS builder
WORKDIR /go/src/github.com/alexellis/href-counter/
RUN go get -d -v golang.org/x/net/html
COPY app.go ./
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
RUN zypper install

FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=builder /go/src/github.com/alexellis/href-counter/app ./
CMD ["./app"]
RUN useradd -ms /bin/bash patrick

USER patrick
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package Cx

import data.generic.dockerfile as dockerLib

CxPolicy[result] {
document := input.document[i]
commands = document.command
some img
some c

commands[img][c].Cmd == "run"
dockerLib.check_multi_stage(img, commands)

some j
command := commands[img][c].Value[j]

commandHasZypperUsage(command)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM golang:1.16 AS builder
WORKDIR /go/src/github.com/alexellis/href-counter/
RUN go get -d -v golang.org/x/net/html
COPY app.go ./
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
RUN zypper install httpd && zypper clean

FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=builder /go/src/github.com/alexellis/href-counter/app ./
CMD ["./app"]
RUN useradd -ms /bin/bash patrick

USER patrick
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package Cx

import data.generic.dockerfile as dockerLib

CxPolicy[result] {
resource := input.document[i].command[name]
dockerLib.check_multi_stage(name, input.document[i].command)

cmdInst := [x | resource[j].Cmd == "cmd"; x := resource[j]]
count(cmdInst) > 1

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM golang:1.16 AS builder
WORKDIR /go/src/github.com/alexellis/href-counter/
RUN go get -d -v golang.org/x/net/html
COPY app.go ./
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
CMD ["./app"]
CMD ["./apps"]

FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=builder /go/src/github.com/alexellis/href-counter/app ./
CMD ["./app"]
RUN useradd -ms /bin/bash patrick

USER patrick
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ WORKDIR /go/src/github.com/alexellis/href-counter/
RUN go get -d -v golang.org/x/net/html
COPY app.go .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
CMD ["./app"]
CMD ["./apps"]

FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=0 /go/src/github.com/alexellis/href-counter/app .
CMD ["./app"]
CMD ["./apps"]
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
"queryName": "Multiple CMD Instructions Listed",
"severity": "MEDIUM",
"line": 6
"line": 11,
"fileName": "positive.dockerfile"
}
]
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package Cx

import data.generic.dockerfile as dockerLib

CxPolicy[result] {
resource := input.document[i].command[name]
dockerLib.check_multi_stage(name, input.document[i].command)

cmdInst := [x | resource[j].Cmd == "entrypoint"; x := resource[j]]
count(cmdInst) > 1

result := {
"documentId": input.document[i].id,
"searchKey": sprintf("FROM={{%s}}.{{%s}}", [name, cmdInst[0].Original]),
"issueType": "RedundantAttribute", #"MissingAttribute" / "RedundantAttribute"
"issueType": "RedundantAttribute",
"keyExpectedValue": "There is only one ENTRYPOINT instruction",
"keyActualValue": sprintf("There are %d ENTRYPOINT instructions", [count(cmdInst)]),
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM golang:1.16 AS builder
WORKDIR /go/src/github.com/alexellis/href-counter/
RUN go get -d -v golang.org/x/net/html
COPY app.go ./
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
ENTRYPOINT [ "/opt/app/run.sh", "--port", "8080" ]
ENTRYPOINT [ "/opt/app/run.sh", "--port", "8000" ]

FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=builder /go/src/github.com/alexellis/href-counter/app ./
CMD ["./app"]
RUN useradd -ms /bin/bash patrick

USER patrick
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ WORKDIR /go/src/github.com/alexellis/href-counter/
RUN go get -d -v golang.org/x/net/html
COPY app.go .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
ENTRYPOINT [ "/opt/app/run.sh", "--port", "8080" ]
ENTRYPOINT [ "/opt/app/run.sh", "--port", "8000" ]

FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=0 /go/src/github.com/alexellis/href-counter/app .
ENTRYPOINT [ "/opt/app/run.sh", "--port", "8080" ]
ENTRYPOINT [ "/opt/app/run.sh", "--port", "8080" ]
ENTRYPOINT [ "/opt/app/run.sh", "--port", "8000" ]
Loading

0 comments on commit 26ee35a

Please sign in to comment.