Skip to content

Commit 04e9734

Browse files
fix(community): admin 审核页外链过 sanitizeExternalUrl (CR)
Copilot CR 反馈: - app/admin/community/page.tsx 直接把 link.url 渲染到 <a href>,虽然后端 UrlNormalizer 已拒非 http/https,但 defense-in-depth 要求前端也兜底, 防止后端某天放松校验导致 javascript:/data: XSS 改动: - 外链过 sanitizeExternalUrl,非法链接降级为不可点击的灰色文字 + ⚠ 标记
1 parent 5868366 commit 04e9734

1 file changed

Lines changed: 25 additions & 9 deletions

File tree

app/admin/community/page.tsx

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { useEffect, useState } from "react";
1515
import Image from "next/image";
1616
import { AdminGuard } from "@/app/admin/events/AdminGuard";
1717
import type { SharedLinkView } from "@/app/feed/types";
18+
import { sanitizeExternalUrl } from "@/lib/url-safety";
1819
import { approveLink, listPendingLinks, rejectLink } from "./lib";
1920

2021
export default function AdminCommunityPage() {
@@ -164,15 +165,30 @@ function AdminCommunityInner() {
164165
{link.host}
165166
</span>
166167
</div>
167-
<a
168-
href={link.url}
169-
target="_blank"
170-
rel="noopener noreferrer"
171-
className="block mt-2 font-semibold text-base hover:underline truncate"
172-
title={link.ogTitle ?? link.url}
173-
>
174-
{link.ogTitle ?? link.url}
175-
</a>
168+
{(() => {
169+
// defense-in-depth:后端 UrlNormalizer 已拒非 http/https,
170+
// 前端仍用 sanitizeExternalUrl 兜底过滤 javascript:/data: 协议。
171+
const safe = sanitizeExternalUrl(link.url);
172+
const title = link.ogTitle ?? link.url;
173+
return safe ? (
174+
<a
175+
href={safe}
176+
target="_blank"
177+
rel="noopener noreferrer"
178+
className="block mt-2 font-semibold text-base hover:underline truncate"
179+
title={title}
180+
>
181+
{title}
182+
</a>
183+
) : (
184+
<span
185+
className="block mt-2 font-semibold text-base text-neutral-400 truncate"
186+
title="链接协议不安全,已禁用点击"
187+
>
188+
{title}
189+
</span>
190+
);
191+
})()}
176192
{link.ogDescription && (
177193
<p className="mt-1 text-sm text-neutral-600 dark:text-neutral-300 line-clamp-2">
178194
{link.ogDescription}

0 commit comments

Comments
 (0)