Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.
TypeScript albumsexif-extractiongeocodinglive-photomotion-photophoto-galleryphoto-sharingwebgl-viewer
fix(storage): distinguish multipart probe failures safely
Timothy Yin4fdce6cc1347
diff --git a/apps/shared/platform/runtime.ts b/apps/shared/platform/runtime.tsindex e644fdfb..a925703a 100644--- a/apps/shared/platform/runtime.ts+++ b/apps/shared/platform/runtime.ts@@ -85,7 +85,15 @@ export function definePlatformHandler<T>(createError({statusCode: 502,statusMessage: 'Storage connection check failed',- data: { code: error.code },+ data: {+ code: error.code,+ ...(error.providerCode+ ? { providerCode: error.providerCode }+ : {}),+ ...(error.providerStatus+ ? { providerStatus: error.providerStatus }+ : {}),+ },}),false,)diff --git a/packages/contracts/src/storage-diagnostics.ts b/packages/contracts/src/storage-diagnostics.tsindex c279a8b8..3d74194e 100644--- a/packages/contracts/src/storage-diagnostics.ts+++ b/packages/contracts/src/storage-diagnostics.ts@@ -10,6 +10,12 @@ export const STORAGE_CONNECTION_MESSAGES = {'Signed storage access failed. Check the endpoint, region and S3 credentials.',storage_multipart_failed:'Multipart upload check failed. Check multipart creation, listing and abort permissions.',+ storage_multipart_create_failed:+ 'Creating a multipart upload failed. Check storage permissions and S3 service compatibility.',+ storage_multipart_list_failed:+ 'Listing multipart uploads failed or did not return the new upload. Check multipart listing permissions and S3 service compatibility.',+ storage_multipart_abort_failed:+ 'Aborting the test multipart upload failed. Check multipart abort permissions.',storage_cors_failed:'Storage CORS check failed. Allow this portal origin, GET/PUT/HEAD, Content-Type, and expose ETag.',storage_cleanup_failed:diff --git a/packages/storage/src/connection-probe.ts b/packages/storage/src/connection-probe.tsindex fe253272..929ed4bd 100644--- a/packages/storage/src/connection-probe.ts+++ b/packages/storage/src/connection-probe.ts@@ -5,16 +5,56 @@ export type StorageProbeStage =| 'read'| 'signed_read'| 'multipart'+ | 'multipart_create'+ | 'multipart_list'+ | 'multipart_abort'| 'cors'| 'cleanup'export class StorageConnectionError extends Error {readonly code: StorageConnectionErrorCodereadonly stage: StorageProbeStage- constructor(stage: StorageProbeStage) {+ readonly providerCode?: string+ readonly providerStatus?: number+ constructor(stage: StorageProbeStage, upstream?: unknown) {super('Storage connection check failed')this.name = 'StorageConnectionError'this.stage = stagethis.code = `storage_${stage}_failed`+ // Only explicit protocol codes and an HTTP status may cross the boundary.+ // Never retain provider messages, response bodies, request IDs or URLs.+ if (upstream && typeof upstream === 'object') {+ const source = upstream as {+ name?: unknown+ $metadata?: { httpStatusCode?: unknown }+ }+ if (+ typeof source.name === 'string' &&+ [+ 'AccessDenied',+ 'InvalidAccessKeyId',+ 'SignatureDoesNotMatch',+ 'NoSuchBucket',+ 'NoSuchUpload',+ 'NotImplemented',+ 'InvalidRequest',+ 'InvalidArgument',+ 'MalformedXML',+ 'InternalError',+ 'ServiceUnavailable',+ 'SlowDown',+ 'RequestTimeout',+ ].includes(source.name)+ )+ this.providerCode = source.name+ const status = source.$metadata?.httpStatusCode+ if (+ typeof status === 'number' &&+ Number.isInteger(status) &&+ status >= 400 &&+ status <= 599+ )+ this.providerStatus = status+ }}}export interface StorageProbeClient {@@ -61,24 +101,48 @@ export async function probeStorageConnection(await response.body?.cancel().catch(() => undefined)}stage = 'multipart'- const id = await s3.multipart(key, 'application/octet-stream')+ const operation = async <T>(+ step: StorageProbeStage,+ run: () => Promise<T>,+ ) => {+ try {+ return await run()+ } catch (error) {+ throw new StorageConnectionError(step, error)+ }+ }+ const id = await operation('multipart_create', () =>+ s3.multipart(key, 'application/octet-stream'),+ )+ let multipartFailure: StorageConnectionError | undefinedtry {- if (!(await s3.recoverMultipart(key)).includes(id))- throw new Error('Multipart recovery failed')- } finally {- await s3.abort(key, id)+ const ids = await operation('multipart_list', () =>+ s3.recoverMultipart(key),+ )+ if (!ids.includes(id)) throw new StorageConnectionError('multipart_list')+ } catch (error) {+ multipartFailure = error as StorageConnectionError+ }+ try {+ await operation('multipart_abort', () => s3.abort(key, id))+ } catch (error) {+ multipartFailure ??= error as StorageConnectionError}+ if (multipartFailure) throw multipartFailurestage = 'cors'if (!(await s3.browserCors(origin, key)))throw new Error('Browser CORS failed')- } catch {- failure = new StorageConnectionError(stage)+ } catch (error) {+ failure =+ error instanceof StorageConnectionError+ ? error+ : new StorageConnectionError(stage, error)} finally {// The PUT may have succeeded even when its response was lost.try {await s3.remove(key)- } catch {- failure ??= new StorageConnectionError('cleanup')+ } catch (error) {+ failure ??= new StorageConnectionError('cleanup', error)}}if (failure) throw failurediff --git a/tests/cloud-storage-probe.test.ts b/tests/cloud-storage-probe.test.tsindex 19fddd7a..62116238 100644--- a/tests/cloud-storage-probe.test.ts+++ b/tests/cloud-storage-probe.test.ts@@ -70,7 +70,12 @@ test('Connection probe checks object operations and returns only safe fixed fail),(error) => {assert(error instanceof StorageConnectionError)- assert.equal(error.code, 'storage_' + stage + '_failed')+ assert.equal(+ error.code,+ 'storage_' ++ (stage === 'multipart' ? 'multipart_create' : stage) ++ '_failed',+ )assert(!JSON.stringify(error).includes('secret'))assert(!error.message.includes('private.example'))return true@@ -112,7 +117,7 @@ test('Probe detects truncated content and preserves the primary error when compe'https://portal.example',async () => new Response('ok'),),- { code: 'storage_multipart_failed' },+ { code: 'storage_multipart_list_failed' },)assert(broken.calls.includes('abort'))const redirect = fixture()@@ -187,3 +192,69 @@ test('Browser CORS checks do not require GetBucketCors or bucket administration'assert.deepEqual(methods, ['OPTIONS', 'OPTIONS', 'OPTIONS', 'GET'])}})++test('Multipart diagnostics identify each operation and redact arbitrary provider fields', async () => {+ for (const [method, step] of [+ ['multipart', 'create'],+ ['recoverMultipart', 'list'],+ ['abort', 'abort'],+ ] as const) {+ const f = fixture()+ f.client[method] = async () => {+ throw Object.assign(new Error('secret signed URL'), {+ name: 'AccessDenied',+ $metadata: { httpStatusCode: 403, requestId: 'secret' },+ response: 'secret',+ })+ }+ await assert.rejects(+ probeStorageConnection(+ f.client,+ 'https://portal.example',+ async () => new Response('ok'),+ ),+ (error) => {+ assert(error instanceof StorageConnectionError)+ assert.equal(error.code, `storage_multipart_${step}_failed`)+ assert.equal(error.providerCode, 'AccessDenied')+ assert.equal(error.providerStatus, 403)+ assert(!JSON.stringify(error).includes('secret'))+ return true+ },+ )+ assert(f.calls.includes('cleanup'))+ }+ const unknown = new StorageConnectionError('multipart_create', {+ name: 'https://secret.example',+ $metadata: { httpStatusCode: 'secret' },+ })+ assert.equal(unknown.providerCode, undefined)+ assert.equal(unknown.providerStatus, undefined)+ const both = fixture('recover')+ both.client.abort = async () => {+ throw new Error('abort also failed')+ }+ await assert.rejects(+ probeStorageConnection(+ both.client,+ 'https://portal.example',+ async () => new Response('ok'),+ ),+ {+ code: 'storage_multipart_list_failed',+ },+ )+ const absent = fixture()+ absent.client.recoverMultipart = async () => []+ await assert.rejects(+ probeStorageConnection(+ absent.client,+ 'https://portal.example',+ async () => new Response('ok'),+ ),+ {+ code: 'storage_multipart_list_failed',+ },+ )+ assert(absent.calls.includes('abort'))+})