From patchwork Sun Oct 10 16:16:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Peter_M=C3=BCller?= X-Patchwork-Id: 4776 Return-Path: Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384 client-signature ECDSA (P-384) client-digest SHA384) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by web04.haj.ipfire.org (Postfix) with ESMTPS id 4HS6Vw4lBgz44S9 for ; Sun, 10 Oct 2021 16:16:24 +0000 (UTC) Received: from mail02.haj.ipfire.org (mail02.haj.ipfire.org [172.28.1.201]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384 client-signature ECDSA (P-384) client-digest SHA384) (Client CN "mail02.haj.ipfire.org", Issuer "R3" (verified OK)) by mail01.ipfire.org (Postfix) with ESMTPS id 4HS6Vw2wgvzj4; Sun, 10 Oct 2021 16:16:24 +0000 (UTC) Received: from mail02.haj.ipfire.org (localhost [127.0.0.1]) by mail02.haj.ipfire.org (Postfix) with ESMTP id 4HS6Vw1TPnz2xMX; Sun, 10 Oct 2021 16:16:24 +0000 (UTC) Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384 client-signature ECDSA (P-384) client-digest SHA384) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by mail02.haj.ipfire.org (Postfix) with ESMTPS id 4HS6Vv0k2Wz2xKc for ; Sun, 10 Oct 2021 16:16:23 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by mail01.ipfire.org (Postfix) with ESMTPSA id 4HS6Vs5q82zj4 for ; Sun, 10 Oct 2021 16:16:21 +0000 (UTC) DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=ipfire.org; s=202003ed25519; t=1633882582; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=K3qFe+dUuQC6uTzqhUIN/JeP3WK8fgTd3UoCSLg9lIo=; b=4yEG6dw5A3fERFBjp+M9j1HbGhxI7Sa4LJCZwmyusCC9CnEquM7bCw/vjesKB/FYNv7ySV XItrh8Nmto/W2/Cg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ipfire.org; s=202003rsa; t=1633882582; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=K3qFe+dUuQC6uTzqhUIN/JeP3WK8fgTd3UoCSLg9lIo=; b=L5RhZBt/eVhB8TN4jYQGy8LyQtcrbU0MGX32WhI2+Jtd2FQVRgXB8RTjs+wbDbEWzccoe7 QBcuvnZj0Fi/NiZtxrJNuDNrm2qj9MfGQFI7AB5+fl+62PX8WtXPkkcaGpdUtIjtgye9K2 CmpgeYjeeRknffZdRLfg55HINtCMbM55KQHXukD6Jw2IjQXLW8o9JHuWJNPnLtNGlg5te+ jYdwSIS4e4lbjYYvE5gQGR+lPnibfE2TEqDGzMzIK5x9P3l0SyneRD1xhOm2WYlOH6oeDW k/NVey3haryOHl1FwT8y9jLhG9klQDFX/AgfnAFnub9k4DLYa+hRfUef0DhqOQ== To: "IPFire: Location" From: =?utf-8?q?Peter_M=C3=BCller?= Subject: [PATCH 1/2] location-importer: Introduce auxiliary function to sanitise ASNs Message-ID: <6e7024bb-71ec-66e6-252d-283c2a22666b@ipfire.org> Date: Sun, 10 Oct 2021 18:16:19 +0200 MIME-Version: 1.0 Content-Language: en-US X-BeenThere: location@lists.ipfire.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: location-bounces@lists.ipfire.org Sender: "Location" Signed-off-by: Peter Müller --- src/python/location-importer.in | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/python/location-importer.in b/src/python/location-importer.in index da058d3..c2b3e41 100644 --- a/src/python/location-importer.in +++ b/src/python/location-importer.in @@ -574,6 +574,22 @@ class CLI(object): # be suitable for libloc consumption... return True + def _check_parsed_asn(self, asn): + """ + Assistive function to filter Autonomous System Numbers not being suitable + for adding to our database. Returns False in such cases, and True otherwise. + """ + + if not asn or not isinstance(asn, int): + return False + + if not ((1 <= asn and asn <= 23455) or (23457 <= asn and asn <= 64495) or (131072 <= asn and asn <= 4199999999)): + log.debug("Skipping invalid ASN: %s" % asn) + return False + + # ASN is fine if we made it here... + return True + def _parse_block(self, block, source_key, validcountries = None): # Get first line to find out what type of block this is line = block[0] @@ -829,8 +845,8 @@ class CLI(object): log.debug("Skipping ARIN AS names line not containing an integer for ASN") continue - if not ((1 <= asn and asn <= 23455) or (23457 <= asn and asn <= 64495) or (131072 <= asn and asn <= 4199999999)): - log.debug("Skipping ARIN AS names line not containing a valid ASN: %s" % asn) + # Filter invalid ASNs... + if not self._check_parsed_asn(asn): continue # Skip any AS name that appears to be a placeholder for a different RIR or entity...