diff --git a/OWNERS.md b/OWNERS.md new file mode 100644 index 0000000..8a9c918 --- /dev/null +++ b/OWNERS.md @@ -0,0 +1,13 @@ +# Code Ownership + +## Owner +- **Name:** Elliot Taylor +- **GitHub:** [@ejntaylor](https://github.com/ejntaylor) + +## Category +Engineering + +## Responsibilities +- Security posture and patching +- Answering questions about purpose, lifecycle, and dependencies +- Deciding on continued maintenance or deprecation diff --git a/src/Extensions/Drupal/Extension.php b/src/Extensions/Drupal/Extension.php index d227b47..89cb0c9 100644 --- a/src/Extensions/Drupal/Extension.php +++ b/src/Extensions/Drupal/Extension.php @@ -17,23 +17,31 @@ class Extension implements ExtensionInterface { * * @param int $ruleId * The firewall rule ID that matched. - * @param string $bodyData + * @param string|array $request * The request body data. * @param string $blockType * The type of block performed (BLOCK, LOG, or REDIRECT). */ - public function logRequest($ruleId, $bodyData, $blockType): void { + public function logRequest($ruleId, $request, $blockType): void { try { - $request = \Drupal::request(); + // Transform raw payload. + if (is_array($request) && array_key_exists('raw', $request)) { + $request['raw'] = isset($request['raw']) && is_array($request['raw']) ? $request['raw'][0] : $request['raw']; + + // Remove raw payload if not present. + if ((is_array($request['raw']) && count($request['raw'])) == 0 || empty($request['raw'])) { + unset($request['raw']); + } + } - // Get POST data. - $postData = $request->request->all(); - if (empty($postData)) { - $rawContent = $request->getContent(); - $postData = !empty($rawContent) ? $rawContent : NULL; + // Remove files payload if not present. + if (isset($request['files']) && is_array($request['files']) && count($request['files']) == 0) { + unset($request['files']); } - else { - $postData = json_encode($postData); + + // Remove post payload if not present. + if (isset($request['post']) && is_array($request['post']) && count($request['post']) == 0) { + unset($request['post']); } // Insert into the logs table. @@ -41,13 +49,13 @@ public function logRequest($ruleId, $bodyData, $blockType): void { $connection->insert('patchstack_logs') ->fields([ 'ip' => $this->getIpAddress(), - 'request_uri' => $request->getRequestUri() ?? '', - 'user_agent' => $request->headers->get('User-Agent') ?? '', - 'method' => $request->getMethod() ?? '', - 'fid' => (int) $ruleId, - 'post_data' => $postData, + 'request_uri' => isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '', + 'user_agent' => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '', + 'method' => isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : '', + 'fid' => '55' . $ruleId, + 'post_data' => json_encode($request), 'block_type' => $blockType, - 'log_date' => \Drupal::time()->getRequestTime(), + 'log_date' => \Drupal::time()->getRequestTime() ]) ->execute(); } @@ -61,6 +69,7 @@ public function logRequest($ruleId, $bodyData, $blockType): void { /** * Determine if the current visitor can bypass the firewall. + * This is disabled in this extension. * * @param bool $isMuCall * Whether this is an early call before user session is available. @@ -69,28 +78,12 @@ public function logRequest($ruleId, $bodyData, $blockType): void { * TRUE if the visitor can bypass, FALSE otherwise. */ public function canBypass($isMuCall): bool { - // If called too early, we can't check user permissions. - if ($isMuCall) { - return FALSE; - } - - try { - $currentUser = \Drupal::currentUser(); - - // Allow administrators to bypass. - if ($currentUser->hasPermission('administer site configuration')) { - return TRUE; - } - } - catch (\Throwable $e) { - // If we can't determine user, don't bypass. - } - return FALSE; } /** * Determine if the visitor is blocked due to too many blocked requests. + * This is disabled in this extension. * * @param int $minutes * The time window in minutes. @@ -103,32 +96,7 @@ public function canBypass($isMuCall): bool { * TRUE if the visitor should be blocked, FALSE otherwise. */ public function isBlocked($minutes, $blockTime, $attempts): bool { - // Currently disabled - return false to not auto-block based on attempts. - // This can be enabled later with proper configuration. - if (empty($minutes) || empty($blockTime) || empty($attempts)) { - return FALSE; - } - - try { - // Calculate the time window. - $time = ($minutes + $blockTime) * 60; - $cutoffTime = \Drupal::time()->getRequestTime() - $time; - - // Count blocked requests from this IP in the time window. - $connection = \Drupal::database(); - $count = $connection->select('patchstack_logs', 'pl') - ->condition('block_type', 'BLOCK') - ->condition('ip', $this->getIpAddress()) - ->condition('log_date', $cutoffTime, '>=') - ->countQuery() - ->execute() - ->fetchField(); - - return (int) $count >= $attempts; - } - catch (\Throwable $e) { - return FALSE; - } + return FALSE; } /** @@ -193,6 +161,7 @@ public function getHostName(): string { /** * Determine if the request should bypass the firewall based on whitelist. + * This is disabled in this extension. * * @param array $whitelistRules * The whitelist rules. @@ -203,8 +172,6 @@ public function getHostName(): string { * TRUE if the request is whitelisted, FALSE otherwise. */ public function isWhitelisted($whitelistRules, $request): bool { - // Whitelist processing is handled by the firewall engine itself. - // This method can be used for additional Drupal-specific whitelisting. return FALSE; }