Skip to content
Back to skills

Error Message Sanitization

ASecurity

Raw error messages leak internal information to users:

  • 4 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added September 3, 2026
ai-agentsjavascriptgojavasqlazureapisecurity

Works with

  • api

Security analysis

A100/100

Scanned September 3, 2026

npx -y skills add fabioc-aloha/Alex_Skill_Mall --skill error-message-sanitization --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Error Message Sanitization?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Error Message Sanitization
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/fabioc-aloha-error-message-sanitization/badge)](https://www.skillsdirectory.com/skills/fabioc-aloha-error-message-sanitization)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: error-message-sanitization
description: "Raw error messages leak internal information to users:"
lastReviewed: 2026-04-30
---

# Error Message Sanitization

## The Problem

Raw error messages leak internal information to users:

- File paths reveal directory structure
- Stack traces expose code organization
- Internal state shows implementation details
- SDK errors contain connection strings or endpoints

```javascript
// Bad: leaks internals
catch (err) {
  res.status(500).json({ error: err.message, stack: err.stack });
}
```

## The Solution

Sanitize at system boundaries before returning errors to users.

```javascript
function sanitizeForUser(error) {
  let msg = error.message || 'An error occurred';
  
  // Strip absolute paths
  msg = msg.replace(/[A-Z]:\\[^\s]+/gi, '[path]');
  msg = msg.replace(/\/(?:home|usr|var|etc)[^\s]+/gi, '[path]');
  
  // Strip stack traces
  msg = msg.replace(/\s+at\s+.+\(.+:\d+:\d+\)/g, '');
  
  // Strip connection strings
  msg = msg.replace(/(?:mongodb|postgresql|mysql|redis):\/\/[^\s]+/gi, '[connection]');
  
  // Strip Azure resource IDs
  msg = msg.replace(/\/subscriptions\/[a-f0-9-]+/gi, '/subscriptions/[id]');
  
  return msg;
}

// Usage
catch (err) {
  console.error('Internal error:', err); // Full error for logs
  res.status(500).json({ 
    error: sanitizeForUser(err),
    requestId: req.id // For support correlation
  });
}
```

## Verification

```javascript
const testCases = [
  'Failed at C:\\Users\\dev\\project\\src\\api.js:42',
  'Connection failed: mongodb://user:pass@host:27017/db',
  '/subscriptions/abc-123/resourceGroups/prod/...',
  'Error in /home/deploy/app/server.js'
];

testCases.forEach(msg => {
  const sanitized = sanitizeForUser({ message: msg });
  console.log(sanitized);
  // Should not contain actual paths, credentials, or resource IDs
});
```

## When to Apply

- REST API error responses
- GraphQL error messages
- WebSocket error events
- Any user-facing error display

## Tags

`security` `error-handling` `api` `privacy`

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.