|
1 | 1 | import { sanitizeUrl } from "./urlValidation"; |
2 | 2 |
|
| 3 | +const sanitizeRepeated = (text: string, pattern: RegExp, replacement: string): string => { |
| 4 | + let result = text; |
| 5 | + let previousResult; |
| 6 | + do { |
| 7 | + previousResult = result; |
| 8 | + result = result.replace(pattern, replacement); |
| 9 | + } while (result !== previousResult); |
| 10 | + return result; |
| 11 | +}; |
| 12 | + |
3 | 13 | export const sanitizeMarkdown = (markdown: string): string => { |
4 | 14 | if (!markdown || typeof markdown !== "string") { |
5 | 15 | return ""; |
6 | 16 | } |
7 | 17 |
|
8 | | - return markdown |
9 | | - .replace(/\[([^\]]*)\]\(([^)]+)\)/g, (match, text, url) => { |
10 | | - const sanitizedUrl = sanitizeUrl(url); |
11 | | - return `[${text}](${sanitizedUrl})`; |
12 | | - }) |
13 | | - .replace(/<a\s+href="([^"]*)"[^>]*>([^<]*)<\/a>/gi, (match, url, text) => { |
14 | | - const sanitizedUrl = sanitizeUrl(url); |
15 | | - return `[${text}](${sanitizedUrl})`; |
16 | | - }) |
17 | | - .replace(/<script[^>]*>.*?<\/script>/gis, "") |
18 | | - .replace(/<iframe[^>]*>.*?<\/iframe>/gis, "") |
19 | | - .replace(/<object[^>]*>.*?<\/object>/gis, "") |
20 | | - .replace(/<embed[^>]*\/?>/gi, "") |
21 | | - .replace(/javascript:/gi, "") |
22 | | - .replace(/vbscript:/gi, "") |
23 | | - .replace(/data:/gi, "") |
24 | | - .replace(/on\w+\s*=/gi, ""); |
| 18 | + let sanitized = markdown; |
| 19 | + |
| 20 | + sanitized = sanitized.replace(/\[([^\]]*)\]\(([^)]+)\)/g, (match, text, url) => { |
| 21 | + const sanitizedUrl = sanitizeUrl(url); |
| 22 | + return `[${text}](${sanitizedUrl})`; |
| 23 | + }); |
| 24 | + |
| 25 | + sanitized = sanitized.replace(/<a\s+href="([^"]*)"[^>]*>([^<]*)<\/a>/gi, (match, url, text) => { |
| 26 | + const sanitizedUrl = sanitizeUrl(url); |
| 27 | + return `[${text}](${sanitizedUrl})`; |
| 28 | + }); |
| 29 | + |
| 30 | + sanitized = sanitizeRepeated(sanitized, /<script[^>]*>.*?<\/script>/gis, ""); |
| 31 | + sanitized = sanitizeRepeated(sanitized, /<iframe[^>]*>.*?<\/iframe>/gis, ""); |
| 32 | + sanitized = sanitizeRepeated(sanitized, /<object[^>]*>.*?<\/object>/gis, ""); |
| 33 | + sanitized = sanitizeRepeated(sanitized, /<embed[^>]*\/?>/gi, ""); |
| 34 | + sanitized = sanitizeRepeated(sanitized, /<form[^>]*>.*?<\/form>/gis, ""); |
| 35 | + sanitized = sanitizeRepeated(sanitized, /<input[^>]*\/?>/gi, ""); |
| 36 | + sanitized = sanitizeRepeated(sanitized, /<button[^>]*>.*?<\/button>/gis, ""); |
| 37 | + sanitized = sanitizeRepeated(sanitized, /<applet[^>]*>.*?<\/applet>/gis, ""); |
| 38 | + sanitized = sanitizeRepeated(sanitized, /<audio[^>]*>.*?<\/audio>/gis, ""); |
| 39 | + sanitized = sanitizeRepeated(sanitized, /<video[^>]*>.*?<\/video>/gis, ""); |
| 40 | + sanitized = sanitizeRepeated(sanitized, /<svg[^>]*>.*?<\/svg>/gis, ""); |
| 41 | + sanitized = sanitizeRepeated(sanitized, /<canvas[^>]*>.*?<\/canvas>/gis, ""); |
| 42 | + |
| 43 | + sanitized = sanitizeRepeated(sanitized, /javascript:/gi, ""); |
| 44 | + sanitized = sanitizeRepeated(sanitized, /vbscript:/gi, ""); |
| 45 | + |
| 46 | + sanitized = sanitizeRepeated(sanitized, /on\w+\s*=/gi, ""); |
| 47 | + sanitized = sanitizeRepeated(sanitized, /style\s*=/gi, ""); |
| 48 | + |
| 49 | + sanitized = sanitizeRepeated(sanitized, /<meta[^>]*\/?>/gi, ""); |
| 50 | + sanitized = sanitizeRepeated(sanitized, /<link[^>]*\/?>/gi, ""); |
| 51 | + sanitized = sanitizeRepeated(sanitized, /<base[^>]*\/?>/gi, ""); |
| 52 | + |
| 53 | + sanitized = sanitized.replace(/&#x[0-9a-f]+;/gi, ""); |
| 54 | + sanitized = sanitized.replace(/&#[0-9]+;/gi, ""); |
| 55 | + |
| 56 | + return sanitized; |
25 | 57 | }; |
0 commit comments