comparison libervia/backend/plugins/plugin_comp_ap_gateway/regex.py @ 4071:4b842c1fb686

refactoring: renamed `sat` package to `libervia.backend`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 11:49:51 +0200
parents sat/plugins/plugin_comp_ap_gateway/regex.py@381340b9a9ee
children 0d7bb4df2343
comparison
equal deleted inserted replaced
4070:d10748475025 4071:4b842c1fb686
1 #!/usr/bin/env python3
2
3 # Libervia ActivityPub Gateway
4 # Copyright (C) 2009-2022 Jérôme Poisson (goffi@goffi.org)
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Affero General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU Affero General Public License for more details.
15
16 # You should have received a copy of the GNU Affero General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 """Various Regular Expression for AP gateway"""
20
21 import re
22
23 ## "Signature" header parsing
24
25 # those expression have been generated with abnf-to-regex
26 # (https://github.com/aas-core-works/abnf-to-regexp)
27
28 # the base RFC 7320 ABNF rules come from https://github.com/EricGT/ABNF
29
30 # here is the ABNF file used:
31 # ---
32 # BWS = OWS
33 # OWS = *( SP / HTAB )
34 # tchar = "!" / "#" / "$" / "%" / "&" / "`" / "*" / "+" / "-" / "." / "^" / "_" / "\'" / "|" / "~" / DIGIT / ALPHA
35 # token = 1*tchar
36 # sig-param = token BWS "=" BWS ( token / quoted-string )
37 # quoted-string = DQUOTE *( qdtext / quoted-pair ) DQUOTE
38 # qdtext = HTAB / SP / "!" / %x23-5B ; '#'-'['
39 # / %x5D-7E ; ']'-'~'
40 # / obs-text
41 # quoted-pair = "\" ( HTAB / SP / VCHAR / obs-text )
42 # obs-text = %x80-FF
43 # ---
44
45 ows = '[ \t]*'
46 bws = f'{ows}'
47 obs_text = '[\\x80-\\xff]'
48 qdtext = f'([\t !#-\\[\\]-~]|{obs_text})'
49 quoted_pair = f'\\\\([\t !-~]|{obs_text})'
50 quoted_string = f'"({qdtext}|{quoted_pair})*"'
51 tchar = "([!#$%&`*+\\-.^_]|\\\\'|[|~0-9a-zA-Z])"
52 token = f'({tchar})+'
53 RE_SIG_PARAM = re.compile(
54 f'(?P<key>{token}{bws})={bws}'
55 f'((?P<uq_value>{token})|(?P<quoted_value>{quoted_string}))'
56 )
57
58
59 ## Account/Mention
60
61 # FIXME: naive regex, should be approved following webfinger, but popular implementations
62 # such as Mastodon use a very restricted subset
63 RE_ACCOUNT = re.compile(r"[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-]+")
64 RE_MENTION = re.compile(rf"(?<!\w)@{RE_ACCOUNT.pattern}\b")