fix(security): escape slider description/toast text and validate product brand and alt fields

This commit is contained in:
Jérémy Munsch 2026-11-16 02:50:39 +01:00
parent d46d5e7b23
commit fae7690161
No known key found for this signature in database
GPG key ID: 2AAECDA6B7330F91
6 changed files with 145 additions and 5 deletions

View file

@ -144,7 +144,7 @@ class StoreProductRequest extends FormRequest
{
return [
'name' => ['required', 'string', 'min:3', 'max:255'],
'brand' => ['sometimes', 'nullable', 'string', 'min:3', 'max:255'],
'brand' => ['required', 'string', new Enum(ProductBrand::class)],
'short_description' => ['required', 'string', 'min:15', 'max:100'],
'description' => ['required', 'string', 'min:15'],
'price' => ['required', 'min:0', 'numeric'],
@ -169,7 +169,7 @@ class StoreProductRequest extends FormRequest
'order_unit' => ['required', new Enum(OrderUnit::class)],
'tax_rate' => ['required', new Enum(TaxRate::class)],
'picture' => [
'picture' => [
Rule::imageFile()
->extensions(self::ALLOWED_EXTENSIONS)
->rules([
@ -183,7 +183,9 @@ class StoreProductRequest extends FormRequest
->height(static::PICTURE_HEIGHT)
),
],
'picture_large' => [
'picture_alt' => ['nullable', 'string', 'max:255'],
'picture_title' => ['nullable', 'string', 'max:255'],
'picture_large' => [
Rule::imageFile()
->extensions(self::ALLOWED_EXTENSIONS)
->rules([
@ -197,6 +199,8 @@ class StoreProductRequest extends FormRequest
->height(static::PICTURE_LARGE_HEIGHT)
),
],
'picture_large_alt' => ['nullable', 'string', 'max:255'],
'picture_large_title' => ['nullable', 'string', 'max:255'],
'associated_products' => ['present', 'array'],
'associated_products.*' => ['string', 'exists:products,id'],

View file

@ -0,0 +1,36 @@
// @vitest-environment jsdom
import {beforeEach, describe, expect, it, vi} from "vitest";
import { ToastService } from "./toast.service";
const { showMock } = vi.hoisted(() => ({showMock: vi.fn(),}));
vi.mock("bootstrap", () => ({
Toast: class {
public show = showMock;
public hide = vi.fn();
},
}));
describe("ToastService", () => {
beforeEach(() => {
document.body.innerHTML = "";
showMock.mockClear();
});
it("renders message as plain text without interpreting injected HTML", () => {
// GIVEN
const service = new ToastService();
const xssPayload = "<img src=x onerror=alert(1)>Erreur";
// WHEN
service.show(xssPayload, "danger");
// THEN
const body = document.querySelector("#fo-toast-container .toast-body");
expect(body).not.toBeNull();
expect(body?.querySelector("img")).toBeNull();
expect(body?.textContent).toBe(xssPayload);
expect(showMock).toHaveBeenCalledOnce();
});
});

View file

@ -76,7 +76,28 @@ export class ToastService {
el.dataset["bsDelay"] = String(opts.delay ?? 4000);
}
el.innerHTML = `<div class="d-flex"><div class="toast-body"><i class="${config.icon} fo-toast-icon me-2" aria-hidden="true"></i>${message}</div><button type="button" class="btn-close me-2 m-auto flex-shrink-0" data-bs-dismiss="toast" aria-label="Fermer"></button></div>`;
const wrapper = document.createElement("div");
wrapper.className = "d-flex";
const body = document.createElement("div");
body.className = "toast-body";
const icon = document.createElement("i");
icon.className = `${config.icon} fo-toast-icon me-2`;
icon.setAttribute("aria-hidden", "true");
body.appendChild(icon);
body.appendChild(document.createTextNode(message));
const closeBtn = document.createElement("button");
closeBtn.type = "button";
closeBtn.className = "btn-close me-2 m-auto flex-shrink-0";
closeBtn.dataset["bsDismiss"] = "toast";
closeBtn.setAttribute("aria-label", "Fermer");
wrapper.appendChild(body);
wrapper.appendChild(closeBtn);
el.appendChild(wrapper);
return el;
}

View file

@ -42,7 +42,7 @@
</h3>
<div class="card-text w-100 text-muted small my-2 text-center">
{!! Str::limit(strip_tags($productWithPricing->product->description), 85, '...') !!}
{{ Str::limit(html_entity_decode(strip_tags($productWithPricing->product->description), ENT_QUOTES | ENT_HTML5, 'UTF-8'), 85, '...') }}
</div>
</div>
</div>

View file

@ -210,4 +210,50 @@ describe('HomeController', function () {
// Regular product price span does NOT have text-danger class
->and($content)->toMatch('/Poulet Standard.*?<span class="fw-semibold h4\s*">/s');
});
it('escapes decoded HTML payloads in featured product slider description', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()->mapWithKeys(fn ($page) => [$page->slug->value() => $page]));
$range = new Range();
$range->name = 'Volaille';
$range->slug = 'volaille';
$range->picture = 'ranges/volaille.jpg';
$range->picture_alt = 'Volaille';
$range->picture_title = 'Volaille';
$range->status = true;
$range->order = 1;
$range->save();
$product = new Product();
$product->id = (string) Uuid::uuid7();
$product->name = 'Poulet Sûr';
$product->brand = ProductBrand::loue;
$product->slug = 'poulet-sur';
$product->description = '<p>R&ocirc;ti &lt;img src=x onerror=alert(1)&gt;</p>';
$product->picture_alt = 'alt';
$product->picture_title = 'title';
$product->picture_large_alt = 'alt';
$product->picture_large_title = 'title';
$product->price = Money::EUR(1500);
$product->invoicing_unit = InvoicingUnit::PCE;
$product->sell_unit = SellUnit::PCE;
$product->tax_rate = TaxRate::TVA_5_5;
$product->stock = 10;
$product->range_id = $range->id;
$product->order = 1;
$product->star = true;
$product->status = true;
$product->save();
// WHEN
$response = $this->get('/');
// THEN
$response->assertOk();
$response->assertDontSee('<img src=x onerror=alert(1)>', false);
$response->assertSee('Rôti &lt;img src=x onerror=alert(1)&gt;', false);
});
});

View file

@ -3,6 +3,7 @@
declare(strict_types=1);
use App\Http\Requests\Bo\Products\StoreProductRequest;
use Domain\Enums\ProductBrand;
use Illuminate\Http\UploadedFile;
use Infrastructure\Adapters\Services\AppAdminAuthService;
use Money\Currency;
@ -119,4 +120,36 @@ describe('StoreProductRequest', function () {
expect($smallValidator->errors()->has('picture'))->toBeTrue()
->and($largeValidator->errors()->has('picture_large'))->toBeTrue();
});
it('requires a valid ProductBrand enum value for brand and validates picture alt/title lengths', function () {
// GIVEN
$authService = mock(AppAdminAuthService::class);
$request = new StoreProductRequest($authService, new Currency('EUR'));
$rules = $request->rules();
// WHEN
$invalidBrandValidator = validator(['brand' => 'unknown-brand'], ['brand' => $rules['brand']]);
$nullBrandValidator = validator(['brand' => null], ['brand' => $rules['brand']]);
$validBrandValidator = validator(['brand' => ProductBrand::loue->value], ['brand' => $rules['brand']]);
$tooLongAltValidator = validator([
'picture_alt' => str_repeat('a', 256),
'picture_title' => str_repeat('b', 256),
'picture_large_alt' => str_repeat('c', 256),
'picture_large_title' => str_repeat('d', 256),
], [
'picture_alt' => $rules['picture_alt'],
'picture_title' => $rules['picture_title'],
'picture_large_alt' => $rules['picture_large_alt'],
'picture_large_title' => $rules['picture_large_title'],
]);
// THEN
expect($invalidBrandValidator->fails())->toBeTrue()
->and($nullBrandValidator->fails())->toBeTrue()
->and($validBrandValidator->passes())->toBeTrue()
->and($tooLongAltValidator->errors()->has('picture_alt'))->toBeTrue()
->and($tooLongAltValidator->errors()->has('picture_title'))->toBeTrue()
->and($tooLongAltValidator->errors()->has('picture_large_alt'))->toBeTrue()
->and($tooLongAltValidator->errors()->has('picture_large_title'))->toBeTrue();
});
});