|
5 | 5 | import io |
6 | 6 | import json |
7 | 7 | from typing import Any |
8 | | -from html.parser import HTMLParser |
9 | 8 | from urllib.parse import quote, urlsplit |
10 | 9 |
|
11 | 10 | from fastapi import APIRouter, HTTPException, Request, UploadFile |
@@ -230,75 +229,3 @@ async def mcp_endpoint(request: Request): |
230 | 229 | _jsonrpc_error(request_id, -32001 if exc.status_code == 401 else -32003, str(exc.detail)), |
231 | 230 | status_code=exc.status_code, |
232 | 231 | ) |
233 | | - |
234 | | - |
235 | | -_SAFE_TAGS = { |
236 | | - "a", "abbr", "article", "b", "blockquote", "br", "caption", "code", "dd", "del", "details", |
237 | | - "div", "dl", "dt", "em", "figcaption", "figure", "h1", "h2", "h3", "h4", "h5", "h6", |
238 | | - "hr", "i", "img", "li", "main", "mark", "ol", "p", "pre", "s", "section", "small", "span", |
239 | | - "strong", "sub", "summary", "sup", "table", "tbody", "td", "th", "thead", "tr", "u", "ul", |
240 | | -} |
241 | | -_VOID_TAGS = {"br", "hr", "img"} |
242 | | -_DROP_CONTENT_TAGS = {"script", "style", "iframe", "object", "embed", "svg", "math", "form", "template"} |
243 | | -_SAFE_ATTRS = {"alt", "title", "class", "id", "colspan", "rowspan", "width", "height", "scope", "dir", "lang"} |
244 | | - |
245 | | - |
246 | | -class _HTMLPreviewSanitizer(HTMLParser): |
247 | | - """Whitelist-only HTML serializer; scripts, active embeds and URL attrs are removed.""" |
248 | | - |
249 | | - def __init__(self): |
250 | | - super().__init__(convert_charrefs=True) |
251 | | - self.output: list[str] = [] |
252 | | - self.drop_depth = 0 |
253 | | - |
254 | | - def handle_starttag(self, tag, attrs): |
255 | | - tag = tag.lower() |
256 | | - if self.drop_depth: |
257 | | - if tag in _DROP_CONTENT_TAGS: |
258 | | - self.drop_depth += 1 |
259 | | - return |
260 | | - if tag in _DROP_CONTENT_TAGS: |
261 | | - self.drop_depth = 1 |
262 | | - return |
263 | | - if tag not in _SAFE_TAGS: |
264 | | - return |
265 | | - safe_attrs = [] |
266 | | - for name, value in attrs: |
267 | | - name = name.lower() |
268 | | - if value is None or name.startswith("on") or name not in _SAFE_ATTRS: |
269 | | - continue |
270 | | - safe_attrs.append(f' {name}="{html.escape(value, quote=True)}"') |
271 | | - self.output.append(f"<{tag}{''.join(safe_attrs)}>") |
272 | | - |
273 | | - def handle_startendtag(self, tag, attrs): |
274 | | - self.handle_starttag(tag, attrs) |
275 | | - if tag.lower() in _SAFE_TAGS and tag.lower() not in _VOID_TAGS: |
276 | | - self.handle_endtag(tag) |
277 | | - |
278 | | - def handle_endtag(self, tag): |
279 | | - tag = tag.lower() |
280 | | - if self.drop_depth: |
281 | | - if tag in _DROP_CONTENT_TAGS: |
282 | | - self.drop_depth -= 1 |
283 | | - return |
284 | | - if tag in _SAFE_TAGS and tag not in _VOID_TAGS: |
285 | | - self.output.append(f"</{tag}>") |
286 | | - |
287 | | - def handle_data(self, data): |
288 | | - if not self.drop_depth: |
289 | | - self.output.append(html.escape(data)) |
290 | | - |
291 | | - def handle_entityref(self, name): |
292 | | - if not self.drop_depth: |
293 | | - self.output.append(f"&{html.escape(name)};") |
294 | | - |
295 | | - def handle_charref(self, name): |
296 | | - if not self.drop_depth: |
297 | | - self.output.append(f"&#{html.escape(name)};") |
298 | | - |
299 | | - |
300 | | -def sanitize_html_preview(source: str) -> str: |
301 | | - parser = _HTMLPreviewSanitizer() |
302 | | - parser.feed(source) |
303 | | - parser.close() |
304 | | - return "<!doctype html><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width,initial-scale=1\"><body>" + "".join(parser.output) + "</body>" |
0 commit comments