Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/main/connect/connectionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,11 +422,22 @@ export class ConnectionUtils {
let httpProxy: string | undefined = vscode.workspace.getConfiguration().get('http.proxy');

if (!httpProxy) {
// Fall back to environment variables when no explicit VS Code proxy setting is configured
httpProxy = process.env['HTTPS_PROXY'] ?? process.env['HTTP_PROXY'];
}

if (!httpProxy) {
return false;
}

let proxyUri: URL;
try {
proxyUri = new URL(httpProxy);
} catch {
return false;
}

let proxyConfig: IProxyConfig = {} as IProxyConfig;
let proxyUri: URL = new URL(httpProxy);
proxyConfig.protocol = proxyUri.protocol;
proxyConfig.host = proxyUri.hostname;
if (proxyUri.port) {
Expand Down
7 changes: 5 additions & 2 deletions src/main/scanLogic/scanRunners/analyzerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,11 @@ export class AnalyzerManager {
let optional: IProxyConfig | boolean = ConnectionUtils.getProxyConfig();
if (optional) {
let proxyConfig: IProxyConfig = <IProxyConfig>optional;
proxyHttpsUrl = proxyConfig.protocol == 'https:' ? AnalyzerManager.toProxyUrl(proxyConfig) : proxyHttpsUrl;
proxyHttpUrl = proxyConfig.protocol == 'http:' ? AnalyzerManager.toProxyUrl(proxyConfig) : proxyHttpUrl;
// An HTTP proxy (http://) handles both HTTP and HTTPS traffic via CONNECT tunneling,
// so always set both proxy env vars from the single VS Code proxy configuration.
const proxyUrl: string = AnalyzerManager.toProxyUrl(proxyConfig);
proxyHttpsUrl = proxyUrl;
proxyHttpUrl = proxyUrl;
}

if (params?.tokenValidation && params.tokenValidation === true) {
Expand Down
48 changes: 48 additions & 0 deletions src/test/tests/connectionManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,54 @@ describe('Connection Manager Tests', () => {
assert.deepEqual(proxyAuthorization, 'testProxyAuthorization');
});

describe('getProxyConfig', () => {
afterEach(async () => {
await vscode.workspace.getConfiguration().update('http.proxy', undefined, true);
await vscode.workspace.getConfiguration().update('http.proxySupport', undefined, true);
delete process.env['HTTP_PROXY'];
delete process.env['HTTPS_PROXY'];
});

it('Returns proxy config from VS Code http.proxy setting', async () => {
await vscode.workspace.getConfiguration().update('http.proxy', 'http://proxy.example.com:8080', true);
const result: IProxyConfig | boolean = ConnectionUtils.getProxyConfig();
assert.isObject(result);
const proxyConfig: IProxyConfig = result as IProxyConfig;
assert.equal(proxyConfig.host, 'proxy.example.com');
assert.equal(proxyConfig.port, 8080);
});

it('Falls back to HTTPS_PROXY env var when http.proxy setting is not configured', () => {
process.env['HTTPS_PROXY'] = 'http://envproxy.example.com:3128';
const result: IProxyConfig | boolean = ConnectionUtils.getProxyConfig();
assert.isObject(result);
const proxyConfig: IProxyConfig = result as IProxyConfig;
assert.equal(proxyConfig.host, 'envproxy.example.com');
assert.equal(proxyConfig.port, 3128);
});

it('Falls back to HTTP_PROXY env var when http.proxy and HTTPS_PROXY are not set', () => {
process.env['HTTP_PROXY'] = 'http://httpenvproxy.example.com:8888';
const result: IProxyConfig | boolean = ConnectionUtils.getProxyConfig();
assert.isObject(result);
const proxyConfig: IProxyConfig = result as IProxyConfig;
assert.equal(proxyConfig.host, 'httpenvproxy.example.com');
assert.equal(proxyConfig.port, 8888);
});

it('Returns false when no proxy is configured', () => {
const result: IProxyConfig | boolean = ConnectionUtils.getProxyConfig();
assert.isFalse(result);
});

it('Returns false when proxySupport is off', async () => {
await vscode.workspace.getConfiguration().update('http.proxy', 'http://proxy.example.com:8080', true);
await vscode.workspace.getConfiguration().update('http.proxySupport', 'off', true);
const result: IProxyConfig | boolean = ConnectionUtils.getProxyConfig();
assert.isFalse(result);
});
});

describe('Populate credentials from env', async () => {
[
{
Expand Down
36 changes: 33 additions & 3 deletions src/test/tests/scanAnlayzerRunner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { assert } from 'chai';
import * as fs from 'fs';
import { describe } from 'mocha';
import * as path from 'path';
import * as vscode from 'vscode';
import { ConnectionManager } from '../../main/connect/connectionManager';
import { LogManager } from '../../main/log/logManager';

Expand Down Expand Up @@ -84,6 +85,11 @@ describe('Analyzer BinaryRunner tests', async () => {
})(connection, logManager);
}

afterEach(() => {
delete process.env['HTTP_PROXY'];
delete process.env['HTTPS_PROXY'];
});

[
{
name: 'With password credentials',
Expand Down Expand Up @@ -122,7 +128,7 @@ describe('Analyzer BinaryRunner tests', async () => {
user: '',
pass: '',
token: 'access-token',
proxy: 'proxyUrlEnvVarValue',
proxy: 'http://proxy.example.com:8080',
logPath: undefined
},
{
Expand All @@ -138,8 +144,14 @@ describe('Analyzer BinaryRunner tests', async () => {
].forEach(test => {
it('Create environment variables for execution - ' + test.name, () => {
let runner: AnalyzerManager = createDummyAnalyzerManager(createBinaryRunnerConnectionManager(test.url, test.user, test.pass, test.token));
process.env['HTTP_PROXY'] = test.proxy;
process.env['HTTPS_PROXY'] = test.proxy;

if (test.proxy !== undefined) {
process.env['HTTP_PROXY'] = test.proxy;
process.env['HTTPS_PROXY'] = test.proxy;
} else {
delete process.env['HTTP_PROXY'];
delete process.env['HTTPS_PROXY'];
}

let envVars: NodeJS.ProcessEnv | undefined = runner.createEnvForRun({ executionLogDirectory: test.logPath });
if (test.shouldFail) {
Expand All @@ -162,6 +174,24 @@ describe('Analyzer BinaryRunner tests', async () => {
});
});

it('Create environment variables for execution - With VS Code http.proxy setting (http:// proxy sets both HTTP_PROXY and HTTPS_PROXY)', async () => {
const proxyUrl: string = 'http://vscodeproxy.example.com:8080';
await vscode.workspace.getConfiguration().update('http.proxy', proxyUrl, true);
delete process.env['HTTP_PROXY'];
delete process.env['HTTPS_PROXY'];

try {
let runner: AnalyzerManager = createDummyAnalyzerManager(createBinaryRunnerConnectionManager('platformUrl', '', '', 'access-token'));
let envVars: NodeJS.ProcessEnv | undefined = runner.createEnvForRun({});
assert.isDefined(envVars);
// An http:// proxy must be set for both protocols since it handles HTTPS via CONNECT tunneling
assert.equal(envVars?.[AnalyzerManager.ENV_HTTP_PROXY], proxyUrl);
assert.equal(envVars?.[AnalyzerManager.ENV_HTTPS_PROXY], proxyUrl);
} finally {
await vscode.workspace.getConfiguration().update('http.proxy', undefined, true);
}
});

[
{
name: 'One root',
Expand Down
Loading