Cache basic auth (#220034)

This commit is contained in:
Christof Marti 2024-07-05 16:34:05 +02:00
parent ea8d37ead2
commit fd18af08ab

View file

@ -33,7 +33,7 @@ export function connectProxyResolver(
const doUseHostProxy = typeof useHostProxy === 'boolean' ? useHostProxy : !initData.remote.isRemote;
const params: ProxyAgentParams = {
resolveProxy: url => extHostWorkspace.resolveProxy(url),
lookupProxyAuthorization: lookupProxyAuthorization.bind(undefined, extHostWorkspace, extHostLogService, mainThreadTelemetry, configProvider, {}, initData.remote.isRemote),
lookupProxyAuthorization: lookupProxyAuthorization.bind(undefined, extHostWorkspace, extHostLogService, mainThreadTelemetry, configProvider, {}, {}, initData.remote.isRemote),
getProxyURL: () => configProvider.getConfiguration('http').get('proxy'),
getProxySupport: () => configProvider.getConfiguration('http').get<ProxySupportSetting>('proxySupport') || 'off',
getNoProxyConfig: () => configProvider.getConfiguration('http').get<string[]>('noProxy') || [],
@ -146,10 +146,11 @@ async function lookupProxyAuthorization(
mainThreadTelemetry: MainThreadTelemetryShape,
configProvider: ExtHostConfigProvider,
proxyAuthenticateCache: Record<string, string | string[] | undefined>,
basicAuthCache: Record<string, string | undefined>,
isRemote: boolean,
proxyURL: string,
proxyAuthenticate: string | string[] | undefined,
state: { kerberosRequested?: boolean; basicAuthAttempt?: number }
state: { kerberosRequested?: boolean; basicAuthCacheUsed?: boolean; basicAuthAttempt?: number }
): Promise<string | undefined> {
const cached = proxyAuthenticateCache[proxyURL];
if (proxyAuthenticate) {
@ -177,6 +178,17 @@ async function lookupProxyAuthorization(
const basicAuthHeader = authenticate.find(a => /^Basic( |$)/i.test(a));
if (basicAuthHeader) {
try {
const cachedAuth = basicAuthCache[proxyURL];
if (cachedAuth) {
if (state.basicAuthCacheUsed) {
extHostLogService.debug('ProxyResolver#lookupProxyAuthorization Basic authentication deleting cached credentials', `proxyURL:${proxyURL}`);
delete basicAuthCache[proxyURL];
} else {
extHostLogService.debug('ProxyResolver#lookupProxyAuthorization Basic authentication using cached credentials', `proxyURL:${proxyURL}`);
state.basicAuthCacheUsed = true;
return cachedAuth;
}
}
state.basicAuthAttempt = (state.basicAuthAttempt || 0) + 1;
const realm = / realm="([^"]+)"/i.exec(basicAuthHeader)?.[1];
extHostLogService.debug('ProxyResolver#lookupProxyAuthorization Basic authentication lookup', `proxyURL:${proxyURL}`, `realm:${realm}`);
@ -191,10 +203,15 @@ async function lookupProxyAuthorization(
};
const credentials = await extHostWorkspace.lookupAuthorization(authInfo);
if (credentials) {
return 'Basic ' + Buffer.from(`${credentials.username}:${credentials.password}`).toString('base64');
extHostLogService.debug('ProxyResolver#lookupProxyAuthorization Basic authentication received credentials', `proxyURL:${proxyURL}`, `realm:${realm}`);
const auth = 'Basic ' + Buffer.from(`${credentials.username}:${credentials.password}`).toString('base64');
basicAuthCache[proxyURL] = auth;
return auth;
} else {
extHostLogService.debug('ProxyResolver#lookupProxyAuthorization Basic authentication received no credentials', `proxyURL:${proxyURL}`, `realm:${realm}`);
}
} catch (err) {
extHostLogService.error('ProxyResolver#lookupProxyAuthorization Kerberos authentication failed', err);
extHostLogService.error('ProxyResolver#lookupProxyAuthorization Basic authentication failed', err);
}
}
return undefined;