Fix/cidr error handling (#102)

* fix docker build

* catch error from CIDR which can happen with invalid sip gateway data
This commit is contained in:
Dave Horton
2023-05-31 09:11:53 -04:00
committed by GitHub
parent 8448e003f6
commit e641c590b2
2 changed files with 19 additions and 7 deletions

View File

@@ -2,6 +2,8 @@ name: Docker
on: on:
push: push:
branches:
- main
tags: tags:
- '*' - '*'
@@ -18,7 +20,7 @@ jobs:
- name: prepare tag - name: prepare tag
id: prepare_tag id: prepare_tag
run: | run: |
IMAGE_ID=$GITHUB_REPOSITORY IMAGE_ID=jambonz/sbc-inbound
# Strip git ref prefix from version # Strip git ref prefix from version
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')

View File

@@ -11,6 +11,8 @@ vc.application_sid, sg.inbound, sg.outbound, sg.is_active, sg.ipv4, sg.netmask
FROM sip_gateways sg, voip_carriers vc, accounts acc FROM sip_gateways sg, voip_carriers vc, accounts acc
WHERE acc.sip_realm = ? WHERE acc.sip_realm = ?
AND vc.account_sid = acc.account_sid AND vc.account_sid = acc.account_sid
AND vc.is_active = 1
AND sg.inbound = 1
AND sg.voip_carrier_sid = vc.voip_carrier_sid`; AND sg.voip_carrier_sid = vc.voip_carrier_sid`;
const sqlSelectAllCarriersForSPByRealm = const sqlSelectAllCarriersForSPByRealm =
@@ -20,6 +22,8 @@ FROM sip_gateways sg, voip_carriers vc, accounts acc
WHERE acc.sip_realm = ? WHERE acc.sip_realm = ?
AND vc.service_provider_sid = acc.service_provider_sid AND vc.service_provider_sid = acc.service_provider_sid
AND vc.account_sid IS NULL AND vc.account_sid IS NULL
AND vc.is_active = 1
AND sg.inbound = 1
AND sg.voip_carrier_sid = vc.voip_carrier_sid`; AND sg.voip_carrier_sid = vc.voip_carrier_sid`;
const sqlSelectAllGatewaysForSP = const sqlSelectAllGatewaysForSP =
@@ -37,6 +41,8 @@ vc.application_sid, sg.inbound, sg.outbound, sg.is_active, sg.ipv4, sg.netmask
FROM sip_gateways sg, voip_carriers vc, accounts acc FROM sip_gateways sg, voip_carriers vc, accounts acc
WHERE acc.account_sid = ? WHERE acc.account_sid = ?
AND vc.account_sid = acc.account_sid AND vc.account_sid = acc.account_sid
AND vc.is_active = 1
AND sg.inbound = 1
AND sg.voip_carrier_sid = vc.voip_carrier_sid`; AND sg.voip_carrier_sid = vc.voip_carrier_sid`;
const sqlAccountByRealm = 'SELECT * from accounts WHERE sip_realm = ?'; const sqlAccountByRealm = 'SELECT * from accounts WHERE sip_realm = ?';
@@ -68,11 +74,15 @@ AND vc.is_active = 1
AND vc.register_sip_realm = ? AND vc.register_sip_realm = ?
AND vc.register_username = ?`; AND vc.register_username = ?`;
const gatewayMatchesSourceAddress = (source_address, gw) => { const gatewayMatchesSourceAddress = (logger, source_address, gw) => {
if (32 === gw.netmask && gw.ipv4 === source_address) return true; if (32 === gw.netmask && gw.ipv4 === source_address) return true;
if (gw.netmask < 32) { if (gw.netmask < 32) {
const matcher = new CIDRMatcher([`${gw.ipv4}/${gw.netmask}`]); try {
return matcher.contains(source_address); const matcher = new CIDRMatcher([`${gw.ipv4}/${gw.netmask}`]);
return matcher.contains(source_address);
} catch (err) {
logger.info({err, gw}, 'gatewayMatchesSourceAddress: Error parsing netmask');
}
} }
return false; return false;
}; };
@@ -140,7 +150,7 @@ module.exports = (srf, logger) => {
const [gwAcc] = await pp.query(sqlSelectAllCarriersForAccountByRealm, [uri.host]); const [gwAcc] = await pp.query(sqlSelectAllCarriersForAccountByRealm, [uri.host]);
const [gwSP] = gwAcc.length ? [[]] : await pp.query(sqlSelectAllCarriersForSPByRealm, uri.host); const [gwSP] = gwAcc.length ? [[]] : await pp.query(sqlSelectAllCarriersForSPByRealm, uri.host);
const gw = gwAcc.concat(gwSP); const gw = gwAcc.concat(gwSP);
const selected = gw.find(gatewayMatchesSourceAddress.bind(null, req.source_address)); const selected = gw.find(gatewayMatchesSourceAddress.bind(null, logger, req.source_address));
if (selected) { if (selected) {
const [a] = await pp.query(sqlAccountByRealm, [uri.host]); const [a] = await pp.query(sqlAccountByRealm, [uri.host]);
if (0 === a.length) return failure; if (0 === a.length) return failure;
@@ -161,7 +171,7 @@ module.exports = (srf, logger) => {
user: uri.user user: uri.user
}, 'sip realm is not associated with an account, checking carriers'); }, 'sip realm is not associated with an account, checking carriers');
const [gw] = await pp.query(sqlSelectCarrierRequiringRegistration, [uri.host, uri.user]); const [gw] = await pp.query(sqlSelectCarrierRequiringRegistration, [uri.host, uri.user]);
const matches = gw.filter(gatewayMatchesSourceAddress.bind(null, req.source_address)); const matches = gw.filter(gatewayMatchesSourceAddress.bind(null, logger, req.source_address));
if (1 === matches.length) { if (1 === matches.length) {
// bingo // bingo
//TODO: this assumes the carrier is associate to an account, not an SP //TODO: this assumes the carrier is associate to an account, not an SP
@@ -212,7 +222,7 @@ module.exports = (srf, logger) => {
/* find all carrier entries that have an inbound gateway matching the source IP */ /* find all carrier entries that have an inbound gateway matching the source IP */
const [gw] = await pp.query(sqlSelectAllGatewaysForSP); const [gw] = await pp.query(sqlSelectAllGatewaysForSP);
let matches = gw let matches = gw
.filter(gatewayMatchesSourceAddress.bind(null, req.source_address)) .filter(gatewayMatchesSourceAddress.bind(null, logger, req.source_address))
.map((gw) => { .map((gw) => {
return { return {
voip_carrier_sid: gw.voip_carrier_sid, voip_carrier_sid: gw.voip_carrier_sid,