Skip to content

Latest commit

 

History

History
231 lines (179 loc) · 6.98 KB

File metadata and controls

231 lines (179 loc) · 6.98 KB

Input Sanitization Implementation Checklist

Task: Sanitize Donation Metadata Inputs

Description: User-provided metadata (memos, descriptions) should be sanitized to prevent injection or logging issues.


✅ Acceptance Criteria

1. Identify User-Controlled Fields

  • memo - Transaction memos (donation metadata)
  • donor - Donor identifier (donation metadata)
  • recipient - Recipient identifier (donation metadata)
  • label - Wallet label (wallet metadata)
  • ownerName - Wallet owner name (wallet metadata)

2. Sanitize Before Processing

  • Sanitization applied in donation routes before business logic
  • Sanitization applied in wallet routes before business logic
  • Centralized sanitization utility created
  • Field-specific sanitization functions implemented

3. Sanitize Before Logging

  • Logging utility updated to sanitize all data
  • Recursive sanitization for objects and arrays
  • Control characters removed from log output
  • ANSI escape codes removed from log output

4. No Unsafe Strings Stored or Logged

  • Null bytes removed from all user input
  • Control characters removed from all user input
  • Newlines removed to prevent log injection
  • ANSI escape sequences removed
  • Sanitized data stored in database
  • Sanitized data logged to console

📁 Files Created/Modified

New Files

  • src/utils/sanitizer.js - Core sanitization utility
  • tests/sanitizer.test.js - Unit tests
  • tests/sanitization-integration.test.js - Integration tests
  • docs/security/INPUT_SANITIZATION.md - Full documentation
  • docs/security/SANITIZATION_QUICK_REFERENCE.md - Quick reference
  • SANITIZATION_IMPLEMENTATION_SUMMARY.md - Implementation summary

Modified Files

  • src/utils/log.js - Added sanitization to logging
  • src/utils/memoValidator.js - Uses centralized sanitization
  • src/routes/wallet.js - Sanitizes label and ownerName
  • src/routes/donation.js - Sanitizes donor, recipient, memo

🔒 Security Threats Addressed

  • Log Injection - Newlines and control chars removed
  • Null Byte Injection - Null bytes stripped
  • ANSI Escape Code Injection - ANSI codes removed
  • XSS (Partial) - Special chars removed from identifiers

🧪 Testing

Unit Tests

  • Test basic sanitization (whitespace, null bytes)
  • Test control character removal
  • Test ANSI escape sequence removal
  • Test length truncation
  • Test character restriction modes
  • Test nested object/array sanitization
  • Test security attack scenarios
  • Test edge cases (null, undefined, non-strings)

Integration Tests

  • Test donation endpoint sanitization
  • Test wallet endpoint sanitization
  • Test log injection prevention
  • Test XSS prevention
  • Test null byte injection prevention

Test Execution

  • Run unit tests: npm test -- sanitizer.test.js
  • Run integration tests: npm test -- sanitization-integration.test.js
  • Run full test suite: npm test
  • Verify all tests pass

📝 Documentation

  • Full implementation documentation created
  • Quick reference guide created
  • Code comments added to all functions
  • Usage examples provided
  • Security considerations documented
  • Maintenance guide included

🔍 Code Quality

  • No syntax errors (verified with getDiagnostics)
  • Consistent code style
  • JSDoc comments for all functions
  • Error handling for edge cases
  • Backward compatible

🎯 Implementation Details

Sanitization Functions Implemented

  • sanitizeText() - General text sanitization
  • sanitizeMemo() - Transaction memo (28 bytes)
  • sanitizeLabel() - Wallet label (100 chars)
  • sanitizeName() - Owner name (100 chars)
  • sanitizeIdentifier() - Donor/recipient ID (strict)
  • sanitizeForLogging() - Log data (recursive)
  • sanitizeRequestBody() - Batch sanitization

Integration Points

  • POST /donations - Sanitizes memo, donor, recipient
  • POST /donations/send - Sanitizes memo
  • POST /wallets - Sanitizes label, ownerName
  • PATCH /wallets/:id - Sanitizes label, ownerName
  • All log.info() calls - Automatic sanitization
  • All log.warn() calls - Automatic sanitization
  • All log.error() calls - Automatic sanitization

✨ Features

What Gets Removed

  • Null bytes (\x00)
  • Control characters (\x01-\x1F, \x7F)
  • ANSI escape sequences (\x1B[...)
  • Leading/trailing whitespace
  • Newlines (configurable)
  • Special characters (configurable)

Configuration Options

  • maxLength - Character limit
  • allowNewlines - Keep or remove newlines
  • allowSpecialChars - Keep or remove special chars

🚀 Deployment Checklist

  • Code review completed
  • All tests passing
  • Documentation reviewed
  • Staging deployment
  • Smoke testing in staging
  • Production deployment
  • Monitor logs for issues
  • Update API documentation (if needed)

📊 Verification Steps

Manual Testing

  1. Test Memo Sanitization

    curl -X POST http://localhost:3000/donations \
      -H "Content-Type: application/json" \
      -H "X-API-Key: your-key" \
      -H "X-Idempotency-Key: test-123" \
      -d '{"amount": 100, "recipient": "GXXX...", "memo": "test\nmemo\x00"}'
    • Verify memo is sanitized in response
    • Verify memo is sanitized in logs
    • Verify memo is sanitized in database
  2. Test Wallet Label Sanitization

    curl -X POST http://localhost:3000/wallets \
      -H "Content-Type: application/json" \
      -d '{"address": "GXXX...", "label": "My\nWallet\x00"}'
    • Verify label is sanitized in response
    • Verify label is sanitized in logs
    • Verify label is sanitized in database
  3. Test Log Injection Prevention

    # Check logs for injected content
    tail -f logs/app.log
    • Verify no fake log entries created
    • Verify control characters removed
    • Verify ANSI codes removed

Automated Testing

  • Run npm test -- sanitizer.test.js
  • Run npm test -- sanitization-integration.test.js
  • Verify 100% of sanitization tests pass

📚 Resources

  • Full Documentation: docs/security/INPUT_SANITIZATION.md
  • Quick Reference: docs/security/SANITIZATION_QUICK_REFERENCE.md
  • Implementation Summary: SANITIZATION_IMPLEMENTATION_SUMMARY.md
  • Unit Tests: tests/sanitizer.test.js
  • Integration Tests: tests/sanitization-integration.test.js

✅ Final Sign-Off

  • All user-controlled fields identified
  • Sanitization implemented before processing
  • Sanitization implemented before logging
  • No unsafe strings stored or logged
  • Comprehensive tests created
  • Documentation completed
  • Code quality verified

Status: COMPLETE

All acceptance criteria have been met. The implementation is ready for testing and deployment.