Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix security Issue #1314 #1580

Merged
merged 3 commits into from
Nov 11, 2023
Merged
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
29 changes: 25 additions & 4 deletions website/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import humanize
from collections import deque
from datetime import datetime, timezone, timedelta
from urllib.parse import urlparse
from urllib.parse import urlparse, urlunparse
from urllib.parse import urlsplit

import requests
Expand Down Expand Up @@ -84,7 +84,20 @@
from decimal import Decimal
from django.conf import settings
from comments.models import Comment
from django.core.validators import URLValidator
from django.core.exceptions import ValidationError

def is_valid_https_url(url):
validate = URLValidator(schemes=['https']) # Only allow HTTPS URLs
try:
validate(url)
return True
except ValidationError:
return False
def rebuild_safe_url(url):
parsed_url = urlparse(url)
# Rebuild the URL with scheme, netloc, and path only
return urlunparse((parsed_url.scheme, parsed_url.netloc, parsed_url.path, '', '', ''))

#@cache_page(60 * 60 * 24)
def index(request, template="index.html"):
Expand Down Expand Up @@ -560,9 +573,17 @@ def post(self, request, *args, **kwargs):
pass

else:
response = requests.get( "https://" + url ,timeout=2)
if response.status_code == 200:
print('Web site exists')
full_url = "https://" + url
if is_valid_https_url(full_url):
safe_url = rebuild_safe_url(full_url)
try:
response = requests.get(safe_url, timeout=5)
if response.status_code == 200:
print('Web site exists')
else:
raise Exception
except Exception as e:
raise Exception
else:
raise Exception
except:
Expand Down
Loading