Skip to content

Conversation

@NikoRoberts
Copy link
Contributor

@NikoRoberts NikoRoberts commented Nov 26, 2025

Summary

Add --redact-body-fields CLI option to redact sensitive JSON fields in request and response bodies during tape recording. This prevents credentials like passwords, tokens, and API keys from being written to disk.

What was done

Implemented body field redaction in Proxay core:

1. CLI Option

proxay --mode record --host https://api.example.com \
  --tapes-dir ./tapes \
  --redact-body-fields password,access_token,refresh_token,api_key

2. Implementation Details

  • Location: src/persistence.ts (redaction before serialization)
  • Parsing: UTF8-encoded JSON bodies only (binary/non-JSON unchanged)
  • Matching: Case-insensitive field names
  • Recursion: Handles nested objects and arrays
  • Redaction value: XXXX (matches existing --redact-headers pattern)

3. Example

Before redaction:

{"email": "user@example.com", "password": "secret123"}

After redaction:

{"email": "user@example.com", "password": "XXXX"}

Changes

Core Files

  • src/cli.ts - Added --redact-body-fields option
  • src/server.ts - Wired option to Persistence layer
  • src/persistence.ts - Implemented redaction logic
    • redactBodyFields() - Main entry point
    • redactBufferFields() - JSON parsing and Buffer conversion
    • redactObjectFields() - Recursive field redaction

Tests Added (9 comprehensive tests)

  • ✅ Redacts simple JSON fields
  • ✅ Case-insensitive matching
  • ✅ Nested object redaction
  • ✅ Array handling
  • ✅ Response body redaction
  • ✅ Non-JSON body handling (returns unchanged)
  • ✅ Binary body handling (returns unchanged)
  • ✅ Empty body handling
  • ✅ No-op when no fields specified

Add support for redacting sensitive JSON fields in request and response
bodies during tape recording. This prevents credentials like passwords,
tokens, and API keys from being written to tape files.

## Changes

### CLI (src/cli.ts)
- Added `--redact-body-fields <fields>` option accepting comma-separated list
- Passes field names to RecordReplayServer

### Server (src/server.ts)
- Added `redactBodyFields` parameter to constructor options
- Passes field names to Persistence layer

### Persistence (src/persistence.ts)
- Updated constructor to accept `redactBodyFields` parameter
- Added `redactBodyFields()` function to redact both request/response bodies
- Added `redactBufferFields()` to parse JSON and redact field values
- Added `redactObjectFields()` for recursive redaction of nested objects/arrays
- Case-insensitive field matching
- Gracefully handles non-JSON bodies (returns unchanged)

### Tests (src/persistence.spec.ts)
- Added 9 comprehensive tests for body field redaction:
  - Simple JSON field redaction
  - Case-insensitive matching
  - Nested object redaction
  - Array handling
  - Response body redaction
  - Non-JSON body handling
  - Binary body handling
  - Empty body handling
  - No-op when no fields specified

## Usage

```bash
# Record with body field redaction
proxay --mode record --host https://api.example.com \
  --tapes-dir ./tapes \
  --redact-body-fields password,access_token,refresh_token,api_key

# Redact specific fields
PROXAY_REDACT_BODY_FIELDS=password,token proxay --mode record ...
```

## Example

Before redaction:
```json
{"email": "user@example.com", "password": "secret123"}
```

After redaction:
```json
{"email": "user@example.com", "password": "XXXX"}
```

## Compatibility

- Works alongside existing `--redact-headers` option
- Only affects utf8-encoded JSON bodies
- Binary bodies and non-JSON bodies are unchanged
- Matches existing "XXXX" redaction pattern from header redaction

Related: Phase 2 of security alert remediation (GitHub secret scanning)
@NikoRoberts NikoRoberts requested a review from a team as a code owner November 26, 2025 22:27
@CLAassistant
Copy link

CLAassistant commented Nov 26, 2025

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

@timdawborn timdawborn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM apart from the linting issues.

NikoRoberts and others added 4 commits January 5, 2026 16:18
- Fix prettier formatting in src/persistence.ts and src/server.ts
- Rename redactBodyFields function to redactRecordBodyFields to avoid
  shadowing the class property of the same name

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Copy link

@Camcan Camcan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nicely done @NikoRoberts
I assume we've considered redacting fields by default and have arrived here through some deliberation? I'd have imagined there are very few instances where we'd not want to be redacting *_key & *_token values in logs.

@NikoRoberts
Copy link
Contributor Author

@Camcan the main reasoning is that it isn't the default today and it would then mean that all tapes need re-recording. If we want to change that default I would suggest we do a major version change

@NikoRoberts NikoRoberts merged commit 6e2be91 into master Jan 8, 2026
9 checks passed
@NikoRoberts NikoRoberts deleted the niko/add-redact-body-fields branch January 8, 2026 08:04
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.

6 participants