From 47026ca506245be68d1cbc834f06721d74ac9d9b Mon Sep 17 00:00:00 2001 From: Benjamin Date: Tue, 21 Jul 2026 11:31:55 +0200 Subject: [PATCH] fix(response): avoid error if response is too long --- src/Handlers/ResponseHandler.php | 10 +++++++++- src/Models/DownloadSegment.php | 6 +++--- src/Models/Segment.php | 6 +++--- src/Services/HttpClient.php | 6 +++++- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/Handlers/ResponseHandler.php b/src/Handlers/ResponseHandler.php index bb40a972..77e94015 100644 --- a/src/Handlers/ResponseHandler.php +++ b/src/Handlers/ResponseHandler.php @@ -124,6 +124,11 @@ public function retrieveH000ReportText(DOMDocument $xml): string public function extractInitializationSegment(Response $response, Keyring $keyring): InitializationSegment { $transactionKeyEncoded = $this->retrieveH00XTransactionKey($response); + + if (null === $transactionKeyEncoded) { + throw new \RuntimeException('EBICS initialization segment is missing TransactionKey. Response may be truncated.'); + } + $transactionKey = $this->base64Service->decode($transactionKeyEncoded); $orderDataEncrypted = $this->base64Service->decode($this->retrieveH00XOrderData($response)); @@ -148,7 +153,10 @@ public function extractDownloadSegment(Response $response): DownloadSegment $transactionId = $this->retrieveH00XTransactionId($response); $transactionPhase = $this->retrieveH00XTransactionPhase($response); $transactionKeyEncoded = $this->retrieveH00XTransactionKey($response); - $transactionKey = $this->base64Service->decode($transactionKeyEncoded); + + $transactionKey = null !== $transactionKeyEncoded + ? $this->base64Service->decode($transactionKeyEncoded) + : null; $numSegments = $this->retrieveH00XNumSegments($response); $segmentNumber = $this->retrieveH00XSegmentNumber($response); $orderDataEncrypted = $this->retrieveH00XOrderData($response); diff --git a/src/Models/DownloadSegment.php b/src/Models/DownloadSegment.php index 3c187856..ae7c4d79 100644 --- a/src/Models/DownloadSegment.php +++ b/src/Models/DownloadSegment.php @@ -12,7 +12,7 @@ final class DownloadSegment extends Segment { private string $transactionId; private ?string $transactionPhase; - private string $transactionKey; + private ?string $transactionKey; private ?int $segmentNumber; private ?int $numSegments; private string $orderData; @@ -37,12 +37,12 @@ public function setTransactionPhase(?string $phase): void $this->transactionPhase = $phase; } - public function getTransactionKey(): string + public function getTransactionKey(): ?string { return $this->transactionKey; } - public function setTransactionKey(string $transactionKey): void + public function setTransactionKey(?string $transactionKey): void { $this->transactionKey = $transactionKey; } diff --git a/src/Models/Segment.php b/src/Models/Segment.php index f3a2297e..4eccbb08 100644 --- a/src/Models/Segment.php +++ b/src/Models/Segment.php @@ -12,15 +12,15 @@ */ abstract class Segment { - private string $transactionKey; + private ?string $transactionKey; private Response $response; - public function getTransactionKey(): string + public function getTransactionKey(): ?string { return $this->transactionKey; } - public function setTransactionKey(string $transactionKey): void + public function setTransactionKey(?string $transactionKey): void { $this->transactionKey = $transactionKey; } diff --git a/src/Services/HttpClient.php b/src/Services/HttpClient.php index 7636cf05..b1401470 100644 --- a/src/Services/HttpClient.php +++ b/src/Services/HttpClient.php @@ -47,7 +47,11 @@ protected function createResponse(string $contents): Response } $response = new Response(); - $response->loadXML($contents); + $loaded = @$response->loadXML($contents); + + if (false === $loaded) { + throw new RuntimeException('Failed to parse EBICS XML response. Response may be truncated or malformed.'); + } return $response; }