feat(editor): add external link warning page

A malicious user could modify a valid looking link (for example of the editor UI)
to point to a malicous site, by using overlays and CSS. Since CSS should stay
enabled, we need to make the user aware of possible risks when leaving the
HedgeDoc instance, in order to protect them from credential-theft.

This commit adds a new interstitial page for external links, that shows the
target URL and asks the user, whether they really want to continue.

Signed-off-by: Erik Michelson <github@erik.michelson.eu>
This commit is contained in:
Erik Michelson
2026-06-09 11:25:33 +02:00
committed by Philip Molares
parent fa00e34635
commit 814271dc0b
8 changed files with 125 additions and 7 deletions

View File

@@ -557,10 +557,6 @@ export function postProcess (code) {
html = html.replace(/@import url\(([^)]*)\);?/gi, '')
$(value).html(html)
})
// link should open in new window or tab
// also add noopener to prevent clickjacking
// See details: https://mathiasbynens.github.io/rel-noopener/
result.find('a:not([href^="#"]):not([target])').attr('target', '_blank').attr('rel', 'noopener')
// If it's hashtag link then make it base uri independent
result.find('a[href^="#"]').each((index, linkTag) => {
@@ -568,6 +564,7 @@ export function postProcess (code) {
currentLocation.hash = linkTag.hash
linkTag.href = currentLocation.toString()
})
rewriteExternalLinks(result)
// update continue line numbers
const linenumberdivs = result.find('.gutter.linenumber').toArray()
@@ -593,6 +590,30 @@ export function postProcess (code) {
}
window.postProcess = postProcess
// rewrite external links to go through the /_link warning page
export function rewriteExternalLinks (view) {
view.find('a[href]').each((index, linkTag) => {
const anchor = $(linkTag)
const href = anchor.attr('href')
if (!href || href.startsWith('#')) {
return
}
let parsed
try {
parsed = new URL(href)
} catch (err) {
return
}
// only rewrite links that have an absolute http(s) URL on a different origin
if (!['http:', 'https:'].includes(parsed.protocol) || parsed.origin === window.location.origin) {
return
}
const noteURL = window.location.pathname.split('/').pop()
anchor.attr('href', `${window.location.origin}/_link?url=${encodeURIComponent(parsed.href)}&note=${noteURL}`)
})
}
window.rewriteExternalLinks = rewriteExternalLinks
const domevents = Object.getOwnPropertyNames(document).concat(Object.getOwnPropertyNames(Object.getPrototypeOf(Object.getPrototypeOf(document)))).concat(Object.getOwnPropertyNames(Object.getPrototypeOf(window))).filter(function (i) {
return !i.indexOf('on') && (document[i] === null || typeof document[i] === 'function')
}).filter(function (elem, pos, self) {

View File

@@ -55,6 +55,7 @@ import {
renderTOC,
renderTags,
renderTitle,
rewriteExternalLinks,
scrollToHash,
smoothHashScroll,
updateLastChange,
@@ -3498,6 +3499,7 @@ function updateViewInner () {
}
removeDOMEvents(ui.area.markdown)
finishView(ui.area.markdown)
rewriteExternalLinks(ui.area.markdown)
autoLinkify(ui.area.markdown)
deduplicatedHeaderId(ui.area.markdown)
renderTOC(ui.area.markdown)