diff --git a/src/schemas/card.ts b/src/schemas/card.ts index d35c9f109..dc4ba2e6b 100644 --- a/src/schemas/card.ts +++ b/src/schemas/card.ts @@ -1,5 +1,6 @@ import punycode from 'punycode'; +import DOMPurify from 'isomorphic-dompurify'; import { z } from 'zod'; import { groupSchema } from './group'; @@ -54,6 +55,33 @@ const cardSchema = z.object({ } } + const html = DOMPurify.sanitize(card.html, { + ALLOWED_TAGS: ['iframe'], + ALLOWED_ATTR: ['src', 'width', 'height', 'frameborder', 'allowfullscreen'], + RETURN_DOM: true, + }); + + html.querySelectorAll('iframe').forEach((frame) => { + try { + const src = new URL(frame.src); + if (src.protocol !== 'https:') { + throw new Error('iframe must be https'); + } + if (src.origin === location.origin) { + throw new Error('iframe must not be same origin'); + } + frame.setAttribute('sandbox', 'allow-scripts allow-same-origin allow-presentation'); + } catch (e) { + frame.remove(); + } + }); + + card.html = html.innerHTML; + + if (!card.html) { + card.type = 'link'; + } + return card; });