r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
Security.hpp
Go to the documentation of this file.
1///
2/// @file Security.hpp
3/// @brief Security utilities and input validation
4/// @namespace utl
5///
6
7#pragma once
8
9#include <algorithm>
10#include <cctype>
11#include <string>
12
13namespace utl
14{
15 ///
16 /// @class InputValidator
17 /// @brief Input validation utilities for security-critical data
18 /// @namespace utl
19 ///
20 class InputValidator final
21 {
22 public:
23 InputValidator() = delete;
24 ~InputValidator() = delete;
25 InputValidator(const InputValidator &) = delete;
29
30 ///
31 /// @brief Validate player name for security and format compliance
32 /// @param name Player name to validate
33 /// @return True if valid (alphanumeric + spaces, max 32 chars), false otherwise
34 /// @security Prevents injection attacks and buffer overflow
35 ///
36 [[nodiscard]] static bool isValidPlayerName(const std::string &name)
37 {
38 // Maximum length check
39 if (name.empty() || name.length() > 32)
40 {
41 return false;
42 }
43
44 // Only alphanumeric and spaces allowed
45 return std::all_of(name.begin(), name.end(),
46 [](unsigned char c) { return std::isalnum(c) || c == ' '; });
47 }
48
49 ///
50 /// @brief Validate lobby name for security and format compliance
51 /// @param name Lobby name to validate
52 /// @return True if valid (alphanumeric + spaces + dash, max 32 chars), false otherwise
53 /// @security Prevents injection attacks and buffer overflow
54 ///
55 [[nodiscard]] static bool isValidLobbyName(const std::string &name)
56 {
57 // Maximum length check
58 if (name.empty() || name.length() > 32)
59 {
60 return false;
61 }
62
63 // Only alphanumeric, spaces, and dashes allowed
64 return std::all_of(name.begin(), name.end(),
65 [](unsigned char c) { return std::isalnum(c) || c == ' ' || c == '-'; });
66 }
67
68 ///
69 /// @brief Validate server IP address format (basic check)
70 /// @param ip IP address string to validate
71 /// @return True if plausible IPv4 format, false otherwise
72 /// @security Prevents invalid IP configurations
73 ///
74 [[nodiscard]] static bool isValidIPAddress(const std::string &ip)
75 {
76 if (ip.empty() || ip.length() > 15)
77 {
78 return false;
79 }
80
81 // Basic check: only digits and dots
82 return std::all_of(ip.begin(), ip.end(), [](char c) { return std::isdigit(c) || c == '.'; });
83 }
84
85 ///
86 /// @brief Validate port number range
87 /// @param port Port number to validate
88 /// @return True if port in valid range (1-65535), false otherwise
89 /// @security Prevents invalid port configuration
90 ///
91 [[nodiscard]] static bool isValidPort(std::uint16_t port) { return port > 0 && port <= 65535; }
92
93 ///
94 /// @brief Sanitize string by removing dangerous characters
95 /// @param input Input string to sanitize
96 /// @param maxLength Maximum output length
97 /// @return Sanitized string containing only safe characters
98 /// @security Removes potential injection vectors
99 ///
100 [[nodiscard]] static std::string sanitize(const std::string &input, size_t maxLength = 32)
101 {
102 std::string result;
103 result.reserve(std::min(input.length(), maxLength));
104
105 for (unsigned char c : input)
106 {
107 if (std::isalnum(c) || c == ' ' || c == '-' || c == '_')
108 {
109 if (result.length() < maxLength)
110 {
111 result.push_back(c);
112 }
113 else
114 {
115 break;
116 }
117 }
118 }
119
120 return result;
121 }
122 };
123
124 ///
125 /// @namespace SecurityConfig
126 /// @brief Security-related configuration constants
127 ///
129 {
130 /// @brief Maximum player name length
131 inline constexpr size_t MAX_PLAYER_NAME_LENGTH = 32;
132
133 /// @brief Maximum lobby name length
134 inline constexpr size_t MAX_LOBBY_NAME_LENGTH = 32;
135
136 /// @brief Maximum packet size (bytes)
137 inline constexpr size_t MAX_PACKET_SIZE = 512;
138
139 /// @brief Maximum payload size (bytes)
140 inline constexpr size_t MAX_PAYLOAD_SIZE = 512;
141
142 /// @brief Client timeout duration (seconds)
143 inline constexpr int CLIENT_TIMEOUT_SECONDS = 30;
144
145 /// @brief Session timeout duration (minutes)
146 inline constexpr int SESSION_TIMEOUT_MINUTES = 30;
147
148 /// @brief Maximum packets per second per client
149 inline constexpr size_t MAX_PACKETS_PER_SECOND = 100;
150
151 /// @brief Maximum concurrent connections per IP
152 inline constexpr size_t MAX_CLIENTS_PER_IP = 10;
153
154 /// @brief HMAC size for message integrity (bytes) - SHA256
155 inline constexpr size_t HMAC_SIZE = 32;
156
157 /// @brief Minimum session ID value
158 inline constexpr std::uint32_t MIN_SESSION_ID = 1;
159
160 /// @brief Maximum session ID value
161 inline constexpr std::uint32_t MAX_SESSION_ID = 0xFFFFFFFF;
162
163 /// @brief Maximum number of retry attempts
164 inline constexpr int MAX_RETRY_ATTEMPTS = 3;
165
166 /// @brief Connection attempt timeout (milliseconds)
167 inline constexpr int CONNECTION_TIMEOUT_MS = 5000;
168 } // namespace SecurityConfig
169
170} // namespace utl
Input validation utilities for security-critical data.
Definition Security.hpp:21
static bool isValidLobbyName(const std::string &name)
Validate lobby name for security and format compliance.
Definition Security.hpp:55
InputValidator & operator=(const InputValidator &)=delete
InputValidator(const InputValidator &)=delete
static bool isValidPlayerName(const std::string &name)
Validate player name for security and format compliance.
Definition Security.hpp:36
~InputValidator()=delete
static bool isValidPort(std::uint16_t port)
Validate port number range.
Definition Security.hpp:91
static std::string sanitize(const std::string &input, size_t maxLength=32)
Sanitize string by removing dangerous characters.
Definition Security.hpp:100
static bool isValidIPAddress(const std::string &ip)
Validate server IP address format (basic check)
Definition Security.hpp:74
InputValidator & operator=(InputValidator &&)=delete
InputValidator(InputValidator &&)=delete
Security-related configuration constants.
constexpr size_t MAX_PACKETS_PER_SECOND
Maximum packets per second per client.
Definition Security.hpp:149
constexpr int SESSION_TIMEOUT_MINUTES
Session timeout duration (minutes)
Definition Security.hpp:146
constexpr std::uint32_t MAX_SESSION_ID
Maximum session ID value.
Definition Security.hpp:161
constexpr int CONNECTION_TIMEOUT_MS
Connection attempt timeout (milliseconds)
Definition Security.hpp:167
constexpr size_t HMAC_SIZE
HMAC size for message integrity (bytes) - SHA256.
Definition Security.hpp:155
constexpr size_t MAX_PLAYER_NAME_LENGTH
Maximum player name length.
Definition Security.hpp:131
constexpr std::uint32_t MIN_SESSION_ID
Minimum session ID value.
Definition Security.hpp:158
constexpr size_t MAX_PAYLOAD_SIZE
Maximum payload size (bytes)
Definition Security.hpp:140
constexpr size_t MAX_LOBBY_NAME_LENGTH
Maximum lobby name length.
Definition Security.hpp:134
constexpr int MAX_RETRY_ATTEMPTS
Maximum number of retry attempts.
Definition Security.hpp:164
constexpr size_t MAX_CLIENTS_PER_IP
Maximum concurrent connections per IP.
Definition Security.hpp:152
constexpr int CLIENT_TIMEOUT_SECONDS
Client timeout duration (seconds)
Definition Security.hpp:143
constexpr size_t MAX_PACKET_SIZE
Maximum packet size (bytes)
Definition Security.hpp:137