|
| 1 | +import type { EmbedBlockData } from "@wdprlib/ast"; |
| 2 | +import type { RenderContext } from "../context"; |
| 3 | + |
| 4 | +/** |
| 5 | + * Boolean attributes that should be normalized to attr="attr" format |
| 6 | + * (Wikidot normalizes these attributes in its output) |
| 7 | + */ |
| 8 | +const BOOLEAN_ATTRIBUTES = [ |
| 9 | + "allowfullscreen", |
| 10 | + "async", |
| 11 | + "autofocus", |
| 12 | + "autoplay", |
| 13 | + "checked", |
| 14 | + "controls", |
| 15 | + "default", |
| 16 | + "defer", |
| 17 | + "disabled", |
| 18 | + "formnovalidate", |
| 19 | + "hidden", |
| 20 | + "ismap", |
| 21 | + "loop", |
| 22 | + "multiple", |
| 23 | + "muted", |
| 24 | + "novalidate", |
| 25 | + "open", |
| 26 | + "readonly", |
| 27 | + "required", |
| 28 | + "reversed", |
| 29 | + "selected", |
| 30 | +]; |
| 31 | + |
| 32 | +/** |
| 33 | + * Default allowlist patterns for embed content (ported from Wikidot's default.php) |
| 34 | + * Only content matching these patterns will be rendered. |
| 35 | + */ |
| 36 | +export const DEFAULT_EMBED_ALLOWLIST: RegExp[] = [ |
| 37 | + // Any iframe with standard attributes (Wikidot's 'anyiframe' pattern) |
| 38 | + /^<iframe(\s+[a-z0-9_]+\s*=\s*"[^"]*")+>\s*<\/iframe>$/is, |
| 39 | + |
| 40 | + // YouTube embed |
| 41 | + /^<iframe[^>]*\s+src="https?:\/\/(www\.)?youtube\.com\/embed\/[a-zA-Z0-9_-]+"[^>]*>\s*<\/iframe>$/is, |
| 42 | + /^<iframe[^>]*\s+src="https?:\/\/(www\.)?youtube-nocookie\.com\/embed\/[a-zA-Z0-9_-]+"[^>]*>\s*<\/iframe>$/is, |
| 43 | + |
| 44 | + // Vimeo embed |
| 45 | + /^<iframe[^>]*\s+src="https?:\/\/player\.vimeo\.com\/video\/[0-9]+"[^>]*>\s*<\/iframe>$/is, |
| 46 | + |
| 47 | + // Google Maps |
| 48 | + /^<iframe[^>]*\s+src="https?:\/\/www\.google\.com\/maps\/embed[^"]*"[^>]*>\s*<\/iframe>$/is, |
| 49 | + |
| 50 | + // Google Calendar |
| 51 | + /^<iframe[^>]*\s+src="https?:\/\/calendar\.google\.com\/calendar\/embed[^"]*"[^>]*>\s*<\/iframe>$/is, |
| 52 | + |
| 53 | + // Spotify |
| 54 | + /^<iframe[^>]*\s+src="https?:\/\/open\.spotify\.com\/embed\/[^"]*"[^>]*>\s*<\/iframe>$/is, |
| 55 | + |
| 56 | + // SoundCloud |
| 57 | + /^<iframe[^>]*\s+src="https?:\/\/w\.soundcloud\.com\/player\/[^"]*"[^>]*>\s*<\/iframe>$/is, |
| 58 | + |
| 59 | + // Twitter/X embed |
| 60 | + /^<blockquote[^>]*class="twitter-tweet"[^>]*>[\s\S]*<\/blockquote>\s*<script[^>]*src="https?:\/\/platform\.twitter\.com\/widgets\.js"[^>]*>\s*<\/script>$/is, |
| 61 | + |
| 62 | + // CodePen |
| 63 | + /^<iframe[^>]*\s+src="https?:\/\/codepen\.io\/[^"]*"[^>]*>\s*<\/iframe>$/is, |
| 64 | +]; |
| 65 | + |
| 66 | +/** |
| 67 | + * Check if JS event handlers are present in the content (XSS prevention) |
| 68 | + */ |
| 69 | +function hasJsEventHandlers(content: string): boolean { |
| 70 | + // Match on* event handlers (onclick, onerror, onload, etc.) |
| 71 | + return /<[^>]*\s+on[a-z]+\s*=/i.test(content); |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Validate embed content against allowlist |
| 76 | + */ |
| 77 | +function isAllowedEmbed(content: string, allowlist: RegExp[]): boolean { |
| 78 | + const trimmed = content.trim(); |
| 79 | + |
| 80 | + // Check for JS event handlers |
| 81 | + if (hasJsEventHandlers(trimmed)) { |
| 82 | + return false; |
| 83 | + } |
| 84 | + |
| 85 | + // Check against allowlist patterns |
| 86 | + for (const pattern of allowlist) { |
| 87 | + if (pattern.test(trimmed)) { |
| 88 | + return true; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return false; |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Normalize boolean attributes to Wikidot format (attr -> attr="attr") |
| 97 | + */ |
| 98 | +function normalizeBooleanAttributes(html: string): string { |
| 99 | + let result = html; |
| 100 | + for (const attr of BOOLEAN_ATTRIBUTES) { |
| 101 | + // Match standalone boolean attribute (not already having a value) |
| 102 | + // Pattern: attr followed by whitespace, > or /> |
| 103 | + const pattern = new RegExp(`\\s${attr}(?=\\s|>|/>)`, "gi"); |
| 104 | + result = result.replace(pattern, ` ${attr}="${attr}"`); |
| 105 | + } |
| 106 | + return result; |
| 107 | +} |
| 108 | + |
| 109 | +/** |
| 110 | + * Render embed-block element (Wikidot style [[embed]]..[[/embed]]) |
| 111 | + * |
| 112 | + * Content is validated against an allowlist to prevent XSS. |
| 113 | + * Only matching content is rendered; otherwise an error message is shown. |
| 114 | + */ |
| 115 | +export function renderEmbedBlock(ctx: RenderContext, data: EmbedBlockData): void { |
| 116 | + const allowlist = ctx.options.embedAllowlist ?? DEFAULT_EMBED_ALLOWLIST; |
| 117 | + |
| 118 | + if (!isAllowedEmbed(data.contents, allowlist)) { |
| 119 | + ctx.push('<div class="error-block">Sorry, no match for the embedded content.</div>'); |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + // Normalize boolean attributes and output |
| 124 | + const normalized = normalizeBooleanAttributes(data.contents); |
| 125 | + ctx.push(normalized); |
| 126 | +} |
0 commit comments