Skip to content

Commit

Permalink
Fix use of sanitizeHTML (woocommerce#7231)
Browse files Browse the repository at this point in the history
* Remove object from sanitizeHTML return value

* Import sanitizeHTML from utils

* Fix dangerously set inner HTML format

* Update package-lock

* Update package-lock

* Update package-lock

* Update @types/dompurify version

Co-authored-by: Thomas Roberts <[email protected]>
  • Loading branch information
2 people authored and senadir committed Nov 12, 2022
1 parent 12d9f94 commit 49a575a
Show file tree
Hide file tree
Showing 4 changed files with 42,099 additions and 65,628 deletions.
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 ) }
</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,
} );
};
Loading

0 comments on commit 49a575a

Please sign in to comment.