Skip to content

Commit 760aaeb

Browse files
authored
Add support for proxy env var aliases (#677)
1 parent 8f9e25c commit 760aaeb

File tree

4 files changed

+48
-11
lines changed

4 files changed

+48
-11
lines changed

src/commands/fix/npm-fix.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export async function npmFix(
8282
definitions: npmConfigDefinitions,
8383
// Lazily access constants.execPath.
8484
execPath: constants.execPath,
85-
env: process.env,
85+
env: { ...process.env },
8686
flatten: npmConfigFlatten,
8787
npmPath,
8888
platform: process.platform,

src/constants.mts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ type ENV = Remap<
6262
INLINED_SYNP_VERSION: string
6363
LOCALAPPDATA: string
6464
NODE_COMPILE_CACHE: string
65+
NODE_EXTRA_CA_CERTS: string
6566
PATH: string
6667
SOCKET_CLI_ACCEPT_RISKS: boolean
6768
SOCKET_CLI_API_BASE_URL: string
@@ -308,6 +309,15 @@ const LAZY_ENV = () => {
308309
? // Lazily access constants.socketCachePath.
309310
constants.socketCachePath
310311
: '',
312+
// When set, the well known "root" CAs (like VeriSign) will be extended with
313+
// the extra certificates in file. The file should consist of one or more
314+
// trusted certificates in PEM format.
315+
// https://nodejs.org/api/cli.html#node_extra_ca_certsfile
316+
NODE_EXTRA_CA_CERTS:
317+
envAsString(env['NODE_EXTRA_CA_CERTS']) ||
318+
// Commonly used environment variable to specify the path to a single
319+
// PEM-encoded certificate file.
320+
envAsString(env['SSL_CERT_FILE']),
311321
// PATH is an environment variable that lists directories where executable
312322
// programs are located. When a command is run, the system searches these
313323
// directories to find the executable.
@@ -323,7 +333,11 @@ const LAZY_ENV = () => {
323333
// https://github.com/SocketDev/socket-cli?tab=readme-ov-file#environment-variables-for-development
324334
SOCKET_CLI_API_PROXY:
325335
envAsString(env['SOCKET_CLI_API_PROXY']) ||
326-
envAsString(env['SOCKET_SECURITY_API_PROXY']),
336+
envAsString(env['SOCKET_SECURITY_API_PROXY']) ||
337+
// Commonly used environment variables to specify routing requests through
338+
// a proxy server.
339+
envAsString(env['HTTPS_PROXY']) ||
340+
envAsString(env['https_proxy']),
327341
// Flag to set the API token.
328342
// https://github.com/SocketDev/socket-cli?tab=readme-ov-file#environment-variables
329343
SOCKET_CLI_API_TOKEN:

src/utils/sdk.mts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HttpsProxyAgent } from 'hpagent'
1+
import { HttpProxyAgent, HttpsProxyAgent } from 'hpagent'
22

33
import isInteractive from '@socketregistry/is-interactive/index.cjs'
44
import { password } from '@socketsecurity/registry/lib/prompts'
@@ -10,8 +10,6 @@ import constants from '../constants.mts'
1010

1111
import type { CResult } from '../types.mts'
1212

13-
const { SOCKET_PUBLIC_API_TOKEN } = constants
14-
1513
const TOKEN_PREFIX = 'sktsec_'
1614

1715
const { length: TOKEN_PREFIX_LENGTH } = TOKEN_PREFIX
@@ -21,15 +19,26 @@ function getDefaultApiBaseUrl(): string | undefined {
2119
const baseUrl =
2220
// Lazily access constants.ENV.SOCKET_CLI_API_BASE_URL.
2321
constants.ENV.SOCKET_CLI_API_BASE_URL || getConfigValueOrUndef('apiBaseUrl')
24-
return isNonEmptyString(baseUrl) ? baseUrl : undefined
22+
return isUrl(baseUrl) ? baseUrl : undefined
2523
}
2624

2725
// The API server that should be used for operations.
28-
function getDefaultHttpProxy(): string | undefined {
26+
function getDefaultProxyUrl(): string | undefined {
2927
const apiProxy =
3028
// Lazily access constants.ENV.SOCKET_CLI_API_PROXY.
3129
constants.ENV.SOCKET_CLI_API_PROXY || getConfigValueOrUndef('apiProxy')
32-
return isNonEmptyString(apiProxy) ? apiProxy : undefined
30+
return isUrl(apiProxy) ? apiProxy : undefined
31+
}
32+
33+
function isUrl(value: any): value is string {
34+
if (isNonEmptyString(value)) {
35+
try {
36+
// eslint-disable-next-line no-new
37+
new URL(value)
38+
return true
39+
} catch {}
40+
}
41+
return false
3342
}
3443

3544
// This API key should be stored globally for the duration of the CLI execution.
@@ -64,14 +73,15 @@ export function getPublicToken(): string {
6473
return (
6574
// Lazily access constants.ENV.SOCKET_CLI_API_TOKEN.
6675
(constants.ENV.SOCKET_CLI_API_TOKEN || getDefaultToken()) ??
67-
SOCKET_PUBLIC_API_TOKEN
76+
// Lazily access constants.SOCKET_PUBLIC_API_TOKEN.
77+
constants.SOCKET_PUBLIC_API_TOKEN
6878
)
6979
}
7080

7181
export async function setupSdk(
7282
apiToken: string | undefined = getDefaultToken(),
7383
apiBaseUrl: string | undefined = getDefaultApiBaseUrl(),
74-
proxy: string | undefined = getDefaultHttpProxy(),
84+
proxy: string | undefined,
7585
): Promise<CResult<SocketSdk>> {
7686
if (typeof apiToken !== 'string' && isInteractive()) {
7787
apiToken = await password({
@@ -87,10 +97,22 @@ export async function setupSdk(
8797
cause: 'You need to provide an API Token. Run `socket login` first.',
8898
}
8999
}
100+
if (!isUrl(proxy)) {
101+
proxy = getDefaultProxyUrl()
102+
}
103+
104+
const ProxyAgent = proxy?.startsWith('http:')
105+
? HttpProxyAgent
106+
: HttpsProxyAgent
107+
90108
return {
91109
ok: true,
92110
data: new SocketSdk(apiToken, {
93-
agent: proxy ? new HttpsProxyAgent({ proxy }) : undefined,
111+
agent: proxy
112+
? new ProxyAgent({
113+
proxy,
114+
})
115+
: undefined,
94116
baseUrl: apiBaseUrl,
95117
userAgent: createUserAgentFromPkgJson({
96118
// Lazily access constants.ENV.INLINED_SOCKET_CLI_NAME.

test/socket-npm.test.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ for (const npmDir of ['npm9', 'npm10', 'npm11']) {
5656
{
5757
cwd: path.join(npmFixturesPath, 'lacking-typosquat'),
5858
env: {
59+
...process.env,
5960
// Lazily access constants.ENV.PATH.
6061
PATH: `${npmBinPath}:${constants.ENV.PATH}`,
6162
},

0 commit comments

Comments
 (0)