Mitigation Strategy: Workerman Connection Timeout Configuration
- Locate Configuration: Identify where Workerman is initialized and configured (usually in a
start.php
file or similar). connectTimeout
: Set theconnectTimeout
property (if available) on theWorker
instance. This controls the timeout (in seconds) for establishing a new connection to a remote server (e.g., when your Workerman application acts as a client). A typical value might be 1-5 seconds.readTimeout
: Set thereadTimeout
property (if available) on theWorker
instance or individual connection objects. This controls the timeout (in seconds) for reading data from a connection. This is crucial for preventing Slowloris-type attacks. A value of 5-30 seconds is common, depending on the expected data transfer rate.writeTimeout
: Set thewriteTimeout
property (if available) on theWorker
instance or individual connection objects. This controls the timeout (in seconds) for writing data to a connection. A value similar toreadTimeout
is often appropriate.- Event Handler Timeouts: Within event handlers (like
onMessage
), if you perform any blocking operations (e.g., database queries, external API calls), use the timeout features of those libraries. Workerman's timeouts apply to the connection itself, not to operations performed within the connection handler. - Test: Simulate slow network conditions to verify that the timeouts are working as expected.
-
Threats Mitigated:
- Slowloris Attacks (Severity: High):
readTimeout
directly mitigates Slowloris by closing connections that send data too slowly. - Hanging Connections (Severity: Medium):
connectTimeout
,readTimeout
, andwriteTimeout
prevent the application from waiting indefinitely on unresponsive clients or servers. - Resource Exhaustion (Severity: Medium): By preventing hanging connections, timeouts indirectly help prevent resource exhaustion.
- Denial of Service (DoS) (Severity: High): Mitigates DoS attacks that rely on slow or unresponsive connections.
- Slowloris Attacks (Severity: High):
-
Impact:
- Slowloris Attacks: Risk reduced significantly (from High to Low).
- Hanging Connections: Risk reduced significantly (from Medium to Low).
- Resource Exhaustion: Risk reduced moderately (from Medium to Low/Medium).
- Denial of Service (DoS): Risk reduced significantly (from High to Low/Medium).
-
Currently Implemented: (Example:
connectTimeout
is set, butreadTimeout
andwriteTimeout
are not.) You need to fill this in. -
Missing Implementation: (Example:
readTimeout
andwriteTimeout
need to be configured on theWorker
instance instart.php
. Timeouts for database queries within event handlers also need to be reviewed.) You need to fill this in.
Mitigation Strategy: Workerman Connection Limit Configuration
- Locate Configuration: Identify where Workerman worker processes are started (usually in
start.php
). count
Property: Set thecount
property on theWorker
instance. This determines the number of worker processes that Workerman will start. This is indirectly related to connection limits, as each process can handle a certain number of connections. Choose a value appropriate for your server's resources (e.g., the number of CPU cores).maxConnections
(If Available): If Workerman provides amaxConnections
property (or a similar mechanism) on theWorker
or connection objects, set this to a reasonable limit. This directly limits the total number of concurrent connections that the Workerman instance will accept.- Monitor: Use Workerman's built-in statistics (if available) to monitor the number of active connections and processes. Adjust
count
andmaxConnections
as needed based on observed load and resource usage.
-
Threats Mitigated:
- Denial of Service (DoS) (Severity: High): Prevents attackers from overwhelming the server with connection requests.
- Resource Exhaustion (Severity: High): Limits the resources (memory, file handles) consumed by connections.
-
Impact:
- Denial of Service (DoS): Risk reduced significantly (from High to Low/Medium).
- Resource Exhaustion: Risk reduced significantly (from High to Low/Medium).
-
Currently Implemented: (Example:
count
is set to 4, butmaxConnections
is not used.) You need to fill this in. -
Missing Implementation: (Example: Check if
maxConnections
is available and applicable. Implement monitoring of connection counts and adjust settings dynamically.) You need to fill this in.
Mitigation Strategy: Workerman Connection Context Usage
- Identify Connection-Specific Data: Within event handlers (
onConnect
,onMessage
,onClose
), identify any data that is specific to a particular client connection (e.g., user ID, session data, request-specific state). - Use
$connection
Object: Store this data directly on the$connection
object provided by Workerman. For example:$connection->userId = $userId;
. Do not use global variables, static variables, or class properties for this purpose. - Access Data: Access the data using the same
$connection
object:echo $connection->userId;
. - Automatic Cleanup: Workerman automatically cleans up data stored on the
$connection
object when the connection closes. This prevents memory leaks and data leakage between connections. You do not need to manuallyunset
this data. - Avoid Large Objects: Be mindful of storing very large objects directly on the connection context, as this could still contribute to memory pressure if you have many concurrent connections. For large data, consider using an external store (database, Redis) and storing only a reference (e.g., an ID) in the connection context.
-
Threats Mitigated:
- Information Disclosure (Severity: Medium/High): Prevents connection-specific data from being accidentally exposed to other clients.
- State Corruption (Severity: Medium): Ensures that data is associated with the correct connection, preventing unexpected behavior.
- Memory Leaks (Severity: Medium): Avoids memory leaks by relying on Workerman's automatic cleanup.
-
Impact:
- Information Disclosure: Risk reduced significantly (from Medium/High to Low).
- State Corruption: Risk reduced significantly (from Medium to Low).
- Memory Leaks: Risk reduced significantly (from Medium to Low).
-
Currently Implemented: (Example: Partially implemented; user ID is stored on
$connection
, but some session data is still in a global array.) You need to fill this in. -
Missing Implementation: (Example: Review all event handlers to ensure all connection-specific data is stored on the
$connection
object. Refactor any code using global or static variables for this purpose.) You need to fill this in.
Mitigation Strategy: Workerman Graceful Reload/Restart
- Signal Handling: Workerman uses signals (e.g.,
SIGUSR1
,SIGTERM
) for control. Understand how these signals work. - Graceful Reload (
SIGUSR1
): Use theSIGUSR1
signal to trigger a graceful reload of the Workerman worker processes. This allows new code to be loaded without dropping existing connections. Existing connections will continue to be handled by the old processes until they close, while new connections will be handled by the new processes. This is essential for zero-downtime deployments. Send this signal usingposix_kill(posix_getppid(), SIGUSR1)
from within a worker process, or from an external process using the process ID of the master Workerman process. - Graceful Stop (
SIGTERM
): Use theSIGTERM
signal to trigger a graceful stop of Workerman. This will stop accepting new connections and wait for existing connections to close before exiting. - Avoid
SIGKILL
: Never useSIGKILL
(orkill -9
) to stop Workerman processes, as this will immediately terminate them, dropping all connections and potentially corrupting data. - Deployment Scripts: Integrate graceful reload/restart into your deployment scripts. After deploying new code, send a
SIGUSR1
signal to the Workerman master process. - Monitor: After a reload, monitor the application to ensure that the new processes are working correctly and that the old processes have exited.
-
Threats Mitigated:
- Denial of Service (DoS) (Severity: Medium): Prevents brief service interruptions during deployments or restarts.
- Data Loss (Severity: Low/Medium): Prevents data loss that could occur if connections are abruptly terminated.
- Connection Dropping (Severity: Medium): Avoids dropping active connections during updates.
-
Impact:
- Denial of Service (DoS): Risk reduced significantly (from Medium to Low).
- Data Loss: Risk reduced significantly (from Low/Medium to Low).
- Connection Dropping: Risk reduced significantly (from Medium to Low).
-
Currently Implemented: (Example: Deployment script uses
kill -9
, which is incorrect.) You need to fill this in. -
Missing Implementation: (Example: Deployment script needs to be updated to use
posix_kill
withSIGUSR1
for graceful reloads. Monitoring needs to be added to verify successful reloads.) You need to fill this in.
Mitigation Strategy: Workerman WebSocket Origin Validation (If using WebSockets)
onWebSocketConnect
Handler: Within theonWebSocketConnect
event handler (or the equivalent handler if you're using a custom WebSocket implementation on top of Workerman), access the HTTP headers.$connection->headers
: Access the headers using$connection->headers
(or the appropriate property provided by Workerman).Origin
Header: Retrieve the value of theOrigin
header:$origin = $connection->headers['Origin'] ?? null;
. Handle the case where the header might be missing.- Whitelist: Compare the
$origin
value against a predefined whitelist of allowed origins (domains). This whitelist should be stored in a configuration file or environment variable, not hardcoded. - Reject Invalid Connections: If the origin is not in the whitelist (or if the
Origin
header is missing and you choose to reject such requests), close the connection using$connection->close();
. You might also send a specific error code or message. - Strict Comparison: Use a strict comparison (e.g.,
===
) when checking the origin against the whitelist to avoid potential bypasses.
-
Threats Mitigated:
- Cross-Site WebSocket Hijacking (CSWSH) (Severity: High): Prevents attackers from using malicious websites to establish WebSocket connections to your server on behalf of legitimate users.
-
Impact:
- Cross-Site WebSocket Hijacking (CSWSH): Risk reduced significantly (from High to Low).
-
Currently Implemented: (Example: No origin validation is currently implemented.) You need to fill this in.
-
Missing Implementation: (Example: Needs to be implemented within the
onWebSocketConnect
handler insrc/Handlers/WebSocketHandler.php
. The whitelist of allowed origins needs to be defined.) You need to fill this in.