Skip to content

Commit dca7f8c

Browse files
authored
Merge pull request #1832 from hackmdio/bugfix/sanitize-url-to-prevent-xss
fix: sanitize pdf url to prevent XSS on inline PDFs
2 parents 6d95fd1 + 11cd200 commit dca7f8c

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

public/js/extra.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import './lib/renderer/lightbox'
2828
import { renderCSVPreview } from './lib/renderer/csvpreview'
2929

3030
import { escapeAttrValue } from './render'
31+
import { sanitizeUrl } from './utils'
3132

3233
import markdownit from 'markdown-it'
3334
import markdownitContainer from 'markdown-it-container'
@@ -630,10 +631,11 @@ export function finishView (view) {
630631
view.find('div.pdf.raw').removeClass('raw')
631632
.each(function (key, value) {
632633
const url = $(value).attr('data-pdfurl')
634+
const cleanUrl = sanitizeUrl(url)
633635
const inner = $('<div></div>')
634636
$(this).append(inner)
635637
setTimeout(() => {
636-
PDFObject.embed(url, inner, {
638+
PDFObject.embed(cleanUrl, inner, {
637639
height: '400px'
638640
})
639641
}, 1)

public/js/utils.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,23 @@ export function decodeNoteId (encodedId) {
2626
idParts.push(id.substr(20, 12))
2727
return idParts.join('-')
2828
}
29+
30+
/**
31+
* sanitize url to prevent XSS
32+
* @see {@link https://github.com/braintree/sanitize-url/issues/52#issue-1593777166}
33+
*
34+
* @param {string} rawUrl
35+
* @returns {string} sanitized url
36+
*/
37+
export function sanitizeUrl (rawUrl) {
38+
try {
39+
const url = new URL(rawUrl)
40+
if (url.protocol === 'http:' || url.protocol === 'https:') {
41+
return url.toString()
42+
}
43+
44+
throw new Error('Invalid protocol')
45+
} catch (error) {
46+
return 'about:blank'
47+
}
48+
}

0 commit comments

Comments
 (0)