Skip to content

Commit bbd24a5

Browse files
committed
fix(spec)!: the presigned upload request declares a whole, non-negative size and a non-empty filename and mimeType
GetPresignedUrlRequestSchema.size becomes int().min(0), and filename / mimeType take min(1), the accept set POST /api/v1/storage/upload/presigned enforces. The door's byte-count check now also refuses a fractional size, which minted a URL and a pending sys_file row no upload could complete. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L9qibvsQx1XxK9FrejoXno
1 parent 2e955dd commit bbd24a5

4 files changed

Lines changed: 78 additions & 14 deletions

File tree

‎packages/services/service-storage/src/storage-routes.test.ts‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
44
import type { IHttpRequest, IHttpResponse, RouteHandler } from '@objectstack/spec/contracts';
5-
import { InitiateChunkedUploadRequestSchema } from '@objectstack/spec/api';
5+
import { GetPresignedUrlRequestSchema, InitiateChunkedUploadRequestSchema } from '@objectstack/spec/api';
66
import { LocalStorageAdapter } from './local-storage-adapter.js';
77
import { StorageMetadataStore } from './metadata-store.js';
88
import { registerStorageRoutes } from './storage-routes.js';
@@ -130,28 +130,55 @@ describe('Storage REST Routes', () => {
130130
['a numeric string', '100'],
131131
['a negative number', -5],
132132
['a negative fraction', -0.5],
133+
// [#27598] A byte count is whole: a fractional size minted a URL and a
134+
// pending row that no upload could ever complete (the held byte count
135+
// never equals it).
136+
['a positive fraction', 1.5],
133137
['NaN', Number.NaN],
134138
['Infinity', Number.POSITIVE_INFINITY],
135139
['a boolean', true],
136140
])('REFUSED 400 INVALID_REQUEST: size as %s, with no sys_file row written', async (_label, size) => {
137141
const handler = httpServer._getHandler('POST', '/api/v1/storage/upload/presigned')!;
138142
const createFile = vi.spyOn(store, 'createFile');
143+
const body = { filename: 'a.png', mimeType: 'image/png', size };
139144
const res = createMockRes();
140-
await handler(createMockReq({ body: { filename: 'a.png', mimeType: 'image/png', size } }), res);
145+
await handler(createMockReq({ body }), res);
141146

142147
expect(res._status, JSON.stringify(res._json)).toBe(400);
143148
expect(res._json.success).toBe(false);
144149
expect(res._json.error.code).toBe('INVALID_REQUEST');
145150
expect(res._json.error.message).toMatch(/^size must be the file size in bytes/);
151+
expect(GetPresignedUrlRequestSchema.safeParse(body).success, 'the declaration refuses it').toBe(false);
146152
expect(createFile, 'no pending sys_file row').not.toHaveBeenCalled();
147153
});
148154

155+
// [#27598] The door's presence check refuses an empty `filename` or
156+
// `mimeType`; the declaration says the same.
157+
it.each(['filename', 'mimeType'] as const)(
158+
'REFUSED 400 INVALID_REQUEST: an empty %s, which the declared schema refuses too, with no row written',
159+
async (key) => {
160+
const handler = httpServer._getHandler('POST', '/api/v1/storage/upload/presigned')!;
161+
const createFile = vi.spyOn(store, 'createFile');
162+
const body = { filename: 'a.png', mimeType: 'image/png', size: 1, [key]: '' };
163+
const res = createMockRes();
164+
await handler(createMockReq({ body }), res);
165+
166+
expect(res._status, JSON.stringify(res._json)).toBe(400);
167+
expect(res._json.success).toBe(false);
168+
expect(res._json.error.code).toBe('INVALID_REQUEST');
169+
expect(GetPresignedUrlRequestSchema.safeParse(body).success, 'the declaration refuses it').toBe(false);
170+
expect(createFile, 'no pending sys_file row').not.toHaveBeenCalled();
171+
},
172+
);
173+
149174
it.each([0, 1024])('CONTROL: size %d is accepted and stored on the pending row as sent', async (size) => {
150175
const handler = httpServer._getHandler('POST', '/api/v1/storage/upload/presigned')!;
176+
const body = { filename: 'a.png', mimeType: 'image/png', size };
151177
const res = createMockRes();
152-
await handler(createMockReq({ body: { filename: 'a.png', mimeType: 'image/png', size } }), res);
178+
await handler(createMockReq({ body }), res);
153179

154180
expect(res._status, JSON.stringify(res._json)).toBe(200);
181+
expect(GetPresignedUrlRequestSchema.safeParse(body).success, 'the declaration accepts it').toBe(true);
155182
const row = await store.getFile(res._json.data.fileId);
156183
expect(row!.size).toBe(size);
157184
expect(row!.status).toBe('pending');

‎packages/services/service-storage/src/storage-routes.ts‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,19 +162,21 @@ function uploadScopeRefusalMessage(scope: unknown): string {
162162
}
163163

164164
/**
165-
* [#27413] The presigned door's declared `size`: a finite, non-negative JSON
166-
* number, the byte count `GetPresignedUrlRequestSchema.size` declares. A
167-
* string (even a numeric one), `NaN`, an infinity or a negative count is
168-
* refused `400` / `INVALID_REQUEST` before the pending `sys_file` row is
169-
* written — the code the door already answers a request missing its size
170-
* with. `0` is a legal empty file.
165+
* [#27413] The presigned door's declared `size`: a whole, non-negative JSON
166+
* number, the byte count `GetPresignedUrlRequestSchema.size` declares
167+
* (`int().min(0)`). A string (even a numeric one), `NaN`, an infinity, a
168+
* negative count or [#27598] a fraction is refused `400` / `INVALID_REQUEST`
169+
* before the pending `sys_file` row is written — the code the door already
170+
* answers a request missing its size with. A fraction used to mint a URL and
171+
* a pending row no upload could complete: the held byte count is whole, so
172+
* the complete door's size check never matched it. `0` is a legal empty file.
171173
*/
172174
function isDeclaredByteCount(size: unknown): size is number {
173-
return typeof size === 'number' && Number.isFinite(size) && size >= 0;
175+
return typeof size === 'number' && Number.isInteger(size) && size >= 0;
174176
}
175177

176178
const PRESIGNED_SIZE_REFUSAL_MESSAGE =
177-
'size must be the file size in bytes: a non-negative number, sent as a JSON number';
179+
'size must be the file size in bytes: a non-negative whole number, sent as a JSON number';
178180

179181
/**
180182
* [#27549] The chunked-upload init door's refusal for a body that does not

‎packages/spec/src/api/storage.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,38 @@ describe('GetPresignedUrlRequestSchema', () => {
7878
})
7979
).toThrow();
8080
});
81+
82+
// [#27598] `size` is a whole, non-negative byte count, and `filename` /
83+
// `mimeType` are non-empty: what the presigned door refuses, declared.
84+
it.each<[string, number]>([
85+
['a negative size', -1],
86+
['a fractional size', 1.5],
87+
['a negative fraction', -0.5],
88+
])('refuses %s, naming size', (_label, size) => {
89+
const result = GetPresignedUrlRequestSchema.safeParse({ filename: 'a.png', mimeType: 'image/png', size });
90+
expect(result.success).toBe(false);
91+
expect(result.error!.issues).toHaveLength(1);
92+
expect(result.error!.issues[0].path).toEqual(['size']);
93+
});
94+
95+
it.each(['filename', 'mimeType'] as const)('refuses an empty %s, naming that key', (key) => {
96+
const result = GetPresignedUrlRequestSchema.safeParse({
97+
filename: 'a.png',
98+
mimeType: 'image/png',
99+
size: 1,
100+
[key]: '',
101+
});
102+
expect(result.success).toBe(false);
103+
expect(result.error!.issues).toHaveLength(1);
104+
expect(result.error!.issues[0].path).toEqual([key]);
105+
expect(result.error!.issues[0].code).toBe('too_small');
106+
});
107+
108+
it.each([0, 1, 1048576])('CONTROL: size %d parses (0 is an empty file)', (size) => {
109+
const result = GetPresignedUrlRequestSchema.safeParse({ filename: 'a', mimeType: 'b', size });
110+
expect(result.success).toBe(true);
111+
expect(result.data!.size).toBe(size);
112+
});
81113
});
82114

83115
describe('CompleteUploadRequestSchema', () => {

‎packages/spec/src/api/storage.zod.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,12 @@ const UPLOAD_SCOPE_DESCRIPTION =
6464
+ 'carries no acl; every upload is stored private.';
6565

6666
export const GetPresignedUrlRequestSchema = lazySchema(() => z.object({
67-
filename: z.string().describe('Original filename'),
68-
mimeType: z.string().describe('File MIME type'),
69-
size: z.number().describe('File size in bytes'),
67+
// [#27598] What the presigned door refuses, declared: an empty `filename` or
68+
// `mimeType` (400 INVALID_REQUEST, as the chunked-init sibling declares
69+
// since #27801), and a `size` that is not a whole, non-negative byte count.
70+
filename: z.string().min(1).describe('Original filename (non-empty)'),
71+
mimeType: z.string().min(1).describe('File MIME type (non-empty)'),
72+
size: z.number().int().min(0).describe('File size in bytes: a non-negative whole number (0 is an empty file)'),
7073
scope: UploadScopeSchema.default('user').describe(UPLOAD_SCOPE_DESCRIPTION),
7174
bucket: z.string().optional().describe('Specific bucket override (admin only)'),
7275
}));

0 commit comments

Comments
 (0)