secret-scrubber-js
secret-scrubber
is a JS package for removing sensitive data (such as passwords or API secrets) from arbitrary objects. It was written by Zapier to programmatically censor user logs without knowing what we were looking for.
Installation + Quick Start
yarn add @zapier/secret-scrubber
There are two main functions:
scrub
takes an object/array/string and an array of sensitive strings. It returns the input with anything from the sensitive array censored.findSensitiveValues
uses our battle-tested heuristics to guess which values should be censored in an object.
Used together, these functions make it easy to pull sensitive data out of a user-supplied object without knowing the exact secrets you're looking for. To get value from this library, it's very important to use the scrub
function. Without its transform logic, there's a much greater chance of leaking secrets. See the scrub docs for more info about the transforms.
Here's an example:
import { scrub, findSensitiveValues } from '@zapier/secret-scrubber'
scrub('Hey there! The password is "very-secret-password"', [
'very-secret-password',
])
const request = {
url: 'https://site.com?api_key=this%20is%20my%20key',
body: {
text: 'The password is "this is my key"',
},
headers: {
authorization: 'Basic ZGF2aWQ6aHVudGVyMg==',
accept: 'application/json',
},
}
findSensitiveValues(request)
scrub(request, findSensitiveValues(request))
API
The following are the functions exported by this package:
scrub
scrub: (input: object | any[] | string, secretValues: Array<string | number>) =>
any
Recursively removes any version of each secretValue
found in input
. It looks for secrets both as plaintext and after a number of transformations. For instance, it you pass 'secret code'
as a secret, it properly censors https://example.com?key=secret%20code
. It currently performs the following transforms:
- as-is (no transform)
- percent encoded (via encodeURIComponent)
- percent encoded, but strings are replaces with
+
- via
JSON.stringify
, so secrets with control characters
You can supply your own list of secret values (if you know them) or use one of the below functions to help extract a list of secrets.
findSensitiveValues
findSensitiveValues: (obj: object) => string[]
A convenience function to find potentially sensitive data in objects. It grabs:
- any value tied to a typically sensitive key (such as
authorization
or password
) - the username, password, and all querystring items with sensitive keys from any URL
recurseExtract: (obj: object | any[], matcher: (key: string, value: any) => boolean): string[]
The underlying recursive function that powers findSensitiveValues
. It takes an object to recurse and a matcher function. It returns stringified versions of any values that were found in the below steps. It's algorithm is as follows:
In the root object, each key/value pair are passed to the matcher
function. Then:
- if the value is an object, the function recurses
- if the value is an array, we iterate each element:
- if the element is an object or array, we recurse
- otherwise, we call the matcher with only the value (
matcher('', value)
)
- otherwise, we pass the key and value to the matcher. If it's true, we collect the value
Versioning
This project uses SemVer as its versioning scheme.
- Major versions will change change the public API (changing call signatures of exported functions or removing/renaming them entirely) or change platform support (aka, dropping support for older versions of Node.js)
- Minor versions will add options, exported functions, or add transforms (see the the list of current transforms)
- Patch versions will be internal bug fixes that don't affect either of the above cases
Deploying New Versions
To release a new version publicly on the npm
registry, do the following:
- Create a PR that updates the
version
in package.json
and updates the CHANGELOG.md
with the new version number. - Once that's approved merge it.
- in CI, once tests pass, there's a "deploy" button. Clicking it will run the job that releases whatever version is in
package.json
on the main
branch.
Created using generator-xavdid during the Summer '21 Zapier hackathon.