feat(security): confirm before opening a link whose text names a different site (#122)
utils/linkSafety.ts compares the registrable domain the visible text claims (when it looks like a URL/host) with the href's; a mismatch, or a punycode (IDN) destination, renders the anchor as SuspiciousLink, whose click opens a confirm — "It shows matrix.lotusguild.org but goes to evil.example." with the full URL, Cancel / Open anyway (opens in a new tab with noopener). Honest links are untouched: same registrable domain (youtube.com text over www.youtube.com, bbc.co.uk over news.bbc.co.uk), plain-word text, mailto:, matrix.to and Lotus permalinks, anchors with non-text children. Comparator unit-tested (incl. a Cyrillic-а paypal homograph); verified headless that the phish and IDN messages are flagged, the honest ones are not, the click shows the confirm and does not navigate, Cancel keeps you put, Open anyway opens the real target. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PPmy3tPq869XDW4njjVaKA
This commit is contained in:
@@ -23,6 +23,8 @@ import Linkify from 'linkify-react';
|
||||
import { ErrorBoundary } from 'react-error-boundary';
|
||||
import { ChildNode } from 'domhandler';
|
||||
import { stripTrackingParams } from '../utils/urlTracking';
|
||||
import { analyzeLink } from '../utils/linkSafety';
|
||||
import { SuspiciousLink } from '../components/suspicious-link';
|
||||
import { lotusPermalinkToMatrixTo } from './lotus-permalink';
|
||||
import * as css from '../styles/CustomHtml.css';
|
||||
import {
|
||||
@@ -595,6 +597,27 @@ export const getReactCustomHtmlParser = (
|
||||
if (mention) return mention;
|
||||
}
|
||||
|
||||
// [Gitea #122] Link text that names another site than the href (or a
|
||||
// punycode destination) gets a confirm on click instead of a straight
|
||||
// navigation. Only plain-text anchors are checked — an image or
|
||||
// formatted child isn't a URL claim.
|
||||
if (name === 'a' && typeof props.href === 'string' && !matrixHref) {
|
||||
// Duck-typed: with duplicate domhandler copies (dev server) the
|
||||
// parser's Text class is not the one we import, so instanceof lies.
|
||||
const textOnly = children.every((c) => c.type === 'text');
|
||||
const visible = textOnly
|
||||
? children.map((c) => (c.type === 'text' ? (c as DOMText).data : '')).join('')
|
||||
: '';
|
||||
const safety = textOnly ? analyzeLink(visible, String(props.href)) : null;
|
||||
if (safety && (safety.mismatch || safety.punycode)) {
|
||||
return (
|
||||
<SuspiciousLink href={String(props.href)} safety={safety} anchorProps={props}>
|
||||
{domToReact(children as unknown as DOMNode[], opts)}
|
||||
</SuspiciousLink>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if ((name === 'span' || name === 'div') && 'data-mx-maths' in props) {
|
||||
// Spec (CS-API §11.5): render the `data-mx-maths` LaTeX with KaTeX
|
||||
// (block for <div>, inline for <span>). On failure fall back to the
|
||||
|
||||
Reference in New Issue
Block a user