Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

fix(storage): validate opaque multipart handles by capability

Timothy Yince416da674ae
diff --git a/packages/storage/src/connection-probe.ts b/packages/storage/src/connection-probe.ts
index 929ed4bd..4062774a 100644
--- a/packages/storage/src/connection-probe.ts
+++ b/packages/storage/src/connection-probe.ts
@@ -64,6 +64,7 @@ export interface StorageProbeClient {
signGet(key: string): Promise<string>
multipart(key: string, contentType: string): Promise<string>
recoverMultipart(key: string): Promise<string[]>
+ parts(key: string, uploadId: string): Promise<readonly unknown[]>
abort(key: string, uploadId: string): Promise<unknown>
browserCors(origin: string, key: string): Promise<boolean>
remove(key: string): Promise<unknown>
@@ -119,7 +120,17 @@ export async function probeStorageConnection(
const ids = await operation('multipart_list', () =>
s3.recoverMultipart(key),
)
- if (!ids.includes(id)) throw new StorageConnectionError('multipart_list')
+ // Upload IDs are opaque handles. Some S3-compatible services return a
+ // different usable handle when listing the same upload. Validate capability
+ // instead of assuming the create and list strings must be identical.
+ if (!ids.length) throw new StorageConnectionError('multipart_list')
+ for (const handle of new Set([id, ids[0]!])) {
+ const parts = await operation('multipart_list', () =>
+ s3.parts(key, handle),
+ )
+ // This random-key probe has not uploaded any parts.
+ if (parts.length) throw new StorageConnectionError('multipart_list')
+ }
} catch (error) {
multipartFailure = error as StorageConnectionError
}
diff --git a/tests/cloud-storage-probe.test.ts b/tests/cloud-storage-probe.test.ts
index 98869380..ea28e0a5 100644
--- a/tests/cloud-storage-probe.test.ts
+++ b/tests/cloud-storage-probe.test.ts
@@ -39,6 +39,10 @@ function fixture(fail?: string) {
hit('recover')
return ['upload-id']
},
+ async parts(_key, uploadId) {
+ hit('parts:' + uploadId)
+ return []
+ },
async abort() {
hit('abort')
},
@@ -95,6 +99,7 @@ test('Connection probe checks object operations and returns only safe fixed fail
'signed_read',
'multipart',
'recover',
+ 'parts:upload-id',
'abort',
'cors',
'cleanup',
@@ -335,3 +340,59 @@ test('Multipart recovery preserves literal percent keys and rejects incomplete o
)
assert.equal(calls, 20)
})
+
+test('Connection probe accepts different usable opaque multipart IDs returned by create and list', async () => {
+ const f = fixture(),
+ checked: string[] = []
+ f.client.recoverMultipart = async () => ['listed-opaque-token']
+ f.client.parts = async (_key, id) => {
+ checked.push(id)
+ assert(['upload-id', 'listed-opaque-token'].includes(id))
+ return []
+ }
+ await probeStorageConnection(
+ f.client,
+ 'https://portal.example',
+ async () => new Response('ok'),
+ )
+ assert.deepEqual(checked, ['upload-id', 'listed-opaque-token'])
+ assert(f.calls.includes('abort'))
+ assert(f.calls.includes('cors'))
+ assert(f.calls.includes('cleanup'))
+})
+
+test('Opaque multipart ID validation rejects unusable, missing and nonempty probes and still cleans up', async () => {
+ for (const invalid of ['listed', 'created', 'missing', 'nonempty']) {
+ const f = fixture()
+ f.client.recoverMultipart = async () =>
+ invalid === 'missing' ? [] : ['listed-token']
+ f.client.parts = async (_key, handle) => {
+ if (invalid === 'nonempty') return [{}]
+ if (
+ (invalid === 'listed' && handle === 'listed-token') ||
+ (invalid === 'created' && handle === 'upload-id')
+ )
+ throw Object.assign(new Error('provider secret must not leak'), {
+ name: 'NoSuchUpload',
+ $metadata: { httpStatusCode: 404 },
+ })
+ return []
+ }
+ await assert.rejects(
+ probeStorageConnection(
+ f.client,
+ 'https://portal.example',
+ async () => new Response('ok'),
+ ),
+ (error) => {
+ assert(error instanceof StorageConnectionError)
+ assert.equal(error.code, 'storage_multipart_list_failed')
+ assert(!JSON.stringify(error).includes('secret'))
+ return true
+ },
+ )
+ assert(f.calls.includes('abort'))
+ assert(f.calls.includes('cleanup'))
+ assert(!f.calls.includes('cors'))
+ }
+})
diff --git a/tests/s3-worker-runtime.test.ts b/tests/s3-worker-runtime.test.ts
index 3684581b..a2f508d9 100644
--- a/tests/s3-worker-runtime.test.ts
+++ b/tests/s3-worker-runtime.test.ts
@@ -16,7 +16,7 @@ test('S3 browser distribution parses multipart/CORS XML in real workerd without
const bundle = await build({
stdin: {
resolveDir: process.cwd(),
- contents: `import {createCloudS3} from '@chronoframe/storage/s3-edge';export default {async fetch(){const s=createCloudS3({endpoint:'https://s3.us-east-1.amazonaws.com',region:'us-east-1',bucket:'test-bucket',accessKeyId:'test-access',secretAccessKey:'test-secret'});const id=await s.multipart('test/file','image/jpeg');const recover=await s.recoverMultipart('test/file');const parts=await s.parts('test/file',id);const cors=await s.cors('https://app.chronoframe.art');const grant=await s.signPart('test/file',id,1,32);await s.abort('test/file',id);return Response.json({id,recover,parts,cors,signed:new URL(grant).searchParams.get('X-Amz-Algorithm')})}}`,
+ contents: `import {createCloudS3} from '@chronoframe/storage/s3-edge';export default {async fetch(){const s=createCloudS3({endpoint:'https://s3.us-east-1.amazonaws.com',region:'us-east-1',bucket:'test-bucket',accessKeyId:'test-access',secretAccessKey:'test-secret'});const id=await s.multipart('test/file','image/jpeg');const recover=await s.recoverMultipart('test/file');const parts=await s.parts('test/file',id);const listedParts=await s.parts('test/file',recover[0]);const cors=await s.cors('https://app.chronoframe.art');const grant=await s.signPart('test/file',id,1,32);await s.abort('test/file',id);return Response.json({id,recover,parts,listedParts,cors,signed:new URL(grant).searchParams.get('X-Amz-Algorithm')})}}`,
},
bundle: true,
platform: 'browser',
@@ -47,13 +47,19 @@ test('S3 browser distribution parses multipart/CORS XML in real workerd without
assert.equal(u.searchParams.get('key-marker'), 'test/file')
assert.equal(u.searchParams.get('upload-id-marker'), 'marker')
return xml(
- '<ListMultipartUploadsResult><EncodingType>url</EncodingType><IsTruncated>false</IsTruncated><Upload><Key>test%2Ffile</Key><UploadId>test-upload</UploadId></Upload></ListMultipartUploadsResult>',
+ '<ListMultipartUploadsResult><EncodingType>url</EncodingType><IsTruncated>false</IsTruncated><Upload><Key>test%2Ffile</Key><UploadId>listed-opaque-token</UploadId></Upload></ListMultipartUploadsResult>',
)
}
- if (u.searchParams.has('uploadId'))
+ if (u.searchParams.has('uploadId')) {
+ assert(
+ ['test-upload', 'listed-opaque-token'].includes(
+ u.searchParams.get('uploadId')!,
+ ),
+ )
return xml(
'<ListPartsResult><Part><PartNumber>1</PartNumber><ETag>test-etag</ETag><Size>32</Size></Part></ListPartsResult>',
)
+ }
if (u.searchParams.has('cors'))
return xml(
'<CORSConfiguration><CORSRule><AllowedOrigin>https://app.chronoframe.art</AllowedOrigin><AllowedMethod>GET</AllowedMethod><AllowedMethod>PUT</AllowedMethod><AllowedMethod>HEAD</AllowedMethod><AllowedHeader>*</AllowedHeader><ExposeHeader>ETag</ExposeHeader></CORSRule></CORSConfiguration>',
@@ -67,8 +73,9 @@ test('S3 browser distribution parses multipart/CORS XML in real workerd without
assert.equal(response.status, 200, await response.clone().text())
assert.deepEqual(await response.json(), {
id: 'test-upload',
- recover: ['test-upload'],
+ recover: ['listed-opaque-token'],
parts: [{ partNumber: 1, etag: 'test-etag', bytes: 32 }],
+ listedParts: [{ partNumber: 1, etag: 'test-etag', bytes: 32 }],
cors: true,
signed: 'AWS4-HMAC-SHA256',
})