Skip to content

Commit 067658d

Browse files
Raise a more descriptive error if null bytes are found in the path (#1811)
* raise a more descriptive error if null bytes are found in the path * formatting
1 parent 4dd0463 commit 067658d

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

lib/ApiRequestor.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,12 @@ private function _requestRaw($method, $url, $params, $headers, $apiMode, $usage)
516516
{
517517
list($absUrl, $rawHeaders, $params, $hasFile, $myApiKey) = $this->_prepareRequest($method, $url, $params, $headers, $apiMode);
518518

519+
// for some reason, PHP users will sometimes include null bytes in their paths, which leads to cryptic server 400s.
520+
// we'll be louder about this to help catch issues earlier.
521+
if (false !== \strpos($absUrl, "\0") || false !== \strpos($absUrl, '%00')) {
522+
throw new Exception\BadMethodCallException("URLs may not contain null bytes ('\\0'); double check any IDs you're including with the request.");
523+
}
524+
519525
$requestStartMs = Util\Util::currentTimeMillis();
520526

521527
list($rbody, $rcode, $rheaders) = self::httpClient()->request(

tests/Stripe/ApiRequestorTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,4 +726,23 @@ public function testIsDisabled()
726726
$result = $method->invoke(null, 'procopen, php_uname, exec', 'php_uname');
727727
static::assertTrue($result);
728728
}
729+
730+
public function testRaisesForNullBytesInResourceMethod()
731+
{
732+
$this->expectException(\Stripe\Exception\BadMethodCallException::class);
733+
$this->compatExpectExceptionMessageMatches('#null byte#');
734+
735+
Charge::retrieve("abc_123\0");
736+
}
737+
738+
public function testRaisesForNullBytesInRawRequest()
739+
{
740+
$this->expectException(\Stripe\Exception\BadMethodCallException::class);
741+
$this->compatExpectExceptionMessageMatches('#null byte#');
742+
743+
$client = new BaseStripeClient([
744+
'api_key' => 'sk_test_client',
745+
]);
746+
$client->rawRequest('get', "/v1/xyz\0");
747+
}
729748
}

0 commit comments

Comments
 (0)