Skip to content

Commit 8afe797

Browse files
SamJUKrhoerr
andauthored
fix: file validation bypass by targeting non file input types during customer file upload (#174)
* fix: prevent customer uploads for non file based metadata types --------- Co-authored-by: Ryan Hoerr <rhoerr@users.noreply.github.com>
1 parent 0c9c8b5 commit 8afe797

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

app/code/Magento/Customer/Model/FileUploader.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ class FileUploader
4949
*/
5050
private $scope;
5151

52+
/**
53+
* @var string[]
54+
*/
55+
private array $validInputTypes;
56+
5257
/**
5358
* @param CustomerMetadataInterface $customerMetadataService
5459
* @param AddressMetadataInterface $addressMetadataService
@@ -57,6 +62,7 @@ class FileUploader
5762
* @param AttributeMetadataInterface $attributeMetadata
5863
* @param string $entityTypeCode
5964
* @param string $scope
65+
* @param array|null $validInputTypes
6066
*/
6167
public function __construct(
6268
CustomerMetadataInterface $customerMetadataService,
@@ -65,7 +71,8 @@ public function __construct(
6571
FileProcessorFactory $fileProcessorFactory,
6672
AttributeMetadataInterface $attributeMetadata,
6773
$entityTypeCode,
68-
$scope
74+
$scope,
75+
?array $validInputTypes = ['file', 'image']
6976
) {
7077
$this->customerMetadataService = $customerMetadataService;
7178
$this->addressMetadataService = $addressMetadataService;
@@ -74,6 +81,7 @@ public function __construct(
7481
$this->attributeMetadata = $attributeMetadata;
7582
$this->entityTypeCode = $entityTypeCode;
7683
$this->scope = $scope;
84+
$this->validInputTypes = $validInputTypes;
7785
}
7886

7987
/**
@@ -83,6 +91,12 @@ public function __construct(
8391
*/
8492
public function validate()
8593
{
94+
if (!in_array($this->attributeMetadata->getFrontendInput(), $this->validInputTypes)) {
95+
return [
96+
__('"%1" is not a valid input to accept file uploads.', $this->attributeMetadata->getFrontendInput())
97+
];
98+
}
99+
86100
$formElement = $this->elementFactory->create(
87101
$this->attributeMetadata,
88102
null,

app/code/Magento/Customer/Test/Unit/Model/FileUploaderTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,39 @@ public function testValidate()
118118
->with($this->attributeMetadata, null, CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER)
119119
->willReturn($formElement);
120120

121+
$this->attributeMetadata->expects($this->once())
122+
->method('getFrontendInput')
123+
->willReturn('image');
124+
121125
$model = $this->getModel(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, 'customer');
122126
$this->assertTrue($model->validate());
123127
}
124128

129+
public function testValidateInvalidAttributeType()
130+
{
131+
$attributeType = 'select';
132+
$attributeCode = 'attribute_code';
133+
$filename = 'filename.ext1';
134+
135+
$_FILES = [
136+
'customer' => [
137+
'name' => [
138+
$attributeCode => $filename,
139+
],
140+
],
141+
];
142+
143+
$this->attributeMetadata->expects($this->exactly(2))
144+
->method('getFrontendInput')
145+
->willReturn($attributeType);
146+
147+
$model = $this->getModel(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, 'customer');
148+
$expectedErrors = [
149+
__('"%1" is not a valid input to accept file uploads.', $attributeType)
150+
];
151+
$this->assertEquals($expectedErrors, $model->validate());
152+
}
153+
125154
public function testUpload()
126155
{
127156
$attributeCode = 'attribute_code';

app/code/Magento/Customer/etc/di.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,4 +604,12 @@
604604
<argument name="indexerId" xsi:type="string">customer_grid</argument>
605605
</arguments>
606606
</virtualType>
607+
<type name="Magento\Customer\Model\FileUploader">
608+
<arguments>
609+
<argument name="validInputTypes" xsi:type="array">
610+
<item name="file" xsi:type="string">file</item>
611+
<item name="image" xsi:type="string">image</item>
612+
</argument>
613+
</arguments>
614+
</type>
607615
</config>

0 commit comments

Comments
 (0)