audiobookshelf/server/utils/htmlSanitizer.js
advplyr 972193b193
Some checks are pending
CodeQL / Analyze (push) Waiting to run
Run Component Tests / Run Component Tests (push) Waiting to run
Build and Push Docker Image / build (push) Waiting to run
Integration Test / build and test (push) Waiting to run
Run Unit Tests / Run Unit Tests (push) Waiting to run
Update server settings authLoginCustomMessage to sanitize on save and load
2026-03-11 17:18:05 -05:00

48 lines
1.2 KiB
JavaScript

const sanitizeHtml = require('../libs/sanitizeHtml')
const { entities } = require('./htmlEntities')
/**
*
* @param {string} html
* @returns {string}
*/
function sanitize(html) {
if (typeof html !== 'string') {
return ''
}
const sanitizerOptions = {
allowedTags: ['p', 'ol', 'ul', 'li', 'a', 'strong', 'em', 'del', 'br', 'b', 'i'],
disallowedTagsMode: 'discard',
allowedAttributes: {
a: ['href', 'name', 'target']
},
allowedSchemes: ['http', 'https', 'mailto'],
allowProtocolRelative: false
}
return sanitizeHtml(html, sanitizerOptions)
}
module.exports.sanitize = sanitize
function stripAllTags(html, shouldDecodeEntities = true) {
if (typeof html !== 'string') return ''
const sanitizerOptions = {
allowedTags: [],
disallowedTagsMode: 'discard'
}
let sanitized = sanitizeHtml(html, sanitizerOptions)
return shouldDecodeEntities ? decodeHTMLEntities(sanitized) : sanitized
}
module.exports.stripAllTags = stripAllTags
function decodeHTMLEntities(strToDecode) {
return strToDecode.replace(/\&([^;]+);?/g, function (entity) {
if (entity in entities) {
return entities[entity]
}
return entity
})
}