Skip to content

Commit 89634d1

Browse files
validation
1 parent a070ff7 commit 89634d1

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/server/routes/course.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ChatInstance, Enrolment, UserChatInstanceUsage, Prompt, User, Responsib
66
import { getOwnCourses } from '../services/chatInstances/access'
77
import { encrypt, decrypt } from '../util/util'
88
import { ApplicationError } from '../util/ApplicationError'
9+
import { cleanIdStringSchema } from '../util/zodSchemas'
910

1011
const courseRouter = express.Router()
1112

@@ -340,6 +341,15 @@ courseRouter.put('/:id/responsibilities/assign', async (req, res) => {
340341
}
341342
const assignedUserId:string = body.assignedUserId
342343

344+
const chatInstanceIdClean = cleanIdStringSchema.safeParse(chatInstanceId)
345+
if(!chatInstanceIdClean.success){
346+
res.status(400).send('Malformed chat instance id')
347+
}
348+
const assignedUserIdClean = cleanIdStringSchema.safeParse(assignedUserId)
349+
if(!assignedUserIdClean.success){
350+
res.status(400).send('Malformed assigned user id')
351+
}
352+
343353
const request = req as unknown as RequestWithUser
344354
const {user} = request
345355
const chatInstance = await getChatInstance(chatInstanceId)
@@ -348,7 +358,7 @@ courseRouter.put('/:id/responsibilities/assign', async (req, res) => {
348358
const userToAssign = await getUser(assignedUserId)
349359
const userAssignedAlready = await userAssignedAsResponsible(assignedUserId, chatInstance)
350360
if(userAssignedAlready){
351-
res.status(401).send('User is already responsible for the course')
361+
res.status(400).send('User is already responsible for the course')
352362
return
353363
}
354364

src/server/util/zodSchemas.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { z } from 'zod';
2+
3+
export const cleanIdStringSchema = z.string().regex(/^[a-zA-Z0-9]+$/, {
4+
message: 'String must only contain letters',
5+
});

0 commit comments

Comments
 (0)