Skip to content

Commit

Permalink
SCANNPM-47 Assume all proxies use http protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
7PH committed Sep 16, 2024
1 parent e06fd72 commit 999d80c
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 16 deletions.
15 changes: 3 additions & 12 deletions src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,15 @@ import { URL } from 'url';
import { LogLevel, log } from './logging';
import { ScannerProperties, ScannerProperty } from './types';

const DEFAULT_HTTPS_PROXY_PORT = 443;
const DEFAULT_HTTP_PROXY_PORT = 80;

export function getProxyUrl(properties: ScannerProperties): URL | undefined {
const proxyHost = properties[ScannerProperty.SonarScannerProxyHost];
const serverUsesHttps = properties[ScannerProperty.SonarHostUrl].startsWith('https');

if (proxyHost) {
// We assume that the proxy protocol is the same as the endpoint.
const protocol = serverUsesHttps ? 'https' : 'http';
const proxyPort =
properties[ScannerProperty.SonarScannerProxyPort] ??
(serverUsesHttps ? DEFAULT_HTTPS_PROXY_PORT : DEFAULT_HTTP_PROXY_PORT);
const proxyPort = properties[ScannerProperty.SonarScannerProxyPort] ?? '80';
const proxyUser = properties[ScannerProperty.SonarScannerProxyUser] ?? '';
const proxyPassword = properties[ScannerProperty.SonarScannerProxyPassword] ?? '';
const proxyUrl = new URL(
`${protocol}://${proxyUser}:${proxyPassword}@${proxyHost}:${proxyPort}`,
);
// SCANNPM-47 We assume the proxy is HTTP. HTTPS proxies are not supported by the scanner yet (CONNECT over TLS)
const proxyUrl = new URL(`http://${proxyUser}:${proxyPassword}@${proxyHost}:${proxyPort}`);
log(LogLevel.DEBUG, `Detecting proxy: ${proxyUrl}`);
return proxyUrl;
} else if (
Expand Down
2 changes: 1 addition & 1 deletion test/unit/proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('proxy', () => {
};
getProxyUrl(properties);

expect(getProxyUrl(properties)?.toString()).toBe('https://some-proxy.io/');
expect(getProxyUrl(properties)?.toString()).toBe('http://some-proxy.io/');
});

it('should detect proxy with host and port', () => {
Expand Down
6 changes: 3 additions & 3 deletions test/unit/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ describe('request', () => {
[ScannerProperty.SonarScannerProxyHost]: 'proxy.com',
});
expect(agents.httpAgent).toBeInstanceOf(HttpProxyAgent);
expect(agents.httpAgent?.proxy.toString()).toBe('https://proxy.com/');
expect(agents.httpAgent?.proxy.toString()).toBe('http://proxy.com/');
expect(agents.httpsAgent).toBeInstanceOf(HttpsProxyAgent);
expect(agents.httpsAgent?.proxy.toString()).toBe('https://proxy.com/');
expect(agents.httpsAgent?.proxy.toString()).toBe('http://proxy.com/');
});

it('should not define agents when no proxy is provided', async () => {
Expand Down Expand Up @@ -160,7 +160,7 @@ describe('request', () => {
expect(ca).toContain(certificatePem);
expect(httpsAgent?.options.pfx).toEqual(fsExtra.readFileSync(keystorePath));
expect(httpsAgent?.options.passphrase).toBe(keystorePass);
expect(httpsAgent?.proxy.toString()).toBe('https://proxy.com/');
expect(httpsAgent?.proxy.toString()).toBe('http://proxy.com/');
});
});

Expand Down

0 comments on commit 999d80c

Please sign in to comment.