Skip to content

Commit

Permalink
Fallback file_get_contents with curl in case it returns empty string (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
s4ddly authored Feb 15, 2024
1 parent 8df55d3 commit 01d6382
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 4 deletions.
8 changes: 8 additions & 0 deletions .dev-tools/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
"tpay-com/coding-standards": "^1.0.1",
"vimeo/psalm": "^5.15.0"
},
"scripts": {
"sast": [
"composer normalize --diff --dry-run --indent-size=4 --indent-style=space ../composer.json",
"./vendor/bin/php-cs-fixer fix -vvv --diff --dry-run",
"./vendor/bin/phpstan",
"./vendor/bin/psalm --no-progress --shepherd"
]
},
"config": {
"allow-plugins": {
"ergebnis/composer-normalize": true,
Expand Down
1 change: 1 addition & 0 deletions .dev-tools/phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ parameters:
excludePaths:
- ../examples/
- ../vendor/
- ../tests/
1 change: 0 additions & 1 deletion .dev-tools/psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
>
<projectFiles>
<directory name='../src' />
<directory name='../tests' />
</projectFiles>

<globals>
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/sca.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
extensions: none, curl, dom, mbstring, simplexml, tokenizer, xml, xmlwriter
coverage: none

- run: composer update --no-progress --classmap-authoritative
- run: composer install --no-dev --no-progress --classmap-authoritative

- run: composer validate --strict

Expand Down
7 changes: 6 additions & 1 deletion src/Utilities/TpayException.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public function __construct($message, $code = 0)
);
$this->message = sprintf('%s : %s', $code, $message);

return $this->message;
parent::__construct($message, $code);
}

public static function curlNotAvailable()
{
return new self('Curl not available');
}
}
32 changes: 31 additions & 1 deletion src/Webhook/JWSVerifiedPaymentNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,23 @@ protected function checkJwsSignature()
}

$prefix = $this->getResourcePrefix();

if (substr($x5u, 0, strlen($prefix)) !== $prefix) {
throw new TpayException('Wrong x5u url');
}

$certificate = file_get_contents($x5u);
$trusted = file_get_contents($this->getResourcePrefix().'/x509/tpay-jws-root.pem');
$trusted = file_get_contents(sprintf('%s/x509/tpay-jws-root.pem', $this->getResourcePrefix()));

if (empty($certificate) || empty($trusted)) {
$certificate = $this->fallbackGetContents($x5u);
$trusted = $this->fallbackGetContents(sprintf('%s/x509/tpay-jws-root.pem', $this->getResourcePrefix()));
}

$x509 = new X509();
$x509->loadX509($certificate);
$x509->loadCA($trusted);

if (!$x509->validateSignature()) {
throw new TpayException('Signing certificate is not signed by Tpay CA certificate');
}
Expand All @@ -93,6 +100,7 @@ protected function checkJwsSignature()
$decodedSignature = base64_decode(strtr($signature, '-_', '+/'));
$publicKey = $x509->getPublicKey();
$publicKey = $x509->withSettings($publicKey, 'sha256', RSA::SIGNATURE_PKCS1);

if (!$publicKey->verify($headers.'.'.$payload, $decodedSignature)) {
throw new TpayException('FALSE - Invalid JWS signature');
}
Expand Down Expand Up @@ -160,4 +168,26 @@ private function getNotificationObject()

return $this->Manager->getRequestBody();
}

/**
* @param string $url
*
* @throws TpayException
*
* @return bool|string
*/
private function fallbackGetContents($url)
{
if (!function_exists('curl_init')) {
throw TpayException::curlNotAvailable();
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);

return $result;
}
}

0 comments on commit 01d6382

Please sign in to comment.