Skip to content

Commit

Permalink
SCANNPM-58 Avoid conflicts between axios's proxy option and custom ht…
Browse files Browse the repository at this point in the history
…tp(s) agents
  • Loading branch information
7PH committed Nov 26, 2024
1 parent 438ef42 commit cfb6f7b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ async function extractTruststoreCerts(p12Base64: string, password: string = ''):

export async function getHttpAgents(
properties: ScannerProperties,
): Promise<Pick<AxiosRequestConfig, 'httpAgent' | 'httpsAgent'>> {
const agents: Pick<AxiosRequestConfig, 'httpAgent' | 'httpsAgent'> = {};
): Promise<Pick<AxiosRequestConfig, 'httpAgent' | 'httpsAgent' | 'proxy'>> {
const agents: Pick<AxiosRequestConfig, 'httpAgent' | 'httpsAgent' | 'proxy'> = {};
const proxyUrl = getProxyUrl(properties);

// Accumulate https agent options
Expand Down Expand Up @@ -94,6 +94,7 @@ export async function getHttpAgents(
if (proxyUrl) {
agents.httpsAgent = new HttpsProxyAgent({ proxy: proxyUrl.toString(), ...httpsAgentOptions });
agents.httpAgent = new HttpProxyAgent({ proxy: proxyUrl.toString() });
agents.proxy = false; // SCANNPM-58 Avoid conflicts between axios's proxy option and custom http(s) agents
} else if (Object.keys(httpsAgentOptions).length > 0) {
// Only create an agent if there are options
agents.httpsAgent = new https.Agent({ ...httpsAgentOptions });
Expand Down
15 changes: 11 additions & 4 deletions test/unit/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ describe('request', () => {
expect(agents.httpAgent?.proxy.toString()).toBe('http://proxy.com/');
expect(agents.httpsAgent).toBeInstanceOf(HttpsProxyAgent);
expect(agents.httpsAgent?.proxy.toString()).toBe('http://proxy.com/');
expect(agents.proxy).toBe(false);
});

it('should not define agents when no proxy is provided', async () => {
Expand All @@ -56,6 +57,7 @@ describe('request', () => {
});
expect(agents.httpAgent).toBeUndefined();
expect(agents.httpsAgent).toBeUndefined();
expect(agents.proxy).toBeUndefined();
expect(agents).toEqual({});
});
});
Expand Down Expand Up @@ -90,12 +92,13 @@ describe('request', () => {
const truststorePath = path.join(__dirname, 'fixtures', 'ssl', 'truststore-invalid.p12');
const truststorePass = 'password';

const { httpsAgent } = await getHttpAgents({
const { proxy, httpsAgent } = await getHttpAgents({
[ScannerProperty.SonarHostUrl]: SONARCLOUD_URL,
[ScannerProperty.SonarScannerTruststorePath]: truststorePath,
[ScannerProperty.SonarScannerTruststorePassword]: truststorePass,
});

expect(proxy).toBeUndefined();
expect(httpsAgent).toBeUndefined();
expect(logging.log).toHaveBeenCalledWith(
logging.LogLevel.WARN,
Expand All @@ -108,12 +111,13 @@ describe('request', () => {
const truststorePath = path.join(__dirname, 'fixtures', 'ssl', 'truststore-empty.p12');
const truststorePass = 'password';

const { httpsAgent } = await getHttpAgents({
const { proxy, httpsAgent } = await getHttpAgents({
[ScannerProperty.SonarHostUrl]: SONARCLOUD_URL,
[ScannerProperty.SonarScannerTruststorePath]: truststorePath,
[ScannerProperty.SonarScannerTruststorePassword]: truststorePass,
});

expect(proxy).toBeUndefined();
const ca = httpsAgent?.options.ca as string[];
expect(ca).toHaveLength(0);
});
Expand All @@ -123,12 +127,13 @@ describe('request', () => {
const keystorePath = path.join(__dirname, 'fixtures', 'ssl', 'keystore.p12');
const keystorePass = 'password';

const { httpsAgent } = await getHttpAgents({
const { proxy, httpsAgent } = await getHttpAgents({
[ScannerProperty.SonarHostUrl]: SONARCLOUD_URL,
[ScannerProperty.SonarScannerKeystorePath]: keystorePath,
[ScannerProperty.SonarScannerKeystorePassword]: keystorePass,
});

expect(proxy).toBeUndefined();
expect(httpsAgent?.options.pfx).toEqual(fsExtra.readFileSync(keystorePath));
expect(httpsAgent?.options.passphrase).toBe(keystorePass);
});
Expand All @@ -146,7 +151,7 @@ describe('request', () => {
const keystorePath = path.join(__dirname, 'fixtures', 'ssl', 'keystore.p12');
const keystorePass = 'password';

const { httpsAgent } = await getHttpAgents({
const { httpsAgent, proxy } = await getHttpAgents({
[ScannerProperty.SonarHostUrl]: SONARCLOUD_URL,
[ScannerProperty.SonarScannerProxyHost]: 'proxy.com',
[ScannerProperty.SonarScannerTruststorePath]: truststorePath,
Expand All @@ -155,6 +160,8 @@ describe('request', () => {
[ScannerProperty.SonarScannerKeystorePassword]: keystorePass,
});

expect(proxy).toBe(false);

const ca = httpsAgent?.options.ca as string[];
expect(ca).toHaveLength(1);
expect(ca).toContain(certificatePem);
Expand Down

0 comments on commit cfb6f7b

Please sign in to comment.