Skip to content

Commit 8a3bcba

Browse files
darkskygitakumatus
authored andcommitted
feat: add content length check
1 parent 3a6d1a1 commit 8a3bcba

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

packages/backend/server/src/plugins/copilot/context/resolver.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { SafeIntResolver } from 'graphql-scalars';
1919
import GraphQLUpload from 'graphql-upload/GraphQLUpload.mjs';
2020

2121
import {
22+
BlobQuotaExceeded,
2223
CallMetric,
2324
CopilotEmbeddingUnavailable,
2425
CopilotFailedToMatchContext,
@@ -42,6 +43,7 @@ import {
4243
ContextFileStatus,
4344
DocChunkSimilarity,
4445
FileChunkSimilarity,
46+
MAX_EMBEDDABLE_SIZE,
4547
} from './types';
4648

4749
@InputType()
@@ -396,6 +398,12 @@ export class CopilotContextResolver {
396398
if (!lock) {
397399
return new TooManyRequest('Server is busy');
398400
}
401+
402+
const length = Number(ctx.req.headers['content-length']);
403+
if (length && length >= MAX_EMBEDDABLE_SIZE) {
404+
throw new BlobQuotaExceeded();
405+
}
406+
399407
const session = await this.context.get(options.contextId);
400408

401409
try {

packages/backend/server/src/plugins/copilot/context/session.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { nanoid } from 'nanoid';
77
import {
88
BlobQuotaExceeded,
99
CopilotContextFileNotSupported,
10-
OneMB,
1110
PrismaTransaction,
1211
UserFriendlyError,
1312
} from '../../../base';
@@ -22,6 +21,7 @@ import {
2221
DocChunkSimilarity,
2322
EmbeddingClient,
2423
FileChunkSimilarity,
24+
MAX_EMBEDDABLE_SIZE,
2525
} from './types';
2626

2727
export class ContextSession implements AsyncDisposable {
@@ -62,7 +62,7 @@ export class ContextSession implements AsyncDisposable {
6262

6363
private readStream(
6464
readable: Readable,
65-
maxSize = 50 * OneMB
65+
maxSize = MAX_EMBEDDABLE_SIZE
6666
): Promise<Buffer<ArrayBuffer>> {
6767
return new Promise<Buffer<ArrayBuffer>>((resolve, reject) => {
6868
const chunks: Uint8Array[] = [];
@@ -136,7 +136,7 @@ export class ContextSession implements AsyncDisposable {
136136
}
137137

138138
try {
139-
const buffer = await this.readStream(readable, 50 * OneMB);
139+
const buffer = await this.readStream(readable);
140140
const file = new File([buffer], name);
141141
return await this.addFile(file, fileId, signal);
142142
} catch (e: any) {

packages/backend/server/src/plugins/copilot/context/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { File } from 'node:buffer';
22

33
import { z } from 'zod';
44

5-
import { CopilotContextFileNotSupported } from '../../../base';
5+
import { CopilotContextFileNotSupported, OneMB } from '../../../base';
66
import { parseDoc } from '../../../native';
77

88
declare global {
@@ -18,6 +18,8 @@ declare global {
1818
}
1919
}
2020

21+
export const MAX_EMBEDDABLE_SIZE = 50 * OneMB;
22+
2123
export enum ContextFileStatus {
2224
processing = 'processing',
2325
finished = 'finished',

0 commit comments

Comments
 (0)