Skip to content

Commit d72d100

Browse files
committed
feat: validate given sessionID to be a UUID
1 parent 014d19e commit d72d100

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

backend/companion/companionRouter.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import express from 'express';
22
import { TokenManager } from './TokenManager';
3+
import { isValidUUID } from '../utils/isValidUUID';
34

45
const tokenManager = new TokenManager();
56

@@ -88,8 +89,10 @@ async function handleChatMessage(req, res) {
8889
const conversationId = sessionId;
8990

9091
try {
91-
if (!conversationId || typeof conversationId !== 'string') {
92-
return res.status(400).json({ error: 'Invalid conversation ID' });
92+
if (!isValidUUID(sessionId)) {
93+
return res.status(400).json({
94+
error: 'Invalid session ID. Must be a valid UUID v4.',
95+
});
9396
}
9497

9598
const baseUrl =

backend/utils/isValidUUID.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export function isValidUUID(uuid) {
2+
// Standard UUID length
3+
const UUIDlength = 36;
4+
// UUID v4 regex pattern
5+
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
6+
7+
return (
8+
typeof uuid === 'string' &&
9+
uuid.length === UUIDlength &&
10+
uuidRegex.test(uuid)
11+
);
12+
}

0 commit comments

Comments
 (0)