2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-28 03:26:45 +00:00

Fix for URL validation (#9539) (#9540)

* FIx for URL validation

* Further fixes

(cherry picked from commit 8d48f9cecdfbd64170f72d814c7c666461eaf7bc)

Co-authored-by: Oliver <oliver.henry.walters@gmail.com>
This commit is contained in:
github-actions[bot] 2025-04-20 08:31:42 +10:00 committed by GitHub
parent 230ddd926f
commit 86513844f2
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 behaviour for this field type."""
strict_urls = get_global_setting('INVENTREE_STRICT_URLS', cache=False)
if not strict_urls and data is not empty and '://' not in data:
# Validate as if there were a schema provided
data = 'http://' + data
if not strict_urls and data is not empty and data is not None:
data = str(data).strip()
if '://' not in data:
# Validate as if there were a schema provided
data = 'http://' + data
return super().run_validation(data=data)

View File

@ -434,17 +434,27 @@ class ValidatorTest(TestCase):
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
InvenTreeSetting.set_setting('INVENTREE_STRICT_URLS', True, None)
with self.assertRaises(ValidationError):
Part.objects.create(
name=f'Part {n + 1}',
name=f'Part {n + 2}',
description='Link without schema',
category=cat,
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):
"""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)
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
if '://' not in value:
# Validate as if it were http