diff --git a/server/controllers/AuthorController.js b/server/controllers/AuthorController.js index 85af70df5..db4561904 100644 --- a/server/controllers/AuthorController.js +++ b/server/controllers/AuthorController.js @@ -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) { diff --git a/server/models/Author.js b/server/models/Author.js index 65561e211..525f557a8 100644 --- a/server/models/Author.js +++ b/server/models/Author.js @@ -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(), diff --git a/test/server/controllers/AuthorController.test.js b/test/server/controllers/AuthorController.test.js new file mode 100644 index 000000000..b9a46a277 --- /dev/null +++ b/test/server/controllers/AuthorController.test.js @@ -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 = '

Hello

link' +const safeDescription = '

Hello

link' + +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('

Existing

') + const fakeReq = { + body: { + description: '' + }, + 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: '

Bio

', + 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('

Bio

') + + const stored = await Database.authorModel.findByPk(author.id) + expect(stored.description).to.equal('

Bio

') + }) + }) +})