Skip to content

Commit 602d924

Browse files
committed
fix: metadata schema
1 parent 9885347 commit 602d924

File tree

3 files changed

+7
-11
lines changed

3 files changed

+7
-11
lines changed

src/lib/s3/client.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { stringify } from 'superjson';
2-
31
import { env } from '@/env.mjs';
42
import { trpc } from '@/lib/trpc/client';
53
import { RouterInputs } from '@/lib/trpc/types';
@@ -14,8 +12,7 @@ export const uploadFile = async (params: {
1412
try {
1513
const presignedUrlOutput =
1614
await params.trpcClient.files.uploadPresignedUrl.mutate({
17-
// Metadata is a Record<string, string> but should be serialized for trpc-openapi
18-
metadata: params.metadata ? stringify(params.metadata) : undefined,
15+
metadata: params.metadata,
1916
collection: params.collection,
2017
type: params.file.type,
2118
size: params.file.size,

src/lib/s3/schemas.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ export type UploadSignedUrlInput = z.infer<
4242
>;
4343
export const zUploadSignedUrlInput = () =>
4444
z.object({
45-
/**
46-
* Must be a string as trpc-openapi requires that attributes must be serialized
47-
*/
48-
metadata: z.string().optional(),
49-
name: z.string(),
45+
metadata: z.record(z.string(), z.string()).optional(),
46+
name: z
47+
.string()
48+
.max(255)
49+
.regex(/^[^/\\]*$/), // Prevent path traversal (Coderabbitai)
5050
type: z.string(),
5151
size: z.number(),
5252
collection: zFilesCollectionName(),

src/server/routers/files.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { TRPCError } from '@trpc/server';
2-
import { parse } from 'superjson';
32

43
import { FILES_COLLECTIONS_CONFIG } from '@/lib/s3/config';
54
import {
@@ -44,7 +43,7 @@ export const filesRouter = createTRPCRouter({
4443
return await getS3UploadSignedUrl({
4544
key: config.getKey({ user: ctx.user }),
4645
metadata: input.metadata
47-
? { name: input.name, ...parse(input.metadata) }
46+
? { name: input.name, ...input.metadata }
4847
: undefined,
4948
});
5049
}),

0 commit comments

Comments
 (0)