Skip to content

Commit

Permalink
feat(file_get_contents): Remove problematic headers for AppSec request
Browse files Browse the repository at this point in the history
  • Loading branch information
julienloizelet committed Oct 18, 2024
1 parent 639afd3 commit 1d47e6c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 9 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ As far as possible, we try to adhere to [Symfony guidelines](https://symfony.com

---

## [2.3.2](https://github.com/crowdsecurity/php-common/releases/tag/v2.3.2) - 2024-10-18
[_Compare with previous release_](https://github.com/crowdsecurity/php-common/compare/v2.3.1...v2.3.2)


### Fixed

- Remove `Content-Length` header during `file_get_contents` call for AppSec request
- Remove `Host` header during `file_get_contents` call for AppSec request only

---

## [2.3.1](https://github.com/crowdsecurity/php-common/releases/tag/v2.3.1) - 2024-10-16
[_Compare with previous release_](https://github.com/crowdsecurity/php-common/compare/v2.3.0...v2.3.1)

Expand Down
23 changes: 15 additions & 8 deletions src/Client/RequestHandler/FileGetContents.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,26 @@ protected function getResponseHttpCode(array $parts): int
private function createContextConfig(Request $request): array
{
$headers = $request->getValidatedHeaders();
/**
* It's not recommended to set the Host header when using file_get_contents (with follow_location).
*
* @see https://www.php.net/manual/en/context.http.php#context.http.header
* As it was causing issues with PHP 7.2, we are removing it.
* For AppSec requests, original host is sent in the X-Crowdsec-Appsec-Host header.
*/
unset($headers['Host']);
$isAppSec = $request instanceof AppSecRequest;
$rawBody = '';
if ($isAppSec) {
/** @var AppSecRequest $request */
$rawBody = $request->getRawBody();
/**
* It's not recommended to set the Host header when using file_get_contents (with follow_location).
*
* @see https://www.php.net/manual/en/context.http.php#context.http.header
* As it was causing issues with PHP 7.2, we are removing it.
* In all cases, for AppSec requests, the originating host is sent in the X-Crowdsec-Appsec-Host header.
*/
unset($headers['Host']);
/**
* As we are sending the original request Content-Length's header,
* it differs from content-length that should be to sent to AppSec.
* We are removing it because file_get_contents does not automatically calculate this header,
* unlike cURL, and keeping it would result in a 400 error (bad request) from AppSec.
*/
unset($headers['Content-Length']);
}
$header = $this->convertHeadersToString($headers);
$method = $request->getMethod();
Expand Down
2 changes: 1 addition & 1 deletion src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class Constants
/**
* @var string The current version of this library
*/
public const VERSION = 'v2.3.1';
public const VERSION = 'v2.3.2';
/**
* @var string The version regex
*/
Expand Down
6 changes: 6 additions & 0 deletions tests/Unit/CurlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ public function testOptionsForAppSec()
'X-Crowdsec-Appsec-Method' => 'test-value',
'X-Crowdsec-Appsec-Uri' => 'test-value',
'X-Crowdsec-Appsec-Api-Key' => 'test-value',
'Host' => 'test-value.com',
'Content-Length' => '123',
];
$rawBody = 'this is raw body';
$configs = $this->tlsConfigs;
Expand All @@ -329,6 +331,8 @@ public function testOptionsForAppSec()
'X-Crowdsec-Appsec-Method:test-value',
'X-Crowdsec-Appsec-Uri:test-value',
'X-Crowdsec-Appsec-Api-Key:test-value',
'Host:test-value.com',
'Content-Length:123',
],
\CURLOPT_POST => true,
\CURLOPT_POSTFIELDS => 'this is raw body',
Expand Down Expand Up @@ -369,6 +373,8 @@ public function testOptionsForAppSec()
'X-Crowdsec-Appsec-Method:test-value',
'X-Crowdsec-Appsec-Uri:test-value',
'X-Crowdsec-Appsec-Api-Key:test-value',
'Host:test-value.com',
'Content-Length:123',
'User-Agent:' . TestConstants::USER_AGENT_SUFFIX,
],
\CURLOPT_POST => false,
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/FileGetContentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ public function testContextConfigForAppSec()
'X-Crowdsec-Appsec-Uri' => 'test-value',
'X-Crowdsec-Appsec-Api-Key' => 'test-value',
'Host' => 'test-value-should-be-removed',
'Content-Length' => 'test-value-should-be-removed',
'Custom-Header' => 'test-value-should-be-kept',
];
$rawBody = 'This is a raw body';
Expand Down

0 comments on commit 1d47e6c

Please sign in to comment.