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 bool validate_size (size_t size, size_t max_size=message_limits::MAX_MESSAGE_SIZE) noexcept
 Validate message size against limit.
 
static void validate_size_or_throw (size_t size, size_t max_size=message_limits::MAX_MESSAGE_SIZE)
 Validate and throw if size exceeds 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 288 of file message_validator.h.

289 {
290 // Check for NULL byte injection
291 if (data.find('\0') != std::string_view::npos) {
292 return true;
293 }
294
295 // Check for HTTP response splitting
296 if (data.find("\r\n\r\n") != std::string_view::npos) {
297 return true;
298 }
299
300 return false;
301 }

◆ 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 332 of file message_validator.h.

334 {
335 return std::min(requested_size, max_size);
336 }

◆ 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 165 of file message_validator.h.

169 {
170 if (!dest || !src || dest_size == 0 || src_size == 0) {
171 return 0;
172 }
173
174 size_t copy_size = std::min(dest_size, src_size);
175 std::memcpy(dest, src, copy_size);
176 return copy_size;
177 }

◆ 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 187 of file message_validator.h.

190 {
191 if (!dest || dest_size == 0 || !src) {
192 return 0;
193 }
194
195 size_t src_len = std::strlen(src);
196 size_t copy_len = std::min(dest_size - 1, src_len);
197
198 std::memcpy(dest, src, copy_len);
199 dest[copy_len] = '\0';
200
201 return copy_len;
202 }

◆ 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 309 of file message_validator.h.

309 {
310 std::string result;
311 result.reserve(input.size());
312
313 for (char c : input) {
314 // Keep printable ASCII and common whitespace
315 if (c >= 0x20 || c == '\t' || c == '\n' || c == '\r') {
316 result += c;
317 }
318 }
319
320 return result;
321 }

◆ 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 243 of file message_validator.h.

243 {
244 return count <= message_limits::MAX_HEADER_COUNT;
245 }
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 215 of file message_validator.h.

216 {
217 // Check size
218 if (header.size() > message_limits::MAX_HEADER_SIZE) {
220 }
221
222 // Check for NULL bytes (NULL byte injection attack)
223 if (header.find('\0') != std::string_view::npos) {
225 }
226
227 // Check for invalid control characters (except \r\n\t)
228 for (char c : header) {
229 if (c < 0x20 && c != '\r' && c != '\n' && c != '\t') {
231 }
232 }
233
235 }
@ 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 bool 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
true if size is within limit, false 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 return size <= max_size;
134 }

◆ validate_size_or_throw()

static void kcenon::network::message_validator::validate_size_or_throw ( size_t size,
size_t max_size = message_limits::MAX_MESSAGE_SIZE )
inlinestatic

Validate and throw if size exceeds limit.

Parameters
sizeSize to validate
max_sizeMaximum allowed size
Exceptions
std::length_errorif size exceeds limit
Examples
/home/runner/work/network_system/network_system/src/internal/utils/message_validator.h.

Definition at line 143 of file message_validator.h.

145 {
146 if (size > max_size) {
147 throw std::length_error(
148 "Message size " + std::to_string(size) +
149 " exceeds limit " + std::to_string(max_size));
150 }
151 }

◆ 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 266 of file message_validator.h.

267 {
268 if (url.size() > message_limits::MAX_URL_LENGTH) {
270 }
271
272 if (url.find('\0') != std::string_view::npos) {
274 }
275
277 }
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 254 of file message_validator.h.

256 {
257 return payload_length <= max_size;
258 }

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