Skip to content

feat: add support for custom header sanitization and redaction - #37

Open
AdamMagued wants to merge 1 commit into
henvic:mainfrom
AdamMagued:feat-custom-header-sanitization
Open

AdamMagued wants to merge 1 commit into
henvic:mainfrom
AdamMagued:feat-custom-header-sanitization

Conversation

@AdamMagued

Copy link
Copy Markdown

Overview

Currently, httpretty automatically sanitizes default sensitive headers (Authorization, Proxy-Authorization, Cookie, and Set-Cookie) using fixed block characters, but callers cannot configure custom headers for redaction or provide custom sanitization routines without completely bypassing sanitization via SkipSanitize. Because the underlying internal/header package is internal, external callers also cannot reuse standard sanitizers.

This change introduces configurable header sanitization and redaction options on Logger.

Details

  • Added SanitizeHeader(headers []string) and RedactHeaders(headers []string) methods to Logger, as well as SanitizeHeaders []string on the struct, allowing callers to specify custom headers (such as X-Api-Key or custom auth tokens) to redact with [REDACTED]. If Authorization or Cookie is specified in this list, it is redacted with [REDACTED] instead of the default block characters.
  • Added SetHeaderSanitizer(f HeaderSanitizer) method and HeaderSanitizer HeaderSanitizer field to Logger. Custom sanitizers receive (name, value string) and return the sanitized string. Panics within user-provided sanitizers are safely recovered and reported without crashing. If the custom sanitizer returns the original value, fallback to default sanitizers is preserved.
  • Exported AuthorizationSanitizer, CookieSanitizer, and SetCookieSanitizer from package httpretty wrapping internal/header, allowing callers to reuse built-in sanitizers in custom sanitization rules.
  • Added regression tests in logger_test.go verifying header list redaction, custom sanitizer functions, panic recovery, case insensitivity, response header sanitization, concurrency safety, and SkipSanitize behavior.

Fixes #14

Add support for redacting specific headers with [REDACTED] and configuring
custom header sanitization functions on Logger.

- Add SanitizeHeader and RedactHeaders methods to Logger, as well as the
  SanitizeHeaders struct field, to redact designated headers with [REDACTED].
- Add SetHeaderSanitizer method and HeaderSanitizer struct field to Logger
  to allow user-defined sanitization logic per header name/value, with safe
  panic recovery and fallback to default sanitizers.
- Export standard sanitizers (AuthorizationSanitizer, CookieSanitizer, and
  SetCookieSanitizer) from internal/header so users can easily reuse them.
- Add regression tests in logger_test.go.

Fixes henvic#14
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.

Custom header sanitization

1 participant