Skip to content

Commit e328028

Browse files
alexander-b-cleandatorikGlomberg
authored
New. Common. Add "all_headers" parameter. (#49)
* New.Code.Add "all_headers" parameter * Fix. Code. Code style fixed. * Fix. All headers. Remove sensitive data from collected headers. * Fix. All headers. Returning result fixed. * Fix. All headers. Getting headers fixed. --------- Co-authored-by: datorik <datorik@gmail.com> Co-authored-by: Glomberg <bazz@bk.ru>
1 parent adea2d5 commit e328028

3 files changed

Lines changed: 70 additions & 5 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ composer.lock
44
.DS_Store
55
.DS_Store?
66
*.swp
7+
/tests/.phpunit.result.cache

lib/CleantalkAntispam.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,70 @@ private function getCleanTalkResponse()
159159
return new CleantalkResponse(@json_decode($response_raw), null);
160160
}
161161

162+
/**
163+
* Get all HTTP headers from the current request
164+
*
165+
* @return string JSON encoded headers or empty string if not available
166+
*/
167+
private function getAllHeaders()
168+
{
169+
// Try apache_request_headers() first
170+
$ct_tmp = function_exists('apache_request_headers') ? apache_request_headers() : [];
171+
172+
// Fallback for Nginx or other servers - parse from $_SERVER
173+
if ( empty($ct_tmp) ) {
174+
$ct_tmp = Helper::httpGetHeaders();
175+
}
176+
177+
// Remove sensitive headers before sending them to the external service.
178+
$sensitive_headers = array(
179+
'cookie',
180+
'set-cookie',
181+
'authorization',
182+
'proxy-authorization',
183+
'x-csrf-token',
184+
'x-xsrf-token',
185+
'x-api-key',
186+
'api-key',
187+
'x-auth-token',
188+
'x-access-token',
189+
'x-forwarded-client-cert',
190+
);
191+
$sensitive_patterns = array(
192+
'token',
193+
'secret',
194+
'signature',
195+
'api-key',
196+
'apikey',
197+
'auth',
198+
);
199+
foreach ($ct_tmp as $header_name => $_value) {
200+
$normalized_header_name = strtolower($header_name);
201+
if (in_array($normalized_header_name, $sensitive_headers, true)) {
202+
unset($ct_tmp[$header_name]);
203+
continue;
204+
}
205+
foreach ($sensitive_patterns as $pattern) {
206+
if (strpos($normalized_header_name, $pattern) !== false) {
207+
unset($ct_tmp[$header_name]);
208+
break;
209+
}
210+
}
211+
}
212+
213+
if ( empty($ct_tmp) ) {
214+
return '';
215+
}
216+
217+
$json = json_encode($ct_tmp);
218+
219+
if ( $json === false ) {
220+
return '';
221+
}
222+
223+
return $json;
224+
}
225+
162226
/**
163227
* Prepare the request data for the CleanTalk API.
164228
*
@@ -176,6 +240,7 @@ private function prepareCleanTalkRequestData()
176240
'js_on' => !empty($this->event_token) ? 1 : 0,
177241
'event_token' => $this->event_token,
178242
'agent' => 'php-cleantalk-check',
243+
'all_headers' => $this->getAllHeaders(),
179244
'sender_info' => @json_encode(
180245
array(
181246
'REFFERRER' => !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '',

lib/HTTP/Helper.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -502,11 +502,10 @@ public static function httpGetHeaders()
502502
continue;
503503
}
504504

505-
$key_parts[$part_index] = function_exists('mb_strtolower') ? mb_strtolower(
506-
$part
507-
) : strtolower(
508-
$part
509-
);
505+
$key_parts[$part_index] =
506+
function_exists('mb_strtolower')
507+
? mb_strtolower($part)
508+
: strtolower($part);
510509
$key_parts[$part_index][0] = strtoupper($key_parts[$part_index][0]);
511510
}
512511
$server_key = implode('-', $key_parts);

0 commit comments

Comments
 (0)