Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@hackylabs/deep-redact

Package Overview
Dependencies
Maintainers
0
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hackylabs/deep-redact

A fast, safe and configurable zero-dependency library for redacting strings or deeply redacting arrays and objects.

  • 2.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

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.

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({
  stringTests: [
    {
      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>'

Configuration

Main Options

keydescriptiontypeoptionsdefaultrequired
blacklistedKeysDeeply compare names of these keys against the keys in your object.arrayArray<string│RegExp│BlacklistKeyConfig>[]N
stringTestsArray of regular expressions to perform against string values, whether that value is a flat string or nested within an object.arrayArray<RegExp│StringTestConfig>[]N
fuzzyKeyMatchLoosely 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).booleanfalseN
caseSensitiveKeyMatchLoosely 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.booleantrueN
removeDetermines whether or not to remove the key from the object when it is redacted.booleanfalseN
retainStructureDetermines whether or not keep all nested values of a key that is going to be redacted. Circular references are always removed.booleanfalseN
replacementWhen a value is going to be redacted, what would you like to replace it with?string │ function[REDACTED]N
replaceStringByLengthWhen 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.booleanfalseN
typesJS 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.arrayArray<'string'│'number'│'bigint'│'boolean'│'symbol'│'undefined'│'object'│'function'>['string']N
serialiseDetermines 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.booleanfalseN
serializeAlias of serialise for International-English users.booleanfalseN

BlacklistKeyConfig

keytypedefaultrequired
keystring│RegExpY
fuzzyKeyMatchbooleanMain options fuzzyKeyMatchN
caseSensitiveKeyMatchbooleanMain options caseSensitiveKeyMatchN
removebooleanMain options removeN
retainStructurebooleanMain options retainStructureN

StringTestConfig

keydescriptiontyperequired
patternA regular expression to perform against a string value, whether that value is a flat string or nested within an object.RegExpY
replacerA 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.functionY

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

scenarioops / secop duration (ms)margin of errorsample count
DeepRedact, XML171985.010.00581446010.0000485993
JSON.stringify, large object162406.590.00615738550.0000281204
DeepRedact, remove item, single object25293.550.0395357750.0002312647
Regex replace, large object22529.520.04438621530.0002411265
DeepRedact, custom replacer function, single object22324.840.04479315540.0003711163
DeepRedact, default config, large object21437.040.0466482390.0002810719
DeepRedact, replace string by length, single object21018.320.04757755190.0008410510
DeepRedact, fuzzy matching, single object18000.380.05555438580.00039001
DeepRedact, retain structure, single object17581.50.0568779560.000378791
DeepRedact, config per key, single object16914.730.05912005520.00048458
DeepRedact, default config, 1000 large objects7989.050.12517125310.00183995
fast redact, large object5929.580.16864590960.001262965
ObGlob, large object4939.980.20243006640.011052470
DeepRedact, case insensitive matching, single object4721.590.21179325410.003782361
DeepRedact, fuzzy and case insensitive matching, single object4686.290.21338829480.00182344
JSON.stringify, 1000 large objects222.054.50359126790.04553112
ObGlob, 1000 large objects164.556.07713236140.1182983
fast redact, 1000 large objects120.78.28482022950.0570261
Regex replace, 1000 large objects93.510.69545253190.3901447

Keywords

FAQs

Package last updated on 05 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc