Przejdź do głównej zawartości

Secure Logging

Frontend używa secureLogger do zapobiegania przypadkowemu logowaniu wrażliwych danych (PII).

FeatureDescription
Automatic PII MaskingEmail, phone, PESEL, NIP, passwords, tokens
Structured LoggingSpecial methods for auth, database, and API operations
Development-Only Debugdebug() only works in development mode
Semgrep EnforcementDirect console.* usage triggers warnings
import { secureLogger } from '@/utils/secureLogger';
// or use named imports:
import {
log,
info,
warn,
error,
logAuth,
logDb,
logApi,
} from '@/utils/secureLogger';
// Simple logging
secureLogger.log('Application started');
secureLogger.info('User action completed');
secureLogger.warn('Unusual activity detected');
secureLogger.error('API call failed', error);
// Debug (only in development)
secureLogger.debug('Debug information', data);
// Authentication events
secureLogger.logAuth('success', userId); // Masks user ID
secureLogger.logAuth('failure', userId);
secureLogger.logAuth('logout');
// Database operations
secureLogger.logDb('INSERT', 'users', recordId); // Masks record ID
secureLogger.logDb('UPDATE', 'patients', patientId);
// API calls
secureLogger.logApi('POST', '/api/users', 201); // Masks UUIDs in paths
secureLogger.logApi('GET', '/api/visits/uuid-here', 200);

Logger automatycznie sanityzuje:

  1. Sensitive field names: password, token, sessionId, email, phone, address, etc.
  2. Pattern matching: PESEL (11 digits), NIP (10 digits), email addresses, credit cards

Przykład:

const user = {
name: 'Jan Kowalski',
email: 'jan@example.com',
password: 'secret123',
phone: '123-456-789',
};
secureLogger.log('User data:', user);
// Logs: User data: { name: 'Jan Kowalski', email: '[REDACTED]',
// password: '[REDACTED]', phone: '[REDACTED]' }
FeatureBackend (Rust)Frontend (TS)
PII MaskingEmail, TokenEmail, Phone, PESEL, NIP
Structured LogsJSON with contextSpecial methods
Log Levelsdebug/info/warn/errordebug/info/warn/error
EnforcementManualSemgrep rules
OutputTerminal/Console.appBrowser DevTools
console.log('User:', user.email);
console.error('Failed to save:', error);
import { secureLogger } from '@/utils/secureLogger';
secureLogger.log('User:', user); // Email automatically masked
secureLogger.error('Failed to save:', error);

Direct console.* usage is blocked by Semgrep:

.semgrep.yml
- id: no-direct-console-usage
message: Use secureLogger instead
severity: WARNING

Exceptions:

  • Test files (.test.ts, .spec.ts)
  • secureLogger.ts itself
  • scripts/ directory
// Use secureLogger
import { secureLogger } from '@/utils/secureLogger';
secureLogger.info('Action completed');
// Let automatic masking work
secureLogger.log('User data:', userData); // PII masked automatically
// Use structured logging
secureLogger.logAuth('success', userId);
// Don't use console directly
console.log('User email:', email); // Semgrep warning
// Don't log raw sensitive data
secureLogger.log(password); // Still insecure if not in object
// Don't bypass sanitization
const rawEmail = user.email;
console.log(rawEmail); // Blocked by Semgrep
ItemStatus
Secure logger module (secureLogger.ts)Done
PII masking functionsDone
Structured logging methodsDone
Semgrep enforcement rulesDone
DocumentationDone

Test the logger in your browser console:

import { secureLogger } from '@/utils/secureLogger';
// Test PII masking
secureLogger.log('Email test:', { email: 'user@example.com' });
// Expected: { email: '[REDACTED]' }
// Test phone masking
secureLogger.log('Phone test:', '123-456-789');
// Expected: '[REDACTED]'