getSafeRequestBody method

String? getSafeRequestBody()

Extract safe request body (limit size and filter sensitive data)

Implementation

String? getSafeRequestBody() {
  if (body == null) return null;

  // Limit body size for performance
  const maxBodySize = 1024; // 1KB limit
  if (body!.length > maxBodySize) {
    return '${body!.substring(0, maxBodySize)}... [truncated]';
  }

  // Check for sensitive patterns in body
  final bodyLower = body!.toLowerCase();
  const sensitivePatterns = [
    'password',
    'token',
    'secret',
    'credential',
    'apikey',
    'auth',
  ];

  bool hasSensitiveData = sensitivePatterns.any(
    (pattern) => bodyLower.contains(pattern)
  );

  if (hasSensitiveData) {
    return '[SENSITIVE_DATA_FILTERED]';
  }

  return body;
}