From a6910b70a019c54b85763c23cefb2076349ed757 Mon Sep 17 00:00:00 2001 From: Joe Anderson Date: Thu, 1 Aug 2024 15:41:44 +0100 Subject: [PATCH] Fix XSS in examples (#5688) --- site/examples/embeds.tsx | 17 ++++++++++++++++- site/examples/inlines.tsx | 17 ++++++++++++++++- site/examples/paste-html.tsx | 26 ++++++++++++++++++++++++-- 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/site/examples/embeds.tsx b/site/examples/embeds.tsx index 47e5951aff..248becff1e 100644 --- a/site/examples/embeds.tsx +++ b/site/examples/embeds.tsx @@ -41,9 +41,24 @@ const Element = props => { } } +const allowedSchemes = ['http:', 'https:'] + const VideoElement = ({ attributes, children, element }) => { const editor = useSlateStatic() const { url } = element + + const safeUrl = useMemo(() => { + let parsedUrl: URL = null + try { + parsedUrl = new URL(url) + // eslint-disable-next-line no-empty + } catch {} + if (parsedUrl && allowedSchemes.includes(parsedUrl.protocol)) { + return parsedUrl.href + } + return 'about:blank' + }, [url]) + return (
@@ -54,7 +69,7 @@ const VideoElement = ({ attributes, children, element }) => { }} >