fix: accept empty attachment_ids, normalize member uuid case, reject repeated query params

This commit is contained in:
2026-11-01 17:51:37 +03:00
parent 210ff67c01
commit 8f8c89cb19
8 changed files with 239 additions and 14 deletions
+12 -5
View File
@@ -20,19 +20,26 @@ export class ChatsService {
async create(
title: string,
memberIds: string[],
rawMemberIds: string[],
callerId: string,
): Promise<Chat> {
const invalid = rawMemberIds.find((id) => !isUuid(id));
if (invalid) {
throw new ApiError('invalid_request', `unknown member_id: ${invalid}`);
}
const memberIds = rawMemberIds.map((id) => id.toLowerCase());
if (new Set(memberIds).size !== memberIds.length) {
throw new ApiError(
'invalid_request',
"All member_ids's elements must be unique",
);
}
if (!memberIds.includes(callerId)) {
throw new ApiError(
'invalid_request',
'member_ids must include the caller',
);
}
const invalid = memberIds.find((id) => !isUuid(id));
if (invalid) {
throw new ApiError('invalid_request', `unknown member_id: ${invalid}`);
}
try {
const chat = await this.prisma.$transaction(async (tx) => {
const created = await tx.chat.create({
+4 -6
View File
@@ -8,11 +8,10 @@ export function parseLimit(raw: unknown): number {
if (raw === undefined) {
return DEFAULT_LIMIT;
}
const value = Array.isArray(raw) ? raw[0] : raw;
if (typeof value !== 'string' || !/^\d+$/.test(value)) {
if (typeof raw !== 'string' || !/^\d+$/.test(raw)) {
throw new ApiError('invalid_request', 'limit must be an integer');
}
const limit = Number(value);
const limit = Number(raw);
if (limit < MIN_LIMIT || limit > MAX_LIMIT) {
throw new ApiError(
'invalid_request',
@@ -26,12 +25,11 @@ export function rawCursorParam(raw: unknown): string | null {
if (raw === undefined) {
return null;
}
const value = Array.isArray(raw) ? raw[0] : raw;
if (typeof value !== 'string' || value.length === 0) {
if (typeof raw !== 'string' || raw.length === 0) {
throw new ApiError(
'invalid_request',
'cursor must be a non-empty string',
);
}
return value;
return raw;
}
+6 -1
View File
@@ -1,8 +1,13 @@
import { IsString } from 'class-validator';
import { ArrayMaxSize, IsArray, IsString, ValidateIf } from 'class-validator';
import { CodePointLength } from '../../common/dto/code-point-length.validator';
export class CreateMessageDto {
@IsString()
@CodePointLength(0, 4000)
text!: string;
@ValidateIf((dto: CreateMessageDto) => dto.attachment_ids !== undefined)
@IsArray()
@ArrayMaxSize(0, { message: 'attachment_ids must be empty: attachments are not supported' })
attachment_ids?: unknown[];
}