GitLabgitlab-org

cli

main
ply9 git calls231 ms103 KiB7 cachedprotocol v2
369 lines21 KiBRaw
1package redact
3import (
4 "encoding/json"
5 "strings"
6 "testing"
8 "github.com/stretchr/testify/assert"
9 "github.com/stretchr/testify/require"
12// fake builds a token at run time, so the source holds nothing that looks like
13// a real secret to secret scanners.
14func fake(prefix string, n int) string {
15 return prefix + strings.Repeat("a1B2", n/4+1)[:n]
18func TestSecrets(t *testing.T) {
19 t.Parallel()
21 privateKey := "-----BEGIN OPENSSH " + "PRIVATE KEY-----\nb3BlbnNzaC1rZXk\n-----END OPENSSH " + "PRIVATE KEY-----"
22 jwt := "eyJ" + fake("", 20) + ".eyJ" + fake("", 20) + "." + fake("", 20)
24 tests := []struct {
25 name string
26 in string
27 want string
28 }{
29 {"GitLab personal access token", "token " + fake("glpat-", 20) + " here", "token [REDACTED:gitlab_token] here"},
30 {"GitLab CI job token", fake("glcbt-", 24), "[REDACTED:gitlab_token]"},
31 {"GitHub token", fake("ghp_", 36), "[REDACTED:github_token]"},
32 {"AWS access key", "AKIA" + strings.Repeat("A", 16), "[REDACTED:aws_access_key]"},
33 {"AWS secret key", "aws_secret_access_key = " + fake("", 40), "aws_secret_access_key = [REDACTED:aws_secret_key]"},
34 {"Google API key", fake("AIza", 35), "[REDACTED:google_api_key]"},
35 {"Slack token", fake("xoxb-", 20), "[REDACTED:slack_token]"},
36 {"Stripe key", fake("sk_live_", 24), "[REDACTED:stripe_key]"},
37 {"Anthropic key", fake("sk-ant-", 30), "[REDACTED:ai_api_key]"},
38 {"OpenAI key", fake("sk-proj-", 30), "[REDACTED:ai_api_key]"},
39 {"GitHub fine-grained token", fake("github_pat_", 60), "[REDACTED:github_token]"},
40 {"temporary AWS key", "ASIA" + strings.Repeat("B", 16), "[REDACTED:aws_access_key]"},
41 {"Stripe restricted key", fake("rk_live_", 24), "[REDACTED:stripe_key]"},
42 {"PyPI token", fake("pypi-", 60), "[REDACTED:pypi_token]"},
43 {"Slack app token", fake("xapp-", 20), "[REDACTED:slack_token]"},
44 {"GitLab token after a word", "TOKEN_" + fake("glpat-", 20), "TOKEN_[REDACTED:gitlab_token]"},
45 {
46 "encrypted PEM key",
47 "-----BEGIN RSA " + "PRIVATE KEY-----\nProc-Type: 4,ENCRYPTED\nDEK-Info: AES-128-CBC,0A1B\n\nMIIEpAIB\n-----END RSA " + "PRIVATE KEY-----",
48 "[REDACTED:private_key]",
49 },
50 {
51 "PGP key with a version header",
52 "-----BEGIN PGP " + "PRIVATE KEY BLOCK-----\nVersion: GnuPG v2 (GNU/Linux)\n\nlQOYBF2xYzEBCADxKq",
53 "[REDACTED:private_key]",
54 },
55 {
56 "key written with escaped newlines",
57 `printf '-----BEGIN RSA ` + `PRIVATE KEY-----\nMIIE\n-----END RSA ` + `PRIVATE KEY-----' > id_rsa`,
58 `printf '[REDACTED:private_key]' > id_rsa`,
59 },
60 {"bearer token with a dot", "Authorization: Bearer ya29." + fake("", 30), "Authorization: Bearer [REDACTED:http_credential]"},
61 {"PGP private key", "-----BEGIN PGP " + "PRIVATE KEY BLOCK-----\nlQOYBF\n-----END PGP " + "PRIVATE KEY BLOCK-----", "[REDACTED:private_key]"},
62 {"private_token query parameter", "/api/v4/user?private_token=" + fake("", 20) + "&x=1", "/api/v4/user?private_token=[REDACTED:http_credential]&x=1"},
63 {"URL password without a user", "redis://:" + fake("", 12) + "@cache:6379", "redis://:[REDACTED:url_password]@cache:6379"},
64 {"URL password with an @", "postgres://u:p@" + fake("", 12) + "@db/app", "postgres://u:[REDACTED:url_password]@db/app"},
65 {"npm token", fake("npm_", 36), "[REDACTED:npm_token]"},
66 {"JWT", "cookie " + jwt, "cookie [REDACTED:jwt]"},
67 {"private key", "key:\n" + privateKey + "\nend", "key:\n[REDACTED:private_key]\nend"},
68 {"private key cut off before its end", "-----BEGIN RSA " + "PRIVATE KEY-----\nMIIEpAIBAAKCAQEA3x", "[REDACTED:private_key]"},
69 {
70 "indented key in YAML",
71 "tls:\n key: |\n -----BEGIN RSA " + "PRIVATE KEY-----\n MIIEpAIBAAKCAQEA3xYz\n AbCdEf123==\n",
72 "tls:\n key: |\n [REDACTED:private_key]\n",
73 },
74 {
75 "key with trailing spaces",
76 "-----BEGIN RSA " + "PRIVATE KEY----- \nMIIEpAIBAAKCAQEA3xYz \nAbCd",
77 "[REDACTED:private_key]",
78 },
79 {
80 "key with escaped CRLF",
81 `"-----BEGIN RSA ` + `PRIVATE KEY-----\r\nMIIEpAIBAAKCAQEA3xYz\r\nAbCd"`,
82 `"[REDACTED:private_key]"`,
83 },
84 {
85 "key escaped twice",
86 `-----BEGIN RSA ` + `PRIVATE KEY-----\\nMIIEpAIBAAKCAQEA3xYz\\nAbCd`,
87 "[REDACTED:private_key]",
88 },
89 {
90 "encrypted key cut off",
91 "-----BEGIN RSA " + "PRIVATE KEY-----\nProc-Type: 4,ENCRYPTED\nDEK-Info: AES-128-CBC,0A1B\n\nMIIEpAIBAAKCAQEA3xYz",
92 "[REDACTED:private_key]",
93 },
94 {
95 "key header in a sentence",
96 "-----BEGIN RSA " + "PRIVATE KEY----- see docs section 3.4 (page 12) for the format.",
97 "-----BEGIN RSA " + "PRIVATE KEY----- see docs section 3.4 (page 12) for the format.",
98 },
99 {
100 "key header followed by a long word and prose",
101 "-----BEGIN RSA " + "PRIVATE KEY-----\nabcdefghijklmnopqrstuvwxyz see docs section 3.4 for the format.",
102 "[REDACTED:private_key] see docs section 3.4 for the format.",
103 },
104 {
105 "cut-off key followed by prose",
106 "-----BEGIN RSA " + "PRIVATE KEY-----\nMIIEpAIBAAKCAQEA3xYz\nAbCd\nsee docs section 3.4 for the format.\n\nNext paragraph.",
107 "[REDACTED:private_key]\nsee docs section 3.4 for the format.\n\nNext paragraph.",
108 },
109 {
110 "cut-off key in a quoted string",
111 `key = "-----BEGIN RSA ` + `PRIVATE KEY-----\nMIIEpAIBAAKCAQEA3xYz\nAbCd" # see docs`,
112 `key = "[REDACTED:private_key]" # see docs`,
113 },
114 {
115 "key cut off in its first line",
116 "-----BEGIN RSA " + "PRIVATE KEY-----\nMIIEpAIBAAKCAQEA3xYz… [truncated]",
117 "[REDACTED:private_key]… [truncated]",
118 },
119 {
120 "key cut off in a later line",
121 "-----BEGIN RSA " + "PRIVATE KEY-----\nMIIEpAIBAAKCAQEA3xYz\nAbCdEfGhIjKlMnOp12 [truncated]",
122 "[REDACTED:private_key] [truncated]",
123 },
124 {
125 "key in a heredoc",
126 "cat <<'EOF' > id\n-----BEGIN RSA " + "PRIVATE KEY-----\nMIIEpAIBAAKCAQEA3xYz\nAbCd\nEOF\n",
127 "cat <<'EOF' > id\n[REDACTED:private_key]\nEOF\n",
128 },
129 {
130 "key with escaped slashes",
131 `"-----BEGIN RSA ` + `PRIVATE KEY-----\nMIIEpAIBAAKCAQEA3x\/Yz\nAb\/Cd"`,
132 `"[REDACTED:private_key]"`,
133 },
134 {
135 "key escaped twice with CRLF",
136 `-----BEGIN RSA ` + `PRIVATE KEY-----\\r\\nMIIEpAIBAAKCAQEA3xYz\\r\\nAbCd`,
137 "[REDACTED:private_key]",
138 },
139 {
140 "PGP key with a Windows path in a comment",
141 "-----BEGIN PGP " + "PRIVATE KEY BLOCK-----\nComment: C:\\keys\\a.asc\n\nlQOYBF2xYzEBCADxKq\n=AbCd",
142 "[REDACTED:private_key]",
143 },
144 {
145 "key header in a key's comment",
146 "-----BEGIN PGP " + "PRIVATE KEY BLOCK-----\nComment: -----BEGIN PGP " + "PRIVATE KEY BLOCK-----\nlQOYBF2xYzEBCADxKq",
147 "[REDACTED:private_key]",
148 },
149 {
150 "cut-off key followed by lines that start with long words",
151 "-----BEGIN RSA " + "PRIVATE KEY-----\nMIIEpAIBAAKCAQEA3xYz\nsomeLongIdentifierName1 is used here\nanotherLongIdentifier12 too",
152 "[REDACTED:private_key] is used here\nanotherLongIdentifier12 too",
153 },
154 {
155 "key header followed by prose lines",
156 "-----BEGIN RSA " + "PRIVATE KEY-----\nis how the file starts.\nThen the body follows.",
157 "-----BEGIN RSA " + "PRIVATE KEY-----\nis how the file starts.\nThen the body follows.",
158 },
159 {
160 "bearer header",
161 `curl -H "Authorization: Bearer ` + fake("", 30) + `" https://example.com`,
162 `curl -H "Authorization: Bearer [REDACTED:http_credential]" https://example.com`,
163 },
164 {"PRIVATE-TOKEN header", "PRIVATE-TOKEN: " + fake("", 20), "PRIVATE-TOKEN: [REDACTED:http_credential]"},
165 {"password in a URL", "git clone https://user:" + fake("", 12) + "@gitlab.com/g/p.git", "git clone https://user:[REDACTED:url_password]@gitlab.com/g/p.git"},
166 {"variable exported in a command", "export MY_API_TOKEN=" + fake("", 18) + " && make", "export MY_API_TOKEN=[REDACTED:credential] && make"},
167 {"line in a .env file", "DB_PASSWORD=" + fake("", 16) + "\nDEBUG=true", "DB_PASSWORD=[REDACTED:credential]\nDEBUG=true"},
168 {"value in a YAML file", "db:\n password: " + fake("", 16) + "\n", "db:\n password: [REDACTED:credential]\n"},
169 {"Go short variable declaration", `password := "` + fake("", 14) + `"`, `password := "[REDACTED:credential]"`},
170 {"Python walrus operator", "if (token := " + fake("", 12) + "):", "if (token := [REDACTED:credential]):"},
171 {"quoted value", `client_secret = "` + fake("", 16) + `"`, `client_secret = "[REDACTED:credential]"`},
172 {"JSON in a file", `{"api_key": "` + fake("", 16) + `"}`, `{"api_key": "[REDACTED:credential]"}`},
173 {"flag in a YAML value", "args: --token=" + fake("", 16), "args: --token=[REDACTED:credential]"},
174 {"variable in a CI script line", "run: API_TOKEN=" + fake("", 16) + " make", "run: API_TOKEN=[REDACTED:credential] make"},
175 {"PowerShell environment variable", `$env:DB_PASSWORD = "` + fake("", 16) + `"`, `$env:DB_PASSWORD = "[REDACTED:credential]"`},
176 {"query parameter in a URL value", "url=https://h/p?token=" + fake("", 16), "url=https://h/p?token=[REDACTED:credential]"},
177 {"variable inside a JSON string", `{"cmd":"A_TOKEN=` + fake("", 16) + `"}`, `{"cmd":"A_TOKEN=[REDACTED:credential]"}`},
178 {"keyword argument", "connect(password=" + fake("", 12) + ")", "connect(password=[REDACTED:credential])"},
179 {"escaped newlines", `A_TOKEN=` + fake("", 12) + `\nNEXT_SETTING=value\n`, `A_TOKEN=[REDACTED:credential]\nNEXT_SETTING=value\n`},
180 {"name after an escaped newline", `printf 'user=bob\npassword=` + fake("", 12) + `'`, `printf 'user=bob\npassword=[REDACTED:credential]'`},
181 {"name after an escaped tab", `echo -e "a\ttoken=` + fake("", 12) + `"`, `echo -e "a\ttoken=[REDACTED:credential]"`},
182 {"pagination token after an escaped newline", `a=1\npage_token=` + fake("", 16), `a=1\npage_token=` + fake("", 16)},
183 {"YAML flow mapping", "{user: bob, password: " + fake("", 12) + "}", "{user: bob, password: [REDACTED:credential]}"},
184 {"Go map", "map[token:" + fake("", 12) + "]", "map[token:[REDACTED:credential]]"},
185 {"quoted value with spaces", `password = "my secret ` + fake("", 8) + `"`, `password = "[REDACTED:credential]"`},
186 {"value piped to a command", "API_TOKEN=" + fake("", 12) + "|grep x", "API_TOKEN=[REDACTED:credential]|grep x"},
187 {"bracket in a value", "DB_PASSWORD=" + fake("", 10) + "(z!", "DB_PASSWORD=[REDACTED:credential](z!"},
188 {"call whose name has a digit", "password = hashlib.sha256(pw)", "password = [REDACTED:credential](pw)"},
189 {"JSON in a shell command", `curl -d "{\"password\": \"` + fake("", 14) + `\"}"`, `curl -d "{\"password\": \"[REDACTED:credential]\"}"`},
190 {"quoted value with an escaped quote", `password: "ab\"` + fake("", 12) + `"`, `password: "[REDACTED:credential]"`},
191 {"value in escaped quotes", `A_TOKEN=\"` + fake("", 16) + `\"`, `A_TOKEN=\"[REDACTED:credential]\"`},
192 {"value cut off in its quotes", `password: "` + fake("", 16), `password: "[REDACTED:credential]`},
193 {"backslash before a line break in quotes", "password: \"" + fake("", 12) + "\\\nnext: line", "password: \"[REDACTED:credential]\\\nnext: line"},
194 {"value longer than the limit", "password=" + fake("", 300), "password=[REDACTED:credential]" + fake("", 300)[256:]},
195 {
196 "two variables in one command",
197 "A_TOKEN=" + fake("", 12) + " B_SECRET=" + fake("", 12) + " ./run",
198 "A_TOKEN=[REDACTED:credential] B_SECRET=[REDACTED:credential] ./run",
199 },
201 {"variable in a header", `curl -H "PRIVATE-TOKEN: $GITLAB_TOKEN"`, `curl -H "PRIVATE-TOKEN: $GITLAB_TOKEN"`},
202 {"braced variable in a header", "Authorization: Bearer ${TOKEN}", "Authorization: Bearer ${TOKEN}"},
203 {"placeholder in a header", "PRIVATE-TOKEN: <your_access_token>", "PRIVATE-TOKEN: <your_access_token>"},
204 {"variable in a URL", "https://user:$PASSWORD@gitlab.com", "https://user:$PASSWORD@gitlab.com"},
205 {"header named in prose", "the Authorization header is required", "the Authorization header is required"},
206 {"header with an explanation", "Authorization: Bearer authentication is required", "Authorization: Bearer authentication is required"},
207 {"header set from code", "const authorization = getHeader(req)", "const authorization = getHeader(req)"},
208 {"token header from an environment variable", "private-token: process.env.TOKEN", "private-token: process.env.TOKEN"},
209 {"kebab-case name after sk-", "npm i sk-learn-pipeline-component-v2", "npm i sk-learn-pipeline-component-v2"},
210 {"placeholder in a URL", "https://user:<password>@gitlab.com", "https://user:<password>@gitlab.com"},
211 {"SSH remote", "git@gitlab.com:group/project.git and ssh://git@gitlab.com:22/g/p", "git@gitlab.com:group/project.git and ssh://git@gitlab.com:22/g/p"},
212 {"code that checks for a key header", `strings.HasPrefix(s, "-----BEGIN RSA ` + `PRIVATE KEY-----") && ok`, `strings.HasPrefix(s, "-----BEGIN RSA ` + `PRIVATE KEY-----") && ok`},
213 {"short prefix lookalike", "glpat-short and sk-12", "glpat-short and sk-12"},
214 {"word ending in sk", "a task-" + fake("", 30), "a task-" + fake("", 30)},
215 {"type annotation", "token: Optional[str] = None", "token: Optional[str] = None"},
216 {"type name", "apiKey: ApiKeyType", "apiKey: ApiKeyType"},
217 {"comparison", `if password == "` + fake("", 12) + `" {`, `if password == "` + fake("", 12) + `" {`},
218 {"password named in prose", "password: see 1Password for it", "password: see 1Password for it"},
219 {"variable as the value", "DB_PASSWORD=${DB_PASSWORD}", "DB_PASSWORD=${DB_PASSWORD}"},
220 {"placeholder as the value", "password: <your-password-here>", "password: <your-password-here>"},
221 {"value from os.Getenv", `token := os.Getenv("API_TOKEN")`, `token := os.Getenv("API_TOKEN")`},
222 {"value from a call", "token = getToken(ctx, user1)", "token = getToken(ctx, user1)"},
223 {"pagination token", "page_token=" + fake("", 16), "page_token=" + fake("", 16)},
224 {"name that isn't a credential", "max_tokens: 4096000", "max_tokens: 4096000"},
225 {"prefixed API key in a command", "export OPENAI_API_KEY=" + fake("", 24), "export OPENAI_API_KEY=[REDACTED:credential]"},
226 {"password variable without a separator", "PGPASSWORD=" + fake("", 12) + " psql", "PGPASSWORD=[REDACTED:credential] psql"},
227 {"MySQL password variable", "MYSQL_PWD=" + fake("", 12), "MYSQL_PWD=[REDACTED:credential]"},
228 {"prefixed secret key in a .env file", "STRIPE_SECRET_KEY=" + fake("", 20) + "\n", "STRIPE_SECRET_KEY=[REDACTED:credential]\n"},
229 {"name ending in a credential word plus more", "api_key_id: k1abcdefgh secret_key_name: main12345", "api_key_id: k1abcdefgh secret_key_name: main12345"},
230 {"file named after a password", "password_file: pass1.txt", "password_file: pass1.txt"},
231 {"camelCase name in code", `const openaiApiKey = "` + fake("", 24) + `";`, `const openaiApiKey = "[REDACTED:credential]";`},
232 {"name starting with an acronym", "APIToken=" + fake("", 16), "APIToken=[REDACTED:credential]"},
233 {"token name starting with an acronym", "JWTToken=" + fake("", 16), "JWTToken=[REDACTED:credential]"},
234 {"secret access key starting with an acronym", "AWSSecretAccessKey=" + fake("", 40), "AWSSecretAccessKey=[REDACTED:credential]"},
235 {"API key starting with an acronym", "OPENAIApiKey=" + fake("", 24), "OPENAIApiKey=[REDACTED:credential]"},
236 {"acronym name that isn't a credential", "APITokenCount=" + fake("", 16), "APITokenCount=" + fake("", 16)},
237 {"camelCase pagination token", "nextPageToken=" + fake("", 16), "nextPageToken=" + fake("", 16)},
238 {"camelCase pagination token after an escaped newline", `a=1\nnextPageToken=` + fake("", 16), `a=1\nnextPageToken=` + fake("", 16)},
239 {"camelCase name read from the environment", "const apiKey = process.env.API_KEY;", "const apiKey = process.env.API_KEY;"},
240 {"password that starts with a slash", "password=/" + fake("", 15), "password=[REDACTED:credential]"},
241 {"password in an ODBC connection string", "Driver=x;UID=bob;PWD=" + fake("", 12) + ";", "Driver=x;UID=bob;PWD=[REDACTED:credential];"},
242 {"working directory", "PWD=/home/u1/app OLDPWD=~/src2", "PWD=/home/u1/app OLDPWD=~/src2"},
243 {"plain text", "fix the flaky test in sync_test.go", "fix the flaky test in sync_test.go"},
244 }
245 for _, tt := range tests {
246 t.Run(tt.name, func(t *testing.T) {
247 t.Parallel()
248 assert.Equal(t, tt.want, Secrets(tt.in))
249 })
250 }
253func TestJSON(t *testing.T) {
254 t.Parallel()
256 t.Run("keeps input without secrets byte for byte", func(t *testing.T) {
257 t.Parallel()
258 in := json.RawMessage(`{"z": 1.50, "a": ["x"]}`)
259 assert.Equal(t, string(in), string(JSON(in)))
260 })
262 t.Run("redacts nested string values and keeps numbers", func(t *testing.T) {
263 t.Parallel()
264 in := json.RawMessage(`{"command":"echo ` + fake("glpat-", 20) + `","env":{"KEY":"` + fake("ghp_", 36) + `"},"n":12345678901234567890}`)
265 assert.JSONEq(t,
266 `{"command":"echo [REDACTED:gitlab_token]","env":{"KEY":"[REDACTED:github_token]"},"n":12345678901234567890}`,
267 string(JSON(in)))
268 })
270 t.Run("redacts a secret split across JSON escapes", func(t *testing.T) {
271 t.Parallel()
272 key := "-----BEGIN EC " + "PRIVATE KEY-----\nMHc\n-----END EC " + "PRIVATE KEY-----\n"
273 encoded, err := json.Marshal(map[string]string{"content": key})
274 require.NoError(t, err)
275 assert.JSONEq(t, `{"content":"[REDACTED:private_key]\n"}`, string(JSON(encoded)))
276 })
278 t.Run("redacts values under credential keys", func(t *testing.T) {
279 t.Parallel()
280 in := json.RawMessage(`{"headers":{"Authorization":"Bearer ` + fake("", 30) + `","X-Auth-Token":"t0k3n"},"env":{"AWS_SECRET_ACCESS_KEY":"` + fake("", 40) + `","HOME":"/home/me","TOKEN":"$CI_JOB_TOKEN"},"next_page_token":"c2"}`)
281 assert.JSONEq(t,
282 `{"headers":{"Authorization":"[REDACTED:credential]","X-Auth-Token":"[REDACTED:credential]"},"env":{"AWS_SECRET_ACCESS_KEY":"[REDACTED:credential]","HOME":"/home/me","TOKEN":"$CI_JOB_TOKEN"},"next_page_token":"c2"}`,
283 string(JSON(in)))
284 })
286 t.Run("redacts values under prefixed credential names", func(t *testing.T) {
287 t.Parallel()
288 in := json.RawMessage(`{"env":{"OPENAI_API_KEY":"` + fake("", 20) + `","STRIPE_SECRET_KEY":"` + fake("", 20) + `","PGPASSWORD":"` + fake("", 12) + `","MYSQL_PWD":"` + fake("", 12) + `","SSH_PRIVATE_KEY":"` + fake("", 20) + `"}}`)
289 assert.JSONEq(t,
290 `{"env":{"OPENAI_API_KEY":"[REDACTED:credential]","STRIPE_SECRET_KEY":"[REDACTED:credential]","PGPASSWORD":"[REDACTED:credential]","MYSQL_PWD":"[REDACTED:credential]","SSH_PRIVATE_KEY":"[REDACTED:credential]"}}`,
291 string(JSON(in)))
292 })
294 t.Run("keeps names that only contain a credential word, and the working directory", func(t *testing.T) {
295 t.Parallel()
296 in := json.RawMessage(`{"password_file":"pass1.txt","api_key_id":"k1abcdefgh","secret_key_name":"main1","PWD":"/home/me/src1","OLDPWD":"/tmp/x1"}`)
297 assert.JSONEq(t, string(in), string(JSON(in)))
298 })
300 t.Run("redacts credentials that start with a slash or tilde", func(t *testing.T) {
301 t.Parallel()
302 in := json.RawMessage(`{"AWS_SECRET_ACCESS_KEY":"/` + fake("", 39) + `","password":"~` + fake("", 15) + `","PWD":"` + fake("", 12) + `"}`)
303 assert.JSONEq(t,
304 `{"AWS_SECRET_ACCESS_KEY":"[REDACTED:credential]","password":"[REDACTED:credential]","PWD":"[REDACTED:credential]"}`,
305 string(JSON(in)))
306 })
308 t.Run("redacts values under camelCase credential names", func(t *testing.T) {
309 t.Parallel()
310 in := json.RawMessage(`{"openaiApiKey":"` + fake("", 20) + `","clientSecret":"` + fake("", 20) + `","authToken":"` + fake("", 20) + `","secretKey":"` + fake("", 20) + `","sshPrivateKey":"` + fake("", 20) + `","oauth2Token":"` + fake("", 20) + `"}`)
311 assert.JSONEq(t,
312 `{"openaiApiKey":"[REDACTED:credential]","clientSecret":"[REDACTED:credential]","authToken":"[REDACTED:credential]","secretKey":"[REDACTED:credential]","sshPrivateKey":"[REDACTED:credential]","oauth2Token":"[REDACTED:credential]"}`,
313 string(JSON(in)))
314 })
316 t.Run("redacts names that camelCase splitting would break", func(t *testing.T) {
317 t.Parallel()
318 in := json.RawMessage(`{"passPhrase":"` + fake("", 16) + `","PassWord":"` + fake("", 16) + `","passWd":"` + fake("", 16) + `"}`)
319 assert.JSONEq(t,
320 `{"passPhrase":"[REDACTED:credential]","PassWord":"[REDACTED:credential]","passWd":"[REDACTED:credential]"}`,
321 string(JSON(in)))
322 })
324 t.Run("keeps camelCase names that aren't credentials", func(t *testing.T) {
325 t.Parallel()
326 in := json.RawMessage(`{"nextPageToken":"c2abcdefgh1","maxTokens":"4096","tokenCount":"12345678","apiKeyId":"k1abcdefgh","secretKeyName":"main12345"}`)
327 assert.JSONEq(t, string(in), string(JSON(in)))
328 })
330 t.Run("redacts secrets in keys", func(t *testing.T) {
331 t.Parallel()
332 in := json.RawMessage(`{"` + fake("glpat-", 20) + `":1}`)
333 assert.JSONEq(t, `{"[REDACTED:gitlab_token]":1}`, string(JSON(in)))
334 })
336 t.Run("keeps keys that redact to the same marker", func(t *testing.T) {
337 t.Parallel()
338 in := json.RawMessage(`{"` + fake("glpat-", 20) + `":1,"` + fake("glpat-", 21) + `":2}`)
339 var out map[string]int
340 require.NoError(t, json.Unmarshal(JSON(in), &out))
341 assert.Len(t, out, 2)
342 assert.ElementsMatch(t, []int{1, 2}, []int{out["[REDACTED:gitlab_token]"], out["[REDACTED:gitlab_token]#2"]})
343 })
345 t.Run("does not send a secret that a duplicate key hides from the walk", func(t *testing.T) {
346 t.Parallel()
347 in := json.RawMessage(`{"a":"` + fake("glpat-", 20) + `","a":"other"}`)
348 assert.NotContains(t, string(JSON(in)), "glpat-")
349 })
351 t.Run("keeps empty and null input", func(t *testing.T) {
352 t.Parallel()
353 assert.Empty(t, JSON(nil))
354 assert.Equal(t, "null", string(JSON(json.RawMessage("null"))))
355 })
357 t.Run("redacts input that is not JSON as text", func(t *testing.T) {
358 t.Parallel()
359 assert.Equal(t, "not json [REDACTED:gitlab_token]", string(JSON(json.RawMessage("not json "+fake("glpat-", 20)))))
360 })
363func BenchmarkSecrets(b *testing.B) {
364 prompt := strings.Repeat("Please refactor the sync loop so it retries on timeouts. ", 150)
365 b.SetBytes(int64(len(prompt)))
366 for b.Loop() {
367 Secrets(prompt)
368 }
Diagnostics9 git calls · 231 ms
CallStatusSizeTime0231 ms
discover200185 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=185
git.server.agent=git/2.56.g1891de4-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=185
git.pkt.payload.max=35
ls-refs20058 KiB · hit2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=59849
git.response.cache=HIT
git.response.duration_ms=2
git.protocol.version=2
git.pkt.frames=731
git.pkt.data=730
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=59849
git.pkt.payload.max=143
git.ls_refs.count=730
git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 1020032 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=32926
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=22
git.pkt.data=20
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=32926
git.pkt.payload.max=32046
git.sideband.band1.bytes=32046
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=80
git.pack.bytes=32046
git.pack.ofs_delta=36
git.pack.ref_delta=0
git.pack.chain.max=2
git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91
git.pack.full=44
Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=4273
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=combine:blob:none+tree:2
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=1
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=4273
git.pkt.payload.max=4173
git.sideband.band1.bytes=4173
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=11
git.pack.bytes=4173
git.fetch.wide.objects=11
git.fetch.wide.extra_bytes=3930
git.fetch.wide.blobs=0
fetchtree:02001.8 KiB · hit5 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=5
git.response.bytes=1857
git.response.cache=HIT
git.response.duration_ms=5
git.fetch.filter=tree:0
git.fetch.wants=dce174ff8c632a19d17eea99c8a9b7c37fb2b592
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=1857
git.pkt.payload.max=1830
git.sideband.band1.bytes=1830
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=1830
git.pack.trailer=fb03183b7909876778f740d196d7516811003517
Open pack map
fetchtree:0200261 B · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=261
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=3bb9946301aa8c0dc8d202b83bc9f84bf0400f6d
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=261
git.pkt.payload.max=234
git.sideband.band1.bytes=234
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=234
git.pack.trailer=e0a52f164f79506da6b0dcc275ed62911f5383bf
Open pack map
fetchtree:0200273 B · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=273
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=5fa160737ad30857ff606d64de1d596bb211ac3d
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=273
git.pkt.payload.max=246
git.sideband.band1.bytes=246
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=246
git.pack.trailer=06dd39b7545811625e04a7645a0a64c9a62c99c6
Open pack map
fetchtree:0200136 B · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=136
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=c373651eab5dc06c5c2898f79c90b0403869e983
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=136
git.pkt.payload.max=109
git.sideband.band1.bytes=109
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=109
git.pack.trailer=998c9f92a1a0beae5b1a66f5eb1c0f6feee4e4b4
Open pack map
fetch2005.3 KiB214 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=214
git.response.bytes=5419
git.response.cache=MISS
git.response.duration_ms=214
git.fetch.wants=59690b88351a3bb8095f0d0da0adcff20598974a
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=5419
git.pkt.payload.max=5392
git.sideband.band1.bytes=5392
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://gitlab.com/gitlab-org/cli
Ref
refs/heads/main
Commit
0dd7574dce9e385c5c30540e6c15657adc6bdce9
cf-ray
a5e3006ab94df3c9
Request
863dae52…
Protocol
v2 · sha1
Agent
git/2.56.g1891de4-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format