Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
kcenon::network::message_validator Class Reference

Message validator for network input validation. More...

#include <message_validator.h>

Collaboration diagram for kcenon::network::message_validator:
Collaboration graph

Static Public Member Functions

static validation_result validate_size (size_t size, size_t max_size=message_limits::MAX_MESSAGE_SIZE) noexcept
 Validate message size against limit.
 
static size_t safe_copy (void *dest, size_t dest_size, const void *src, size_t src_size) noexcept
 Safe buffer copy with size validation.
 
static size_t safe_strcpy (char *dest, size_t dest_size, const char *src) noexcept
 Safe string copy with null termination.
 
static validation_result validate_http_header (std::string_view header) noexcept
 Validate HTTP header.
 
static bool validate_header_count (size_t count) noexcept
 Validate HTTP header count.
 
static bool validate_websocket_frame (size_t payload_length, size_t max_size=message_limits::MAX_WEBSOCKET_FRAME) noexcept
 Validate WebSocket frame payload size.
 
static validation_result validate_url (std::string_view url) noexcept
 Validate URL length.
 
static bool contains_suspicious_pattern (std::string_view data) noexcept
 Check if data contains potential injection patterns.
 
static std::string sanitize_string (std::string_view input)
 Sanitize string by removing control characters.
 
static size_t safe_buffer_size (size_t requested_size, size_t max_size=message_limits::MAX_MESSAGE_SIZE) noexcept
 Calculate safe buffer size for operations.
 

Detailed Description

Message validator for network input validation.

Provides static methods for validating network input to prevent buffer overflow and other input-related vulnerabilities.

Thread Safety

All methods are stateless and thread-safe.

Security Considerations

  • Always validate input size before processing
  • Use safe_copy for buffer operations
  • Check for NULL bytes in strings from untrusted sources

Definition at line 121 of file message_validator.h.

Member Function Documentation

◆ contains_suspicious_pattern()

static bool kcenon::network::message_validator::contains_suspicious_pattern ( std::string_view data)
inlinestaticnodiscardnoexcept

Check if data contains potential injection patterns.

Basic check for common injection patterns. Should be used in conjunction with proper input sanitization.

Parameters
dataData to check
Returns
true if suspicious pattern detected
Examples
/home/runner/work/network_system/network_system/src/internal/utils/message_validator.h.

Definition at line 275 of file message_validator.h.

276 {
277 // Check for NULL byte injection
278 if (data.find('\0') != std::string_view::npos) {
279 return true;
280 }
281
282 // Check for HTTP response splitting
283 if (data.find("\r\n\r\n") != std::string_view::npos) {
284 return true;
285 }
286
287 return false;
288 }

◆ safe_buffer_size()

static size_t kcenon::network::message_validator::safe_buffer_size ( size_t requested_size,
size_t max_size = message_limits::MAX_MESSAGE_SIZE )
inlinestaticnodiscardnoexcept

Calculate safe buffer size for operations.

Returns a buffer size that is safe to use, capped at max_size.

Parameters
requested_sizeRequested buffer size
max_sizeMaximum allowed size
Returns
Safe buffer size
Examples
/home/runner/work/network_system/network_system/src/internal/utils/message_validator.h.

Definition at line 319 of file message_validator.h.

321 {
322 return std::min(requested_size, max_size);
323 }

◆ safe_copy()

static size_t kcenon::network::message_validator::safe_copy ( void * dest,
size_t dest_size,
const void * src,
size_t src_size )
inlinestaticnodiscardnoexcept

Safe buffer copy with size validation.

Copies data from source to destination, ensuring no buffer overflow. Copies the minimum of dest_size and src_size bytes.

Parameters
destDestination buffer
dest_sizeSize of destination buffer
srcSource buffer
src_sizeSize of source data
Returns
Number of bytes actually copied
Examples
/home/runner/work/network_system/network_system/src/internal/utils/message_validator.h.

Definition at line 152 of file message_validator.h.

156 {
157 if (!dest || !src || dest_size == 0 || src_size == 0) {
158 return 0;
159 }
160
161 size_t copy_size = std::min(dest_size, src_size);
162 std::memcpy(dest, src, copy_size);
163 return copy_size;
164 }

◆ safe_strcpy()

static size_t kcenon::network::message_validator::safe_strcpy ( char * dest,
size_t dest_size,
const char * src )
inlinestaticnodiscardnoexcept

Safe string copy with null termination.

Parameters
destDestination buffer
dest_sizeSize of destination buffer
srcSource string
Returns
Number of characters copied (excluding null terminator)
Examples
/home/runner/work/network_system/network_system/src/internal/utils/message_validator.h.

Definition at line 174 of file message_validator.h.

177 {
178 if (!dest || dest_size == 0 || !src) {
179 return 0;
180 }
181
182 size_t src_len = std::strlen(src);
183 size_t copy_len = std::min(dest_size - 1, src_len);
184
185 std::memcpy(dest, src, copy_len);
186 dest[copy_len] = '\0';
187
188 return copy_len;
189 }

◆ sanitize_string()

static std::string kcenon::network::message_validator::sanitize_string ( std::string_view input)
inlinestaticnodiscard

Sanitize string by removing control characters.

Parameters
inputString to sanitize
Returns
Sanitized string with control characters removed
Examples
/home/runner/work/network_system/network_system/src/internal/utils/message_validator.h.

Definition at line 296 of file message_validator.h.

296 {
297 std::string result;
298 result.reserve(input.size());
299
300 for (char c : input) {
301 // Keep printable ASCII and common whitespace
302 if (c >= 0x20 || c == '\t' || c == '\n' || c == '\r') {
303 result += c;
304 }
305 }
306
307 return result;
308 }

◆ validate_header_count()

static bool kcenon::network::message_validator::validate_header_count ( size_t count)
inlinestaticnodiscardnoexcept

Validate HTTP header count.

Parameters
countNumber of headers
Returns
true if within limit
Examples
/home/runner/work/network_system/network_system/src/internal/utils/message_validator.h.

Definition at line 230 of file message_validator.h.

230 {
231 return count <= message_limits::MAX_HEADER_COUNT;
232 }
static constexpr size_t MAX_HEADER_COUNT
Maximum number of HTTP headers (default: 100)

References kcenon::network::message_limits::MAX_HEADER_COUNT.

◆ validate_http_header()

static validation_result kcenon::network::message_validator::validate_http_header ( std::string_view header)
inlinestaticnodiscardnoexcept

Validate HTTP header.

Checks for:

  • Size within limits
  • No NULL bytes
  • Valid header format
Parameters
headerHeader string to validate
Returns
validation_result indicating success or failure type
Examples
/home/runner/work/network_system/network_system/src/internal/utils/message_validator.h.

Definition at line 202 of file message_validator.h.

203 {
204 // Check size
205 if (header.size() > message_limits::MAX_HEADER_SIZE) {
207 }
208
209 // Check for NULL bytes (NULL byte injection attack)
210 if (header.find('\0') != std::string_view::npos) {
212 }
213
214 // Check for invalid control characters (except \r\n\t)
215 for (char c : header) {
216 if (c < 0x20 && c != '\r' && c != '\n' && c != '\t') {
218 }
219 }
220
222 }
@ size_exceeded
Size limit exceeded.
@ null_byte_detected
NULL byte found in string.
@ invalid_character
Invalid character detected.
static constexpr size_t MAX_HEADER_SIZE
Maximum HTTP header size (default: 8KB - Apache default)

References kcenon::network::invalid_character, kcenon::network::message_limits::MAX_HEADER_SIZE, kcenon::network::null_byte_detected, kcenon::network::ok, and kcenon::network::size_exceeded.

◆ validate_size()

static validation_result kcenon::network::message_validator::validate_size ( size_t size,
size_t max_size = message_limits::MAX_MESSAGE_SIZE )
inlinestaticnodiscardnoexcept

Validate message size against limit.

Parameters
sizeSize to validate
max_sizeMaximum allowed size (default: MAX_MESSAGE_SIZE)
Returns
validation_result::ok if valid, validation_result::size_exceeded otherwise
Examples
/home/runner/work/network_system/network_system/src/internal/utils/message_validator.h.

Definition at line 130 of file message_validator.h.

132 {
133 if (size > max_size) {
135 }
137 }

References kcenon::network::ok, and kcenon::network::size_exceeded.

◆ validate_url()

static validation_result kcenon::network::message_validator::validate_url ( std::string_view url)
inlinestaticnodiscardnoexcept

Validate URL length.

Parameters
urlURL to validate
Returns
validation_result
Examples
/home/runner/work/network_system/network_system/src/internal/utils/message_validator.h.

Definition at line 253 of file message_validator.h.

254 {
255 if (url.size() > message_limits::MAX_URL_LENGTH) {
257 }
258
259 if (url.find('\0') != std::string_view::npos) {
261 }
262
264 }
static constexpr size_t MAX_URL_LENGTH
Maximum URL length (default: 2KB)

References kcenon::network::message_limits::MAX_URL_LENGTH, kcenon::network::null_byte_detected, kcenon::network::ok, and kcenon::network::size_exceeded.

◆ validate_websocket_frame()

static bool kcenon::network::message_validator::validate_websocket_frame ( size_t payload_length,
size_t max_size = message_limits::MAX_WEBSOCKET_FRAME )
inlinestaticnodiscardnoexcept

Validate WebSocket frame payload size.

Parameters
payload_lengthPayload length to validate
max_sizeMaximum allowed size (default: MAX_WEBSOCKET_FRAME)
Returns
true if within limit
Examples
/home/runner/work/network_system/network_system/src/internal/utils/message_validator.h.

Definition at line 241 of file message_validator.h.

243 {
244 return payload_length <= max_size;
245 }

The documentation for this class was generated from the following file: