@hackylabs/deep-redact
TypeScript icon, indicating that this package has built-in type declarations

2.2.1 • Public • Published

Deep Redact

npm version GitHub license

Faster than Fast Redact 1 as well as being safer and more configurable than many other redaction solutions, Deep Redact is a zero-dependency tool that redacts sensitive information from strings and objects. It is designed to be used in a production environment where sensitive information needs to be redacted from logs, error messages, files, and other outputs. Supporting both strings and objects or a mix of both, Deep Redact can be used to redact sensitive information from more data structures than any other redaction library. Even partially redacting sensitive information from strings is supported, by way of custom regex patterns and replacers.

Circular references and other unsupported values are handled gracefully, and the library is designed to be as fast as possible while still being easy to use and configure.

Supporting both CommonJS and ESM, with named and default exports, Deep Redact is designed to be versatile and easy to use in any modern JavaScript or TypeScript project in Node or the browser.

ko-fi

Installation

npm install @hackylabs/deep-redact

Usage

In order to maintain a consistent usage throughout your project, it is not advised to call this library outside of your global logging/error-reporting libraries.

// ./src/example.ts
import {DeepRedact} from '@hackylabs/deep-redact'; // If you're using CommonJS, import with require('@hackylabs/deep-redact') instead. Both CommonJS and ESM support named and default imports.

const objRedaction = new DeepRedact({
  blacklistedKeys: ['sensitive', 'password', /name/i],
})

const obj = {
  keepThis: 'This is fine',
  sensitive: 'This is not fine',
  user: {
    id: 1,
    password: '<h1><strong>Password</strong></h1>',
    firstName: 'John',
  }
}

// Recursively redact sensitive information from an object
objRedaction.redact(obj)
// {
//  keepThis: 'This is fine',
//  sensitive: '[REDACTED]',
//  user: {
//    id: 1,
//    password: '[REDACTED]',
//    firstName: '[REDACTED]'
//  }
// }

const strRedaction = new DeepRedact({
  partialStringTests: [
    {
      pattern: /<(email|password)>([^<]+)<\/\1>/gi,
      replacer: (value: string, pattern: RegExp) => value.replace(pattern, '<$1>[REDACTED]</$1>'),
    },
  ],
})

// Partially redact sensitive information from a string
strRedaction.redact('<email>someone@somewhere.com</email><keepThis>This is fine</keepThis><password>secret</password>')
// '<email>[REDACTED]</email><keepThis>This is fine</keepThis><password>[REDACTED]</password>'

// Override the unsupportedTransformer method to handle unsupported values

class CustomRedaction extends DeepRedact {
  constructor(options) {
    super(options)
    this.rewriteUnsupported = (value) => {
      if (value instanceof BigInt) return value.toString()

      // Add more conditional statements for unsupported value types here (e.g. Error, Date, Map, Set, etc.)

      // If the value is supported, return it
      return value
    }
  }
}

const customRedaction = new CustomRedaction({
  blacklistedKeys: ['sensitive', 'password', /name/i],
})

customRedaction.redact({ a: BigInt(1) })

Configuration

Main Options

key description type options default required
blacklistedKeys Deeply compare names of these keys against the keys in your object. array Array<string│RegExp│BlacklistKeyConfig> [] N
stringTests Array of regular expressions to perform against string values, whether that value is a flat string or nested within an object. Will redact whole string values. If you want to redact only part of the string, use partialStringTests instead. If a replacer function is provided in the config for the associated test, it will be used to redact the value. array Array<RegExp│StringTestConfig> [] N
partialStringTests Array of regular expressions to perform against string values, whether that value is a flat string or nested within an object. Will redact only the matched part of the string using the replacer function provided in the config for the associated test. array StringTestConfig[] [] N
fuzzyKeyMatch Loosely compare key names by checking if the key name of your unredacted object is included anywhere within the name of your blacklisted key. For example, is "pass" (your key) included in "password" (from config). boolean false N
caseSensitiveKeyMatch Loosely compare key names by normalising the strings. This involves removing non-word characters and transforms the string to lowercase. This means you never have to worry having to list duplicate keys in different formats such as snake_case, camelCase, PascalCase or any other case. boolean true N
remove Determines whether or not to remove the key from the object when it is redacted. boolean false N
retainStructure Determines whether or not keep all nested values of a key that is going to be redacted. Circular references are always removed. boolean false N
replacement When a value is going to be redacted, what would you like to replace it with? string │ function [REDACTED] N
replaceStringByLength When a string value is going to be replaced, optionally replace it by repeating the replacement to match the length of the value. For example, if replaceStringByLength were set to true and replacement was set to "x", then redacting "secret" would return "xxxxxx". This is sometimes useful for debugging purposes, although it may be less secure as it could give hints to the original value. boolean false N
types JS types (values of typeof keyword). Only values with a typeof equal to string, number, bigint, boolean, symbol, object, or function will be redacted. Undefined values will never be redacted, although the type undefined is included in this list to keep TypeScript happy. array Array<'string'│'number'│'bigint'│'boolean'│'symbol'│'undefined'│'object'│'function'> ['string'] N
serialise Determines whether or not to serialise the object after redacting. Typical use cases for this are when you want to send it over the network or save to a file, both of which are common use cases for redacting sensitive information. boolean false N
serialize Alias of serialise for International-English users. boolean false N

BlacklistKeyConfig

key type default required
key string│RegExp Y
fuzzyKeyMatch boolean Main options fuzzyKeyMatch N
caseSensitiveKeyMatch boolean Main options caseSensitiveKeyMatch N
remove boolean Main options remove N
retainStructure boolean Main options retainStructure N

StringTestConfig

key description type required
pattern A regular expression to perform against a string value, whether that value is a flat string or nested within an object. RegExp Y
replacer A function that will be called with the value of the string that matched the pattern and the pattern itself. This function should return the new (redacted) value to replace the original value. function Y

Benchmark

Comparisons are made against JSON.stringify, Regex.replace, Fast Redact & (one of my other creations, @hackylabs/obglob) as well as different configurations of Deep Redact, using this test object. Fast Redact was configured to redact the same keys on the same object as Deep Redact without using wildcards.

The benchmark is run on a 2021 iMac with an M1 chip with 16GB memory running macOS Sequoia 15.0.0.

JSON.stringify is included as a benchmark because it is the fastest way to deeply iterate over an object, although it doesn't redact any sensitive information.

Regex.replace is included as a benchmark because it is the fastest way to redact sensitive information from a string. However, a regex pattern for all keys to be redacted is much harder to configure than a dedicated redaction library, especially when dealing with multiple types of values. It also doesn't handle circular references or other unsupported values as gracefully as deep-redact unless a third-party library is used to stringify the object beforehand.

Fast-redact is included as a benchmark because it's the next fastest library available specifically for redaction.

Neither JSON.stringify, Regex.replace nor Fast Redact offer the same level of configurability as deep-redact. Both Fast Redact and Obglob are slower and rely on dependencies.

Benchmark

scenario ops / sec op duration (ms) margin of error sample count
DeepRedact, partial redaction 176654.38 0.0056607711 0.00003 88329
JSON.stringify, large object 164287.01 0.0060869085 0.00002 82144
DeepRedact, remove item, single object 25142.69 0.0397729959 0.00029 12572
Regex replace, large object 23061.11 0.0433630529 0.00022 11531
DeepRedact, default config, large object 21454.71 0.0466098038 0.00086 10728
DeepRedact, custom replacer function, single object 21026.51 0.047559016 0.00047 10514
DeepRedact, replace string by length, single object 19629.37 0.0509440788 0.00032 9815
DeepRedact, retain structure, single object 18238.97 0.0548276723 0.00049 9120
DeepRedact, fuzzy matching, single object 17470.6 0.0572390237 0.00029 8736
DeepRedact, config per key, single object 15398.94 0.0649395488 0.00036 7700
DeepRedact, default config, 1000 large objects 8401.8 0.1190220507 0.00103 4201
fast redact, large object 5898.84 0.1695249305 0.00133 2950
ObGlob, large object 4876.54 0.2050635404 0.01142 2439
DeepRedact, case insensitive matching, single object 3576.62 0.279593299 0.00282 1789
DeepRedact, fuzzy and case insensitive matching, single object 3379.78 0.295877197 0.00244 1690
JSON.stringify, 1000 large objects 220.76 4.5298012342 0.10929 111
ObGlob, 1000 large objects 166.2 6.0168303571 0.07621 84
DeepRedact, partial redaction large string 126.88 7.8814680469 0.28048 64
fast redact, 1000 large objects 122.12 8.1884899032 0.06661 62
Regex replace, 1000 large objects 93.88 10.6515390208 0.36668 48

Package Sidebar

Install

npm i @hackylabs/deep-redact

Weekly Downloads

840

Version

2.2.1

License

MIT

Unpacked Size

68.9 kB

Total Files

12

Last publish

Collaborators

  • gcmbeng