Skip to content

Commit d1e2e95

Browse files
authored
⚖️ feat: Add Violation Scores (danny-avila#8304)
- Introduced new violation scores for TTS, STT, Fork, Import, and File Upload actions in the .env.example file. - Updated logViolation function to accept a score parameter, allowing for dynamic severity levels based on the action type. - Modified limiters for Fork, Import, Message, STT, TTS, Tool Call, and File Upload to utilize the new violation scores when logging violations.
1 parent a4a1752 commit d1e2e95

File tree

11 files changed

+61
-14
lines changed

11 files changed

+61
-14
lines changed

.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,11 @@ REGISTRATION_VIOLATION_SCORE=1
349349
CONCURRENT_VIOLATION_SCORE=1
350350
MESSAGE_VIOLATION_SCORE=1
351351
NON_BROWSER_VIOLATION_SCORE=20
352+
TTS_VIOLATION_SCORE=0
353+
STT_VIOLATION_SCORE=0
354+
FORK_VIOLATION_SCORE=0
355+
IMPORT_VIOLATION_SCORE=0
356+
FILE_UPLOAD_VIOLATION_SCORE=0
352357

353358
LOGIN_MAX=7
354359
LOGIN_WINDOW=5

api/cache/logViolation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const banViolation = require('./banViolation');
99
* @param {Object} res - Express response object.
1010
* @param {string} type - The type of violation.
1111
* @param {Object} errorMessage - The error message to log.
12-
* @param {number} [score=1] - The severity of the violation. Defaults to 1
12+
* @param {number | string} [score=1] - The severity of the violation. Defaults to 1
1313
*/
1414
const logViolation = async (req, res, type, errorMessage, score = 1) => {
1515
const userId = req.user?.id ?? req.user?._id;

api/server/middleware/limiters/forkLimiters.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const getEnvironmentVariables = () => {
1111
const FORK_IP_WINDOW = parseInt(process.env.FORK_IP_WINDOW) || 1;
1212
const FORK_USER_MAX = parseInt(process.env.FORK_USER_MAX) || 7;
1313
const FORK_USER_WINDOW = parseInt(process.env.FORK_USER_WINDOW) || 1;
14+
const FORK_VIOLATION_SCORE = process.env.FORK_VIOLATION_SCORE;
1415

1516
const forkIpWindowMs = FORK_IP_WINDOW * 60 * 1000;
1617
const forkIpMax = FORK_IP_MAX;
@@ -27,12 +28,18 @@ const getEnvironmentVariables = () => {
2728
forkUserWindowMs,
2829
forkUserMax,
2930
forkUserWindowInMinutes,
31+
forkViolationScore: FORK_VIOLATION_SCORE,
3032
};
3133
};
3234

3335
const createForkHandler = (ip = true) => {
34-
const { forkIpMax, forkIpWindowInMinutes, forkUserMax, forkUserWindowInMinutes } =
35-
getEnvironmentVariables();
36+
const {
37+
forkIpMax,
38+
forkUserMax,
39+
forkViolationScore,
40+
forkIpWindowInMinutes,
41+
forkUserWindowInMinutes,
42+
} = getEnvironmentVariables();
3643

3744
return async (req, res) => {
3845
const type = ViolationTypes.FILE_UPLOAD_LIMIT;
@@ -43,7 +50,7 @@ const createForkHandler = (ip = true) => {
4350
windowInMinutes: ip ? forkIpWindowInMinutes : forkUserWindowInMinutes,
4451
};
4552

46-
await logViolation(req, res, type, errorMessage);
53+
await logViolation(req, res, type, errorMessage, forkViolationScore);
4754
res.status(429).json({ message: 'Too many conversation fork requests. Try again later' });
4855
};
4956
};

api/server/middleware/limiters/importLimiters.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const getEnvironmentVariables = () => {
1111
const IMPORT_IP_WINDOW = parseInt(process.env.IMPORT_IP_WINDOW) || 15;
1212
const IMPORT_USER_MAX = parseInt(process.env.IMPORT_USER_MAX) || 50;
1313
const IMPORT_USER_WINDOW = parseInt(process.env.IMPORT_USER_WINDOW) || 15;
14+
const IMPORT_VIOLATION_SCORE = process.env.IMPORT_VIOLATION_SCORE;
1415

1516
const importIpWindowMs = IMPORT_IP_WINDOW * 60 * 1000;
1617
const importIpMax = IMPORT_IP_MAX;
@@ -27,12 +28,18 @@ const getEnvironmentVariables = () => {
2728
importUserWindowMs,
2829
importUserMax,
2930
importUserWindowInMinutes,
31+
importViolationScore: IMPORT_VIOLATION_SCORE,
3032
};
3133
};
3234

3335
const createImportHandler = (ip = true) => {
34-
const { importIpMax, importIpWindowInMinutes, importUserMax, importUserWindowInMinutes } =
35-
getEnvironmentVariables();
36+
const {
37+
importIpMax,
38+
importUserMax,
39+
importViolationScore,
40+
importIpWindowInMinutes,
41+
importUserWindowInMinutes,
42+
} = getEnvironmentVariables();
3643

3744
return async (req, res) => {
3845
const type = ViolationTypes.FILE_UPLOAD_LIMIT;
@@ -43,7 +50,7 @@ const createImportHandler = (ip = true) => {
4350
windowInMinutes: ip ? importIpWindowInMinutes : importUserWindowInMinutes,
4451
};
4552

46-
await logViolation(req, res, type, errorMessage);
53+
await logViolation(req, res, type, errorMessage, importViolationScore);
4754
res.status(429).json({ message: 'Too many conversation import requests. Try again later' });
4855
};
4956
};

api/server/middleware/limiters/messageLimiters.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const {
1111
MESSAGE_IP_WINDOW = 1,
1212
MESSAGE_USER_MAX = 40,
1313
MESSAGE_USER_WINDOW = 1,
14+
MESSAGE_VIOLATION_SCORE: score,
1415
} = process.env;
1516

1617
const ipWindowMs = MESSAGE_IP_WINDOW * 60 * 1000;
@@ -39,7 +40,7 @@ const createHandler = (ip = true) => {
3940
windowInMinutes: ip ? ipWindowInMinutes : userWindowInMinutes,
4041
};
4142

42-
await logViolation(req, res, type, errorMessage);
43+
await logViolation(req, res, type, errorMessage, score);
4344
return await denyRequest(req, res, errorMessage);
4445
};
4546
};

api/server/middleware/limiters/sttLimiters.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const getEnvironmentVariables = () => {
1111
const STT_IP_WINDOW = parseInt(process.env.STT_IP_WINDOW) || 1;
1212
const STT_USER_MAX = parseInt(process.env.STT_USER_MAX) || 50;
1313
const STT_USER_WINDOW = parseInt(process.env.STT_USER_WINDOW) || 1;
14+
const STT_VIOLATION_SCORE = process.env.STT_VIOLATION_SCORE;
1415

1516
const sttIpWindowMs = STT_IP_WINDOW * 60 * 1000;
1617
const sttIpMax = STT_IP_MAX;
@@ -27,11 +28,12 @@ const getEnvironmentVariables = () => {
2728
sttUserWindowMs,
2829
sttUserMax,
2930
sttUserWindowInMinutes,
31+
sttViolationScore: STT_VIOLATION_SCORE,
3032
};
3133
};
3234

3335
const createSTTHandler = (ip = true) => {
34-
const { sttIpMax, sttIpWindowInMinutes, sttUserMax, sttUserWindowInMinutes } =
36+
const { sttIpMax, sttIpWindowInMinutes, sttUserMax, sttUserWindowInMinutes, sttViolationScore } =
3537
getEnvironmentVariables();
3638

3739
return async (req, res) => {
@@ -43,7 +45,7 @@ const createSTTHandler = (ip = true) => {
4345
windowInMinutes: ip ? sttIpWindowInMinutes : sttUserWindowInMinutes,
4446
};
4547

46-
await logViolation(req, res, type, errorMessage);
48+
await logViolation(req, res, type, errorMessage, sttViolationScore);
4749
res.status(429).json({ message: 'Too many STT requests. Try again later' });
4850
};
4951
};

api/server/middleware/limiters/toolCallLimiter.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const logViolation = require('~/cache/logViolation');
66
const { isEnabled } = require('~/server/utils');
77
const { logger } = require('~/config');
88

9+
const { TOOL_CALL_VIOLATION_SCORE: score } = process.env;
10+
911
const handler = async (req, res) => {
1012
const type = ViolationTypes.TOOL_CALL_LIMIT;
1113
const errorMessage = {
@@ -15,7 +17,7 @@ const handler = async (req, res) => {
1517
windowInMinutes: 1,
1618
};
1719

18-
await logViolation(req, res, type, errorMessage, 0);
20+
await logViolation(req, res, type, errorMessage, score);
1921
res.status(429).json({ message: 'Too many tool call requests. Try again later' });
2022
};
2123

api/server/middleware/limiters/ttsLimiters.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const getEnvironmentVariables = () => {
1111
const TTS_IP_WINDOW = parseInt(process.env.TTS_IP_WINDOW) || 1;
1212
const TTS_USER_MAX = parseInt(process.env.TTS_USER_MAX) || 50;
1313
const TTS_USER_WINDOW = parseInt(process.env.TTS_USER_WINDOW) || 1;
14+
const TTS_VIOLATION_SCORE = process.env.TTS_VIOLATION_SCORE;
1415

1516
const ttsIpWindowMs = TTS_IP_WINDOW * 60 * 1000;
1617
const ttsIpMax = TTS_IP_MAX;
@@ -27,11 +28,12 @@ const getEnvironmentVariables = () => {
2728
ttsUserWindowMs,
2829
ttsUserMax,
2930
ttsUserWindowInMinutes,
31+
ttsViolationScore: TTS_VIOLATION_SCORE,
3032
};
3133
};
3234

3335
const createTTSHandler = (ip = true) => {
34-
const { ttsIpMax, ttsIpWindowInMinutes, ttsUserMax, ttsUserWindowInMinutes } =
36+
const { ttsIpMax, ttsIpWindowInMinutes, ttsUserMax, ttsUserWindowInMinutes, ttsViolationScore } =
3537
getEnvironmentVariables();
3638

3739
return async (req, res) => {
@@ -43,7 +45,7 @@ const createTTSHandler = (ip = true) => {
4345
windowInMinutes: ip ? ttsIpWindowInMinutes : ttsUserWindowInMinutes,
4446
};
4547

46-
await logViolation(req, res, type, errorMessage);
48+
await logViolation(req, res, type, errorMessage, ttsViolationScore);
4749
res.status(429).json({ message: 'Too many TTS requests. Try again later' });
4850
};
4951
};

api/server/middleware/limiters/uploadLimiters.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const getEnvironmentVariables = () => {
1111
const FILE_UPLOAD_IP_WINDOW = parseInt(process.env.FILE_UPLOAD_IP_WINDOW) || 15;
1212
const FILE_UPLOAD_USER_MAX = parseInt(process.env.FILE_UPLOAD_USER_MAX) || 50;
1313
const FILE_UPLOAD_USER_WINDOW = parseInt(process.env.FILE_UPLOAD_USER_WINDOW) || 15;
14+
const FILE_UPLOAD_VIOLATION_SCORE = process.env.FILE_UPLOAD_VIOLATION_SCORE;
1415

1516
const fileUploadIpWindowMs = FILE_UPLOAD_IP_WINDOW * 60 * 1000;
1617
const fileUploadIpMax = FILE_UPLOAD_IP_MAX;
@@ -27,6 +28,7 @@ const getEnvironmentVariables = () => {
2728
fileUploadUserWindowMs,
2829
fileUploadUserMax,
2930
fileUploadUserWindowInMinutes,
31+
fileUploadViolationScore: FILE_UPLOAD_VIOLATION_SCORE,
3032
};
3133
};
3234

@@ -36,6 +38,7 @@ const createFileUploadHandler = (ip = true) => {
3638
fileUploadIpWindowInMinutes,
3739
fileUploadUserMax,
3840
fileUploadUserWindowInMinutes,
41+
fileUploadViolationScore,
3942
} = getEnvironmentVariables();
4043

4144
return async (req, res) => {
@@ -47,7 +50,7 @@ const createFileUploadHandler = (ip = true) => {
4750
windowInMinutes: ip ? fileUploadIpWindowInMinutes : fileUploadUserWindowInMinutes,
4851
};
4952

50-
await logViolation(req, res, type, errorMessage);
53+
await logViolation(req, res, type, errorMessage, fileUploadViolationScore);
5154
res.status(429).json({ message: 'Too many file upload requests. Try again later' });
5255
};
5356
};

e2e/playwright.config.a11y.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ const config: PlaywrightTestConfig = {
2626
CONCURRENT_VIOLATION_SCORE: '0',
2727
MESSAGE_VIOLATION_SCORE: '0',
2828
NON_BROWSER_VIOLATION_SCORE: '0',
29+
FORK_VIOLATION_SCORE: '0',
30+
IMPORT_VIOLATION_SCORE: '0',
31+
TTS_VIOLATION_SCORE: '0',
32+
STT_VIOLATION_SCORE: '0',
33+
FILE_UPLOAD_VIOLATION_SCORE: '0',
34+
RESET_PASSWORD_VIOLATION_SCORE: '0',
35+
VERIFY_EMAIL_VIOLATION_SCORE: '0',
36+
TOOL_CALL_VIOLATION_SCORE: '0',
37+
CONVO_ACCESS_VIOLATION_SCORE: '0',
2938
ILLEGAL_MODEL_REQ_SCORE: '0',
3039
LOGIN_MAX: '20',
3140
LOGIN_WINDOW: '1',

0 commit comments

Comments
 (0)