npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redact-it

v0.2.5

Published

A flexible tool to redact JS objects' data

Downloads

7

Readme

Redact-it

A flexible and easy way to redact data from objects.

Why?

This project was designed from real-world scenarios composed of 2 main concerns:

  1. Preventing sensitive data to be written in logs;
  2. Investigating production requests by using meaningful log messages and contexts;

The first one is by far the most important. If an attack or some failure exposes logs, sensitive data might come within them. Surely, most systems are designed to never let it happen. But in case it does, have sensitive data redacted mitigates the negative impact.

To solve that, one way is to never print any data to logs. However, context logging may save a lot of time on debugging or investigating a specific situation in production.

How?

Since neither approaches are ideal for both parts, the owner of the data may determine how much of the actual data can be printed out to logs. Depending on the type of data logged, partial printing can be used to be a middle point between the two concerns. This is where this tiny library comes in handy :)

This library helps to build a replacer function flexible enough to declare which fields are going to be redacted and how.

Then, the replacer function might be used with the logging tool of your choice. In the tests and following examples, we are using the JSON.stringify() for its simplicity.

Docs

The most important concepts to read about are documented in this file.

Examples

We have some tests with more usage examples, check them out here!

For the following examples, we are going to use this main object as reference:

const userInfoToBeLogged = {
  password: "123",
  name: "foo",
  TOKEN: "my-secret-access-token",
  card: {
    number: "1234567887654321",
    cvv: "123",
    expirationDate: "2020-12-20",
  },
  authorization: "Bearer token",
};

Default usage

If the idea is to redact the data entirely, you just need to to name the fields. The default Mask is the replace with the fixed string [redacted]:

const redactItConfig: RedactItConfig = {
  fields: ["password", "cvv", "TOKEN", "authorization"],
};

const replacerFunction: ReplacerFunction = redactIt(redactItConfig);

const stringResult = JSON.stringify(userInfoToBeLogged, replacerFunction);
const parsedResult = JSON.parse(stringResult);

/* parsedResult
{
  name: 'foo',
  password: '[redacted]',
  TOKEN: '[redacted]'
  card: {
    number: '1234567887654321',
    cvv: '[redacted]',
    expirationDate: '2020-12-20',
  },
  authorization: "[redacted]"
}
*/

With multiple sets of fields and masks to apply to each set

const redactItConfig: RedactItConfig = [
  {
    fields: ["password"], // which fields to redact
    mask: {
      // How to redact the fields
      type: "undefine", // the undefine mask removes the fields
    },
  },
  {
    fields: [/token/i], // Will match the regex against all fields
  },
  {
    fields: ["expirationDate", "number"],
    mask: {
      type: "percentage", // Percentage masks redact data partially
      redactWith: "•", // Redacted characters replaced by •
      percentage: 75, // 75% of the value should be redacted
    },
  },
  {
    fields: ["authorization"],
    mask: {
      type: "replace", // Replaces the whole value with the `redactWith` string
      redactWith: "[REDACTED FOR COMPLIANCE REASONS]",
    },
  },
  { fields: ["cvv"] }, // if no mask is passed, fields values are redacted as [redacted]
];

const replacerFunction: ReplacerFunction = redactIt(redactItConfig);

const stringResult = JSON.stringify(userInfoToBeLogged, replacerFunction);
const parsedResult = JSON.parse(stringResult);

/* parsedResult
{
  name: 'foo',
  TOKEN: '[redacted]',
  card: {
    number: '••••••••••••4321',
    cvv: '[redacted]',
    expirationDate: '••••••••20'
  },
  authorization: "[REDACTED FOR COMPLIANCE REASONS]"
}
*/

Objectives

This project aims to have a flexible yet powerful API to create a replacer function to redact partial or entire object values.

Roadmap

  • Support partial email redacting

Contributing

If you like this project and want to contribute with it, you can fork it and create a pull request :)