Code Monkey home page Code Monkey logo

maskify-sense's Introduction

maskify-sense

Mask sensitive data for loggers

Created this tiny library as a way to log data objects in loggers even if they contained JSON stringify values

Install

yarn add maskify-sense
npm install maskify-sense

Usage

To use it we need to create a mask instance using the creator with a configuration object

// import MaskifySense from 'maskify-sense';
const MaskifySense = require('maskify-sense');

const mask = MaskifySense({
  ssn: {
    mask: (str) => `+++_++_${str.slice(-4)}`,
    fields: ['ssn', 'taxId', 'sin'],
  },
  accounts: {
    fields: ['account', 'routing'],
  },
  password: {
    mask: '[redacted]',
    fields: ['password'],
  },
});

The configuration object is an object with categories of masking... each category will have a mask field which can be a String or a Function and a fields field which will be an array of targeted fields to mask (transform).

The category names (keys of the configuration object) are only to have an organization of the different mask strategies for all your fields. They are not used in any other place so you can name them whatever you want.

If the mask field is empty a default mask function will take place (can be override later on).

(str) => `*****${str.slice(-4)}`;

If the mask is a String it will override completely the value of the field in the data object with that mask string value.

const MaskifySense = require('maskify-sense');
const mask = MaskifySense({
  simple: {
    mask: '[redacted]',
    fields: ['password'],
  },
});

console.log(mask({ password: 'mySecretPassword' }));
// logs => { password: '[redacted]' }

If the mask is a Function it will transform the value accordingly with the masked function.

const MaskifySense = require('maskify-sense');
const mask = MaskifySense({
  simple: {
    mask: (v) => `*****${str.slice(-4)}`,
    fields: ['password'],
  },
});

console.log(mask({ password: 'mySecretPassword' }));
// logs => { password: '*****word' }

The masked function accepts only one argument which would be the value that is being evaluated.

The mask can evaluate also stringified json strings

const MaskifySense = require('maskify-sense');
const mask = MaskifySense({
  passwords: {
    mask: '***',
    fields: ['password', 'secret'],
  },
  accounts: {
    mask: (v) => `*****${str.slice(-4)}`,
    fields: ['routing', 'bankAccount'],
  },
});

console.log(
  mask({
    name: 'John Doe',
    bankData: '{"bankAccount":12345678,"routing":9876543}',
    credentials: '{"username":"UA","password":"12345","secret":"ABC"}',
  }),
);
// logs =>
// {
//   name: 'John Doe',
//   bankData: '{"bankAccount":"*****5678","routing":"*****6543"}',
//   credentials: '{"username":"UA","password":"***","secret":"***"}',
// }

You can check also the test/script.js file for more examples.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.