Skip to content

Fix/redact sensitive auth logs#333

Open
Priyanshubhartistm wants to merge 3 commits into
microcks:masterfrom
Priyanshubhartistm:fix/redact-sensitive-auth-logs
Open

Fix/redact sensitive auth logs#333
Priyanshubhartistm wants to merge 3 commits into
microcks:masterfrom
Priyanshubhartistm:fix/redact-sensitive-auth-logs

Conversation

@Priyanshubhartistm
Copy link
Copy Markdown

Description

  • Introduced a new pkg/utils package with sanitization helpers (SanitizeString, SanitizeJSON, SanitizeHeaders, MaskSecret) that redact sensitive values before they are written to logs or stdout.
  • Fixed DumpRequestIfRequired and DumpResponseIfRequired in pkg/config/config.go to pass all raw HTTP dump output through SanitizeString before printing, ensuring Authorization: Basic/Bearer headers, access_token/refresh_token response bodies, and URL-encoded form secrets (e.g. client_secret=) are always replaced with [REDACTED] — this single change covers all callers across keycloak_client.go and microcks_client.go.
  • Fixed cmd/login.go to mask the SSO access token and refresh token with MaskSecret before logging, and to sanitize the OAuth2 callback URL with SanitizeString to prevent authorization codes from leaking in log output.
  • Added 7 unit tests in pkg/utils/sanitize_test.go covering MaskSecret, SanitizeHeaders, nested JSON redaction, case-insensitive key matching, malformed JSON passthrough, HTTP dump with form body, and Basic auth header redaction.

Before (with --verbose):

Authorization: Basic dGVzdGNsaWVudDpzdXBlcnNlY3JldA==
...
{"access_token":"eyJhbGciOiJSUzI1NiJ9...","refresh_token":"eyJhbGc..."}
...
Token: eyJhbGciOiJSUzI1NiJ9...
Refresh Token: eyJhbGc...

After (with --verbose):

Authorization: [REDACTED]
...
{"access_token":"[REDACTED]","refresh_token":"[REDACTED]"}
...
Token: [REDACTED]
Refresh Token: [REDACTED]

Test results

$ go test ./pkg/utils/ -v

=== RUN   TestMaskSecret
--- PASS: TestMaskSecret (0.00s)
=== RUN   TestSanitizeHeaders
--- PASS: TestSanitizeHeaders (0.00s)
=== RUN   TestSanitizeJSONNested
--- PASS: TestSanitizeJSONNested (0.00s)
=== RUN   TestSanitizeJSONCaseInsensitive
--- PASS: TestSanitizeJSONCaseInsensitive (0.00s)
=== RUN   TestSanitizeJSONMalformed
--- PASS: TestSanitizeJSONMalformed (0.00s)
=== RUN   TestSanitizeStringHeadersAndForm
--- PASS: TestSanitizeStringHeadersAndForm (0.00s)
=== RUN   TestSanitizeStringBasicAuth
--- PASS: TestSanitizeStringBasicAuth (0.00s)
PASS
ok  	github.com/microcks/microcks-cli/pkg/utils	0.006s

Related issue(s)

Fixes #265

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 8, 2026

👋 @Priyanshubhartistm

Welcome to the Microcks community! 💖

Thanks and congrats 🎉 for opening your first pull request here! Be sure to follow the pull request template or please update it accordingly.

Hope you have a great time there!

Introduce a new pkg/utils/sanitize.go package with utilities to redact
sensitive values before they are written to logs or stdout:

- MaskSecret: replaces any non-empty secret with '[REDACTED]'
- SanitizeHeaders: returns a copy of http.Header with sensitive header
  values replaced
- SanitizeJSON: recursively redacts sensitive keys in JSON payloads
- SanitizeString: handles raw HTTP dump strings (CRLF/LF), sanitizing
  headers, Authorization scheme tokens, and URL-encoded form fields
  (access_token=, client_secret=, password=, etc.)

Sensitive key matching is case-insensitive and covers authorization,
proxy-authorization, access_token, refresh_token, id_token,
client_secret, password, token, api_key, cookie, set-cookie, x-api-key.

Also adds sanitize_test.go with 7 unit tests covering all helpers.

Closes microcks#265

Signed-off-by: Priyanshubhartistm <bhartipriyanshustm@gmail.com>
DumpRequestIfRequired and DumpResponseIfRequired previously printed raw
httputil dump output directly to stdout, which included Authorization
headers (Basic and Bearer), token response bodies, and form-encoded
secrets when --verbose was active.

Pass the dump through utils.SanitizeString before printing so that all
sensitive headers and body values are replaced with '[REDACTED]'.

This single fix covers all callers across the codebase:
- pkg/connectors/keycloak_client.go  (Keycloak auth requests/responses)
- pkg/connectors/microcks_client.go  (Microcks API requests/responses)

Fixes microcks#265

Signed-off-by: Priyanshubhartistm <bhartipriyanshustm@gmail.com>
The oauth2login function logged the raw access token and refresh token
directly via log.Printf, making them visible in any terminal or CI/CD
log where --verbose is active.

- Replace token log calls with utils.MaskSecret so the values are
  always printed as '[REDACTED]' regardless of --verbose state
- Sanitize the OAuth2 callback URL logged on each redirect using
  utils.SanitizeString to redact any authorization codes or state
  params that may appear in query strings

Fixes microcks#265

Signed-off-by: Priyanshubhartistm <bhartipriyanshustm@gmail.com>
@Priyanshubhartistm Priyanshubhartistm force-pushed the fix/redact-sensitive-auth-logs branch from d9a0239 to 572d7d0 Compare May 8, 2026 18:21
@Priyanshubhartistm
Copy link
Copy Markdown
Author

Priyanshubhartistm commented May 8, 2026

Hii @lbroudoux, @yada , @Harsh4902 could you please take a look at this PR when you get a chance? Would appreciate your review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: verbose leaks authentication tokens and client secrets in CLI logs

1 participant