diff --git a/app/Http/Requests/Bo/Products/StoreProductRequest.php b/app/Http/Requests/Bo/Products/StoreProductRequest.php index 896bd189..c97a0b52 100644 --- a/app/Http/Requests/Bo/Products/StoreProductRequest.php +++ b/app/Http/Requests/Bo/Products/StoreProductRequest.php @@ -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'], diff --git a/resources/ts/fo/services/toast.service.test.ts b/resources/ts/fo/services/toast.service.test.ts new file mode 100644 index 00000000..3f5bc3dc --- /dev/null +++ b/resources/ts/fo/services/toast.service.test.ts @@ -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 = "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(); + }); +}); diff --git a/resources/ts/fo/services/toast.service.ts b/resources/ts/fo/services/toast.service.ts index 0632a1bb..10579987 100644 --- a/resources/ts/fo/services/toast.service.ts +++ b/resources/ts/fo/services/toast.service.ts @@ -76,7 +76,28 @@ export class ToastService { el.dataset["bsDelay"] = String(opts.delay ?? 4000); } - el.innerHTML = `
${message}
`; + 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; } diff --git a/resources/views/fo/home-parts/sliders/discounts.blade.php b/resources/views/fo/home-parts/sliders/discounts.blade.php index db8daa5e..476c8389 100644 --- a/resources/views/fo/home-parts/sliders/discounts.blade.php +++ b/resources/views/fo/home-parts/sliders/discounts.blade.php @@ -42,7 +42,7 @@
- {!! 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, '...') }}
diff --git a/tests/Feature/App/Http/Controllers/Fo/Pages/HomeControllerTest.php b/tests/Feature/App/Http/Controllers/Fo/Pages/HomeControllerTest.php index be346894..e8f2243f 100644 --- a/tests/Feature/App/Http/Controllers/Fo/Pages/HomeControllerTest.php +++ b/tests/Feature/App/Http/Controllers/Fo/Pages/HomeControllerTest.php @@ -210,4 +210,50 @@ describe('HomeController', function () { // Regular product price span does NOT have text-danger class ->and($content)->toMatch('/Poulet Standard.*?/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 = '

Rôti <img src=x onerror=alert(1)>

'; + $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('', false); + $response->assertSee('Rôti <img src=x onerror=alert(1)>', false); + }); }); diff --git a/tests/Feature/App/Http/Requests/Bo/Products/StoreProductRequestTest.php b/tests/Feature/App/Http/Requests/Bo/Products/StoreProductRequestTest.php index 8c56cdf7..4689e318 100644 --- a/tests/Feature/App/Http/Requests/Bo/Products/StoreProductRequestTest.php +++ b/tests/Feature/App/Http/Requests/Bo/Products/StoreProductRequestTest.php @@ -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(); + }); });