Skip to content

Commit

Permalink
enable addition checks in routing images through a custom web proxy c…
Browse files Browse the repository at this point in the history
…ode, #312
  • Loading branch information
vladimiry committed Oct 19, 2020
1 parent b7c4484 commit ca5a312
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions src/electron-main/web-request/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const requestProxyCache = (() => {
| OnHeadersReceivedListenerDetails
| OnErrorOccurredListenerDetails
| OnCompletedListenerDetails;
type MapValue = { imageUrlProxified?: boolean; corsProxy?: CorsProxy };
type MapValue = { additionAllowedOrigin?: string; corsProxy?: CorsProxy };

const map = new Map<MapKeyArg["id"], MapValue>();

Expand Down Expand Up @@ -67,13 +67,6 @@ export function initWebRequestListenersByAccount(
}

const origins = {
externalContentProxyUrlPattern: (
enableExternalContentProxy
&&
externalContentProxyUrlPattern
&&
parseUrlOriginWithNullishCheck(externalContentProxyUrlPattern)
),
webClientEntryUrl: parseUrlOriginWithNullishCheck(webClient.entryUrl),
devTools: parseUrlOriginWithNullishCheck("devtools://devtools"),
} as const;
Expand All @@ -97,7 +90,7 @@ export function initWebRequestListenersByAccount(
if (
enableExternalContentProxy
&&
!requestProxyCache.get(details)?.imageUrlProxified // has not yet been proxified
!requestProxyCache.get(details)?.additionAllowedOrigin // has not yet been proxified (preventing infinity redirect loop)
&&
String(details.resourceType).toLowerCase() === "image"
&&
Expand All @@ -112,21 +105,35 @@ export function initWebRequestListenersByAccount(
if (!externalContentProxyUrlPattern) {
throw new Error(`Invalid "external content proxy URL pattern" value.`);
}
const redirectURL = externalContentProxyUrlPattern.replace(
ACCOUNT_EXTERNAL_CONTENT_PROXY_URL_REPLACE_PATTERN,
details.url,

const redirectURL = externalContentProxyUrlPattern.replace(ACCOUNT_EXTERNAL_CONTENT_PROXY_URL_REPLACE_PATTERN, url);

if (
redirectURL === externalContentProxyUrlPattern
||
!redirectURL.includes(url)
) {
throw new Error(`Failed to substitute "${url}" in "${externalContentProxyUrlPattern}" pattern.`);
}

requestProxyCache.patch(
details,
{additionAllowedOrigin: parseUrlOriginWithNullishCheck(redirectURL)},
);
requestProxyCache.patch(details, {imageUrlProxified: true});

callback({redirectURL});

return;
}

const additionAllowedOrigin = requestProxyCache.get(details)?.additionAllowedOrigin;

const bannedUrlAccessMsg: null | string = blockNonEntryUrlBasedRequests
? buildUrlOriginsFailedMsgTester([
...allowedOrigins,
...(
origins.externalContentProxyUrlPattern && requestProxyCache.get(details)?.imageUrlProxified
? [origins.externalContentProxyUrlPattern]
additionAllowedOrigin
? [additionAllowedOrigin]
: []
),
])(url)
Expand Down

0 comments on commit ca5a312

Please sign in to comment.