Skip to content

feat(browser): add redaction support#2406

Open
mahmoodhamdi wants to merge 1 commit intopinojs:mainfrom
mahmoodhamdi:feat/browser-redaction
Open

feat(browser): add redaction support#2406
mahmoodhamdi wants to merge 1 commit intopinojs:mainfrom
mahmoodhamdi:feat/browser-redaction

Conversation

@mahmoodhamdi
Copy link

What

Added redaction support to the browser version of pino using the existing @pinojs/redact package.

Why

The redact option was not supported in the browser version (#670, #2355). Since @pinojs/redact no longer relies on the vm module (unlike the old fast-redact), there's no blocker for browser support anymore.

I was using pino in a browser project where I needed to redact sensitive user data before sending logs to a remote service via transmit.send. Having to manually strip fields was error-prone — native redaction support solves this cleanly.

How

  • Import @pinojs/redact in browser.js (already a dependency, browser-compatible)
  • Parse the redact option (supports both array shorthand and object config with paths, censor, remove)
  • Create a redactor function at logger initialization time with serialize: false (returns objects, not JSON strings)
  • In asObject/write mode: redact the final log object before passing to write
  • In default console mode: redact each object argument before passing to console
  • @pinojs/redact performs selective cloning — original objects are never modified

All redact option formats are supported:

// Array shorthand
pino({ redact: ['password', 'user.ssn'] })

// Object with custom censor
pino({ redact: { paths: ['password'], censor: '***' } })

// Object with remove
pino({ redact: { paths: ['password'], remove: true } })

// Function censor
pino({ redact: { paths: ['password'], censor: (val) => val[0] + '***' } })

// Wildcards
pino({ redact: ['users[*].password'] })

Tests

Added 12 tests in new test/browser-redact.test.js:

  • Redact with array of paths
  • Redact with nested paths
  • Redact with custom censor
  • Redact with remove option
  • Redact with multiple paths
  • Non-matching properties unaffected
  • Works with child loggers
  • Works with wildcard paths
  • Works in non-asObject mode (console)
  • Works with formatters
  • Throws on invalid paths
  • Works with censor function

Docs

Updated docs/redaction.md:

  • Removed outdated "Redaction is not supported in the browser" note
  • Removed outdated reference to fast-redact VM context in Safety section

Checklist

  • Tests pass (npm test)
  • Linter passes (npm run lint)
  • Type tests pass (npm run test-types)
  • Uses existing @pinojs/redact dependency (no new dependencies)
  • No breaking changes

Closes #2355

Add support for the redact option in the browser version of pino,
using the existing @pinojs/redact package which is already
browser-compatible (no VM or Node.js-specific dependencies).

In asObject/write mode, redaction is applied to the final log object.
In default console mode, redaction is applied to each object argument.
Supports all redact option formats: array of paths, object with paths,
custom censor (string or function), and remove option.

Updated docs/redaction.md to remove the outdated "not supported in
browser" note and the fast-redact safety section (pino now uses
@pinojs/redact).

Closes pinojs#2355
Copy link
Member

@Fdawgs Fdawgs left a comment

Choose a reason for hiding this comment

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

Blocking for now. Surge of pull requests across multiple repos from same user appears to be largely AI-generated. User shouldn't recieve contribution credit for deploying a bot.

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.

is redaction now supported in the browser?

2 participants