mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-11-20 07:31:34 +01:00
Sanitize author descriptions before they are stored or returned
This commit is contained in:
1 parent
83dce682e8
commit
bf1c5688b9
3 files changed
+169
-4
No files matched your search
@@ -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) {
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
const { expect } = require('chai')
|
||||
const { Sequelize } = require('sequelize')
|
||||
const sinon = require('sinon')
|
||||
|
||||
const Database = require('../../../server/Database')
|
||||
const AuthorController = require('../../../server/controllers/AuthorController')
|
||||
const AuthorFinder = require('../../../server/finders/AuthorFinder')
|
||||
const Logger = require('../../../server/Logger')
|
||||
|
||||
const unsafeDescription = '<p>Hello</p><script>alert(1)</script><img src="x" onerror="alert(document.domain)"><a href="https://example.com">link</a>'
|
||||
const safeDescription = '<p>Hello</p><a href="https://example.com">link</a>'
|
||||
|
||||
describe('AuthorController', () => {
|
||||
beforeEach(async () => {
|
||||
global.ServerSettings = {}
|
||||
Database.sequelize = new Sequelize({ dialect: 'sqlite', storage: ':memory:', logging: false })
|
||||
Database.sequelize.uppercaseFirst = (str) => (str ? `${str[0].toUpperCase()}${str.substr(1)}` : '')
|
||||
await Database.buildModels()
|
||||
|
||||
sinon.stub(Logger, 'info')
|
||||
sinon.stub(Logger, 'debug')
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
sinon.restore()
|
||||
|
||||
// Clear all tables
|
||||
await Database.sequelize.sync({ force: true })
|
||||
})
|
||||
|
||||
/**
|
||||
* @param {string} [description]
|
||||
*/
|
||||
async function createAuthor(description) {
|
||||
const library = await Database.libraryModel.create({ name: 'Test Library', mediaType: 'book' })
|
||||
return Database.authorModel.create({
|
||||
name: 'Test Author',
|
||||
libraryId: library.id,
|
||||
description: description || null
|
||||
})
|
||||
}
|
||||
|
||||
describe('update', () => {
|
||||
it('should store and return a sanitized description', async () => {
|
||||
const author = await createAuthor()
|
||||
const fakeReq = {
|
||||
body: {
|
||||
description: unsafeDescription
|
||||
},
|
||||
author
|
||||
}
|
||||
const fakeRes = {
|
||||
status: sinon.stub().returnsThis(),
|
||||
send: sinon.spy(),
|
||||
json: sinon.spy()
|
||||
}
|
||||
|
||||
await AuthorController.update(fakeReq, fakeRes)
|
||||
|
||||
expect(fakeRes.json.calledOnce).to.be.true
|
||||
const response = fakeRes.json.firstCall.args[0]
|
||||
expect(response.updated).to.be.true
|
||||
expect(response.author.description).to.equal(safeDescription)
|
||||
expect(response.author.description).to.not.include('script')
|
||||
expect(response.author.description).to.not.include('onerror')
|
||||
|
||||
const stored = await Database.authorModel.findByPk(author.id)
|
||||
expect(stored.description).to.equal(safeDescription)
|
||||
})
|
||||
|
||||
it('should store null when sanitizing leaves an empty description', async () => {
|
||||
const author = await createAuthor('<p>Existing</p>')
|
||||
const fakeReq = {
|
||||
body: {
|
||||
description: '<script>alert(1)</script><img src="x" onerror="alert(1)">'
|
||||
},
|
||||
author
|
||||
}
|
||||
const fakeRes = {
|
||||
json: sinon.spy()
|
||||
}
|
||||
|
||||
await AuthorController.update(fakeReq, fakeRes)
|
||||
|
||||
const response = fakeRes.json.firstCall.args[0]
|
||||
expect(response.author.description).to.equal(null)
|
||||
|
||||
const stored = await Database.authorModel.findByPk(author.id)
|
||||
expect(stored.description).to.equal(null)
|
||||
})
|
||||
})
|
||||
|
||||
describe('toOldJSON', () => {
|
||||
it('should sanitize a stored description without rewriting the row', async () => {
|
||||
const author = await createAuthor(unsafeDescription)
|
||||
|
||||
expect(author.toOldJSON().description).to.equal(safeDescription)
|
||||
expect(author.description).to.equal(unsafeDescription)
|
||||
|
||||
const stored = await Database.authorModel.findByPk(author.id)
|
||||
expect(stored.description).to.equal(unsafeDescription)
|
||||
expect(stored.toOldJSON().description).to.equal(safeDescription)
|
||||
})
|
||||
})
|
||||
|
||||
describe('match', () => {
|
||||
it('should store the sanitized Audnexus description', async () => {
|
||||
const author = await createAuthor()
|
||||
sinon.stub(AuthorFinder, 'findAuthorByASIN').resolves({
|
||||
asin: 'B00TEST000',
|
||||
name: 'Test Author',
|
||||
description: '<p>Bio</p><script>alert(1)</script>',
|
||||
image: null
|
||||
})
|
||||
|
||||
const fakeReq = {
|
||||
body: {
|
||||
asin: 'B00TEST000'
|
||||
},
|
||||
author
|
||||
}
|
||||
const fakeRes = {
|
||||
status: sinon.stub().returnsThis(),
|
||||
send: sinon.spy(),
|
||||
json: sinon.spy()
|
||||
}
|
||||
|
||||
await AuthorController.match(fakeReq, fakeRes)
|
||||
|
||||
expect(fakeRes.json.calledOnce).to.be.true
|
||||
const response = fakeRes.json.firstCall.args[0]
|
||||
expect(response.updated).to.be.true
|
||||
expect(response.author.description).to.equal('<p>Bio</p>')
|
||||
|
||||
const stored = await Database.authorModel.findByPk(author.id)
|
||||
expect(stored.description).to.equal('<p>Bio</p>')
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user