Skip to content

Commit 9250127

Browse files
authored
1 parent 96ef6db commit 9250127

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

Diff for: csirtg_indicator/utils/__init__.py

+8
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
RE_FQDN = re.compile('^((?!-))(xn--)?[a-z0-9][a-z0-9-_\.]{0,245}[a-z0-9]{0,1}\.(xn--)?([a-z0-9\-]{1,61}|[a-z0-9-]{1,30}\.[a-z]{2,})$')
2828
RE_URI_SCHEMES = re.compile('^(https?|ftp)$')
2929
RE_EMAIL = re.compile('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$')
30+
RE_ASN = re.compile('^(AS|as)[0-9]{1,5}$')
3031

3132
RE_HASH = {
3233
'uuid': re.compile('^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$'),
@@ -147,6 +148,10 @@ def _email(s):
147148
if re.match(RE_EMAIL, s):
148149
return True
149150

151+
def _asn(s):
152+
if re.match(RE_ASN, s):
153+
return True
154+
150155
if test_broken and _url_broken(indicator):
151156
return 'broken_url'
152157

@@ -168,6 +173,9 @@ def _email(s):
168173
elif _fqdn(indicator):
169174
return 'fqdn'
170175

176+
elif _asn(indicator):
177+
return 'asn'
178+
171179
try:
172180
error = 'unknown itype for "{}"'.format(indicator)
173181
except UnicodeEncodeError:

Diff for: test/test_asns.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from csirtg_indicator import Indicator
2+
from csirtg_indicator.exceptions import InvalidIndicator
3+
4+
5+
def _not(data):
6+
for d in data:
7+
try:
8+
d = Indicator(d)
9+
assert d.itype is not 'asn'
10+
except InvalidIndicator:
11+
pass
12+
13+
14+
def test_asn_nok():
15+
data = [
16+
'example.com',
17+
'http://example.com:81',
18+
'192.168.1.1',
19+
'127.0.0./1'
20+
]
21+
22+
_not(data)
23+
24+
25+
def test_asn_random():
26+
for d in [1, 10, 500, 65535]:
27+
assert Indicator(indicator='AS{}'.format(d)).itype == 'asn'
28+
print(Indicator(indicator='AS{}'.format(d)))

0 commit comments

Comments
 (0)