/// Security utilities for input validation and sanitization
class SecurityUtils {
/// Sanitize user-generated text content
static String sanitizeText(String input) {
if (input.isEmpty) return input;
// Remove potentially dangerous characters
String sanitized = input
.replaceAll(RegExp(r''), '') // Remove script tags
.replaceAll(RegExp(r''), '') // Remove iframe tags
.replaceAll(RegExp(r''), '') // Remove object tags
.replaceAll(RegExp(r''), '') // Remove embed tags
.replaceAll(RegExp(r']*>.*?'), '') // Remove link tags
.replaceAll(RegExp(r']*>.*?'), '') // Remove meta tags
.replaceAll(RegExp(r'javascript:'), '') // Remove javascript: protocol
.replaceAll(RegExp(r'vbscript:'), '') // Remove vbscript: protocol
.replaceAll(RegExp(r'on\w+\s*='), '') // Remove event handlers
.replaceAll(RegExp(r'eval\s*\('), '') // Remove eval calls
.replaceAll(RegExp(r'expression\s*\('), '') // Remove expression calls
.trim();
return sanitized;
}
/// Validate and sanitize URLs
static String? sanitizeUrl(String url) {
if (url.isEmpty) return null;
try {
final uri = Uri.parse(url.startsWith('http') ? url : 'https://$url');
// Only allow safe protocols
if (!['http', 'https'].contains(uri.scheme)) {
return null;
}
// Remove potentially dangerous query parameters
final sanitizedParams = {};
uri.queryParameters.forEach((key, value) {
// Remove dangerous query parameters
if (!_isDangerousQueryParam(key) && !_isDangerousQueryParam(value)) {
sanitizedParams[key] = value;
}
});
final sanitizedUri = Uri(
scheme: uri.scheme,
host: uri.host,
port: uri.port,
path: uri.path,
queryParameters: sanitizedParams,
);
return sanitizedUri.toString();
} catch (e) {
return null;
}
}
/// Check if a query parameter is dangerous
static bool _isDangerousQueryParam(String value) {
final dangerousPatterns = [
'script', 'javascript', 'vbscript', 'onload', 'onerror', 'onclick',
'eval', 'expression', 'alert', 'confirm', 'prompt',
'<', '>', '"', "'", '\\', '\n', '\r', '\t'
];
final lowerValue = value.toLowerCase();
return dangerousPatterns.any((pattern) => lowerValue.contains(pattern));
}
/// Validate and limit text length
static String limitText(String text, {int maxLength = 1000}) {
if (text.length <= maxLength) return text;
return '${text.substring(0, maxLength)}...';
}
/// Check for potential XSS patterns
static bool containsXSS(String input) {
final xssPatterns = [
RegExp(r'