2020-01-31 16:56:52 +01:00
|
|
|
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
|
2018-03-29 10:58:24 +02:00
|
|
|
|
2021-07-13 11:05:15 +02:00
|
|
|
import { expect } from 'chai'
|
2023-07-31 14:34:36 +02:00
|
|
|
import { wait } from '@peertube/peertube-core-utils'
|
|
|
|
import { HttpStatusCode } from '@peertube/peertube-models'
|
|
|
|
import { cleanupTests, createSingleServer, PeerTubeServer, setAccessTokensToServers } from '@peertube/peertube-server-commands'
|
2018-03-29 10:58:24 +02:00
|
|
|
|
|
|
|
describe('Test application behind a reverse proxy', function () {
|
2021-07-16 09:47:51 +02:00
|
|
|
let server: PeerTubeServer
|
2022-05-30 11:33:38 +02:00
|
|
|
let userAccessToken: string
|
2021-07-15 10:02:54 +02:00
|
|
|
let videoId: string
|
2018-03-29 10:58:24 +02:00
|
|
|
|
|
|
|
before(async function () {
|
2022-09-12 08:29:01 +02:00
|
|
|
this.timeout(60000)
|
2019-07-04 16:42:40 +02:00
|
|
|
|
|
|
|
const config = {
|
|
|
|
rates_limit: {
|
|
|
|
api: {
|
|
|
|
max: 50,
|
|
|
|
window: 5000
|
|
|
|
},
|
|
|
|
signup: {
|
|
|
|
max: 3,
|
|
|
|
window: 5000
|
|
|
|
},
|
|
|
|
login: {
|
|
|
|
max: 20
|
|
|
|
}
|
|
|
|
},
|
|
|
|
signup: {
|
|
|
|
limit: 20
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 09:47:51 +02:00
|
|
|
server = await createSingleServer(1, config)
|
2018-03-29 10:58:24 +02:00
|
|
|
await setAccessTokensToServers([ server ])
|
|
|
|
|
2022-05-30 11:33:38 +02:00
|
|
|
userAccessToken = await server.users.generateUserAndToken('user')
|
|
|
|
|
2021-07-16 09:04:35 +02:00
|
|
|
const { uuid } = await server.videos.upload()
|
2021-07-15 10:02:54 +02:00
|
|
|
videoId = uuid
|
2018-03-29 10:58:24 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should view a video only once with the same IP by default', async function () {
|
2022-09-12 08:29:01 +02:00
|
|
|
this.timeout(40000)
|
2018-08-29 16:26:25 +02:00
|
|
|
|
2022-03-24 13:36:47 +01:00
|
|
|
await server.views.simulateView({ id: videoId })
|
|
|
|
await server.views.simulateView({ id: videoId })
|
2018-03-29 10:58:24 +02:00
|
|
|
|
2018-08-29 16:26:25 +02:00
|
|
|
// Wait the repeatable job
|
|
|
|
await wait(8000)
|
|
|
|
|
2021-07-16 09:04:35 +02:00
|
|
|
const video = await server.videos.get({ id: videoId })
|
2021-07-15 10:02:54 +02:00
|
|
|
expect(video.views).to.equal(1)
|
2018-03-29 10:58:24 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should view a video 2 times with the X-Forwarded-For header set', async function () {
|
2018-08-29 16:26:25 +02:00
|
|
|
this.timeout(20000)
|
|
|
|
|
2022-03-24 13:36:47 +01:00
|
|
|
await server.views.simulateView({ id: videoId, xForwardedFor: '0.0.0.1,127.0.0.1' })
|
|
|
|
await server.views.simulateView({ id: videoId, xForwardedFor: '0.0.0.2,127.0.0.1' })
|
2018-03-29 10:58:24 +02:00
|
|
|
|
2018-08-29 16:26:25 +02:00
|
|
|
// Wait the repeatable job
|
|
|
|
await wait(8000)
|
|
|
|
|
2021-07-16 09:04:35 +02:00
|
|
|
const video = await server.videos.get({ id: videoId })
|
2021-07-15 10:02:54 +02:00
|
|
|
expect(video.views).to.equal(3)
|
2018-03-29 10:58:24 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should view a video only once with the same client IP in the X-Forwarded-For header', async function () {
|
2018-08-29 16:26:25 +02:00
|
|
|
this.timeout(20000)
|
|
|
|
|
2022-03-24 13:36:47 +01:00
|
|
|
await server.views.simulateView({ id: videoId, xForwardedFor: '0.0.0.4,0.0.0.3,::ffff:127.0.0.1' })
|
|
|
|
await server.views.simulateView({ id: videoId, xForwardedFor: '0.0.0.5,0.0.0.3,127.0.0.1' })
|
2018-03-29 10:58:24 +02:00
|
|
|
|
2018-08-29 16:26:25 +02:00
|
|
|
// Wait the repeatable job
|
|
|
|
await wait(8000)
|
|
|
|
|
2021-07-16 09:04:35 +02:00
|
|
|
const video = await server.videos.get({ id: videoId })
|
2021-07-15 10:02:54 +02:00
|
|
|
expect(video.views).to.equal(4)
|
2018-03-29 10:58:24 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should view a video two times with a different client IP in the X-Forwarded-For header', async function () {
|
2018-08-29 16:26:25 +02:00
|
|
|
this.timeout(20000)
|
|
|
|
|
2022-03-24 13:36:47 +01:00
|
|
|
await server.views.simulateView({ id: videoId, xForwardedFor: '0.0.0.8,0.0.0.6,127.0.0.1' })
|
|
|
|
await server.views.simulateView({ id: videoId, xForwardedFor: '0.0.0.8,0.0.0.7,127.0.0.1' })
|
2018-03-29 10:58:24 +02:00
|
|
|
|
2018-08-29 16:26:25 +02:00
|
|
|
// Wait the repeatable job
|
|
|
|
await wait(8000)
|
|
|
|
|
2021-07-16 09:04:35 +02:00
|
|
|
const video = await server.videos.get({ id: videoId })
|
2021-07-15 10:02:54 +02:00
|
|
|
expect(video.views).to.equal(6)
|
2018-03-29 10:58:24 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should rate limit logins', async function () {
|
|
|
|
const user = { username: 'root', password: 'fail' }
|
|
|
|
|
2024-02-12 16:28:18 +01:00
|
|
|
for (let i = 0; i < 18; i++) {
|
|
|
|
await server.login.login({ user, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
|
2018-03-29 10:58:24 +02:00
|
|
|
}
|
|
|
|
|
2021-07-16 09:04:35 +02:00
|
|
|
await server.login.login({ user, expectedStatus: HttpStatusCode.TOO_MANY_REQUESTS_429 })
|
2018-03-29 10:58:24 +02:00
|
|
|
})
|
|
|
|
|
2019-07-04 16:42:40 +02:00
|
|
|
it('Should rate limit signup', async function () {
|
2019-11-27 10:13:31 +01:00
|
|
|
for (let i = 0; i < 10; i++) {
|
|
|
|
try {
|
2023-01-19 09:28:29 +01:00
|
|
|
await server.registrations.register({ username: 'test' + i })
|
2019-11-27 10:13:31 +01:00
|
|
|
} catch {
|
|
|
|
// empty
|
|
|
|
}
|
2019-07-04 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
2023-01-19 09:28:29 +01:00
|
|
|
await server.registrations.register({ username: 'test42', expectedStatus: HttpStatusCode.TOO_MANY_REQUESTS_429 })
|
2019-07-04 16:42:40 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should not rate limit failed signup', async function () {
|
|
|
|
this.timeout(30000)
|
|
|
|
|
|
|
|
await wait(7000)
|
|
|
|
|
|
|
|
for (let i = 0; i < 3; i++) {
|
2023-01-19 09:28:29 +01:00
|
|
|
await server.registrations.register({ username: 'test' + i, expectedStatus: HttpStatusCode.CONFLICT_409 })
|
2019-07-04 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
2023-01-19 09:28:29 +01:00
|
|
|
await server.registrations.register({ username: 'test43', expectedStatus: HttpStatusCode.NO_CONTENT_204 })
|
2019-07-04 16:42:40 +02:00
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should rate limit API calls', async function () {
|
|
|
|
this.timeout(30000)
|
|
|
|
|
|
|
|
await wait(7000)
|
|
|
|
|
2019-09-24 10:19:55 +02:00
|
|
|
for (let i = 0; i < 100; i++) {
|
|
|
|
try {
|
2021-07-16 09:04:35 +02:00
|
|
|
await server.videos.get({ id: videoId })
|
2019-09-24 10:19:55 +02:00
|
|
|
} catch {
|
|
|
|
// don't care if it fails
|
|
|
|
}
|
2019-07-04 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
2021-07-16 09:04:35 +02:00
|
|
|
await server.videos.get({ id: videoId, expectedStatus: HttpStatusCode.TOO_MANY_REQUESTS_429 })
|
2019-07-04 16:42:40 +02:00
|
|
|
})
|
|
|
|
|
2022-05-30 11:33:38 +02:00
|
|
|
it('Should rate limit API calls with a user but not with an admin', async function () {
|
|
|
|
await server.videos.get({ id: videoId, token: userAccessToken, expectedStatus: HttpStatusCode.TOO_MANY_REQUESTS_429 })
|
|
|
|
|
|
|
|
await server.videos.get({ id: videoId, token: server.accessToken, expectedStatus: HttpStatusCode.OK_200 })
|
|
|
|
})
|
|
|
|
|
2019-04-24 15:10:37 +02:00
|
|
|
after(async function () {
|
|
|
|
await cleanupTests([ server ])
|
2018-03-29 10:58:24 +02:00
|
|
|
})
|
|
|
|
})
|