Sanitize author descriptions before they are stored or returned

This commit is contained in:
mikiher committed 2026-11-18 06:52:42 +02:00
1 parent 83dce682e8
commit bf1c5688b9
3 files changed
+169 -4

No files matched your search

+23 -3
View File
@@ -11,11 +11,24 @@ const CoverManager = require('../managers/CoverManager')
const AuthorFinder = require('../finders/AuthorFinder')
const { reqSupportsWebp, isValidASIN, clampPositiveInt } = require('../utils/index')
const htmlSanitizer = require('../utils/htmlSanitizer')
const naturalSort = createNewSortInstance({
comparer: new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }).compare
})
/**
* @param {string} description
* @returns {string|null}
*/
function sanitizeAuthorDescription(description) {
const sanitizedDescription = htmlSanitizer.sanitize(description)
if (sanitizedDescription !== description) {
Logger.debug(`[AuthorController] Sanitized description from "${description}" to "${sanitizedDescription}"`)
}
return sanitizedDescription || null
}
/**
* @typedef RequestUserObject
* @property {import('../models/User')} user
@@ -101,6 +114,10 @@ class AuthorController {
payload[key] = req.body[key]
}
}
if (payload.description) {
payload.description = sanitizeAuthorDescription(payload.description)
}
if (!Object.keys(payload).length) {
Logger.error(`[AuthorController] Invalid request payload. No valid keys found`, req.body)
return res.status(400).send('Invalid request payload. No valid keys found')
@@ -364,9 +381,12 @@ class AuthorController {
}
}
if (authorData.description && req.author.description !== authorData.description) {
req.author.description = authorData.description
hasUpdates = true
if (authorData.description) {
const description = sanitizeAuthorDescription(authorData.description)
if (req.author.description !== description) {
req.author.description = description
hasUpdates = true
}
}
if (hasUpdates) {
+7 -1
View File
@@ -1,5 +1,6 @@
const { DataTypes, Model, where, fn, col } = require('sequelize')
const parseNameString = require('../utils/parsers/parseNameString')
const htmlSanitizer = require('../utils/htmlSanitizer')
class Author extends Model {
constructor(values, options) {
@@ -175,11 +176,16 @@ class Author extends Model {
}
toOldJSON() {
let description = this.description
if (description) {
description = htmlSanitizer.sanitize(description) || null
}
return {
id: this.id,
asin: this.asin,
name: this.name,
description: this.description,
description,
imagePath: this.imagePath,
libraryId: this.libraryId,
addedAt: this.createdAt.valueOf(),