From ed2b96276df32dab7e91a89dc3fa2f0237c68095 Mon Sep 17 00:00:00 2001 From: Jakob Ackermann Date: Mon, 13 Feb 2023 10:19:18 +0000 Subject: [PATCH] Merge pull request #11770 from overleaf/jpa-flag-403 [web] catch unexpected response status codes from user content domain GitOrigin-RevId: 654141e1be8721f92be271733aca70a7bc672973 --- .../util/fetchFromCompileDomain.ts | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/services/web/frontend/js/features/pdf-preview/util/fetchFromCompileDomain.ts b/services/web/frontend/js/features/pdf-preview/util/fetchFromCompileDomain.ts index c5b1d6f317..cb790cc63e 100644 --- a/services/web/frontend/js/features/pdf-preview/util/fetchFromCompileDomain.ts +++ b/services/web/frontend/js/features/pdf-preview/util/fetchFromCompileDomain.ts @@ -6,6 +6,24 @@ import { postJSON } from '../../../infrastructure/fetch-json' let useFallbackDomainUntil = performance.now() const ONE_HOUR_IN_MS = 1000 * 60 * 60 +class MaybeBlockedByProxyError extends OError {} + +function checkForBlockingByProxy(res: Response) { + const statusCode = res.status + switch (statusCode) { + case 200: // full response + case 206: // range response + case 404: // file not found + case 416: // range not found + return + default: + throw new MaybeBlockedByProxyError('request might be blocked by proxy', { + res, + statusCode, + }) + } +} + export async function fetchFromCompileDomain(url: string, init: RequestInit) { const userContentDomain = getMeta('ol-compilesUserContentDomain') let isUserContentDomain = @@ -17,9 +35,18 @@ export async function fetchFromCompileDomain(url: string, init: RequestInit) { url = withFallbackCompileDomain(url) } try { - return await fetch(url, init) + const res = await fetch(url, init) + if (isUserContentDomain) { + // Only throw a MaybeBlockedByProxyError when the request will be retried + // on the fallback domain below. + checkForBlockingByProxy(res) + } + return res } catch (err) { - if (isNetworkError(err) && isUserContentDomain) { + if ( + (isNetworkError(err) || err instanceof MaybeBlockedByProxyError) && + isUserContentDomain + ) { try { const res = await fetch(withFallbackCompileDomain(url), init) // Only switch to the fallback when fetch does not throw there as well.