Skip to content
This repository was archived by the owner on Aug 29, 2024. It is now read-only.

Commit 42dd0c8

Browse files
Fix regex bug, and several python 3 fixes
1 parent 7927cea commit 42dd0c8

File tree

3 files changed

+26
-61
lines changed

3 files changed

+26
-61
lines changed

safeurl/safeurl.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,12 @@
1212
from numbers import Number
1313
from socket import gethostbyname_ex
1414

15-
import re
1615
import netaddr
1716
import pycurl
1817
import socket
19-
import StringIO
20-
21-
# Python 2.7/3 urlparse
22-
try:
23-
# Python 2.7
24-
from urlparse import urlparse
25-
from urllib import quote
26-
except:
27-
# Python 3
28-
from urllib.parse import urlparse
29-
from urllib.parse import quote
18+
import io
19+
from urllib.parse import urlparse
20+
from urllib.parse import quote
3021

3122
class ObsoletePyCurlException(Exception): pass
3223
class InvalidOptionException(Exception): pass
@@ -204,10 +195,9 @@ def isInList(self, lst, type_, value):
204195
else:
205196
return False
206197

207-
# For domains, a regex match is needed
208198
if type_ == "domain":
209199
for domain in dst:
210-
if re.match("(?i)^%s" % domain, value) is not None:
200+
if domain.lower() == value.lower():
211201
return True
212202
return False
213203
else:
@@ -661,7 +651,7 @@ def execute(self, url):
661651
self._handle.setopt(pycurl.URL, url["cleanUrl"])
662652

663653
# Execute the cURL request
664-
response = StringIO.StringIO()
654+
response = io.BytesIO()
665655
self._handle.setopt(pycurl.OPENSOCKETFUNCTION, self._openSocketCallback)
666656
self._handle.setopt(pycurl.WRITEFUNCTION, response.write)
667657
self._handle.perform()

safeurl/safeurl_examples.py

Lines changed: 0 additions & 41 deletions
This file was deleted.

safeurl/safeurl_tests.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
sc = safeurl.SafeURL()
77
res = sc.execute("https://fin1te.net")
88
except:
9-
print "Unexpected error:", sys.exc_info()
9+
print("Unexpected error:", sys.exc_info())
1010

1111
# options
1212
try:
@@ -20,13 +20,13 @@
2020
sc.setOptions(opt)
2121
res = sc.execute("http://www.youtube.com")
2222
except:
23-
print "Unexpected error:", sys.exc_info()
23+
print("Unexpected error:", sys.exc_info())
2424

2525
# url
2626
try:
2727
url = safeurl.Url.validateUrl("http://google.com", safeurl.Options())
2828
except:
29-
print "Unexpected error:", sys.exc_info()
29+
print("Unexpected error:", sys.exc_info())
3030

3131
# redirects
3232
try:
@@ -38,7 +38,7 @@
3838

3939
res = sc.execute("http://fin1te.net")
4040
except:
41-
print "Unexpected error:", sys.exc_info()
41+
print("Unexpected error:", sys.exc_info())
4242

4343

4444
# forbidden host
@@ -51,4 +51,20 @@
5151

5252
res = sc.execute("http://localhost")
5353
except:
54-
print "Error:", sys.exc_info()
54+
print("Error:", sys.exc_info())
55+
56+
57+
# regex bug
58+
try:
59+
sc = safeurl.SafeURL()
60+
61+
opt = safeurl.Options()
62+
opt.setList("whitelist", ["exam.le"], "domain")
63+
sc.setOptions(opt)
64+
65+
res = sc.execute("https://example.com/")
66+
67+
except:
68+
print("Error:", sys.exc_info())
69+
70+

0 commit comments

Comments
 (0)