Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Fix use of sanitizeHTML #7231

Merged
merged 9 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ export const ShippingRatesControlPackage = ( {
{ ( showItems || collapsible ) && (
<div
className="wc-block-components-shipping-rates-control__package-title"
dangerouslySetInnerHTML={ sanitizeHTML( packageData.name ) }
dangerouslySetInnerHTML={ {
__html: sanitizeHTML( packageData.name ),
} }
/>
) }
{ showItems && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { Notice } from 'wordpress-components';
import { sanitize } from 'dompurify';
import { sanitizeHTML } from '@woocommerce/utils';
import { useDispatch, useSelect } from '@wordpress/data';
import { PAYMENT_METHOD_DATA_STORE_KEY } from '@woocommerce/block-data';

Expand All @@ -13,15 +13,6 @@ import { PAYMENT_METHOD_DATA_STORE_KEY } from '@woocommerce/block-data';
*/
import './style.scss';

const ALLOWED_TAGS = [ 'a', 'b', 'em', 'i', 'strong', 'p', 'br' ];
const ALLOWED_ATTR = [ 'target', 'href', 'rel', 'name', 'download' ];

const sanitizeHTML = ( html ) => {
return {
__html: sanitize( html, { ALLOWED_TAGS, ALLOWED_ATTR } ),
};
};

const getWooClassName = ( { status = 'default' } ) => {
switch ( status ) {
case 'error':
Expand Down Expand Up @@ -78,11 +69,7 @@ export const StoreNoticesContainer = ( {
}
} }
>
<span
dangerouslySetInnerHTML={ sanitizeHTML(
props.content
) }
/>
{ sanitizeHTML( props.content ) }
nielslange marked this conversation as resolved.
Show resolved Hide resolved
</Notice>
) ) }
</div>
Expand Down
19 changes: 6 additions & 13 deletions assets/js/utils/sanitize-html.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
/**
* External dependencies
*/
import DOMPurify from 'dompurify';

type sanitizedHTMLObject = {
// eslint-disable-next-line @typescript-eslint/naming-convention
__html: string;
};
import { sanitize } from 'dompurify';

const ALLOWED_TAGS = [ 'a', 'b', 'em', 'i', 'strong', 'p', 'br' ];
const ALLOWED_ATTR = [ 'target', 'href', 'rel', 'name', 'download' ];

export const sanitizeHTML = (
html: string,
config?: { tags?: typeof ALLOWED_TAGS; attr?: typeof ALLOWED_ATTR }
): sanitizedHTMLObject => {
) => {
const tagsValue = config?.tags || ALLOWED_TAGS;
const attrValue = config?.attr || ALLOWED_ATTR;

return {
__html: DOMPurify.sanitize( html, {
ALLOWED_TAGS: tagsValue,
ALLOWED_ATTR: attrValue,
} ),
};
return sanitize( html, {
ALLOWED_TAGS: tagsValue,
ALLOWED_ATTR: attrValue,
} );
nielslange marked this conversation as resolved.
Show resolved Hide resolved
};
Loading