Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use vendored CA bundle for all requests #422

Merged
merged 1 commit into from
Jan 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ $curl = new \Stripe\HttpClient\CurlClient([CURLOPT_SSLVERSION => CURL_SSLVERSION
\Stripe\ApiRequestor::setHttpClient($curl);
```

### Configuring CA Bundles

By default, the library will use its own internal bundle of known CA
certificates, but it's possible to configure your own:

```php
\Stripe\Stripe::setCABundlePath("path/to/ca/bundle");
```

## Development

Install dependencies:
Expand Down
27 changes: 2 additions & 25 deletions lib/HttpClient/CurlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,32 +180,14 @@ public function request($method, $absUrl, $headers, $params, $hasFile)
$opts[CURLOPT_TIMEOUT] = $this->timeout;
$opts[CURLOPT_HEADERFUNCTION] = $headerCallback;
$opts[CURLOPT_HTTPHEADER] = $headers;
if (!Stripe::$verifySslCerts) {
$opts[CURLOPT_CAINFO] = Stripe::getCABundlePath();
if (!Stripe::getVerifySslCerts()) {
$opts[CURLOPT_SSL_VERIFYPEER] = false;
}

curl_setopt_array($curl, $opts);
$rbody = curl_exec($curl);

if (!defined('CURLE_SSL_CACERT_BADFILE')) {
define('CURLE_SSL_CACERT_BADFILE', 77); // constant not defined in PHP
}

$errno = curl_errno($curl);
if ($errno == CURLE_SSL_CACERT ||
$errno == CURLE_SSL_PEER_CERTIFICATE ||
$errno == CURLE_SSL_CACERT_BADFILE
) {
array_push(
$headers,
'X-Stripe-Client-Info: {"ca":"using Stripe-supplied CA bundle"}'
);
$cert = self::caBundle();
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_CAINFO, $cert);
$rbody = curl_exec($curl);
}

if ($rbody === false) {
$errno = curl_errno($curl);
$message = curl_error($curl);
Expand Down Expand Up @@ -250,9 +232,4 @@ private function handleCurlError($url, $errno, $message)
$msg .= "\n\n(Network error [errno $errno]: $message)";
throw new Error\ApiConnection($msg);
}

private static function caBundle()
{
return dirname(__FILE__) . '/../../data/ca-certificates.crt';
}
}
27 changes: 27 additions & 0 deletions lib/Stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class Stripe
// @var string|null The account ID for connected accounts requests.
public static $accountId = null;

// @var string Path to the CA bundle used to verify SSL certificates
public static $caBundlePath = null;

// @var boolean Defaults to true.
public static $verifySslCerts = true;

Expand Down Expand Up @@ -116,6 +119,30 @@ public static function setApiVersion($apiVersion)
self::$apiVersion = $apiVersion;
}

/**
* @return string
*/
private static function getDefaultCABundlePath()
{
return realpath(dirname(__FILE__) . '/../data/ca-certificates.crt');
}

/**
* @return string
*/
public static function getCABundlePath()
{
return self::$caBundlePath ?: self::getDefaultCABundlePath();
}

/**
* @param string $caBundlePath
*/
public static function setCABundlePath($caBundlePath)
{
self::$caBundlePath = $caBundlePath;
}

/**
* @return boolean
*/
Expand Down
30 changes: 30 additions & 0 deletions tests/Stripe/StripeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Stripe;

class StripeTest extends TestCase
{
/**
* @before
*/
public function saveOriginalValues()
{
$this->orig = [
'caBundlePath' => Stripe::$caBundlePath,
];
}

/**
* @after
*/
public function restoreOriginalValues()
{
Stripe::$caBundlePath = $this->orig['caBundlePath'];
}

public function testCABundlePathAccessors()
{
Stripe::setCABundlePath('path/to/ca/bundle');
$this->assertEquals('path/to/ca/bundle', Stripe::getCABundlePath());
}
}