Skip to content

Commit 4d6dd71

Browse files
committed
Merge remote-tracking branch 'origin/CHI-3741-html_in_webchat_labels' into CHI-3741-html_in_webchat_labels
# Conflicts: # aselo-webchat-react-app/src/localization/LocalizedTemplate.tsx
2 parents fe671f0 + e2a099b commit 4d6dd71

3 files changed

Lines changed: 55 additions & 3 deletions

File tree

aselo-webchat-react-app/package-lock.json

Lines changed: 39 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

aselo-webchat-react-app/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
"@twilio-paste/icons": "^13.1.0",
1313
"@twilio-paste/theme": "^12.0.1",
1414
"@twilio/conversations": "2.1.0-rc.0",
15+
"@types/dompurify": "^3.0.5",
1516
"@types/file-saver": "2.0.5",
17+
"dompurify": "^3.3.3",
1618
"file-saver": "2.0.5",
1719
"google-auth-library": "8.5.1",
1820
"googleapis": "107.0.0",

aselo-webchat-react-app/src/localization/LocalizedTemplate.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,23 @@
1313
* You should have received a copy of the GNU Affero General Public License
1414
* along with this program. If not, see https://www.gnu.org/licenses/.
1515
*/
16+
import DOMPurify from 'dompurify';
1617
import { useSelector } from 'react-redux';
1718

1819
import { localizeKey } from './localizeKey';
1920
import { selectCurrentTranslations } from '../store/config.reducer';
2021

22+
/**
23+
* Sanitization config that only allows links and text decoration tags.
24+
* Blocks scripts, event handlers, javascript: URLs and all other unsafe content.
25+
*/
26+
const SANITIZE_CONFIG: DOMPurify.Config = {
27+
ALLOWED_TAGS: ['a', 'b', 'i', 'em', 'strong', 'u', 's', 'span', 'br'],
28+
ALLOWED_ATTR: ['href', 'rel'],
29+
};
30+
31+
const sanitizeHtml = (html: string): string => DOMPurify.sanitize(html, SANITIZE_CONFIG) as string;
32+
2133
const LocalizedTemplate: React.FC<{ code: string; renderAsHtml?: string } & Record<string, string>> = ({
2234
code,
2335
renderAsHtml,
@@ -26,8 +38,9 @@ const LocalizedTemplate: React.FC<{ code: string; renderAsHtml?: string } & Reco
2638
const translations = useSelector(selectCurrentTranslations);
2739
const translateForCurrentLocale = localizeKey(translations);
2840
if (renderAsHtml?.toLowerCase() === 'true') {
41+
const safeHtml = sanitizeHtml(translateForCurrentLocale(code, parameters));
2942
// eslint-disable-next-line react/no-danger
30-
return <span dangerouslySetInnerHTML={{ __html: translateForCurrentLocale(code, parameters) }} />;
43+
return <span dangerouslySetInnerHTML={{ __html: safeHtml }} />;
3144
}
3245
return <>{translateForCurrentLocale(code, parameters)}</>;
3346
};

0 commit comments

Comments
 (0)