Socket
Socket
Sign inDemoInstall

@hackylabs/deep-redact

Package Overview
Dependencies
Maintainers
0
Versions
4
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.


Version published
Weekly downloads
647
increased by488.18%
Maintainers
0
Weekly downloads
 
Created
Source

Deep Redact

Faster than fast-redact 1 as well as being safer and more configurable than many other redaction libraries, 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 are handled gracefully, and the library is designed to be as fast as possible while still being configurable.

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.

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 'deep-redact'; // If you're using CommonJS, import with require('deep-redact') instead. Both CommonJS and ESM support named and default imports.

const redaction = new DeepRedact({
  replacement: '*',
  replaceStringByLength: true,
  blacklistedKeys: ['password'],
  stringTests: [
    /^[\d]{13,16}$/, // payment card number
    /^[\d]{3,4}$/ // CVV
  ],
});

const obj = {
  password: '<h1><strong>Password</strong></h1>',
  cardNumber: '1234567812345678',
  cvv: '123',
};

redaction.redact(obj) // { password: '**********************************', cardNumber: '****************', cvv: '***' }

Configuration

Main Options

keydescriptiontypeoptionsdefaultrequired
blacklistedKeysDeeply compare names of these keys against the keys in your object.arrayArray<string│BlacklistKeyConfig>[]N
stringTestsArray of regular expressions to perform against string values, whether that value is a flat string or nested within an object.arrayRegExp[][]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[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 or object may be redacted. The other types are only listed as options to keep TypeScript happy, so you never need to list them.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.booleantrueN
unsupportedTransformerWhen an unsafe value is encountered or a value that cannot be serialised. By default, this function will transform an unsupported value Unsupported object. BigInt values are converted a string. Dates are returned using their own toISOString method. Regular expressions are returned as objects with their source and flags values. Errors are converted objects. This is useful when you have a custom class that you would like to redact. For safety reasons, you should always transform a BigInt to avoid JSON.stringify throwing an error.(value: unknown) => unknownDeepRedact.transformUnsupportedN

BlacklistKeyConfig

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

Benchmark

Comparisons are made against JSON.stringify and fast-redact as well as different configurations of deep-redact, using this test object. The benchmark is run on a 2021 iMac with an M1 chip with 16GB memory running Sonoma 14.5.

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. Fast-redact is included as a benchmark because it's the next fastest redaction library available. Neither JSON.stringify nor fast-redact offer the same level of configurability as deep-redact.

Benchmark

scenarioops / secop duration (ms)margin of errorsample count
JSON.stringify, tiny object3878848.930.000257808401939425
DeepRedact, default config, tiny object1530332.280.00065345290.00001765167
JSON.stringify, large object295526.110.00338379580.00001147764
fast redact, tiny object228053.930.00438492770.00002114027
DeepRedact, default config, large object92714.280.01078582560.0000646358
DeepRedact, remove item, single object92349.450.01082843490.0000546175
DeepRedact, fuzzy matching, single object89414.820.01118382820.0000744708
DeepRedact, fuzzy and case insensitive matching, single object87852.360.01138273340.0000643927
DeepRedact, case insensitive matching, single object86797.230.01152110450.0000643399
DeepRedact, replace string by length, single object84150.950.0118834070.0000642076
DeepRedact, config per key, single object71236.850.01403767860.0000935619
DeepRedact, retain structure, single object69738.860.01433920760.0000734870
fast redact, large object19480.950.05133218650.000389741
JSON.stringify, 1000 tiny objects16003.160.06248765260.000178002
DeepRedact, default config, 1000 tiny objects15827.220.06318229320.000437914
DeepRedact, default config, 1000 large objects13705.520.0729632850.000546853
fast redact, 1000 tiny objects7430.880.13457351640.000673716
JSON.stringify, 1000 large objects423.522.36118805190.00982212
fast redact, 1000 large objects77.3212.93279710260.2593939

Keywords

FAQs

Package last updated on 27 Jul 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