invisi-data
The invisi-data
is a utility designed to identify and mask sensitive information (e.g., API keys, tokens, passwords) in a given string. This is particularly useful for logging or debugging purposes to avoid exposing sensitive data in logs.
Features
- Regex-Based Masking: Uses predefined patterns to detect and replace sensitive information.
- Flexible Patterns: Handles a variety of sensitive data types, including authorization headers, API keys, tokens, and passwords.
- Reusable Utility: Can be integrated into logging frameworks or used as a standalone tool.
Installation
- Clone the repository or copy the
invisi-data
function into your project. - If using ES modules:
- Import the function using
import { maskSensitiveData } from 'invisi-data';
.
- If using CommonJS:
- Require the function after exporting it from a compatible
.cjs
file.
Usage
Example
const { maskSensitiveData } = require('invisi-data');
const input = JSON.stringify({
Authorization: "Bearer abc123",
"api-key": "my-secret-api-key",
password: "supersecretpassword"
});
const maskedOutput = maskSensitiveData(input);
console.log(maskedOutput);
Parameters
input
- Type:
string
- Description: The input string that may contain sensitive data.
- Requirements: Must be a valid string. Non-string inputs will throw an error.
Returns
- Type:
string
- Description: A new string with sensitive data masked using
****
.
Supported Sensitive Data Patterns
- Authorization Header:
"Authorization": "Bearer <value>"
- Authorization in cURL:
"Authorization Bearer <value>"
- API Key:
"api-key": "<value>"
or "api_key": "<value>"
- Access Token:
"access-token": "<value>"
or "access_token": "<value>"
- Refresh Token:
"refresh-token": "<value>"
or "refresh_token": "<value>"
- Client ID:
"client-id": "<value>"
or "client_id": "<value>"
- ID Token:
"id-token": "<value>"
or "id_token": "<value>"
- Client Secret:
"client-secret": "<value>"
or "client_secret": "<value>"
- Generic Token:
"token": "<value>"
- Password:
"password": "<value>"
- Basic Authorization:
"Authorization": "Basic <value>"
Error Handling
- If the input is not a string or is empty, the function will throw an error:
Error: Input must be a valid string
Tests
Example Test Case
const assert = require('assert');
const { maskSensitiveData } = require('invisi-data');
const input = JSON.stringify({
"Authorization": "Bearer abc123",
"api-key": "secretKey123",
"password": "mypassword"
});
const expectedOutput = JSON.stringify({
"Authorization": "Bearer ****",
"api-key": "****",
"password": "****"
});
assert.strictEqual(maskSensitiveData(input), expectedOutput);
console.log("Test passed!");
License
This function is licensed under the MIT License. Feel free to use, modify, and distribute it as needed.
Contribution
Contributions are welcome! Please submit a pull request or open an issue to suggest enhancements or report bugs.