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);