tpmjs/packages/tools/official/redact-secrets/README.md
Ajax Davis 5d2096fb5d feat: add 100+ official TPMJS tools
Implements a comprehensive suite of AI SDK v6 tools across multiple categories:

- Research (5): page-brief, compare-pages, source-credibility, claim-checklist, timeline-from-text
- Web (10): fetch-text, links-catalog, extract-meta, extract-json-ld, redirect-trace, sitemap-read, rss-read, table-extract, robots-policy, url-normalize
- Data (15): csv-parse, csv-stringify, json-repair, json-schema-validate, yaml-parse, yaml-stringify, text-chunk, normalize-whitespace, dedupe-by-key, pivot, rows-filter, rows-sort, rows-group-aggregate, rows-join, schema-infer
- Doc (12): toc-generate, glossary-build, faq-from-text, executive-brief, decision-record-adr, prd-outline, acceptance-criteria, style-rewrite
- Eng (12): diff-text-unified, env-var-docs-generate, dependency-audit-lite, conventional-commit-suggest, markdown-lint-basic, test-case-generate, stacktrace-parse, release-notes, changelog-entry, release-checklist
- Security (7): redact-secrets, secret-scan-text, url-risk-heuristic, csp-compose, hardening-checklist-web, access-control-matrix, data-classification-heuristic
- Stats (9): effect-size-suite, bootstrap-ci, permutation-test, multiple-testing-adjust, linear-regression-ols, logistic-regression, time-series-decompose-lite, anomaly-detect-mad
- Ops (7): slo-draft, runbook-draft, postmortem-draft, postmortem-action-extractor, error-log-triage, coverage-tracker, monitoring-gap-analysis
- Agent (15): prompt-to-workflow-skeleton, workflow-validate-io, workflow-explain, workflow-cost-estimate, tool-call-accuracy-score, eval-fixture-build, guardrail-policy-draft, workflow-auto-repair, tool-selection-plan, novelty-score-workflow, workflow-variant-generate, config-normalize, recipe-*
- Utility (8): base64-encode, base64-decode, hash-text, regex-extract, template-render, date-parse, json-path-query, url-parse
- HTML (3): html-sanitize, html-to-markdown, markdown-to-html

All tools follow AI SDK v6 pattern with tool() and jsonSchema<T>().

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-31 22:55:56 +10:00

4.1 KiB

@tpmjs/tools-redact-secrets

Redact detected secrets from text by replacing them with [REDACTED:type] placeholders.

Features

  • Automatic Detection: Uses the same patterns as @tpmjs/tools-secret-scan-text
  • Custom Patterns: Optionally provide additional regex patterns
  • Safe Output: Replaces sensitive data with labeled placeholders
  • Detailed Report: Returns information about what was redacted

Installation

npm install @tpmjs/tools-redact-secrets

Usage

Basic Usage

import { redactSecrets } from '@tpmjs/tools-redact-secrets';

const code = `
const AWS_KEY = "AKIAIOSFODNN7EXAMPLE";
const apiKey = "sk-proj-1234567890abcdef";
const dbUrl = "postgres://user:password123@localhost:5432/mydb";
`;

const result = await redactSecrets.execute({ text: code });

console.log(result.redacted);
// const AWS_KEY = "[REDACTED:aws-access-key]";
// const apiKey = "[REDACTED:openai-api-key]";
// const dbUrl = "[REDACTED:postgres-connection]";

console.log(result.redactions);
// [
//   {
//     type: 'aws-access-key',
//     originalLength: 20,
//     line: 2,
//     column: 18,
//     replacement: '[REDACTED:aws-access-key]'
//   },
//   {
//     type: 'openai-api-key',
//     originalLength: 24,
//     line: 3,
//     column: 17,
//     replacement: '[REDACTED:openai-api-key]'
//   },
//   {
//     type: 'postgres-connection',
//     originalLength: 50,
//     line: 4,
//     column: 17,
//     replacement: '[REDACTED:postgres-connection]'
//   }
// ]

Custom Patterns

const text = `
My email is user@example.com
My phone is 555-1234
`;

const result = await redactSecrets.execute({
  text,
  customPatterns: [
    '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}', // Email
    '\\d{3}-\\d{4}', // Phone
  ],
});

console.log(result.redacted);
// My email is [REDACTED:custom-pattern]
// My phone is [REDACTED:custom-pattern]

Using with Logs

import { redactSecrets } from '@tpmjs/tools-redact-secrets';

function safelog(message: string) {
  const result = await redactSecrets.execute({ text: message });
  console.log(result.redacted);
}

// Safe to log - secrets automatically redacted
safelog('Error connecting to postgres://admin:secret@db.example.com/prod');
// Output: Error connecting to [REDACTED:postgres-connection]

Detected Secret Types

The tool automatically redacts:

  • AWS credentials (access keys, secret keys)
  • GitHub tokens
  • Slack tokens and webhooks
  • OpenAI API keys
  • Stripe API keys
  • Generic API keys
  • JWT tokens
  • Private keys (RSA, EC, SSH)
  • Database connection strings (PostgreSQL, MySQL, MongoDB)
  • Hardcoded passwords
  • Google API keys
  • Twilio API keys
  • SendGrid API keys
  • Mailchimp API keys
  • Bearer tokens

Custom Pattern Format

Custom patterns can be provided as:

  1. Plain strings: "pattern" - Treated as literal regex with global flag
  2. Regex format: "/pattern/flags" - Parsed as regex with flags

Examples:

customPatterns: [
  'CUSTOM-[A-Z0-9]{10}',           // Plain string
  '/SECRET_\\w+/gi',                // Regex with flags
  '[0-9]{3}-[0-9]{2}-[0-9]{4}',   // SSN pattern
]

Use Cases

  • Log Sanitization: Remove secrets from application logs before storage
  • Error Messages: Redact credentials from error stack traces
  • Debugging Output: Share debug output safely
  • Documentation: Clean up example code that may contain real credentials
  • CI/CD: Sanitize build outputs and test results

Return Value

interface RedactionResult {
  redacted: string;                    // Text with secrets replaced
  redactionCount: number;              // Total number of redactions
  redactions: Array<{                  // Details of each redaction
    type: string;
    originalLength: number;
    line: number;
    column: number;
    replacement: string;
  }>;
  metadata: {
    originalLength: number;
    redactedLength: number;
    linesProcessed: number;
  };
}

Performance

  • Processes text in a single pass per pattern
  • Handles overlapping matches by keeping the first occurrence
  • Efficient for large text inputs (logs, code files, etc.)

License

MIT