Skip to content
This repository was archived by the owner on Aug 29, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[project]
name = "SafeURL"
version="1.2"
description="SafeURL is a library that aids developers in protecting against a class of vulnerabilities known as Server Side Request Forgery."
readme="README.md"
dependencies = [
"pycurl",
"netaddr"
]

[project.urls]
"Homepage" = "https://github.com/IncludeSecurity/safeurl-python"
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())


15 changes: 0 additions & 15 deletions setup.py

This file was deleted.