Skip to content

Commit 16247f6

Browse files
authored
ENGCOM-6962: Correctly escape custom product image attributes #26959
2 parents 9a9e9c9 + 7a7adcf commit 16247f6

File tree

6 files changed

+20
-16
lines changed

6 files changed

+20
-16
lines changed

app/code/Magento/Catalog/Block/Product/Image.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* @method string getHeight()
1515
* @method string getLabel()
1616
* @method float getRatio()
17-
* @method string getCustomAttributes()
17+
* @method array getCustomAttributes()
1818
* @method string getClass()
1919
* @since 100.0.2
2020
*/

app/code/Magento/Catalog/Block/Product/ImageFactory.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,17 @@ public function __construct(
6868
}
6969

7070
/**
71-
* Retrieve image custom attributes for HTML element
71+
* Remove class from custom attributes
7272
*
7373
* @param array $attributes
74-
* @return string
74+
* @return array
7575
*/
76-
private function getStringCustomAttributes(array $attributes): string
76+
private function filterCustomAttributes(array $attributes): array
7777
{
78-
$result = [];
79-
foreach ($attributes as $name => $value) {
80-
if ($name != 'class') {
81-
$result[] = $name . '="' . $value . '"';
82-
}
78+
if (isset($attributes['class'])) {
79+
unset($attributes['class']);
8380
}
84-
return !empty($result) ? implode(' ', $result) : '';
81+
return $attributes;
8582
}
8683

8784
/**
@@ -170,7 +167,7 @@ public function create(Product $product, string $imageId, array $attributes = nu
170167
'height' => $imageMiscParams['image_height'],
171168
'label' => $this->getLabel($product, $imageMiscParams['image_type']),
172169
'ratio' => $this->getRatio($imageMiscParams['image_width'] ?? 0, $imageMiscParams['image_height'] ?? 0),
173-
'custom_attributes' => $this->getStringCustomAttributes($attributes),
170+
'custom_attributes' => $this->filterCustomAttributes($attributes),
174171
'class' => $this->getClass($attributes),
175172
'product_id' => $product->getId()
176173
],

app/code/Magento/Catalog/Test/Unit/Block/Product/ImageFactoryTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ private function getTestDataWithoutAttributes(): array
145145
'height' => 100,
146146
'label' => 'test_image_label',
147147
'ratio' => 1,
148-
'custom_attributes' => '',
148+
'custom_attributes' => [],
149149
'product_id' => null,
150150
'class' => 'product-image-photo'
151151
],
@@ -203,7 +203,10 @@ private function getTestDataWithAttributes(): array
203203
'height' => 50,
204204
'label' => 'test_product_name',
205205
'ratio' => 0.5, // <==
206-
'custom_attributes' => 'name_1="value_1" name_2="value_2"',
206+
'custom_attributes' => [
207+
'name_1' => 'value_1',
208+
'name_2' => 'value_2',
209+
],
207210
'product_id' => null,
208211
'class' => 'my-class'
209212
],

app/code/Magento/Catalog/view/frontend/templates/product/image.phtml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
?>
1111

1212
<img class="photo image <?= $escaper->escapeHtmlAttr($block->getClass()) ?>"
13-
<?= $escaper->escapeHtml($block->getCustomAttributes()) ?>
13+
<?php foreach ($block->getCustomAttributes() as $name => $value): ?>
14+
<?= $escaper->escapeHtmlAttr($name) ?>="<?= $escaper->escapeHtmlAttr($value) ?>"
15+
<?php endforeach; ?>
1416
src="<?= $escaper->escapeUrl($block->getImageUrl()) ?>"
1517
loading="lazy"
1618
width="<?= $escaper->escapeHtmlAttr($block->getWidth()) ?>"

app/code/Magento/Catalog/view/frontend/templates/product/image_with_borders.phtml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
<span class="product-image-wrapper"
1515
style="padding-bottom: <?= ($block->getRatio() * 100) ?>%;">
1616
<img class="<?= $escaper->escapeHtmlAttr($block->getClass()) ?>"
17-
<?= $escaper->escapeHtmlAttr($block->getCustomAttributes()) ?>
17+
<?php foreach ($block->getCustomAttributes() as $name => $value): ?>
18+
<?= $escaper->escapeHtmlAttr($name) ?>="<?= $escaper->escapeHtmlAttr($value) ?>"
19+
<?php endforeach; ?>
1820
src="<?= $escaper->escapeUrl($block->getImageUrl()) ?>"
1921
loading="lazy"
2022
width="<?= $escaper->escapeHtmlAttr($block->getWidth()) ?>"

dev/tests/integration/testsuite/Magento/Swatches/Block/Product/ListProductTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ private function assertProductImage(array $images, string $area, array $expectat
166166
$this->updateProductImages($images);
167167
$productImage = $this->listingBlock->getImage($this->productRepository->get('configurable'), $area);
168168
$this->assertInstanceOf(Image::class, $productImage);
169-
$this->assertEquals($productImage->getCustomAttributes(), '');
169+
$this->assertEquals($productImage->getCustomAttributes(), []);
170170
$this->assertEquals($productImage->getClass(), 'product-image-photo');
171171
$this->assertEquals($productImage->getRatio(), 1.25);
172172
$this->assertEquals($productImage->getLabel(), $expectation['label']);

0 commit comments

Comments
 (0)