2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-28 11:36:44 +00:00

Fix for URL validation (#9539)

* FIx for URL validation

* Further fixes
This commit is contained in:
Oliver 2025-04-20 00:21:59 +10:00 committed by GitHub
parent 9a49c9f19c
commit 8d48f9cecd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 5 deletions

View File

@ -36,9 +36,11 @@ class InvenTreeRestURLField(RestURLField):
"""Override default validation behavior for this field type.""" """Override default validation behavior for this field type."""
strict_urls = get_global_setting('INVENTREE_STRICT_URLS', cache=False) strict_urls = get_global_setting('INVENTREE_STRICT_URLS', cache=False)
if not strict_urls and data is not empty and '://' not in data: if not strict_urls and data is not empty and data is not None:
# Validate as if there were a schema provided data = str(data).strip()
data = 'http://' + data if '://' not in data:
# Validate as if there were a schema provided
data = 'http://' + data
return super().run_validation(data=data) return super().run_validation(data=data)

View File

@ -435,17 +435,27 @@ class ValidatorTest(TestCase):
link='www.google.com', link='www.google.com',
) )
# Check that a blank URL is acceptable
Part.objects.create(
name=f'Part {n + 1}', description='Missing link', category=cat, link=''
)
# With strict URL validation # With strict URL validation
InvenTreeSetting.set_setting('INVENTREE_STRICT_URLS', True, None) InvenTreeSetting.set_setting('INVENTREE_STRICT_URLS', True, None)
with self.assertRaises(ValidationError): with self.assertRaises(ValidationError):
Part.objects.create( Part.objects.create(
name=f'Part {n + 1}', name=f'Part {n + 2}',
description='Link without schema', description='Link without schema',
category=cat, category=cat,
link='www.google.com', link='www.google.com',
) )
# Check that a blank URL is acceptable
Part.objects.create(
name=f'Part {n + 3}', description='Missing link', category=cat, link=''
)
class FormatTest(TestCase): class FormatTest(TestCase):
"""Unit tests for custom string formatting functionality.""" """Unit tests for custom string formatting functionality."""

View File

@ -65,7 +65,10 @@ class AllowedURLValidator(validators.URLValidator):
# Determine if 'strict' URL validation is required (i.e. if the URL must have a schema prefix) # Determine if 'strict' URL validation is required (i.e. if the URL must have a schema prefix)
strict_urls = get_global_setting('INVENTREE_STRICT_URLS', cache=False) strict_urls = get_global_setting('INVENTREE_STRICT_URLS', cache=False)
if not strict_urls: if value is not None:
value = str(value).strip()
if value and not strict_urls:
# Allow URLs which do not have a provided schema # Allow URLs which do not have a provided schema
if '://' not in value: if '://' not in value:
# Validate as if it were http # Validate as if it were http