Skip to content

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

Add file type validation on image upload

fowczarek3691af75d2bf
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index 9b4851f258..8edb5e280c 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -18,7 +18,7 @@ from ...core.scalars import Decimal, WeightScalar
from ...core.types import SeoInput, Upload
from ...core.utils import clean_seo_fields
from ..types import Category, Collection, Product, ProductImage, ProductVariant
-from ..utils import attributes_to_hstore
+from ..utils import attributes_to_hstore, validate_image_file
class CategoryInput(graphene.InputObjectType):
@@ -54,6 +54,9 @@ class CategoryCreate(ModelMutation):
parent = cls.get_node_or_error(
info, parent_id, errors, field='parent', only_type=Category)
cleaned_input['parent'] = parent
+ if input.get('background_image'):
+ image_data = info.context.FILES.get(input['background_image'])
+ validate_image_file(cls, image_data, 'background_image', errors)
clean_seo_fields(cleaned_input)
return cleaned_input
@@ -146,6 +149,9 @@ class CollectionCreate(ModelMutation):
cleaned_input = super().clean_input(info, instance, input, errors)
if 'slug' not in cleaned_input and 'name' in cleaned_input:
cleaned_input['slug'] = slugify(cleaned_input['name'])
+ if input.get('background_image'):
+ image_data = info.context.FILES.get(input['background_image'])
+ validate_image_file(cls, image_data, 'background_image', errors)
clean_seo_fields(cleaned_input)
return cleaned_input
@@ -651,8 +657,7 @@ class ProductImageCreate(BaseMutation):
product = cls.get_node_or_error(
info, input['product'], errors, 'product', only_type=Product)
image_data = info.context.FILES.get(input['image'])
- if not image_data.content_type.startswith('image/'):
- cls.add_error(errors, 'image', 'Invalid file type')
+ validate_image_file(cls, image_data, 'image', errors)
image = None
if not errors:
image = product.images.create(
diff --git a/saleor/graphql/product/utils.py b/saleor/graphql/product/utils.py
index 4e0442ef56..557e49f922 100644
--- a/saleor/graphql/product/utils.py
+++ b/saleor/graphql/product/utils.py
@@ -41,3 +41,9 @@ def attributes_to_hstore(attribute_value_input, attributes_queryset):
attributes_hstore[str(attribute_id)] = str(value_id)
return attributes_hstore
+
+
+def validate_image_file(mutation_cls, file, field_name, errors):
+ """Validate if the file is an image."""
+ if not file.content_type.startswith('image/'):
+ mutation_cls.add_error(errors, field_name, 'Invalid file type')
diff --git a/tests/api/test_category.py b/tests/api/test_category.py
index 35319d81c2..8cb1209d25 100644
--- a/tests/api/test_category.py
+++ b/tests/api/test_category.py
@@ -257,7 +257,7 @@ def test_category_update_mutation_invalid_background_image(
body, permissions=[permission_manage_products])
content = get_graphql_content(response)
data = content['data']['categoryUpdate']
- assert data['errors'][0]['field'] == 'image'
+ assert data['errors'][0]['field'] == 'backgroundImage'
assert data['errors'][0]['message'] == 'Invalid file type'
diff --git a/tests/api/test_collection.py b/tests/api/test_collection.py
index 2f88778966..2b9b310821 100644
--- a/tests/api/test_collection.py
+++ b/tests/api/test_collection.py
@@ -255,7 +255,7 @@ def test_update_collection_invalid_background_image(
body, permissions=[permission_manage_products])
content = get_graphql_content(response)
data = content['data']['collectionUpdate']
- assert data['errors'][0]['field'] == 'image'
+ assert data['errors'][0]['field'] == 'backgroundImage'
assert data['errors'][0]['message'] == 'Invalid file type'