From c7263adcf8f3611b418b2c9f0b80121b1604072b Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Fri, 14 May 2021 09:15:02 +0200 Subject: [PATCH 1/2] utils: add origin module (#53879) --- passerelle/utils/origin.py | 47 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 passerelle/utils/origin.py diff --git a/passerelle/utils/origin.py b/passerelle/utils/origin.py new file mode 100644 index 00000000..7127c68e --- /dev/null +++ b/passerelle/utils/origin.py @@ -0,0 +1,47 @@ +# passerelle - uniform access to multiple data sources and services +# Copyright (C) 2021 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +import urllib.parse + + +def get_url_origin(url): + '''Build origin of an URL''' + parsed = urllib.parse.urlparse(url) + if ':' in parsed.netloc: + host, port = parsed.netloc.split(':', 1) + if parsed.scheme == 'http' and port == 80: + port = None + if parsed.scheme == 'https' and port == 443: + port = None + else: + host, port = parsed.netloc, None + result = '%s://%s' % (parsed.scheme, host) + if port: + result += ':%s' % port + return result + + +def is_same_origin(url1, url2): + if 'null' in [url1, url2] or not url1 or not url2: + return False + + try: + origin1 = get_url_origin(url1) + origin2 = get_url_origin(url2) + except (ValueError, TypeError): + return False + + return origin1 == origin2 -- 2.32.0.rc0