Skip to content

Commit 4664c38

Browse files
Use robrichards/xmlseclibs instead of selective/xmldsig for webhook validation
1 parent a541d1f commit 4664c38

3 files changed

Lines changed: 68 additions & 16 deletions

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"ext-curl": "*",
2323
"ext-json": "*",
2424
"ext-openssl": "*",
25-
"selective/xmldsig": "^3.0"
25+
"selective/xmldsig": "^3.0",
26+
"robrichards/xmlseclibs": "^3.1"
2627
},
2728
"autoload": {
2829
"psr-4": {

composer.lock

Lines changed: 44 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Validators/WebhookSignatureValidation.php

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@
99
namespace Bluem\BluemPHP\Validators;
1010

1111
use Bluem\BluemPHP\Helpers\Now;
12-
use Selective\XmlDSig\PublicKeyStore;
13-
use Selective\XmlDSig\CryptoVerifier;
14-
use Selective\XmlDSig\XmlSignatureVerifier;
15-
12+
use DOMDocument;
1613
use Exception;
14+
use RobRichards\XMLSecLibs\XMLSecurityDSig;
15+
use RobRichards\XMLSecLibs\XMLSecurityKey;
1716

1817
class WebhookSignatureValidation extends WebhookValidator
1918
{
@@ -28,26 +27,36 @@ public function __construct(
2827
* Validate webhook signature based on a key file
2928
* available in the `keys` folder.
3029
*/
30+
3131
public function validate(string $data): self
3232
{
3333
$temp_file = tmpfile();
3434
fwrite($temp_file, $data);
3535
$temp_file_path = stream_get_meta_data($temp_file)['uri'];
3636

37-
$publicKeyStore = new PublicKeyStore();
38-
3937
$public_key_file_path = dirname(__DIR__, 2) . self::KEY_FOLDER . $this->getKeyFileName();
4038

4139
try {
42-
$publicKeyStore->loadFromPem(file_get_contents($public_key_file_path));
43-
$cryptoVerifier = new CryptoVerifier($publicKeyStore);
40+
$xml = new DOMDocument();
41+
$xml->load($temp_file_path);
42+
43+
$objDSig = new XMLSecurityDSig();
44+
45+
$objDSig->locateSignature($xml);
46+
47+
$objDSig->canonicalizeSignedInfo();
48+
49+
// Validate the reference
50+
if (! $objDSig->validateReference()) {
51+
$this->addError("Reference validation failed");
52+
}
4453

45-
// Create a verifier instance and pass the crypto decoder
46-
$xmlSignatureVerifier = new XmlSignatureVerifier($cryptoVerifier);
54+
// Load the public key
55+
$key = new XMLSecurityKey(XMLSecurityKey::RSA_SHA256, ['type' => 'public']);
56+
$key->loadKey($public_key_file_path, true, true);
4757

48-
// Verify a XML file
49-
$xmlVerified = $xmlSignatureVerifier->verifyXml(file_get_contents($temp_file_path));
50-
if (! $xmlVerified) {
58+
// Verify the signature
59+
if (! $objDSig->verify($key)) {
5160
$this->addError("Invalid signature");
5261
}
5362
} catch (Exception $e) {

0 commit comments

Comments
 (0)