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`.
This commit is contained in:
Adam Daley authored and GitHub committed 2026-01-14 17:49:37 +00:00
1 parent 37deea9d38
commit a48c0ec998
3 files changed
+20 -1

No files matched your search

+9
View File
@@ -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;
@@ -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');
@@ -103,7 +103,6 @@ final class ServiceTransactionTest extends \BBTestCase
$data = [
'skip_validation' => false,
'gateway_id' => 1,
];
$this->expectException(\FOSSBilling\Exception::class);