Skip to content
Merged
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
4 changes: 3 additions & 1 deletion public/js/extra.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import './lib/renderer/lightbox'
import { renderCSVPreview } from './lib/renderer/csvpreview'

import { escapeAttrValue } from './render'
import { sanitizeUrl } from './utils'

import markdownit from 'markdown-it'
import markdownitContainer from 'markdown-it-container'
Expand Down Expand Up @@ -630,10 +631,11 @@ export function finishView (view) {
view.find('div.pdf.raw').removeClass('raw')
.each(function (key, value) {
const url = $(value).attr('data-pdfurl')
const cleanUrl = sanitizeUrl(url)
const inner = $('<div></div>')
$(this).append(inner)
setTimeout(() => {
PDFObject.embed(url, inner, {
PDFObject.embed(cleanUrl, inner, {
height: '400px'
})
}, 1)
Expand Down
20 changes: 20 additions & 0 deletions public/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,23 @@ export function decodeNoteId (encodedId) {
idParts.push(id.substr(20, 12))
return idParts.join('-')
}

/**
* sanitize url to prevent XSS
* @see {@link https://github.com/braintree/sanitize-url/issues/52#issue-1593777166}
*
* @param {string} rawUrl
* @returns {string} sanitized url
*/
export function sanitizeUrl (rawUrl) {
try {
const url = new URL(rawUrl)
if (url.protocol === 'http:' || url.protocol === 'https:') {
return url.toString()
}

throw new Error('Invalid protocol')
} catch (error) {
return 'about:blank'
}
}