Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent Disguised Links #1428

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions components/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Thumb from '@/svgs/thumb-up-fill.svg'
import { toString } from 'mdast-util-to-string'
import copy from 'clipboard-copy'
import MediaOrLink from './media-or-link'
import { IMGPROXY_URL_REGEXP, parseInternalLinks, decodeProxyUrl } from '@/lib/url'
import { IMGPROXY_URL_REGEXP, isMisleadingLink, parseInternalLinks, decodeProxyUrl } from '@/lib/url'
import reactStringReplace from 'react-string-replace'
import { rehypeInlineCodeProperty, rehypeStyler } from '@/lib/md'
import { Button } from 'react-bootstrap'
Expand Down Expand Up @@ -176,9 +176,13 @@ export default memo(function Text ({ rel, imgproxyUrls, children, tab, itemId, o
return href
}

// If [text](url) was parsed as <a> and text is not empty and not a link itself,
// we don't render it as an image since it was probably a conscious choice to include text.
const text = children[0]
let text = children[0]

// Prevents text being used to appear like a link
if (isMisleadingLink(text, href)) {
text = href
}

let url
try {
url = !href.startsWith('/') && new URL(href)
Expand Down Expand Up @@ -254,7 +258,7 @@ export default memo(function Text ({ rel, imgproxyUrls, children, tab, itemId, o
}

// assume the link is an image which will fallback to link if it's not
return <TextMediaOrLink src={href} rel={rel ?? UNKNOWN_LINK_REL} {...props}>{children}</TextMediaOrLink>
return <TextMediaOrLink src={href} rel={rel ?? UNKNOWN_LINK_REL} {...props}>{text}</TextMediaOrLink>
},
img: TextMediaOrLink
}), [outlawed, rel, itemId, Code, P, Heading, Table, TextMediaOrLink])
Expand Down
16 changes: 16 additions & 0 deletions lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,22 @@ export function parseEmbedUrl (href) {
return null
}

export function isMisleadingLink (text, href) {
let misleading = false

if (/^\s*(\w+\.)+\w+/.test(text)) {
try {
const hrefUrl = new URL(href)

if (new URL(hrefUrl.protocol + text).origin !== hrefUrl.origin) {
misleading = true
}
} catch {}
}

return misleading
}

export function stripTrailingSlash (uri) {
return uri.endsWith('/') ? uri.slice(0, -1) : uri
}
Expand Down
Loading