Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Increase allowed media URL length to 2048, introduce length precheck to (#19315) (#19316) (#19317)

Marcin Wcisło5331d9d51182
diff --git a/saleor/graphql/product/tests/mutations/test_product_bulk_create.py b/saleor/graphql/product/tests/mutations/test_product_bulk_create.py
index ac7cffc9f7..d53389f6c8 100644
--- a/saleor/graphql/product/tests/mutations/test_product_bulk_create.py
+++ b/saleor/graphql/product/tests/mutations/test_product_bulk_create.py
@@ -19,6 +19,7 @@ from .....attribute.tests.model_helpers import (
)
from .....core.exceptions import UnsupportedMediaProviderException
from .....discount.utils.promotion import get_active_catalogue_promotion_rules
+from .....product import MEDIA_URL_CHAR_LIMIT
from .....product.error_codes import ProductBulkCreateErrorCode
from .....product.models import Product
from .....product.tests.utils import (
@@ -1274,6 +1275,54 @@ def test_product_bulk_create_with_media_with_media_url_invalid_provider(
assert len(errors) == 1
+def test_product_bulk_create_with_media_url_character_limit(
+ staff_api_client,
+ product_type,
+ permission_manage_products,
+):
+ # given
+ product_type_id = graphene.Node.to_global_id("ProductType", product_type.pk)
+ product_name_1 = "test name 1"
+ product_name_2 = "test name 2"
+
+ too_long_url = "https://example.com/" + "a" * MEDIA_URL_CHAR_LIMIT
+ media = {
+ "alt": "",
+ "mediaUrl": too_long_url,
+ }
+
+ products = [
+ {
+ "productType": product_type_id,
+ "name": product_name_1,
+ },
+ {
+ "productType": product_type_id,
+ "name": product_name_2,
+ "media": [media],
+ },
+ ]
+
+ # when
+ staff_api_client.user.user_permissions.add(permission_manage_products)
+ response = staff_api_client.post_graphql(
+ PRODUCT_BULK_CREATE_MUTATION,
+ {"products": products, "errorPolicy": ErrorPolicyEnum.REJECT_FAILED_ROWS.name},
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["productBulkCreate"]
+
+ # then
+ assert data["count"] == 1
+ errors = data["results"][1]["errors"]
+ assert len(errors) == 1
+ assert errors[0]["code"] == ProductBulkCreateErrorCode.INVALID.name
+ assert errors[0]["path"] == "media.0.mediaUrl"
+ assert errors[0]["message"] == (
+ f"URL field exceeds the character limit of {MEDIA_URL_CHAR_LIMIT}."
+ )
+
+
@patch(
"saleor.graphql.core.validators.file.is_supported_image_mimetype",
return_value=False,
diff --git a/saleor/graphql/product/tests/mutations/test_product_media_create.py b/saleor/graphql/product/tests/mutations/test_product_media_create.py
index 386c21fe14..919143e956 100644
--- a/saleor/graphql/product/tests/mutations/test_product_media_create.py
+++ b/saleor/graphql/product/tests/mutations/test_product_media_create.py
@@ -10,7 +10,7 @@ from requests.exceptions import InvalidSchema
from requests_hardened.ip_filter import InvalidIPAddress
from .....graphql.tests.utils import get_graphql_content, get_multipart_request_body
-from .....product import ProductMediaTypes
+from .....product import MEDIA_URL_CHAR_LIMIT, ProductMediaTypes
from .....product.error_codes import ProductErrorCode
from .....product.tests.utils import create_image, create_zip_file_with_image_ext
@@ -469,6 +469,34 @@ def test_product_media_create_mutation_alt_character_limit(
assert errors[0]["code"] == ProductErrorCode.INVALID.name
+def test_product_media_create_mutation_media_url_character_limit(
+ staff_api_client, product, permission_manage_products, media_root
+):
+ # given
+ too_long_url = "https://example.com/" + "a" * MEDIA_URL_CHAR_LIMIT
+ variables = {
+ "product": graphene.Node.to_global_id("Product", product.id),
+ "mediaUrl": too_long_url,
+ "alt": "",
+ }
+ # when
+ response = staff_api_client.post_graphql(
+ PRODUCT_MEDIA_CREATE_QUERY,
+ variables,
+ permissions=[permission_manage_products],
+ )
+ content = get_graphql_content(response)
+
+ # then
+ errors = content["data"]["productMediaCreate"]["errors"]
+ assert len(errors) == 1
+ assert errors[0]["field"] == "input"
+ assert errors[0]["code"] == ProductErrorCode.INVALID.name
+ assert errors[0]["message"] == (
+ f"URL field exceeds the character limit of {MEDIA_URL_CHAR_LIMIT}."
+ )
+
+
def test_product_media_create_when_alt_is_null(
staff_api_client, product, permission_manage_products, media_root
):
diff --git a/saleor/graphql/product/utils.py b/saleor/graphql/product/utils.py
index e86a65ca0e..9fc979bfea 100644
--- a/saleor/graphql/product/utils.py
+++ b/saleor/graphql/product/utils.py
@@ -22,6 +22,7 @@ from ...core.utils.validators import (
)
from ...order import OrderStatus
from ...order import models as order_models
+from ...product import MEDIA_URL_CHAR_LIMIT
from ...warehouse.models import Stock
from ..core.enums import ProductErrorCode
@@ -68,6 +69,12 @@ def validate_media_input(
code=error_code_enum.INVALID.value,
field="alt",
)
+ if media_url and len(media_url) > MEDIA_URL_CHAR_LIMIT:
+ return MediaValidationError(
+ message=f"URL field exceeds the character limit of {MEDIA_URL_CHAR_LIMIT}.",
+ code=error_code_enum.INVALID.value,
+ field="mediaUrl",
+ )
return None
diff --git a/saleor/product/__init__.py b/saleor/product/__init__.py
index e22574a377..f364e8240f 100644
--- a/saleor/product/__init__.py
+++ b/saleor/product/__init__.py
@@ -1,3 +1,6 @@
+MEDIA_URL_CHAR_LIMIT = 2048
+
+
class ProductMediaTypes:
IMAGE = "IMAGE"
VIDEO = "VIDEO"
diff --git a/saleor/product/migrations/0203_alter_productmedia_external_url.py b/saleor/product/migrations/0203_alter_productmedia_external_url.py
new file mode 100644
index 0000000000..b713de7588
--- /dev/null
+++ b/saleor/product/migrations/0203_alter_productmedia_external_url.py
@@ -0,0 +1,17 @@
+# Generated by Django 5.2.15 on 2026-06-15 10:29
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("product", "0202_category_product_category_tree_id_lf1e1"),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name="productmedia",
+ name="external_url",
+ field=models.CharField(blank=True, max_length=2048, null=True),
+ ),
+ ]
diff --git a/saleor/product/migrations/0205_merge_20260615_1308.py b/saleor/product/migrations/0205_merge_20260615_1308.py
new file mode 100644
index 0000000000..f2643c4fa1
--- /dev/null
+++ b/saleor/product/migrations/0205_merge_20260615_1308.py
@@ -0,0 +1,12 @@
+# Generated by Django 5.2.15 on 2026-06-15 13:08
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("product", "0203_alter_productmedia_external_url"),
+ ("product", "0204_remove_digitalcontent_from_state"),
+ ]
+
+ operations = []
diff --git a/saleor/product/models.py b/saleor/product/models.py
index e830422264..3353519aeb 100644
--- a/saleor/product/models.py
+++ b/saleor/product/models.py
@@ -38,7 +38,12 @@ from ..permission.enums import (
)
from ..seo.models import SeoModel, SeoModelTranslationWithSlug
from ..tax.models import TaxClass
-from . import ProductMediaTypes, ProductTypeKind, managers
+from . import (
+ MEDIA_URL_CHAR_LIMIT,
+ ProductMediaTypes,
+ ProductTypeKind,
+ managers,
+)
ALL_PRODUCTS_PERMISSIONS = [
# List of permissions, where each of them allows viewing all products
@@ -585,7 +590,9 @@ class ProductMedia(SortableModel, ModelWithMetadata):
choices=ProductMediaTypes.CHOICES,
default=ProductMediaTypes.IMAGE,
)
- external_url = models.CharField(max_length=256, blank=True, null=True)
+ external_url = models.CharField(
+ max_length=MEDIA_URL_CHAR_LIMIT, blank=True, null=True
+ )
oembed_data = JSONField(blank=True, default=dict)
# DEPRECATED
to_remove = models.BooleanField(default=False)