Commit 392a957b40e for woocommerce
commit 392a957b40e26f0ed23791fab0c26c0301bb60a9
Author: Luigi Teschio <gigitux@gmail.com>
Date: Fri Nov 20 17:37:31 2026 +0100
Sanitize checkout terms text before rendering it as HTML (#71220)
(cherry picked from commit fe3920aad4644db4af4c0c76fd3979a423343ff9)
Co-authored-by: Mike Jolley <mike.jolley@me.com>
diff --git a/plugins/woocommerce/client/blocks/assets/js/blocks/checkout/inner-blocks/checkout-terms-block/frontend.tsx b/plugins/woocommerce/client/blocks/assets/js/blocks/checkout/inner-blocks/checkout-terms-block/frontend.tsx
index 74e49859d44..a1ef0b58e3e 100644
--- a/plugins/woocommerce/client/blocks/assets/js/blocks/checkout/inner-blocks/checkout-terms-block/frontend.tsx
+++ b/plugins/woocommerce/client/blocks/assets/js/blocks/checkout/inner-blocks/checkout-terms-block/frontend.tsx
@@ -3,7 +3,7 @@
*/
import { __ } from '@wordpress/i18n';
import clsx from 'clsx';
-import { useState, useEffect } from '@wordpress/element';
+import { useState, useEffect, useMemo } from '@wordpress/element';
import {
CheckboxControl,
ValidationInputError,
@@ -12,12 +12,30 @@ import { useCheckoutSubmit } from '@woocommerce/base-context/hooks';
import { withInstanceId } from '@wordpress/compose';
import { useDispatch, useSelect } from '@wordpress/data';
import { validationStore } from '@woocommerce/block-data';
+import {
+ sanitizeHTML,
+ DEFAULT_ALLOWED_TAGS,
+ DEFAULT_ALLOWED_ATTR,
+} from '@woocommerce/sanitize';
/**
* Internal dependencies
*/
import { termsConsentDefaultText, termsCheckboxDefaultText } from './constants';
+// The text can come from a `data-text` attribute in the saved block HTML, which kses doesn't sanitize as HTML.
+// Allow the inline formats the editor's RichText toolbar produces.
+const ALLOWED_TAGS = [
+ ...DEFAULT_ALLOWED_TAGS,
+ 's',
+ 'code',
+ 'sub',
+ 'sup',
+ 'kbd',
+ 'mark',
+];
+const ALLOWED_ATTR = [ ...DEFAULT_ALLOWED_ATTR, 'class' ];
+
const FrontendBlock = ( {
text,
checkbox,
@@ -33,6 +51,18 @@ const FrontendBlock = ( {
} ): JSX.Element => {
const [ checked, setChecked ] = useState( false );
+ const textHtml = useMemo(
+ () =>
+ sanitizeHTML(
+ text ||
+ ( checkbox
+ ? termsCheckboxDefaultText
+ : termsConsentDefaultText ),
+ { tags: ALLOWED_TAGS, attr: ALLOWED_ATTR }
+ ),
+ [ text, checkbox ]
+ );
+
const { isDisabled } = useCheckoutSubmit();
const validationErrorId = 'terms-and-conditions-' + instanceId;
@@ -112,9 +142,7 @@ const FrontendBlock = ( {
>
<span
className="wc-block-components-checkbox__label"
- dangerouslySetInnerHTML={ {
- __html: text || termsCheckboxDefaultText,
- } }
+ dangerouslySetInnerHTML={ { __html: textHtml } }
/>
</CheckboxControl>
<ValidationInputError
@@ -125,9 +153,7 @@ const FrontendBlock = ( {
) : (
<span
className="wc-block-components-checkbox__label"
- dangerouslySetInnerHTML={ {
- __html: text || termsConsentDefaultText,
- } }
+ dangerouslySetInnerHTML={ { __html: textHtml } }
/>
) }
</div>
diff --git a/plugins/woocommerce/client/blocks/assets/js/blocks/checkout/inner-blocks/checkout-terms-block/test/frontend.js b/plugins/woocommerce/client/blocks/assets/js/blocks/checkout/inner-blocks/checkout-terms-block/test/frontend.js
index 4d75e7f082d..7f269b6c3e9 100644
--- a/plugins/woocommerce/client/blocks/assets/js/blocks/checkout/inner-blocks/checkout-terms-block/test/frontend.js
+++ b/plugins/woocommerce/client/blocks/assets/js/blocks/checkout/inner-blocks/checkout-terms-block/test/frontend.js
@@ -124,4 +124,38 @@ describe( 'FrontendBlock', () => {
);
} );
} );
+
+ it.each( [ true, false ] )(
+ 'Strips scripts but keeps formatting in the terms text (checkbox: %s)',
+ ( checkbox ) => {
+ const { container } = render(
+ <SlotFillProvider>
+ <FrontendBlock
+ checkbox={ checkbox }
+ text={
+ 'I agree to the <a href="https://example.com/terms" target="_blank">terms</a> and <strong>policy</strong><img src="x" onerror="alert(1)"><script>alert(2)</script>'
+ }
+ showSeparator={ false }
+ />
+ </SlotFillProvider>
+ );
+
+ const label = container.querySelector(
+ '.wc-block-components-checkbox__label'
+ );
+
+ expect( label.querySelector( 'img' ) ).toBeNull();
+ expect( label.querySelector( 'script' ) ).toBeNull();
+ expect( label.querySelector( '[onerror]' ) ).toBeNull();
+ expect( label.querySelector( 'strong' ) ).toHaveTextContent(
+ 'policy'
+ );
+ const link = label.querySelector( 'a' );
+ expect( link ).toHaveAttribute(
+ 'href',
+ 'https://example.com/terms'
+ );
+ expect( link ).toHaveAttribute( 'target', '_blank' );
+ }
+ );
} );