From a48c0ec998bbf04d2178226bc552995f7806e2e7 Mon Sep 17 00:00:00 2001 From: Adam Daley Date: Wed, 14 Jan 2026 17:49:37 +0000 Subject: [PATCH] Validate `gateway_id` in IPN and transaction creation (#3159) * Validate `gateway_id` in IPN and transaction creation Added input validation for `gateway_id` in `ipn.php` to ensure it is a positive integer, returning a 400 error for invalid values. In ServiceTransaction, now checks if the provided `gateway_id` exists in the database and throws an exception if not, logging a warning for invalid gateway IDs. * Add logger existence check in `ServiceTransaction` Ensures that the logger is set before attempting to log a warning when an invalid `gateway_id` is encountered. Also removes an unnecessary `gateway_id` entry from test data in `ServiceTransactionTest`. --- src/ipn.php | 9 +++++++++ src/modules/Invoice/ServiceTransaction.php | 11 +++++++++++ .../modules/Invoice/ServiceTransactionTest.php | 1 - 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/ipn.php b/src/ipn.php index 61042bdd5..1f93d7ae8 100644 --- a/src/ipn.php +++ b/src/ipn.php @@ -22,6 +22,15 @@ $invoiceID = $_POST['invoice_id'] ?? $_GET['invoice_id'] ?? $_POST['bb_invoice_i $gatewayID = $_POST['gateway_id'] ?? $_GET['gateway_id'] ?? $_POST['bb_gateway_id'] ?? $_GET['bb_gateway_id'] ?? null; +if ($gatewayID !== null) { + $gatewayID = filter_var($gatewayID, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]); + if ($gatewayID === false) { + http_response_code(400); + echo json_encode(['error' => ['message' => 'Invalid gateway ID']]); + exit; + } +} + $_GET['bb_invoice_id'] = $invoiceID; $_GET['bb_gateway_id'] = $gatewayID; diff --git a/src/modules/Invoice/ServiceTransaction.php b/src/modules/Invoice/ServiceTransaction.php index da4d55091..31e9ceff1 100644 --- a/src/modules/Invoice/ServiceTransaction.php +++ b/src/modules/Invoice/ServiceTransaction.php @@ -79,6 +79,17 @@ class ServiceTransaction implements InjectionAwareInterface $this->di['events_manager']->fire(['event' => 'onBeforeAdminTransactionCreate', 'params' => $data]); $skip_validation = isset($data['skip_validation']) && (bool) $data['skip_validation']; + if (!empty($data['gateway_id'])) { + try { + $this->di['db']->getExistingModelById('PayGateway', $data['gateway_id'], 'Gateway was not found'); + } catch (\Exception $e) { + if (isset($this->di['logger'])) { + $this->di['logger']->warning('IPN with invalid gateway_id rejected: ' . $data['gateway_id']); + } + + throw new \FOSSBilling\InformationException('Invalid payment gateway'); + } + } if (!$skip_validation) { if (!isset($data['invoice_id'])) { throw new \FOSSBilling\InformationException('Transaction invoice ID is missing'); diff --git a/tests-legacy/modules/Invoice/ServiceTransactionTest.php b/tests-legacy/modules/Invoice/ServiceTransactionTest.php index 5056dbad8..ab2fe7ed6 100644 --- a/tests-legacy/modules/Invoice/ServiceTransactionTest.php +++ b/tests-legacy/modules/Invoice/ServiceTransactionTest.php @@ -103,7 +103,6 @@ final class ServiceTransactionTest extends \BBTestCase $data = [ 'skip_validation' => false, - 'gateway_id' => 1, ]; $this->expectException(\FOSSBilling\Exception::class);