From patchwork Sat Oct 23 07:15:12 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: 4811 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) client-signature ECDSA (P-384)) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by web04.haj.ipfire.org (Postfix) with ESMTPS id 4HbstZ3G5rz3wcs for ; Sat, 23 Oct 2021 07:15:18 +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) client-signature ECDSA (P-384)) (Client CN "mail02.haj.ipfire.org", Issuer "R3" (verified OK)) by mail01.ipfire.org (Postfix) with ESMTPS id 4HbstY4333z1gl; Sat, 23 Oct 2021 07:15:17 +0000 (UTC) Received: from mail02.haj.ipfire.org (localhost [127.0.0.1]) by mail02.haj.ipfire.org (Postfix) with ESMTP id 4HbstY2sy6z2xHs; Sat, 23 Oct 2021 07:15:17 +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) client-signature ECDSA (P-384)) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by mail02.haj.ipfire.org (Postfix) with ESMTPS id 4HbstW5sDpz2xHs for ; Sat, 23 Oct 2021 07:15:15 +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)) (No client certificate requested) by mail01.ipfire.org (Postfix) with ESMTPSA id 4HbstV4PgXz1gl for ; Sat, 23 Oct 2021 07:15:14 +0000 (UTC) DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=ipfire.org; s=202003ed25519; t=1634973315; 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=iuA7zb+jSZPlgPhyFdGtW4XNpnaX0YUpBJtPVJBEU1Y=; b=xKogsq2WJ0rtbW/lRV7e0TCaSQ5RUtsV2G1gVVvMK74O2hgUZF9F4hCQVGDci6kAwaSn2x 6RzcU+lkTKMys1BA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ipfire.org; s=202003rsa; t=1634973315; 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=iuA7zb+jSZPlgPhyFdGtW4XNpnaX0YUpBJtPVJBEU1Y=; b=ENPI1uL0lcPNXFe0uVzhRxuzNiwgZoj1rAyfUCiN2sQSMddG3AEaEveAGsbcmjJHDHc+Lf 2C3cYNaOOJwO7SyUUhdxbsSzuGMsg7xtX4tBEkhODEZO/8N6d2pHoA6wuP3VreYYJh8dz8 ptHZRIT0SZenD0BwoHxppbU934x3trGsYH73xxzaHGkW3akuAhd1NJdDomPSJ+iEuw/v4y CL9l5iEC2UWD+nBVt9wC88fISIhb5bpbvEqG15/7SdLwHl3d0aJA4JrnsR3I0iTR/pag4J z+2QHUbhEXZFa/DD1sZJc2YhwzJCPQ3E44F6L4KB8DrRHhKbdUJMpVKMCPbRAg== To: "IPFire: Location" From: =?utf-8?q?Peter_M=C3=BCller?= Subject: [PATCH v3 1/2] location-importer: Introduce auxiliary function to sanitise ASNs Message-ID: <12557f79-b0e0-04f1-99ed-3571eb7c68af@ipfire.org> Date: Sat, 23 Oct 2021 09:15:12 +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" The second version of this patch does this in a more Pythonic way. Signed-off-by: Peter Müller --- src/python/location-importer.in | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/python/location-importer.in b/src/python/location-importer.in index da058d3..3ad335f 100644 --- a/src/python/location-importer.in +++ b/src/python/location-importer.in @@ -574,6 +574,21 @@ 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. + """ + + validranges = ((1,23455), (23457,64495), (131072, 4199999999)) + + for singlerange in validranges: + if singlerange[0] <= asn and singlerange[1] >= asn: + return True + + log.info("Supplied ASN %s out of publicly routable ASN ranges" % asn) + return False + 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 +844,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...