Redact Basic Auth

1.0.1 · active · verified Wed Apr 22

redact-basic-auth is a focused utility designed to remove or obscure basic authentication credentials from URLs. This is crucial for logging, debugging, or displaying URLs where sensitive information like usernames and passwords should not be exposed. The package is currently at version 1.0.1, indicating a stable and mature, albeit simple, implementation. Its release cadence is likely very slow, as its functionality is narrowly defined and unlikely to require frequent updates. Key differentiators include its singular focus and minimal API, making it easy to integrate without pulling in larger URL parsing libraries when only basic auth redaction is needed. It operates on string manipulation rather than full URL parsing objects, providing a lightweight solution for specific security and privacy concerns in applications that handle URLs.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic authentication redaction for various URL formats, including those with and without credentials, and how to use it with Node.js URL objects.

import redact from 'redact-basic-auth';

// Example 1: Redacting a URL with basic authentication
const urlWithAuth = 'http://myuser:mypassword@example.com/api/data?param=value';
const redactedUrl = redact(urlWithAuth);
console.log('Original URL:', urlWithAuth);
console.log('Redacted URL:', redactedUrl);
// Expected: Original URL: http://myuser:mypassword@example.com/api/data?param=value
// Expected: Redacted URL: http://myuser:redacted@example.com/api/data?param=value

// Example 2: A URL without basic authentication remains unchanged
const urlWithoutAuth = 'https://www.google.com/search?q=redact-basic-auth';
const unchangedUrl = redact(urlWithoutAuth);
console.log('Original URL (no auth):', urlWithoutAuth);
console.log('Unchanged URL:', unchangedUrl);
// Expected: Original URL (no auth): https://www.google.com/search?q=redact-basic-auth
// Expected: Unchanged URL: https://www.google.com/search?q=redact-basic-auth

// Example 3: Using with a Node.js URL object (convert to string first)
import { URL } from 'url';
const complexUrl = new URL('ftp://anonymous:secret@ftp.example.org/files/doc.txt');
const redactedComplexUrl = redact(complexUrl.toString());
console.log('Original complex URL:', complexUrl.toString());
console.log('Redacted complex URL:', redactedComplexUrl);

view raw JSON →