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:
committed by
Philip Molares
parent
fa00e34635
commit
814271dc0b
@@ -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)}¬e=${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) {
|
||||
|
||||
Reference in New Issue
Block a user