|
| 1 | +import 'package:pubnub/core.dart'; |
| 2 | + |
| 3 | +/// Custom exception for file validation errors |
| 4 | +class FileValidationException extends PubNubException { |
| 5 | + FileValidationException(String message) : super(message); |
| 6 | +} |
| 7 | + |
| 8 | +/// Validates file-related input parameters to prevent path traversal attacks |
| 9 | +/// and other security issues. |
| 10 | +class FileValidation { |
| 11 | + /// List of dangerous patterns that could lead to path traversal attacks |
| 12 | + static const List<String> _dangerousPatterns = [ |
| 13 | + '../', |
| 14 | + '..\\', |
| 15 | + './', |
| 16 | + '.\\', |
| 17 | + '~/', |
| 18 | + '~\\', |
| 19 | + ]; |
| 20 | + |
| 21 | + /// List of dangerous characters that should not be allowed in file names |
| 22 | + /// Using actual character codes instead of escape sequences |
| 23 | + static final List<int> _dangerousCharacterCodes = [ |
| 24 | + 0, // Null byte |
| 25 | + 1, // Start of heading |
| 26 | + 2, // Start of text |
| 27 | + 3, // End of text |
| 28 | + 4, // End of transmission |
| 29 | + 5, // Enquiry |
| 30 | + 6, // Acknowledge |
| 31 | + 7, // Bell |
| 32 | + 8, // Backspace |
| 33 | + 9, // Tab |
| 34 | + 10, // Line feed (newline) |
| 35 | + 11, // Vertical tab |
| 36 | + 12, // Form feed |
| 37 | + 13, // Carriage return |
| 38 | + 14, // Shift out |
| 39 | + 15, // Shift in |
| 40 | + 16, // Data link escape |
| 41 | + 17, // Device control 1 |
| 42 | + 18, // Device control 2 |
| 43 | + 19, // Device control 3 |
| 44 | + 20, // Device control 4 |
| 45 | + 21, // Negative acknowledge |
| 46 | + 22, // Synchronous idle |
| 47 | + 23, // End of transmission block |
| 48 | + 24, // Cancel |
| 49 | + 25, // End of medium |
| 50 | + 26, // Substitute |
| 51 | + 27, // Escape |
| 52 | + 28, // File separator |
| 53 | + 29, // Group separator |
| 54 | + 30, // Record separator |
| 55 | + 31, // Unit separator |
| 56 | + 127, // Delete |
| 57 | + ]; |
| 58 | + |
| 59 | + /// Validates a file name for security issues |
| 60 | + /// |
| 61 | + /// Throws [FileValidationException] if the file name contains: |
| 62 | + /// - Path traversal patterns (../, ..\, etc.) |
| 63 | + /// - Dangerous control characters |
| 64 | + /// - Is null, empty, or only whitespace |
| 65 | + /// - Contains only dots (., .., etc.) |
| 66 | + /// - Exceeds maximum length (255 characters) |
| 67 | + static void validateFileName(String? fileName) { |
| 68 | + if (fileName == null || fileName.trim().isEmpty) { |
| 69 | + throw FileValidationException('File name cannot be null or empty'); |
| 70 | + } |
| 71 | + |
| 72 | + final trimmedFileName = fileName.trim(); |
| 73 | + |
| 74 | + // Check for maximum length (common filesystem limit) |
| 75 | + if (trimmedFileName.length > 255) { |
| 76 | + throw FileValidationException('File name cannot exceed 255 characters'); |
| 77 | + } |
| 78 | + |
| 79 | + // Check for dangerous patterns |
| 80 | + for (final pattern in _dangerousPatterns) { |
| 81 | + if (trimmedFileName.contains(pattern)) { |
| 82 | + throw FileValidationException( |
| 83 | + 'File name contains dangerous path traversal pattern: "$pattern"'); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // Check for dangerous characters |
| 88 | + for (final charCode in _dangerousCharacterCodes) { |
| 89 | + if (trimmedFileName.contains(String.fromCharCode(charCode))) { |
| 90 | + throw FileValidationException( |
| 91 | + 'File name contains dangerous character: "${charCode.toRadixString(16)}"'); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // Check if filename is only dots (., .., etc.) |
| 96 | + if (RegExp(r'^\.*$').hasMatch(trimmedFileName)) { |
| 97 | + throw FileValidationException('File name cannot consist only of dots'); |
| 98 | + } |
| 99 | + |
| 100 | + // Check for reserved names on Windows (even though this is cross-platform) |
| 101 | + final reservedNames = [ |
| 102 | + 'CON', |
| 103 | + 'PRN', |
| 104 | + 'AUX', |
| 105 | + 'NUL', |
| 106 | + 'COM1', |
| 107 | + 'COM2', |
| 108 | + 'COM3', |
| 109 | + 'COM4', |
| 110 | + 'COM5', |
| 111 | + 'COM6', |
| 112 | + 'COM7', |
| 113 | + 'COM8', |
| 114 | + 'COM9', |
| 115 | + 'LPT1', |
| 116 | + 'LPT2', |
| 117 | + 'LPT3', |
| 118 | + 'LPT4', |
| 119 | + 'LPT5', |
| 120 | + 'LPT6', |
| 121 | + 'LPT7', |
| 122 | + 'LPT8', |
| 123 | + 'LPT9' |
| 124 | + ]; |
| 125 | + |
| 126 | + final fileNameUpper = trimmedFileName.toUpperCase(); |
| 127 | + final baseNameUpper = fileNameUpper.split('.').first; |
| 128 | + |
| 129 | + if (reservedNames.contains(baseNameUpper)) { |
| 130 | + throw FileValidationException( |
| 131 | + 'File name cannot be a reserved system name: "$baseNameUpper"'); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + /// Validates a file ID for security issues |
| 136 | + /// |
| 137 | + /// Throws [FileValidationException] if the file ID contains: |
| 138 | + /// - Path traversal patterns |
| 139 | + /// - Dangerous control characters |
| 140 | + /// - Is null, empty, or only whitespace |
| 141 | + static void validateFileId(String? fileId) { |
| 142 | + if (fileId == null || fileId.trim().isEmpty) { |
| 143 | + throw FileValidationException('File ID cannot be null or empty'); |
| 144 | + } |
| 145 | + |
| 146 | + final trimmedFileId = fileId.trim(); |
| 147 | + |
| 148 | + // Check for maximum length |
| 149 | + if (trimmedFileId.length > 255) { |
| 150 | + throw FileValidationException('File ID cannot exceed 255 characters'); |
| 151 | + } |
| 152 | + |
| 153 | + // Check for dangerous patterns |
| 154 | + for (final pattern in _dangerousPatterns) { |
| 155 | + if (trimmedFileId.contains(pattern)) { |
| 156 | + throw FileValidationException( |
| 157 | + 'File ID contains dangerous path traversal pattern: "$pattern"'); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + // Check for dangerous characters |
| 162 | + for (final charCode in _dangerousCharacterCodes) { |
| 163 | + if (trimmedFileId.contains(String.fromCharCode(charCode))) { |
| 164 | + throw FileValidationException( |
| 165 | + 'File ID contains dangerous character: "${charCode.toRadixString(16)}"'); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + // Check if file ID is only dots |
| 170 | + if (RegExp(r'^\.*$').hasMatch(trimmedFileId)) { |
| 171 | + throw FileValidationException('File ID cannot consist only of dots'); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + /// Validates a channel name for security issues |
| 176 | + /// |
| 177 | + /// Throws [FileValidationException] if the channel name contains: |
| 178 | + /// - Path traversal patterns |
| 179 | + /// - Dangerous control characters |
| 180 | + /// - Is null, empty, or only whitespace |
| 181 | + static void validateChannelName(String? channel) { |
| 182 | + if (channel == null || channel.trim().isEmpty) { |
| 183 | + throw FileValidationException('Channel name cannot be null or empty'); |
| 184 | + } |
| 185 | + |
| 186 | + final trimmedChannel = channel.trim(); |
| 187 | + |
| 188 | + // Check for maximum length |
| 189 | + if (trimmedChannel.length > 255) { |
| 190 | + throw FileValidationException( |
| 191 | + 'Channel name cannot exceed 255 characters'); |
| 192 | + } |
| 193 | + |
| 194 | + // Check for dangerous patterns |
| 195 | + for (final pattern in _dangerousPatterns) { |
| 196 | + if (trimmedChannel.contains(pattern)) { |
| 197 | + throw FileValidationException( |
| 198 | + 'Channel name contains dangerous path traversal pattern: "$pattern"'); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + // Check for dangerous characters |
| 203 | + for (final charCode in _dangerousCharacterCodes) { |
| 204 | + if (trimmedChannel.contains(String.fromCharCode(charCode))) { |
| 205 | + throw FileValidationException( |
| 206 | + 'Channel name contains dangerous character: "${charCode.toRadixString(16)}"'); |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | +/// Custom InvalidArgumentsException with specific message |
| 213 | +class InvalidArgumentsException extends PubNubException { |
| 214 | + static final String _defaultMessage = |
| 215 | + '''Invalid Arguments. This may be due to: |
| 216 | + - an invalid subscribe key, |
| 217 | + - missing or invalid timetoken or channelsTimetoken (values must be greater than 0), |
| 218 | + - mismatched number of channels and timetokens, |
| 219 | + - invalid characters in a channel name, |
| 220 | + - other invalid request data.'''; |
| 221 | + |
| 222 | + InvalidArgumentsException() : super(_defaultMessage); |
| 223 | +} |
0 commit comments