Skip to content

Commit 0e4bfa0

Browse files
Merge pull request #2 from patchstack/fix/drupal-fix-logging
Bugfix: fixes request logging in Drupal middleware -> extension call.
2 parents fd076cc + 8f58885 commit 0e4bfa0

1 file changed

Lines changed: 28 additions & 16 deletions

File tree

src/Extensions/Drupal/Extension.php

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,45 @@ class Extension implements ExtensionInterface {
1717
*
1818
* @param int $ruleId
1919
* The firewall rule ID that matched.
20-
* @param string $bodyData
20+
* @param string|array $request
2121
* The request body data.
2222
* @param string $blockType
2323
* The type of block performed (BLOCK, LOG, or REDIRECT).
2424
*/
25-
public function logRequest($ruleId, $bodyData, $blockType): void {
25+
public function logRequest($ruleId, $request, $blockType): void {
2626
try {
27-
$request = \Drupal::request();
27+
// Transform raw payload.
28+
if (is_array($request) && array_key_exists('raw', $request)) {
29+
$request['raw'] = isset($request['raw']) && is_array($request['raw']) ? $request['raw'][0] : $request['raw'];
30+
31+
// Remove raw payload if not present.
32+
if ((is_array($request['raw']) && count($request['raw'])) == 0 || empty($request['raw'])) {
33+
unset($request['raw']);
34+
}
35+
}
2836

29-
// Get POST data.
30-
$postData = $request->request->all();
31-
if (empty($postData)) {
32-
$rawContent = $request->getContent();
33-
$postData = !empty($rawContent) ? $rawContent : NULL;
37+
// Remove files payload if not present.
38+
if (isset($request['files']) && is_array($request['files']) && count($request['files']) == 0) {
39+
unset($request['files']);
3440
}
35-
else {
36-
$postData = json_encode($postData);
41+
42+
// Remove post payload if not present.
43+
if (isset($request['post']) && is_array($request['post']) && count($request['post']) == 0) {
44+
unset($request['post']);
3745
}
3846

3947
// Insert into the logs table.
4048
$connection = \Drupal::database();
4149
$connection->insert('patchstack_logs')
4250
->fields([
4351
'ip' => $this->getIpAddress(),
44-
'request_uri' => $request->getRequestUri() ?? '',
45-
'user_agent' => $request->headers->get('User-Agent') ?? '',
46-
'method' => $request->getMethod() ?? '',
47-
'fid' => (int) $ruleId,
48-
'post_data' => $postData,
52+
'request_uri' => isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '',
53+
'user_agent' => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '',
54+
'method' => isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : '',
55+
'fid' => '55' . $ruleId,
56+
'post_data' => json_encode($request),
4957
'block_type' => $blockType,
50-
'log_date' => \Drupal::time()->getRequestTime(),
58+
'log_date' => \Drupal::time()->getRequestTime()
5159
])
5260
->execute();
5361
}
@@ -105,6 +113,10 @@ public function canBypass($isMuCall): bool {
105113
public function isBlocked($minutes, $blockTime, $attempts): bool {
106114
// Currently disabled - return false to not auto-block based on attempts.
107115
// This can be enabled later with proper configuration.
116+
if (TRUE) {
117+
return FALSE;
118+
}
119+
108120
if (empty($minutes) || empty($blockTime) || empty($attempts)) {
109121
return FALSE;
110122
}

0 commit comments

Comments
 (0)