sanitizeButtonText function

String sanitizeButtonText(
  1. String? text, {
  2. int maxLength = 500,
  3. bool? enableSecurity,
})

Sanitize text input to prevent potential issues.

Implementation

String sanitizeButtonText(
  String? text, {
  int maxLength = 500,
  bool? enableSecurity,
}) {
  if (text == null) return '';
  final shouldValidate =
      enableSecurity ?? ButtonSecurityConfig.enforceValidation;
  if (!shouldValidate) return text;

  // Trim whitespace
  var sanitized = text.trim();

  // Limit length to prevent UI overflow attacks
  if (sanitized.length > maxLength) {
    _logSecurity(
      'Button text truncated from ${sanitized.length} to $maxLength',
    );
    sanitized = '${sanitized.substring(0, maxLength - 3)}...';
  }

  return sanitized;
}