Unify safe URL checking to the one function

This commit is contained in:
dgtlmoon
2025-10-28 09:59:13 +01:00
parent 552e98519b
commit d48c82052a
9 changed files with 54 additions and 49 deletions
+2 -4
View File
@@ -1,10 +1,9 @@
import os
from changedetectionio.strtobool import strtobool
from flask_restful import abort, Resource
from flask import request
import validators
from functools import wraps
from . import auth, validate_openapi_request
from ..html_tools import is_safe_valid_url
def default_content_type(content_type='text/plain'):
@@ -50,14 +49,13 @@ class Import(Resource):
urls = request.get_data().decode('utf8').splitlines()
added = []
allow_simplehost = not strtobool(os.getenv('BLOCK_SIMPLEHOSTS', 'False'))
for url in urls:
url = url.strip()
if not len(url):
continue
# If hosts that only contain alphanumerics are allowed ("localhost" for example)
if not validators.url(url, simple_host=allow_simplehost):
if not is_safe_valid_url(url):
return f"Invalid or unsupported URL - {url}", 400
if dedupe and self.datastore.url_exists(url):
+3 -5
View File
@@ -1,7 +1,7 @@
import os
from changedetectionio.strtobool import strtobool
from changedetectionio.html_tools import is_safe_url
from changedetectionio.html_tools import is_safe_valid_url
from flask_expects_json import expects_json
from changedetectionio import queuedWatchMetaData
@@ -124,7 +124,7 @@ class Watch(Resource):
return validation_error, 400
# XSS etc protection
if request.json.get('url') and not is_safe_url(request.json.get('url')):
if request.json.get('url') and not is_safe_valid_url(request.json.get('url')):
return "Invalid URL", 400
watch.update(request.json)
@@ -232,9 +232,7 @@ class CreateWatch(Resource):
json_data = request.get_json()
url = json_data['url'].strip()
# If hosts that only contain alphanumerics are allowed ("localhost" for example)
allow_simplehost = not strtobool(os.getenv('BLOCK_SIMPLEHOSTS', 'False'))
if not validators.url(url, simple_host=allow_simplehost):
if not is_safe_valid_url(url):
return "Invalid or unsupported URL", 400
if json_data.get('proxy'):