summaryrefslogtreecommitdiff
path: root/packages/backend/test/unit/server/api
diff options
context:
space:
mode:
authorHazelnoot <acomputerdog@gmail.com>2024-12-08 09:46:49 -0500
committerHazelnoot <acomputerdog@gmail.com>2024-12-08 09:46:49 -0500
commit8b091f77ca776c9c7c5279c2f9fa3c41f2958dc3 (patch)
tree53bf330de002b29ee0c9165a92821b5ce8f037ad /packages/backend/test/unit/server/api
parentfix NaN from extremely high rate limits (diff)
downloadsharkey-8b091f77ca776c9c7c5279c2f9fa3c41f2958dc3.tar.gz
sharkey-8b091f77ca776c9c7c5279c2f9fa3c41f2958dc3.tar.bz2
sharkey-8b091f77ca776c9c7c5279c2f9fa3c41f2958dc3.zip
check for invalid rate limit inputs
Diffstat (limited to 'packages/backend/test/unit/server/api')
-rw-r--r--packages/backend/test/unit/server/api/SkRateLimiterServiceTests.ts141
1 files changed, 141 insertions, 0 deletions
diff --git a/packages/backend/test/unit/server/api/SkRateLimiterServiceTests.ts b/packages/backend/test/unit/server/api/SkRateLimiterServiceTests.ts
index 7e0c01f849..2297c2bc03 100644
--- a/packages/backend/test/unit/server/api/SkRateLimiterServiceTests.ts
+++ b/packages/backend/test/unit/server/api/SkRateLimiterServiceTests.ts
@@ -298,6 +298,90 @@ describe(SkRateLimiterService, () => {
expect(counter?.c).toBe(1);
expect(counter?.t).toBe(0);
});
+
+ it('should throw if factor is zero', async () => {
+ const promise = serviceUnderTest().limit(limit, actor, 0);
+
+ await expect(promise).rejects.toThrow(/factor is zero or negative/);
+ });
+
+ it('should throw if factor is negative', async () => {
+ const promise = serviceUnderTest().limit(limit, actor, -1);
+
+ await expect(promise).rejects.toThrow(/factor is zero or negative/);
+ });
+
+ it('should throw if size is zero', async () => {
+ limit.size = 0;
+
+ const promise = serviceUnderTest().limit(limit, actor);
+
+ await expect(promise).rejects.toThrow(/size is less than 1/);
+ });
+
+ it('should throw if size is negative', async () => {
+ limit.size = -1;
+
+ const promise = serviceUnderTest().limit(limit, actor);
+
+ await expect(promise).rejects.toThrow(/size is less than 1/);
+ });
+
+ it('should throw if size is fraction', async () => {
+ limit.size = 0.5;
+
+ const promise = serviceUnderTest().limit(limit, actor);
+
+ await expect(promise).rejects.toThrow(/size is less than 1/);
+ });
+
+ it('should throw if dripRate is zero', async () => {
+ limit.dripRate = 0;
+
+ const promise = serviceUnderTest().limit(limit, actor);
+
+ await expect(promise).rejects.toThrow(/dripRate is less than 1/);
+ });
+
+ it('should throw if dripRate is negative', async () => {
+ limit.dripRate = -1;
+
+ const promise = serviceUnderTest().limit(limit, actor);
+
+ await expect(promise).rejects.toThrow(/dripRate is less than 1/);
+ });
+
+ it('should throw if dripRate is fraction', async () => {
+ limit.dripRate = 0.5;
+
+ const promise = serviceUnderTest().limit(limit, actor);
+
+ await expect(promise).rejects.toThrow(/dripRate is less than 1/);
+ });
+
+ it('should throw if dripSize is zero', async () => {
+ limit.dripSize = 0;
+
+ const promise = serviceUnderTest().limit(limit, actor);
+
+ await expect(promise).rejects.toThrow(/dripSize is less than 1/);
+ });
+
+ it('should throw if dripSize is negative', async () => {
+ limit.dripSize = -1;
+
+ const promise = serviceUnderTest().limit(limit, actor);
+
+ await expect(promise).rejects.toThrow(/dripSize is less than 1/);
+ });
+
+ it('should throw if dripSize is fraction', async () => {
+ limit.dripSize = 0.5;
+
+ const promise = serviceUnderTest().limit(limit, actor);
+
+ await expect(promise).rejects.toThrow(/dripSize is less than 1/);
+ });
});
describe('with min interval', () => {
@@ -451,6 +535,35 @@ describe(SkRateLimiterService, () => {
expect(minCounter?.c).toBe(1);
expect(minCounter?.t).toBe(0);
});
+
+ it('should throw if factor is zero', async () => {
+ const promise = serviceUnderTest().limit(limit, actor, 0);
+
+ await expect(promise).rejects.toThrow(/factor is zero or negative/);
+ });
+
+ it('should throw if factor is negative', async () => {
+ const promise = serviceUnderTest().limit(limit, actor, -1);
+
+ await expect(promise).rejects.toThrow(/factor is zero or negative/);
+ });
+
+ it('should skip if minInterval is zero', async () => {
+ limit.minInterval = 0;
+
+ const info = await serviceUnderTest().limit(limit, actor);
+
+ expect(info.blocked).toBeFalsy();
+ expect(info.remaining).toBe(Number.MAX_SAFE_INTEGER);
+ });
+
+ it('should throw if minInterval is negative', async () => {
+ limit.minInterval = -1;
+
+ const promise = serviceUnderTest().limit(limit, actor);
+
+ await expect(promise).rejects.toThrow(/minInterval is negative/);
+ });
});
describe('with legacy limit', () => {
@@ -578,6 +691,34 @@ describe(SkRateLimiterService, () => {
expect(i1.blocked).toBeTruthy();
expect(i2.blocked).toBeFalsy();
});
+
+ it('should throw if factor is zero', async () => {
+ const promise = serviceUnderTest().limit(limit, actor, 0);
+
+ await expect(promise).rejects.toThrow(/factor is zero or negative/);
+ });
+
+ it('should throw if factor is negative', async () => {
+ const promise = serviceUnderTest().limit(limit, actor, -1);
+
+ await expect(promise).rejects.toThrow(/factor is zero or negative/);
+ });
+
+ it('should throw if max is zero', async () => {
+ limit.max = 0;
+
+ const promise = serviceUnderTest().limit(limit, actor);
+
+ await expect(promise).rejects.toThrow(/size is less than 1/);
+ });
+
+ it('should throw if max is negative', async () => {
+ limit.max = -1;
+
+ const promise = serviceUnderTest().limit(limit, actor);
+
+ await expect(promise).rejects.toThrow(/size is less than 1/);
+ });
});
describe('with legacy limit and min interval', () => {