bug修复
This commit is contained in:
1 parent
b9cc84644c
commit
ec4ca451bb
8 files changed
+177
-36
No files matched your search
+58
-15
@@ -58,22 +58,64 @@ async function post(path: string, body: unknown) {
|
||||
throw new Error((await r.json()).message || "Unable to load content");
|
||||
return r.json();
|
||||
}
|
||||
/** 兼容历史数据中被转义后保存的富文本标签。 */
|
||||
const RICH_TEXT_ALLOWED_TAGS = new Set([
|
||||
"A", "B", "BLOCKQUOTE", "BR", "CODE", "EM", "H2", "H3", "H4", "HR", "I", "IMG",
|
||||
"LI", "OL", "P", "PRE", "S", "STRONG", "TABLE", "TBODY", "TD", "TH", "THEAD", "TR", "U", "UL",
|
||||
]);
|
||||
const RICH_TEXT_REMOVED_TAGS = new Set(["BUTTON", "EMBED", "FORM", "IFRAME", "INPUT", "OBJECT", "SCRIPT", "STYLE"]);
|
||||
|
||||
/** 判断富文本链接或图片地址是否为可安全输出的站内、HTTP 或 HTTPS 地址。 */
|
||||
function isSafeRichTextUrl(value: string): boolean {
|
||||
const normalized = value.trim().replace(/[\u0000-\u0020]+/g, "").toLowerCase();
|
||||
return normalized.startsWith("/") || normalized.startsWith("#") || normalized.startsWith("http://")
|
||||
|| normalized.startsWith("https://") || normalized.startsWith("mailto:") || normalized.startsWith("tel:");
|
||||
}
|
||||
|
||||
/** 清理富文本节点,仅保留前台展示所需的安全标签和属性。 */
|
||||
function sanitizeRichTextHtml(content: string): string {
|
||||
const container = document.createElement("div");
|
||||
container.innerHTML = content;
|
||||
Array.from(container.querySelectorAll("*")).forEach((element) => {
|
||||
if (RICH_TEXT_REMOVED_TAGS.has(element.tagName)) {
|
||||
element.remove();
|
||||
return;
|
||||
}
|
||||
if (!RICH_TEXT_ALLOWED_TAGS.has(element.tagName)) {
|
||||
element.replaceWith(...Array.from(element.childNodes));
|
||||
return;
|
||||
}
|
||||
Array.from(element.attributes).forEach((attribute) => {
|
||||
const allowed = element.tagName === "A" && ["href", "target", "rel"].includes(attribute.name)
|
||||
|| element.tagName === "IMG" && ["src", "alt", "title"].includes(attribute.name)
|
||||
|| ["TD", "TH"].includes(element.tagName) && ["colspan", "rowspan"].includes(attribute.name);
|
||||
if (!allowed) element.removeAttribute(attribute.name);
|
||||
});
|
||||
if (element instanceof HTMLAnchorElement) {
|
||||
if (!isSafeRichTextUrl(element.getAttribute("href") || "")) element.removeAttribute("href");
|
||||
if (element.target === "_blank") element.rel = "noopener noreferrer";
|
||||
}
|
||||
if (element instanceof HTMLImageElement && !isSafeRichTextUrl(element.getAttribute("src") || "")) {
|
||||
element.removeAttribute("src");
|
||||
}
|
||||
});
|
||||
return container.innerHTML;
|
||||
}
|
||||
|
||||
/** 兼容历史数据中被转义后保存的富文本标签,并统一执行安全清理。 */
|
||||
function normalizeRichTextHtml(content?: string): string {
|
||||
if (!content || !/&(?:amp;)?lt;\/?(?:h[1-6]|p|ul|ol|li|blockquote|table|img|a)\b/i.test(content)) return content || "";
|
||||
if (!content) return "";
|
||||
const decoder = document.createElement("textarea");
|
||||
let decoded = content;
|
||||
for (let index = 0; index < 2; index += 1) {
|
||||
decoder.innerHTML = decoded;
|
||||
const next = decoder.value;
|
||||
if (next === decoded) break;
|
||||
decoded = next;
|
||||
if (/&(?:amp;)?lt;\/?(?:h[1-6]|p|ul|ol|li|blockquote|table|img|a)\b/i.test(content)) {
|
||||
for (let index = 0; index < 2; index += 1) {
|
||||
decoder.innerHTML = decoded;
|
||||
const next = decoder.value;
|
||||
if (next === decoded) break;
|
||||
decoded = next;
|
||||
}
|
||||
}
|
||||
decoded = decoded.replace(/^\s*<pre><code>/i, "").replace(/<\/code><\/pre>(?:<p><\/p>)?\s*$/i, "");
|
||||
if (!/<\/?(?:h[1-6]|p|ul|ol|li|blockquote|table|img|a)\b/i.test(decoded)) return content;
|
||||
const container = document.createElement("div");
|
||||
container.innerHTML = decoded;
|
||||
return container.innerHTML;
|
||||
return sanitizeRichTextHtml(decoded);
|
||||
}
|
||||
function setMeta(name: string, content?: string) {
|
||||
const selector = `meta[name="${name}"]`;
|
||||
@@ -495,15 +537,16 @@ function ConfiguredPage({ slug }: { slug: string }) {
|
||||
}, [slug]);
|
||||
if (error) return <ErrorState message={error} />;
|
||||
if (!data) return <div className="loading">Loading…</div>;
|
||||
const agreementPage = ["COOKIE_POLICY", "PRIVACY_POLICY", "TERMS_OF_SERVICE"].includes(data.systemPageType);
|
||||
return (
|
||||
<article className="detail">
|
||||
<article className={`detail configured-page${agreementPage ? " legal-page" : ""}`}>
|
||||
<Crumbs current={data.pageName} />
|
||||
<p className="eyebrow">COMPANY INFORMATION</p>
|
||||
<p className="eyebrow">{agreementPage ? "LEGAL INFORMATION" : "COMPANY INFORMATION"}</p>
|
||||
<h1>{data.pageName}</h1>
|
||||
<p className="lead">{data.summary}</p>
|
||||
{data.summary && <p className="lead">{data.summary}</p>}
|
||||
<div
|
||||
className="content"
|
||||
dangerouslySetInnerHTML={{ __html: data.content }}
|
||||
dangerouslySetInnerHTML={{ __html: normalizeRichTextHtml(data.content) }}
|
||||
/>
|
||||
</article>
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user