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

Commit

Permalink
Fix regex bug, and several python 3 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
includesec-kris committed Jan 17, 2023
1 parent 7927cea commit 42dd0c8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 61 deletions.
20 changes: 5 additions & 15 deletions safeurl/safeurl.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,12 @@
from numbers import Number
from socket import gethostbyname_ex

import re
import netaddr
import pycurl
import socket
import StringIO

# Python 2.7/3 urlparse
try:
# Python 2.7
from urlparse import urlparse
from urllib import quote
except:
# Python 3
from urllib.parse import urlparse
from urllib.parse import quote
import io
from urllib.parse import urlparse
from urllib.parse import quote

class ObsoletePyCurlException(Exception): pass
class InvalidOptionException(Exception): pass
Expand Down Expand Up @@ -204,10 +195,9 @@ def isInList(self, lst, type_, value):
else:
return False

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

# Execute the cURL request
response = StringIO.StringIO()
response = io.BytesIO()
self._handle.setopt(pycurl.OPENSOCKETFUNCTION, self._openSocketCallback)
self._handle.setopt(pycurl.WRITEFUNCTION, response.write)
self._handle.perform()
Expand Down
41 changes: 0 additions & 41 deletions safeurl/safeurl_examples.py

This file was deleted.

26 changes: 21 additions & 5 deletions safeurl/safeurl_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
sc = safeurl.SafeURL()
res = sc.execute("https://fin1te.net")
except:
print "Unexpected error:", sys.exc_info()
print("Unexpected error:", sys.exc_info())

# options
try:
Expand All @@ -20,13 +20,13 @@
sc.setOptions(opt)
res = sc.execute("http://www.youtube.com")
except:
print "Unexpected error:", sys.exc_info()
print("Unexpected error:", sys.exc_info())

# url
try:
url = safeurl.Url.validateUrl("http://google.com", safeurl.Options())
except:
print "Unexpected error:", sys.exc_info()
print("Unexpected error:", sys.exc_info())

# redirects
try:
Expand All @@ -38,7 +38,7 @@

res = sc.execute("http://fin1te.net")
except:
print "Unexpected error:", sys.exc_info()
print("Unexpected error:", sys.exc_info())


# forbidden host
Expand All @@ -51,4 +51,20 @@

res = sc.execute("http://localhost")
except:
print "Error:", sys.exc_info()
print("Error:", sys.exc_info())


# regex bug
try:
sc = safeurl.SafeURL()

opt = safeurl.Options()
opt.setList("whitelist", ["exam.le"], "domain")
sc.setOptions(opt)

res = sc.execute("https://example.com/")

except:
print("Error:", sys.exc_info())


0 comments on commit 42dd0c8

Please sign in to comment.