Skip to content

Commit 14e2cab

Browse files
committed
Add documentation for SecretsUsedInArgOrEnv rule
Signed-off-by: Talon Bowler <[email protected]>
1 parent 20f4864 commit 14e2cab

File tree

6 files changed

+97
-15
lines changed

6 files changed

+97
-15
lines changed

frontend/dockerfile/dockerfile2llb/convert.go

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ const (
5555
sbomScanStage = "BUILDKIT_SBOM_SCAN_STAGE"
5656
)
5757

58+
var (
59+
secretsRegexpOnce sync.Once
60+
secretsRegexp *regexp.Regexp
61+
)
62+
5863
var nonEnvArgs = map[string]struct{}{
5964
sbomScanContext: {},
6065
sbomScanStage: {},
@@ -2347,24 +2352,32 @@ func validateBaseImagePlatform(name string, expected, actual ocispecs.Platform,
23472352
}
23482353
}
23492354

2350-
func validateNoSecretKey(key string, location []parser.Range, lint *linter.Linter) {
2355+
func getSecretsRegex() *regexp.Regexp {
23512356
// Check for either full value or first/last word.
23522357
// Examples: api_key, DATABASE_PASSWORD, GITHUB_TOKEN, secret_MESSAGE, AUTH
23532358
// Case insensitive.
2354-
secretTokens := []string{
2355-
"apikey",
2356-
"auth",
2357-
"credential",
2358-
"credentials",
2359-
"key",
2360-
"password",
2361-
"pword",
2362-
"passwd",
2363-
"secret",
2364-
"token",
2365-
}
2366-
pattern := `(?i)(?:_|^)(?:`+strings.Join(secretTokens, "|")+`)(?:_|$)`
2367-
if matched, _ := regexp.MatchString(pattern, key); matched {
2359+
secretsRegexpOnce.Do(func() {
2360+
secretTokens := []string{
2361+
"apikey",
2362+
"auth",
2363+
"credential",
2364+
"credentials",
2365+
"key",
2366+
"password",
2367+
"pword",
2368+
"passwd",
2369+
"secret",
2370+
"token",
2371+
}
2372+
pattern := `(?i)(?:_|^)(?:` + strings.Join(secretTokens, "|") + `)(?:_|$)`
2373+
secretsRegexp = regexp.MustCompile(pattern)
2374+
})
2375+
return secretsRegexp
2376+
}
2377+
2378+
func validateNoSecretKey(key string, location []parser.Range, lint *linter.Linter) {
2379+
pattern := getSecretsRegex()
2380+
if pattern.MatchString(key) {
23682381
msg := linter.RuleSecretsUsedInArgOrEnv.Format(key)
23692382
lint.Run(&linter.RuleSecretsUsedInArgOrEnv, location, msg)
23702383
}

frontend/dockerfile/dockerfile_lint_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,55 +61,63 @@ ENV git_key=
6161
RuleName: "SecretsUsedInArgOrEnv",
6262
Description: "Potentially sensitive data should not be used in the ARG or ENV commands",
6363
Detail: `Secrets should not be used in the ARG or ENV commands (key named "SECRET_PASSPHRASE")`,
64+
URL: "https://docs.docker.com/go/dockerfile/rule/secrets-used-in-arg-or-env/",
6465
Level: 1,
6566
Line: 3,
6667
},
6768
{
6869
RuleName: "SecretsUsedInArgOrEnv",
6970
Description: "Potentially sensitive data should not be used in the ARG or ENV commands",
7071
Detail: `Secrets should not be used in the ARG or ENV commands (key named "SUPER_Secret")`,
72+
URL: "https://docs.docker.com/go/dockerfile/rule/secrets-used-in-arg-or-env/",
7173
Level: 1,
7274
Line: 4,
7375
},
7476
{
7577
RuleName: "SecretsUsedInArgOrEnv",
7678
Description: "Potentially sensitive data should not be used in the ARG or ENV commands",
7779
Detail: `Secrets should not be used in the ARG or ENV commands (key named "password")`,
80+
URL: "https://docs.docker.com/go/dockerfile/rule/secrets-used-in-arg-or-env/",
7881
Level: 1,
7982
Line: 5,
8083
},
8184
{
8285
RuleName: "SecretsUsedInArgOrEnv",
8386
Description: "Potentially sensitive data should not be used in the ARG or ENV commands",
8487
Detail: `Secrets should not be used in the ARG or ENV commands (key named "secret")`,
88+
URL: "https://docs.docker.com/go/dockerfile/rule/secrets-used-in-arg-or-env/",
8589
Level: 1,
8690
Line: 5,
8791
},
8892
{
8993
RuleName: "SecretsUsedInArgOrEnv",
9094
Description: "Potentially sensitive data should not be used in the ARG or ENV commands",
9195
Detail: `Secrets should not be used in the ARG or ENV commands (key named "super_duper_secret_token")`,
96+
URL: "https://docs.docker.com/go/dockerfile/rule/secrets-used-in-arg-or-env/",
9297
Level: 1,
9398
Line: 6,
9499
},
95100
{
96101
RuleName: "SecretsUsedInArgOrEnv",
97102
Description: "Potentially sensitive data should not be used in the ARG or ENV commands",
98103
Detail: `Secrets should not be used in the ARG or ENV commands (key named "auth")`,
104+
URL: "https://docs.docker.com/go/dockerfile/rule/secrets-used-in-arg-or-env/",
99105
Level: 1,
100106
Line: 6,
101107
},
102108
{
103109
RuleName: "SecretsUsedInArgOrEnv",
104110
Description: "Potentially sensitive data should not be used in the ARG or ENV commands",
105111
Detail: `Secrets should not be used in the ARG or ENV commands (key named "apikey")`,
112+
URL: "https://docs.docker.com/go/dockerfile/rule/secrets-used-in-arg-or-env/",
106113
Level: 1,
107114
Line: 7,
108115
},
109116
{
110117
RuleName: "SecretsUsedInArgOrEnv",
111118
Description: "Potentially sensitive data should not be used in the ARG or ENV commands",
112119
Detail: `Secrets should not be used in the ARG or ENV commands (key named "git_key")`,
120+
URL: "https://docs.docker.com/go/dockerfile/rule/secrets-used-in-arg-or-env/",
113121
Level: 1,
114122
Line: 8,
115123
},

frontend/dockerfile/docs/rules/_index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,9 @@ $ docker build --check .
8484
<td><a href="./redundant-target-platform/">RedundantTargetPlatform</a></td>
8585
<td>Setting platform to predefined $TARGETPLATFORM in FROM is redundant as this is the default behavior</td>
8686
</tr>
87+
<tr>
88+
<td><a href="./secrets-used-in-arg-or-env/">SecretsUsedInArgOrEnv</a></td>
89+
<td>Potentially sensitive data should not be used in the ARG or ENV commands</td>
90+
</tr>
8791
</tbody>
8892
</table>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: SecretsUsedInArgOrEnv
3+
description: Potentially sensitive data should not be used in the ARG or ENV commands
4+
aliases:
5+
- /go/dockerfile/rule/secrets-used-in-arg-or-env/
6+
---
7+
8+
## Output
9+
10+
```text
11+
Potentially sensitive data should not be used in the ARG or ENV commands
12+
```
13+
14+
## Description
15+
16+
While it is common in many local development setups to pass secrets to running
17+
processes through environment variables, setting these within a Dockerfile via
18+
the `ENV` command means that these secrets will be committed to the build
19+
history of the resulting image, exposing the secret. For the same reasons,
20+
passing secrets in as build arguments, via the `ARG` command, will similarly
21+
expose the secret. This rule reports violations where `ENV` and `ARG` key names
22+
appear to be secret-related.
23+
24+
## Examples
25+
26+
❌ Bad: `AWS_SECRET_ACCESS_KEY` is a secret value.
27+
28+
```dockerfile
29+
FROM scratch
30+
ARG AWS_SECRET_ACCESS_KEY
31+
```
32+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## Output
2+
3+
```text
4+
Potentially sensitive data should not be used in the ARG or ENV commands
5+
```
6+
7+
## Description
8+
9+
While it is common in many local development setups to pass secrets to running
10+
processes through environment variables, setting these within a Dockerfile via
11+
the `ENV` command means that these secrets will be committed to the build
12+
history of the resulting image, exposing the secret. For the same reasons,
13+
passing secrets in as build arguments, via the `ARG` command, will similarly
14+
expose the secret. This rule reports violations where `ENV` and `ARG` key names
15+
appear to be secret-related.
16+
17+
## Examples
18+
19+
❌ Bad: `AWS_SECRET_ACCESS_KEY` is a secret value.
20+
21+
```dockerfile
22+
FROM scratch
23+
ARG AWS_SECRET_ACCESS_KEY
24+
```

frontend/dockerfile/linter/ruleset.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ var (
135135
RuleSecretsUsedInArgOrEnv = LinterRule[func(string) string]{
136136
Name: "SecretsUsedInArgOrEnv",
137137
Description: "Potentially sensitive data should not be used in the ARG or ENV commands",
138+
URL: "https://docs.docker.com/go/dockerfile/rule/secrets-used-in-arg-or-env/",
138139
Format: func(secretKey string) string {
139140
return fmt.Sprintf("Secrets should not be used in the ARG or ENV commands (key named %q)", secretKey)
140141
},

0 commit comments

Comments
 (0)