Skip to content

Commit

Permalink
Add attachment size validator
Browse files Browse the repository at this point in the history
  • Loading branch information
jpalumickas committed Oct 29, 2024
1 parent b262737 commit dad6427
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
3 changes: 3 additions & 0 deletions examples/cloudflare-worker/src/services/uplo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export const createUplo = (c: HonoContext) => {
validate: {
// contentType: ['image/png', 'image/jpeg'],
contentType: /image\/\w/,
size: {
max: 1024 * 1024 * 5, // 5MB
},
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import { z } from 'zod';

export const directUploadValidationSchema = z.object({
attachmentName: z
.string()
.string({ message: 'attachmentName is required' })
.trim()
.regex(/^[a-z0-9-]+\.[a-z0-9-]+$/, {
message: 'Invalid attachment name. Format: "model.attachment-name"',
})
.transform((val) => val as `${string}.${string}`),
fileName: z.string(),
contentType: z.string(),
checksum: z.string(),
size: z.number(),
fileName: z.string({ message: 'fileName is required' }).trim(),
contentType: z.string().trim(),
checksum: z.string().trim(),
size: z.number().gt(0).finite(),
metadata: z.object({}).optional(),
});
1 change: 1 addition & 0 deletions packages/server/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface UploConfig {

type AttachmentValidateObjectType = {
contentType?: string | string[] | RegExp | RegExp[];
size?: { min?: number; max?: number };
};

export type AttachmentValidateType = AttachmentValidateObjectType;
Expand Down
39 changes: 35 additions & 4 deletions packages/server/src/utils/validateBobInputData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,55 @@ const validateContentType = (
return true;
};

const validateSize = (
size: number,
sizeValidator: AttachmentValidateType['size']
) => {
if (!sizeValidator) {
return true;
}

if (sizeValidator.min !== undefined && size < sizeValidator.min) {
throw new BlobValidationError('File size is too small');
}

if (sizeValidator.max !== undefined && size > sizeValidator.max) {
throw new BlobValidationError('File size is too large');
}

return true;
};

export const validateBlobInputData = (
blobInputData: BlobInputData,
validator: AttachmentValidateType | null | undefined
) => {
if (!blobInputData?.fileName?.trim()) {
throw new BlobValidationError('Missing file name');
}

if (
!blobInputData?.fileName?.trim() ||
!blobInputData?.contentType?.trim() ||
!blobInputData?.size ||
!blobInputData?.checksum?.trim()
!Number.isFinite(blobInputData.size) ||
blobInputData.size < 0
) {
throw new BlobValidationError('Missing data for attachment');
throw new BlobValidationError('Missing size');
}

if (!blobInputData?.checksum?.trim()) {
throw new BlobValidationError('Missing checksum');
}

if (!blobInputData?.contentType?.trim()) {
throw new BlobValidationError('Missing content type');
}

if (!validator) {
return true;
}

validateContentType(blobInputData.contentType, validator.contentType);
validateSize(blobInputData.size, validator.size);

return true;
};

0 comments on commit dad6427

Please sign in to comment.