Treat 403 from CDN0 the same as a 404 from others CDNs

This commit is contained in:
trevor-signal
2025-08-11 15:21:21 -04:00
committed by GitHub
parent eac9a69e20
commit 64dfe6432e
2 changed files with 68 additions and 0 deletions

View File

@@ -122,6 +122,67 @@ describe('utils/downloadAttachment', () => {
]);
});
it('throw permanently missing error if attachment fails with 403 from cdn 0 and no backup information', async () => {
const stubDownload = sinon
.stub()
.onFirstCall()
.throws(new HTTPError('not found', { code: 403, headers: {} }));
const attachment = { ...baseAttachment, cdnNumber: 0 };
await assert.isRejected(
downloadAttachment({
attachment,
options: {
hasMediaBackups: true,
onSizeUpdate: noop,
abortSignal: abortController.signal,
},
dependencies: {
downloadAttachmentFromLocalBackup: stubDownload,
downloadAttachmentFromServer: stubDownload,
},
}),
AttachmentPermanentlyUndownloadableError
);
assert.equal(stubDownload.callCount, 1);
assertDownloadArgs(stubDownload.getCall(0).args, [
fakeServer,
{ attachment, mediaTier: MediaTier.STANDARD },
{
variant: AttachmentVariant.Default,
onSizeUpdate: noop,
abortSignal: abortController.signal,
logPrefix: '[REDACTED]est',
},
]);
});
it('throw permanently missing error if attachment fails with 403 with no cdn number and no backup information', async () => {
const stubDownload = sinon
.stub()
.onFirstCall()
.throws(new HTTPError('not found', { code: 403, headers: {} }));
// nullish cdn number gets converted to 0
const attachment = { ...baseAttachment, cdnNumber: undefined };
await assert.isRejected(
downloadAttachment({
attachment,
options: {
hasMediaBackups: true,
onSizeUpdate: noop,
abortSignal: abortController.signal,
},
dependencies: {
downloadAttachmentFromLocalBackup: stubDownload,
downloadAttachmentFromServer: stubDownload,
},
}),
AttachmentPermanentlyUndownloadableError
);
});
it('downloads from backup tier first if there is backup information', async () => {
const stubDownload = sinon.stub();
const attachment = backupableAttachment;

View File

@@ -138,6 +138,13 @@ export async function downloadAttachment({
// then start returning 404
if (error instanceof HTTPError && error.code === 404) {
throw new AttachmentPermanentlyUndownloadableError(`HTTP ${error.code}`);
} else if (
error instanceof HTTPError &&
// CDN 0 can return 403 which means the same as 404 from other CDNs
error.code === 403 &&
(attachment.cdnNumber == null || attachment.cdnNumber === 0)
) {
throw new AttachmentPermanentlyUndownloadableError(`HTTP ${error.code}`);
} else {
throw error;
}