|
11 | 11 |
|
12 | 12 | namespace Microsoft::ReactNative::InputValidation { |
13 | 13 |
|
14 | | -// Security exception for validation failures |
| 14 | +// Security exceptions for validation failures |
15 | 15 | class ValidationException : public std::runtime_error { |
16 | 16 | public: |
17 | 17 | explicit ValidationException(const std::string &message) : std::runtime_error(message) {} |
18 | 18 | }; |
19 | 19 |
|
| 20 | +// Specific validation exception types |
| 21 | +class InvalidSizeException : public std::logic_error { |
| 22 | + public: |
| 23 | + explicit InvalidSizeException(const std::string &message) : std::logic_error(message) {} |
| 24 | +}; |
| 25 | + |
| 26 | +class InvalidEncodingException : public std::logic_error { |
| 27 | + public: |
| 28 | + explicit InvalidEncodingException(const std::string &message) : std::logic_error(message) {} |
| 29 | +}; |
| 30 | + |
| 31 | +class InvalidPathException : public std::logic_error { |
| 32 | + public: |
| 33 | + explicit InvalidPathException(const std::string &message) : std::logic_error(message) {} |
| 34 | +}; |
| 35 | + |
| 36 | +class InvalidURLException : public std::logic_error { |
| 37 | + public: |
| 38 | + explicit InvalidURLException(const std::string &message) : std::logic_error(message) {} |
| 39 | +}; |
| 40 | + |
| 41 | +// Centralized allowlists for encodings |
| 42 | +namespace AllowedEncodings { |
| 43 | +static const std::vector<std::string> FILE_READER_ENCODINGS = { |
| 44 | + "UTF-8", "utf-8", "utf8", |
| 45 | + "UTF-16", "utf-16", "utf16", |
| 46 | + "ASCII", "ascii", |
| 47 | + "ISO-8859-1", "iso-8859-1", |
| 48 | + "" // Empty is allowed (defaults to UTF-8) |
| 49 | +}; |
| 50 | +} // namespace AllowedEncodings |
| 51 | + |
| 52 | +// Centralized URL scheme allowlists |
| 53 | +namespace AllowedSchemes { |
| 54 | +static const std::vector<std::string> HTTP_SCHEMES = {"http", "https"}; |
| 55 | +static const std::vector<std::string> WEBSOCKET_SCHEMES = {"ws", "wss"}; |
| 56 | +static const std::vector<std::string> FILE_SCHEMES = {"file"}; |
| 57 | +static const std::vector<std::string> LINKING_SCHEMES = {"http", "https", "mailto", "tel"}; |
| 58 | +static const std::vector<std::string> IMAGE_SCHEMES = {"http", "https"}; |
| 59 | +static const std::vector<std::string> DEBUG_SCHEMES = {"http", "https", "file"}; |
| 60 | +} // namespace AllowedSchemes |
| 61 | + |
20 | 62 | // Logging callback for validation failures (SDL requirement) |
21 | 63 | using ValidationLogger = std::function<void(const std::string &category, const std::string &message)>; |
22 | 64 | void SetValidationLogger(ValidationLogger logger); |
@@ -98,6 +140,7 @@ class SizeValidator { |
98 | 140 | static constexpr size_t MAX_CLOSE_REASON = 123; // WebSocket spec |
99 | 141 | static constexpr size_t MAX_URL_LENGTH = 2048; // URL max |
100 | 142 | static constexpr size_t MAX_HEADER_LENGTH = 8192; // Header max |
| 143 | + static constexpr size_t MAX_DATA_URI_SIZE = 10 * 1024 * 1024; // 10MB for data URIs |
101 | 144 | }; |
102 | 145 |
|
103 | 146 | // Encoding Validation - Protects against malformed data (SDL compliant) |
|
0 commit comments