mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-11-20 23:51:35 +01:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5de038dafd | ||
|
|
a5540d07c6 | ||
|
|
1e0921100b | ||
|
|
54f1f5c7a4 | ||
|
|
fc11bb4947 | ||
|
|
0776e8079e | ||
|
|
bf1c5688b9 | ||
|
|
e9ff70e522 | ||
|
|
8af3448ac0 | ||
|
|
cf1f162aba | ||
|
|
420c2b28ed | ||
|
|
f35f40929d |
No files matched your search
@@ -0,0 +1,7 @@
|
||||
# Security
|
||||
|
||||
Report security issues through GitHub private vulnerability reporting.
|
||||
|
||||
Failed logins (`POST /login`) share one server-wide limit. Failed password changes (`PATCH /api/me/password`) are limited per user. Neither limit uses the client IP. Set `RATE_LIMIT_AUTH_MAX=0` to disable both.
|
||||
|
||||
Anything beyond those two limits is the server operator's responsibility. Add a rate limiter in front of the server if you want one. A missing limiter on any other route is out of scope unless the report shows a guessable secret or a resource that grows without a bound.
|
||||
+5
-5
@@ -22,6 +22,7 @@ class Auth {
|
||||
|
||||
/** @type {import('express-rate-limit').RateLimitRequestHandler} */
|
||||
this.authRateLimiter = RateLimiterFactory.getAuthRateLimiter()
|
||||
this.passwordChangeRateLimiter = RateLimiterFactory.getPasswordChangeRateLimiter()
|
||||
|
||||
this.tokenManager = new TokenManager()
|
||||
this.localAuthStrategy = new LocalAuthStrategy()
|
||||
@@ -328,7 +329,7 @@ class Auth {
|
||||
})
|
||||
|
||||
// Refresh token route
|
||||
router.post('/auth/refresh', this.authRateLimiter, async (req, res) => {
|
||||
router.post('/auth/refresh', async (req, res) => {
|
||||
let refreshToken = req.cookies.refresh_token
|
||||
|
||||
// If x-refresh-token header is present, use it instead of the cookie
|
||||
@@ -359,7 +360,7 @@ class Auth {
|
||||
})
|
||||
|
||||
// openid strategy login route (this redirects to the configured openid login provider)
|
||||
router.get('/auth/openid', this.authRateLimiter, (req, res) => {
|
||||
router.get('/auth/openid', (req, res) => {
|
||||
const authorizationUrlResponse = this.oidcAuthStrategy.getAuthorizationUrl(req)
|
||||
|
||||
if (authorizationUrlResponse.error) {
|
||||
@@ -377,12 +378,11 @@ class Auth {
|
||||
|
||||
// This will be the oauth2 callback route for mobile clients
|
||||
// It will redirect to an app-link like audiobookshelf://oauth
|
||||
router.get('/auth/openid/mobile-redirect', this.authRateLimiter, (req, res) => this.oidcAuthStrategy.handleMobileRedirect(req, res))
|
||||
router.get('/auth/openid/mobile-redirect', (req, res) => this.oidcAuthStrategy.handleMobileRedirect(req, res))
|
||||
|
||||
// openid strategy callback route (this receives the token from the configured openid login provider)
|
||||
router.get(
|
||||
'/auth/openid/callback',
|
||||
this.authRateLimiter,
|
||||
(req, res, next) => {
|
||||
const sessionKey = this.oidcAuthStrategy.getStrategy()._key
|
||||
|
||||
@@ -461,7 +461,7 @@ class Auth {
|
||||
*
|
||||
* @example /auth/openid/config?issuer=http://192.168.1.66:9000/application/o/audiobookshelf/
|
||||
*/
|
||||
router.get('/auth/openid/config', this.authRateLimiter, this.isAuthenticated, async (req, res) => {
|
||||
router.get('/auth/openid/config', this.isAuthenticated, async (req, res) => {
|
||||
if (!req.user.isAdminOrUp) {
|
||||
Logger.error(`[Auth] Non-admin user "${req.user.username}" attempted to get issuer config`)
|
||||
return res.sendStatus(403)
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -247,12 +247,17 @@ class CollectionController {
|
||||
return res.status(400).send('Book already in collection')
|
||||
}
|
||||
|
||||
// Create collectionBook record
|
||||
await Database.collectionBookModel.create({
|
||||
collectionId: req.collection.id,
|
||||
bookId: libraryItem.mediaId,
|
||||
order: collectionBooks.length + 1
|
||||
})
|
||||
// Create collectionBook record. A second add can pass the check above and hit the unique pair.
|
||||
try {
|
||||
await Database.collectionBookModel.create({
|
||||
collectionId: req.collection.id,
|
||||
bookId: libraryItem.mediaId,
|
||||
order: collectionBooks.length + 1
|
||||
})
|
||||
} catch (error) {
|
||||
if (error.name !== 'SequelizeUniqueConstraintError') throw error
|
||||
return res.status(400).send('Book already in collection')
|
||||
}
|
||||
const jsonExpanded = await req.collection.getOldJsonExpanded(req.user)
|
||||
if (jsonExpanded) SocketAuthority.clientEmitter(req.user.id, 'collection_updated', jsonExpanded)
|
||||
res.json(jsonExpanded)
|
||||
@@ -358,7 +363,7 @@ class CollectionController {
|
||||
|
||||
let jsonExpanded = null
|
||||
if (hasUpdated) {
|
||||
await Database.collectionBookModel.bulkCreate(collectionBooksToAdd)
|
||||
await Database.collectionBookModel.bulkCreate(collectionBooksToAdd, { ignoreDuplicates: true })
|
||||
|
||||
jsonExpanded = await req.collection.getOldJsonExpanded(req.user)
|
||||
if (jsonExpanded) SocketAuthority.clientEmitter(req.user.id, 'collection_updated', jsonExpanded)
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -188,7 +188,7 @@ class ApiRouter {
|
||||
this.router.post('/me/item/:id/bookmark', MeController.createBookmark.bind(this))
|
||||
this.router.patch('/me/item/:id/bookmark', MeController.updateBookmark.bind(this))
|
||||
this.router.delete('/me/item/:id/bookmark/:time', MeController.removeBookmark.bind(this))
|
||||
this.router.patch('/me/password', this.auth.authRateLimiter, MeController.updatePassword.bind(this))
|
||||
this.router.patch('/me/password', this.auth.passwordChangeRateLimiter, MeController.updatePassword.bind(this))
|
||||
this.router.get('/me/items-in-progress', MeController.getAllLibraryItemsInProgress.bind(this))
|
||||
this.router.get('/me/series/:id/remove-from-continue-listening', MeController.removeSeriesFromContinueListening.bind(this))
|
||||
this.router.get('/me/series/:id/readd-to-continue-listening', MeController.readdSeriesFromContinueListening.bind(this))
|
||||
|
||||
+46
-21
@@ -260,16 +260,21 @@ class Scanner {
|
||||
// Update filter data
|
||||
Database.addAuthorToFilterData(libraryItem.libraryId, author.name, author.id)
|
||||
}
|
||||
await Database.bookAuthorModel
|
||||
.create({
|
||||
let bookAuthorCreated = true
|
||||
try {
|
||||
await Database.bookAuthorModel.create({
|
||||
authorId: author.id,
|
||||
bookId: libraryItem.media.id
|
||||
})
|
||||
.then(() => {
|
||||
Logger.info(`[Scanner] quickMatchBookBuildUpdatePayload: Added author "${author.name}" to "${libraryItem.media.title}"`)
|
||||
libraryItem.media.authors.push(author)
|
||||
hasAuthorUpdates = true
|
||||
})
|
||||
} catch (error) {
|
||||
if (error.name !== 'SequelizeUniqueConstraintError') throw error
|
||||
bookAuthorCreated = false
|
||||
}
|
||||
libraryItem.media.authors.push(author)
|
||||
hasAuthorUpdates = true
|
||||
if (bookAuthorCreated) {
|
||||
Logger.info(`[Scanner] quickMatchBookBuildUpdatePayload: Added author "${author.name}" to "${libraryItem.media.title}"`)
|
||||
}
|
||||
}
|
||||
const authorsRemoved = libraryItem.media.authors.filter((a) => !matchData.author.find((ma) => ma.toLowerCase() === a.name.toLowerCase()))
|
||||
if (authorsRemoved.length) {
|
||||
@@ -306,23 +311,43 @@ class Scanner {
|
||||
} else {
|
||||
let seriesItem = await Database.seriesModel.getByNameAndLibrary(seriesMatchItem.series, libraryItem.libraryId)
|
||||
if (!seriesItem) {
|
||||
seriesItem = await Database.seriesModel.create({
|
||||
name: seriesMatchItem.series,
|
||||
nameIgnorePrefix: getTitleIgnorePrefix(seriesMatchItem.series),
|
||||
libraryId: libraryItem.libraryId
|
||||
})
|
||||
// Update filter data
|
||||
Database.addSeriesToFilterData(libraryItem.libraryId, seriesItem.name, seriesItem.id)
|
||||
SocketAuthority.emitter('series_added', seriesItem.toOldJSON())
|
||||
let seriesCreated = false
|
||||
try {
|
||||
seriesItem = await Database.seriesModel.create({
|
||||
name: seriesMatchItem.series,
|
||||
nameIgnorePrefix: getTitleIgnorePrefix(seriesMatchItem.series),
|
||||
libraryId: libraryItem.libraryId
|
||||
})
|
||||
seriesCreated = true
|
||||
} catch (error) {
|
||||
if (error.name !== 'SequelizeUniqueConstraintError') throw error
|
||||
seriesItem = await Database.seriesModel.findOne({
|
||||
where: { name: seriesMatchItem.series, libraryId: libraryItem.libraryId }
|
||||
})
|
||||
if (!seriesItem) throw error
|
||||
}
|
||||
if (seriesCreated) {
|
||||
// Update filter data
|
||||
Database.addSeriesToFilterData(libraryItem.libraryId, seriesItem.name, seriesItem.id)
|
||||
SocketAuthority.emitter('series_added', seriesItem.toOldJSON())
|
||||
}
|
||||
}
|
||||
let bookSeries
|
||||
try {
|
||||
bookSeries = await Database.bookSeriesModel.create({
|
||||
seriesId: seriesItem.id,
|
||||
bookId: libraryItem.media.id,
|
||||
sequence: seriesMatchItem.sequence
|
||||
})
|
||||
Logger.info(`[Scanner] quickMatchBookBuildUpdatePayload: Added series "${seriesItem.name}" to "${libraryItem.media.title}"`)
|
||||
} catch (error) {
|
||||
if (error.name !== 'SequelizeUniqueConstraintError') throw error
|
||||
bookSeries = await Database.bookSeriesModel.findOne({
|
||||
where: { seriesId: seriesItem.id, bookId: libraryItem.media.id }
|
||||
})
|
||||
}
|
||||
const bookSeries = await Database.bookSeriesModel.create({
|
||||
seriesId: seriesItem.id,
|
||||
bookId: libraryItem.media.id,
|
||||
sequence: seriesMatchItem.sequence
|
||||
})
|
||||
seriesItem.bookSeries = bookSeries
|
||||
libraryItem.media.series.push(seriesItem)
|
||||
Logger.info(`[Scanner] quickMatchBookBuildUpdatePayload: Added series "${seriesItem.name}" to "${libraryItem.media.title}"`)
|
||||
hasSeriesUpdates = true
|
||||
}
|
||||
const seriesRemoved = libraryItem.media.series.filter((s) => !matchData.series.find((ms) => ms.series.toLowerCase() === s.name.toLowerCase()))
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
const { rateLimit, RateLimitRequestHandler } = require('express-rate-limit')
|
||||
const Logger = require('../Logger')
|
||||
const requestIp = require('../libs/requestIp')
|
||||
|
||||
/**
|
||||
* Factory for creating authentication rate limiters
|
||||
@@ -8,9 +7,96 @@ const requestIp = require('../libs/requestIp')
|
||||
class RateLimiterFactory {
|
||||
static DEFAULT_WINDOW_MS = 10 * 60 * 1000 // 10 minutes
|
||||
static DEFAULT_MAX = 40 // 40 attempts
|
||||
static LOCAL_LOGIN_KEY = 'local-login'
|
||||
static UNKNOWN_USER_KEY = 'unknown-user'
|
||||
|
||||
constructor() {
|
||||
this.authRateLimiter = null
|
||||
this.passwordChangeRateLimiter = null
|
||||
this.disabledLimiter = null
|
||||
}
|
||||
|
||||
/**
|
||||
* Shared window, max, and message. Null when rate limiting is disabled.
|
||||
* @returns {{ windowMs: number, max: number, message: string } | null}
|
||||
*/
|
||||
getLimiterConfig() {
|
||||
// Disable by setting max to 0
|
||||
if (process.env.RATE_LIMIT_AUTH_MAX === '0') {
|
||||
return null
|
||||
}
|
||||
|
||||
let windowMs = RateLimiterFactory.DEFAULT_WINDOW_MS
|
||||
if (parseInt(process.env.RATE_LIMIT_AUTH_WINDOW) > 0) {
|
||||
windowMs = parseInt(process.env.RATE_LIMIT_AUTH_WINDOW)
|
||||
}
|
||||
|
||||
let max = RateLimiterFactory.DEFAULT_MAX
|
||||
if (parseInt(process.env.RATE_LIMIT_AUTH_MAX) > 0) {
|
||||
max = parseInt(process.env.RATE_LIMIT_AUTH_MAX)
|
||||
}
|
||||
|
||||
let message = 'Too many authentication requests'
|
||||
if (process.env.RATE_LIMIT_AUTH_MESSAGE) {
|
||||
message = process.env.RATE_LIMIT_AUTH_MESSAGE
|
||||
}
|
||||
|
||||
return { windowMs, max, message }
|
||||
}
|
||||
|
||||
/**
|
||||
* Log non-default limiter settings once per process.
|
||||
* @param {{ windowMs: number, max: number }} config
|
||||
*/
|
||||
logCustomLimiterConfig(config) {
|
||||
if (this.loggedCustomConfig) return
|
||||
this.loggedCustomConfig = true
|
||||
|
||||
if (config.windowMs !== RateLimiterFactory.DEFAULT_WINDOW_MS) {
|
||||
Logger.info(`[RateLimiterFactory] Authentication rate limiting window set to ${config.windowMs}ms by ENV variable`)
|
||||
}
|
||||
if (config.max !== RateLimiterFactory.DEFAULT_MAX) {
|
||||
Logger.info(`[RateLimiterFactory] Authentication rate limiting max set to ${config.max} by ENV variable`)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {RateLimitRequestHandler}
|
||||
*/
|
||||
getDisabledLimiter() {
|
||||
if (!this.disabledLimiter) {
|
||||
this.disabledLimiter = (req, res, next) => next()
|
||||
}
|
||||
return this.disabledLimiter
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {{ windowMs: number, max: number, message: string }} config
|
||||
* @param {(req: import('express').Request) => string} keyGenerator
|
||||
* @returns {RateLimitRequestHandler}
|
||||
*/
|
||||
createLimiter(config, keyGenerator) {
|
||||
return rateLimit({
|
||||
windowMs: config.windowMs,
|
||||
max: config.max,
|
||||
standardHeaders: true,
|
||||
legacyHeaders: false,
|
||||
keyGenerator,
|
||||
skipSuccessfulRequests: true,
|
||||
handler: (req, res) => {
|
||||
const userAgent = req.get('User-Agent') || 'Unknown'
|
||||
const endpoint = req.path
|
||||
const method = req.method
|
||||
const userId = req.user?.id
|
||||
const userLabel = userId ? `, User: ${userId}` : ''
|
||||
|
||||
Logger.warn(`[RateLimiter] Rate limit exceeded - Endpoint: ${method} ${endpoint}${userLabel}, User-Agent: ${userAgent}`)
|
||||
|
||||
res.status(429).json({
|
||||
error: config.message
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -22,61 +108,49 @@ class RateLimiterFactory {
|
||||
return this.authRateLimiter
|
||||
}
|
||||
|
||||
// Disable by setting max to 0
|
||||
if (process.env.RATE_LIMIT_AUTH_MAX === '0') {
|
||||
this.authRateLimiter = (req, res, next) => next()
|
||||
const config = this.getLimiterConfig()
|
||||
if (!config) {
|
||||
Logger.info(`[RateLimiterFactory] Authentication rate limiting disabled by ENV variable`)
|
||||
this.authRateLimiter = this.getDisabledLimiter()
|
||||
return this.authRateLimiter
|
||||
}
|
||||
|
||||
let windowMs = RateLimiterFactory.DEFAULT_WINDOW_MS
|
||||
if (parseInt(process.env.RATE_LIMIT_AUTH_WINDOW) > 0) {
|
||||
windowMs = parseInt(process.env.RATE_LIMIT_AUTH_WINDOW)
|
||||
if (windowMs !== RateLimiterFactory.DEFAULT_WINDOW_MS) {
|
||||
Logger.info(`[RateLimiterFactory] Authentication rate limiting window set to ${windowMs}ms by ENV variable`)
|
||||
}
|
||||
}
|
||||
this.logCustomLimiterConfig(config)
|
||||
|
||||
let max = RateLimiterFactory.DEFAULT_MAX
|
||||
if (parseInt(process.env.RATE_LIMIT_AUTH_MAX) > 0) {
|
||||
max = parseInt(process.env.RATE_LIMIT_AUTH_MAX)
|
||||
if (max !== RateLimiterFactory.DEFAULT_MAX) {
|
||||
Logger.info(`[RateLimiterFactory] Authentication rate limiting max set to ${max} by ENV variable`)
|
||||
}
|
||||
}
|
||||
// One process-wide bucket of failed local logins. Do not key on client IP headers.
|
||||
this.authRateLimiter = this.createLimiter(config, () => RateLimiterFactory.LOCAL_LOGIN_KEY)
|
||||
|
||||
let message = 'Too many authentication requests'
|
||||
if (process.env.RATE_LIMIT_AUTH_MESSAGE) {
|
||||
message = process.env.RATE_LIMIT_AUTH_MESSAGE
|
||||
}
|
||||
|
||||
this.authRateLimiter = rateLimit({
|
||||
windowMs,
|
||||
max,
|
||||
standardHeaders: true,
|
||||
legacyHeaders: false,
|
||||
keyGenerator: (req) => {
|
||||
// Override keyGenerator to handle proxy IPs
|
||||
return requestIp.getClientIp(req) || req.ip
|
||||
},
|
||||
handler: (req, res) => {
|
||||
const userAgent = req.get('User-Agent') || 'Unknown'
|
||||
const endpoint = req.path
|
||||
const method = req.method
|
||||
const ip = requestIp.getClientIp(req) || req.ip
|
||||
|
||||
Logger.warn(`[RateLimiter] Rate limit exceeded - IP: ${ip}, Endpoint: ${method} ${endpoint}, User-Agent: ${userAgent}`)
|
||||
|
||||
res.status(429).json({
|
||||
error: message
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
Logger.debug(`[RateLimiterFactory] Created auth rate limiter: ${max} attempts per ${windowMs / 1000 / 60} minutes`)
|
||||
Logger.debug(`[RateLimiterFactory] Created auth rate limiter: ${config.max} failed attempts per ${config.windowMs / 1000 / 60} minutes`)
|
||||
|
||||
return this.authRateLimiter
|
||||
}
|
||||
|
||||
/**
|
||||
* Failed current-password checks on PATCH /me/password, keyed by the authenticated user.
|
||||
* @returns {RateLimitRequestHandler}
|
||||
*/
|
||||
getPasswordChangeRateLimiter() {
|
||||
if (this.passwordChangeRateLimiter) {
|
||||
return this.passwordChangeRateLimiter
|
||||
}
|
||||
|
||||
const config = this.getLimiterConfig()
|
||||
if (!config) {
|
||||
this.passwordChangeRateLimiter = this.getDisabledLimiter()
|
||||
return this.passwordChangeRateLimiter
|
||||
}
|
||||
|
||||
this.logCustomLimiterConfig(config)
|
||||
|
||||
this.passwordChangeRateLimiter = this.createLimiter(config, (req) => {
|
||||
const userId = req.user?.id
|
||||
return userId ? String(userId) : RateLimiterFactory.UNKNOWN_USER_KEY
|
||||
})
|
||||
|
||||
Logger.debug(`[RateLimiterFactory] Created password change rate limiter: ${config.max} failed attempts per ${config.windowMs / 1000 / 60} minutes`)
|
||||
|
||||
return this.passwordChangeRateLimiter
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new RateLimiterFactory()
|
||||
@@ -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>')
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,113 @@
|
||||
const { expect } = require('chai')
|
||||
const { Sequelize } = require('sequelize')
|
||||
const sinon = require('sinon')
|
||||
|
||||
const Database = require('../../../server/Database')
|
||||
const Logger = require('../../../server/Logger')
|
||||
const CollectionController = require('../../../server/controllers/CollectionController')
|
||||
|
||||
describe('CollectionController', () => {
|
||||
/** @type {import('../../../server/models/Collection')} */
|
||||
let collection
|
||||
/** @type {import('../../../server/models/LibraryItem')} */
|
||||
let libraryItem
|
||||
|
||||
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')
|
||||
sinon.stub(Logger, 'warn')
|
||||
sinon.stub(Logger, 'error')
|
||||
|
||||
const library = await Database.libraryModel.create({ name: 'Test Library', mediaType: 'book' })
|
||||
const libraryFolder = await Database.libraryFolderModel.create({ path: '/test-lib', libraryId: library.id })
|
||||
const book = await Database.bookModel.create({ title: 'Test Book', audioFiles: [], tags: [], narrators: [], genres: [], chapters: [] })
|
||||
libraryItem = await Database.libraryItemModel.create({
|
||||
path: '/test-lib/book',
|
||||
isFile: false,
|
||||
libraryFiles: [],
|
||||
mediaId: book.id,
|
||||
mediaType: 'book',
|
||||
libraryId: library.id,
|
||||
libraryFolderId: libraryFolder.id
|
||||
})
|
||||
collection = await Database.collectionModel.create({ name: 'Test Collection', libraryId: library.id })
|
||||
await Database.collectionBookModel.create({ collectionId: collection.id, bookId: book.id, order: 1 })
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
sinon.restore()
|
||||
await Database.sequelize.sync({ force: true })
|
||||
})
|
||||
|
||||
it('does not throw when the collectionBook link already exists', async () => {
|
||||
sinon.stub(collection, 'getCollectionBooks').resolves([])
|
||||
|
||||
const res = {
|
||||
statusCode: null,
|
||||
body: null,
|
||||
status(code) {
|
||||
this.statusCode = code
|
||||
return this
|
||||
},
|
||||
send(body) {
|
||||
this.body = body
|
||||
return this
|
||||
},
|
||||
json(body) {
|
||||
this.body = body
|
||||
return this
|
||||
}
|
||||
}
|
||||
|
||||
await CollectionController.addBook({ body: { id: libraryItem.id }, collection, user: { id: 'user-id' } }, res)
|
||||
|
||||
expect(res.statusCode).to.equal(400)
|
||||
expect(res.body).to.equal('Book already in collection')
|
||||
const links = await Database.collectionBookModel.findAll({ where: { collectionId: collection.id, bookId: libraryItem.mediaId } })
|
||||
expect(links).to.have.lengthOf(1)
|
||||
})
|
||||
|
||||
it('does not throw when a batch includes a book already in the collection', async () => {
|
||||
const book = await Database.bookModel.create({ title: 'Second Book', audioFiles: [], tags: [], narrators: [], genres: [], chapters: [] })
|
||||
const secondItem = await Database.libraryItemModel.create({
|
||||
path: '/test-lib/book-2',
|
||||
isFile: false,
|
||||
libraryFiles: [],
|
||||
mediaId: book.id,
|
||||
mediaType: 'book',
|
||||
libraryId: libraryItem.libraryId,
|
||||
libraryFolderId: libraryItem.libraryFolderId
|
||||
})
|
||||
sinon.stub(collection, 'getCollectionBooks').resolves([])
|
||||
sinon.stub(collection, 'getOldJsonExpanded').resolves(null)
|
||||
|
||||
const res = {
|
||||
statusCode: null,
|
||||
body: null,
|
||||
status(code) {
|
||||
this.statusCode = code
|
||||
return this
|
||||
},
|
||||
send(body) {
|
||||
this.body = body
|
||||
return this
|
||||
},
|
||||
json(body) {
|
||||
this.body = body
|
||||
return this
|
||||
}
|
||||
}
|
||||
|
||||
await CollectionController.addBatch({ body: { books: [libraryItem.id, secondItem.id] }, collection, user: { id: 'user-id' } }, res)
|
||||
|
||||
const existingLinks = await Database.collectionBookModel.findAll({ where: { collectionId: collection.id, bookId: libraryItem.mediaId } })
|
||||
expect(existingLinks).to.have.lengthOf(1)
|
||||
const newLinks = await Database.collectionBookModel.findAll({ where: { collectionId: collection.id, bookId: secondItem.mediaId } })
|
||||
expect(newLinks).to.have.lengthOf(1)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,120 @@
|
||||
const { expect } = require('chai')
|
||||
const { Sequelize } = require('sequelize')
|
||||
const sinon = require('sinon')
|
||||
|
||||
const Database = require('../../../server/Database')
|
||||
const Logger = require('../../../server/Logger')
|
||||
const Scanner = require('../../../server/scanner/Scanner')
|
||||
|
||||
describe('Scanner.quickMatchBookBuildUpdatePayload', () => {
|
||||
/** @type {import('../../../server/models/Library')} */
|
||||
let library
|
||||
/** @type {import('../../../server/models/LibraryItem')} */
|
||||
let libraryItem
|
||||
/** @type {import('../../../server/models/Book')} */
|
||||
let book
|
||||
|
||||
const apiRouterCtx = {
|
||||
checkRemoveAuthorsWithNoBooks: sinon.stub().resolves(),
|
||||
checkRemoveEmptySeries: sinon.stub().resolves()
|
||||
}
|
||||
|
||||
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')
|
||||
sinon.stub(Logger, 'warn')
|
||||
sinon.stub(Logger, 'error')
|
||||
|
||||
library = await Database.libraryModel.create({ name: 'Test Library', mediaType: 'book' })
|
||||
const libraryFolder = await Database.libraryFolderModel.create({ path: '/test', libraryId: library.id })
|
||||
book = await Database.bookModel.create({ title: 'Test Book', audioFiles: [], tags: [], narrators: [], genres: [], chapters: [] })
|
||||
libraryItem = await Database.libraryItemModel.create({
|
||||
libraryFiles: [],
|
||||
mediaId: book.id,
|
||||
mediaType: 'book',
|
||||
libraryId: library.id,
|
||||
libraryFolderId: libraryFolder.id
|
||||
})
|
||||
libraryItem.media = book
|
||||
// Stale in-memory associations: DB may already have links from a concurrent quick match
|
||||
book.authors = []
|
||||
book.series = []
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
sinon.restore()
|
||||
await Database.sequelize.sync({ force: true })
|
||||
})
|
||||
|
||||
it('does not throw when bookSeries link already exists from a concurrent quick match', async () => {
|
||||
const series = await Database.seriesModel.create({ name: 'Crescent City', libraryId: library.id })
|
||||
await Database.bookSeriesModel.create({ bookId: book.id, seriesId: series.id, sequence: '3' })
|
||||
|
||||
const result = await Scanner.quickMatchBookBuildUpdatePayload(
|
||||
apiRouterCtx,
|
||||
libraryItem,
|
||||
{
|
||||
series: [{ series: 'Crescent City', sequence: '3' }]
|
||||
},
|
||||
{}
|
||||
)
|
||||
|
||||
expect(result.hasSeriesUpdates).to.be.true
|
||||
expect(libraryItem.media.series).to.have.lengthOf(1)
|
||||
expect(libraryItem.media.series[0].id).to.equal(series.id)
|
||||
expect(libraryItem.media.series[0].bookSeries).to.exist
|
||||
expect(libraryItem.media.series[0].bookSeries.sequence).to.equal('3')
|
||||
|
||||
const links = await Database.bookSeriesModel.findAll({ where: { bookId: book.id, seriesId: series.id } })
|
||||
expect(links).to.have.lengthOf(1)
|
||||
})
|
||||
|
||||
it('reuses an existing series when create hits the name unique constraint', async () => {
|
||||
const series = await Database.seriesModel.create({ name: 'Crescent City', libraryId: library.id })
|
||||
sinon.stub(Database.seriesModel, 'getByNameAndLibrary').resolves(null)
|
||||
|
||||
const result = await Scanner.quickMatchBookBuildUpdatePayload(
|
||||
apiRouterCtx,
|
||||
libraryItem,
|
||||
{
|
||||
series: [{ series: 'Crescent City', sequence: '3' }]
|
||||
},
|
||||
{}
|
||||
)
|
||||
|
||||
expect(result.hasSeriesUpdates).to.be.true
|
||||
expect(libraryItem.media.series).to.have.lengthOf(1)
|
||||
expect(libraryItem.media.series[0].id).to.equal(series.id)
|
||||
|
||||
const seriesRows = await Database.seriesModel.findAll({ where: { libraryId: library.id, name: 'Crescent City' } })
|
||||
expect(seriesRows).to.have.lengthOf(1)
|
||||
const links = await Database.bookSeriesModel.findAll({ where: { bookId: book.id, seriesId: series.id } })
|
||||
expect(links).to.have.lengthOf(1)
|
||||
})
|
||||
|
||||
it('does not throw when bookAuthor link already exists from a concurrent quick match', async () => {
|
||||
const author = await Database.authorModel.create({ name: 'Sarah Maas', libraryId: library.id })
|
||||
await Database.bookAuthorModel.create({ bookId: book.id, authorId: author.id })
|
||||
|
||||
const result = await Scanner.quickMatchBookBuildUpdatePayload(
|
||||
apiRouterCtx,
|
||||
libraryItem,
|
||||
{
|
||||
author: ['Sarah Maas']
|
||||
},
|
||||
{}
|
||||
)
|
||||
|
||||
expect(result.hasAuthorUpdates).to.be.true
|
||||
expect(libraryItem.media.authors).to.have.lengthOf(1)
|
||||
expect(libraryItem.media.authors[0].id).to.equal(author.id)
|
||||
|
||||
const links = await Database.bookAuthorModel.findAll({ where: { bookId: book.id, authorId: author.id } })
|
||||
expect(links).to.have.lengthOf(1)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,266 @@
|
||||
const { expect } = require('chai')
|
||||
const express = require('express')
|
||||
const http = require('http')
|
||||
const sinon = require('sinon')
|
||||
|
||||
const Logger = require('../../../server/Logger')
|
||||
const RateLimiterFactory = require('../../../server/utils/rateLimiterFactory')
|
||||
|
||||
function listen(app) {
|
||||
const server = http.createServer(app)
|
||||
return new Promise((resolve) => {
|
||||
server.listen(0, '127.0.0.1', () => {
|
||||
resolve({ server, port: server.address().port })
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function closeServer(server) {
|
||||
return new Promise((resolve, reject) => {
|
||||
server.close((err) => (err ? reject(err) : resolve()))
|
||||
})
|
||||
}
|
||||
|
||||
function patchPassword(port, { userId, headers = {} } = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const body = JSON.stringify({ password: 'current', newPassword: 'next' })
|
||||
const req = http.request(
|
||||
{
|
||||
hostname: '127.0.0.1',
|
||||
port,
|
||||
path: '/me/password',
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Content-Length': Buffer.byteLength(body),
|
||||
'User-Agent': 'rate-limiter-test',
|
||||
'x-user-id': userId,
|
||||
...headers
|
||||
}
|
||||
},
|
||||
(res) => {
|
||||
res.resume()
|
||||
res.on('end', () => resolve({ status: res.statusCode }))
|
||||
}
|
||||
)
|
||||
req.on('error', reject)
|
||||
req.write(body)
|
||||
req.end()
|
||||
})
|
||||
}
|
||||
|
||||
function passwordChangeApp(limiter) {
|
||||
const app = express()
|
||||
app.patch(
|
||||
'/me/password',
|
||||
(req, res, next) => {
|
||||
const userId = req.get('x-user-id')
|
||||
if (userId) req.user = { id: userId }
|
||||
next()
|
||||
},
|
||||
limiter,
|
||||
(req, res) => {
|
||||
res.status(400).send('Invalid password')
|
||||
}
|
||||
)
|
||||
return app
|
||||
}
|
||||
|
||||
function postLogin(port, { headers = {} } = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const body = JSON.stringify({ username: 'alice', password: 'guess' })
|
||||
const req = http.request(
|
||||
{
|
||||
hostname: '127.0.0.1',
|
||||
port,
|
||||
path: '/login',
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Content-Length': Buffer.byteLength(body),
|
||||
'User-Agent': 'rate-limiter-test',
|
||||
...headers
|
||||
}
|
||||
},
|
||||
(res) => {
|
||||
let data = ''
|
||||
res.on('data', (chunk) => {
|
||||
data += chunk
|
||||
})
|
||||
res.on('end', () => {
|
||||
resolve({ status: res.statusCode, body: data, headers: res.headers })
|
||||
})
|
||||
}
|
||||
)
|
||||
req.on('error', reject)
|
||||
req.write(body)
|
||||
req.end()
|
||||
})
|
||||
}
|
||||
|
||||
describe('rateLimiterFactory', () => {
|
||||
const previousMax = process.env.RATE_LIMIT_AUTH_MAX
|
||||
const previousWindow = process.env.RATE_LIMIT_AUTH_WINDOW
|
||||
const previousMessage = process.env.RATE_LIMIT_AUTH_MESSAGE
|
||||
|
||||
beforeEach(() => {
|
||||
RateLimiterFactory.authRateLimiter = null
|
||||
RateLimiterFactory.passwordChangeRateLimiter = null
|
||||
process.env.RATE_LIMIT_AUTH_MAX = '3'
|
||||
process.env.RATE_LIMIT_AUTH_WINDOW = '60000'
|
||||
delete process.env.RATE_LIMIT_AUTH_MESSAGE
|
||||
sinon.stub(Logger, 'info')
|
||||
sinon.stub(Logger, 'warn')
|
||||
sinon.stub(Logger, 'debug')
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
RateLimiterFactory.authRateLimiter = null
|
||||
RateLimiterFactory.passwordChangeRateLimiter = null
|
||||
if (previousMax === undefined) delete process.env.RATE_LIMIT_AUTH_MAX
|
||||
else process.env.RATE_LIMIT_AUTH_MAX = previousMax
|
||||
if (previousWindow === undefined) delete process.env.RATE_LIMIT_AUTH_WINDOW
|
||||
else process.env.RATE_LIMIT_AUTH_WINDOW = previousWindow
|
||||
if (previousMessage === undefined) delete process.env.RATE_LIMIT_AUTH_MESSAGE
|
||||
else process.env.RATE_LIMIT_AUTH_MESSAGE = previousMessage
|
||||
sinon.restore()
|
||||
})
|
||||
|
||||
it('shares one bucket across rotating client IP headers and returns 429 after max failures', async () => {
|
||||
const app = express()
|
||||
const limiter = RateLimiterFactory.getAuthRateLimiter()
|
||||
app.post('/login', limiter, (req, res) => {
|
||||
res.status(401).json({ error: 'Unauthorized' })
|
||||
})
|
||||
|
||||
const { server, port } = await listen(app)
|
||||
try {
|
||||
const statuses = []
|
||||
for (let i = 0; i < 4; i++) {
|
||||
const result = await postLogin(port, {
|
||||
headers: {
|
||||
'X-Client-IP': `10.0.0.${i}`,
|
||||
'X-Forwarded-For': `203.0.113.${i}`
|
||||
}
|
||||
})
|
||||
statuses.push(result.status)
|
||||
}
|
||||
|
||||
expect(statuses).to.deep.equal([401, 401, 401, 429])
|
||||
} finally {
|
||||
await closeServer(server)
|
||||
}
|
||||
})
|
||||
|
||||
it('does not consume a slot for successful logins', async () => {
|
||||
const app = express()
|
||||
const limiter = RateLimiterFactory.getAuthRateLimiter()
|
||||
let attempt = 0
|
||||
app.post('/login', limiter, (req, res) => {
|
||||
attempt += 1
|
||||
if (attempt === 1) {
|
||||
return res.status(200).json({ ok: true })
|
||||
}
|
||||
res.status(401).json({ error: 'Unauthorized' })
|
||||
})
|
||||
|
||||
const { server, port } = await listen(app)
|
||||
try {
|
||||
const success = await postLogin(port)
|
||||
expect(success.status).to.equal(200)
|
||||
|
||||
const statuses = []
|
||||
for (let i = 0; i < 4; i++) {
|
||||
const result = await postLogin(port, {
|
||||
headers: {
|
||||
'X-Client-IP': `10.1.0.${i}`
|
||||
}
|
||||
})
|
||||
statuses.push(result.status)
|
||||
}
|
||||
|
||||
expect(statuses).to.deep.equal([401, 401, 401, 429])
|
||||
} finally {
|
||||
await closeServer(server)
|
||||
}
|
||||
})
|
||||
|
||||
it('limits password changes per user and ignores client IP headers', async () => {
|
||||
const app = passwordChangeApp(RateLimiterFactory.getPasswordChangeRateLimiter())
|
||||
const { server, port } = await listen(app)
|
||||
try {
|
||||
const alice = []
|
||||
for (let i = 0; i < 4; i++) {
|
||||
const result = await patchPassword(port, {
|
||||
userId: 'alice',
|
||||
headers: {
|
||||
'X-Client-IP': `10.2.0.${i}`,
|
||||
'X-Forwarded-For': `198.51.100.${i}`
|
||||
}
|
||||
})
|
||||
alice.push(result.status)
|
||||
}
|
||||
const bob = await patchPassword(port, { userId: 'bob' })
|
||||
|
||||
expect(alice).to.deep.equal([400, 400, 400, 429])
|
||||
expect(bob.status).to.equal(400)
|
||||
expect(Logger.warn.calledWithMatch(/User: alice/)).to.equal(true)
|
||||
} finally {
|
||||
await closeServer(server)
|
||||
}
|
||||
})
|
||||
|
||||
it('does not consume a password-change slot for a successful update', async () => {
|
||||
let attempt = 0
|
||||
const app = express()
|
||||
const limiter = RateLimiterFactory.getPasswordChangeRateLimiter()
|
||||
app.patch(
|
||||
'/me/password',
|
||||
(req, res, next) => {
|
||||
req.user = { id: 'alice' }
|
||||
next()
|
||||
},
|
||||
limiter,
|
||||
(req, res) => {
|
||||
attempt += 1
|
||||
res.status(attempt === 1 ? 200 : 400).send(attempt === 1 ? 'ok' : 'Invalid password')
|
||||
}
|
||||
)
|
||||
|
||||
const { server, port } = await listen(app)
|
||||
try {
|
||||
const success = await patchPassword(port, { userId: 'alice' })
|
||||
expect(success.status).to.equal(200)
|
||||
|
||||
const statuses = []
|
||||
for (let i = 0; i < 4; i++) {
|
||||
const result = await patchPassword(port, { userId: 'alice' })
|
||||
statuses.push(result.status)
|
||||
}
|
||||
expect(statuses).to.deep.equal([400, 400, 400, 429])
|
||||
} finally {
|
||||
await closeServer(server)
|
||||
}
|
||||
})
|
||||
|
||||
it('can be disabled with RATE_LIMIT_AUTH_MAX=0', async () => {
|
||||
RateLimiterFactory.authRateLimiter = null
|
||||
process.env.RATE_LIMIT_AUTH_MAX = '0'
|
||||
|
||||
const app = express()
|
||||
const limiter = RateLimiterFactory.getAuthRateLimiter()
|
||||
app.post('/login', limiter, (req, res) => {
|
||||
res.status(401).json({ error: 'Unauthorized' })
|
||||
})
|
||||
|
||||
const { server, port } = await listen(app)
|
||||
try {
|
||||
for (let i = 0; i < 5; i++) {
|
||||
const result = await postLogin(port)
|
||||
expect(result.status).to.equal(401)
|
||||
}
|
||||
} finally {
|
||||
await closeServer(server)
|
||||
}
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user