@aikidosec/safe-chain 0.0.1-custom-install-dir

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. package/LICENSE +674 -0
  2. package/README.md +537 -0
  3. package/bin/aikido-bun.js +14 -0
  4. package/bin/aikido-bunx.js +14 -0
  5. package/bin/aikido-npm.js +14 -0
  6. package/bin/aikido-npx.js +14 -0
  7. package/bin/aikido-pip.js +17 -0
  8. package/bin/aikido-pip3.js +17 -0
  9. package/bin/aikido-pipx.js +16 -0
  10. package/bin/aikido-pnpm.js +14 -0
  11. package/bin/aikido-pnpx.js +14 -0
  12. package/bin/aikido-poetry.js +13 -0
  13. package/bin/aikido-python.js +19 -0
  14. package/bin/aikido-python3.js +19 -0
  15. package/bin/aikido-uv.js +16 -0
  16. package/bin/aikido-uvx.js +16 -0
  17. package/bin/aikido-yarn.js +14 -0
  18. package/bin/safe-chain.js +147 -0
  19. package/docs/Release.md +25 -0
  20. package/docs/banner.svg +151 -0
  21. package/docs/safe-package-manager-demo.gif +0 -0
  22. package/docs/safe-package-manager-demo.png +0 -0
  23. package/docs/shell-integration.md +149 -0
  24. package/docs/troubleshooting.md +321 -0
  25. package/npm-shrinkwrap.json +3180 -0
  26. package/package.json +71 -0
  27. package/src/api/aikido.js +187 -0
  28. package/src/api/npmApi.js +71 -0
  29. package/src/config/cliArguments.js +161 -0
  30. package/src/config/configFile.js +327 -0
  31. package/src/config/environmentVariables.js +57 -0
  32. package/src/config/safeChainDir.js +71 -0
  33. package/src/config/settings.js +247 -0
  34. package/src/environment/environment.js +14 -0
  35. package/src/environment/userInteraction.js +122 -0
  36. package/src/installLocation.js +42 -0
  37. package/src/main.js +123 -0
  38. package/src/packagemanager/_shared/commandErrors.js +17 -0
  39. package/src/packagemanager/_shared/matchesCommand.js +18 -0
  40. package/src/packagemanager/bun/createBunPackageManager.js +48 -0
  41. package/src/packagemanager/currentPackageManager.js +82 -0
  42. package/src/packagemanager/npm/createPackageManager.js +72 -0
  43. package/src/packagemanager/npm/dependencyScanner/commandArgumentScanner.js +74 -0
  44. package/src/packagemanager/npm/dependencyScanner/nullScanner.js +9 -0
  45. package/src/packagemanager/npm/parsing/parsePackagesFromInstallArgs.js +144 -0
  46. package/src/packagemanager/npm/runNpmCommand.js +20 -0
  47. package/src/packagemanager/npm/utils/abbrevs-generated.js +359 -0
  48. package/src/packagemanager/npm/utils/cmd-list.js +174 -0
  49. package/src/packagemanager/npm/utils/npmCommands.js +34 -0
  50. package/src/packagemanager/npx/createPackageManager.js +15 -0
  51. package/src/packagemanager/npx/dependencyScanner/commandArgumentScanner.js +43 -0
  52. package/src/packagemanager/npx/parsing/parsePackagesFromArguments.js +130 -0
  53. package/src/packagemanager/npx/runNpxCommand.js +20 -0
  54. package/src/packagemanager/pip/createPackageManager.js +25 -0
  55. package/src/packagemanager/pip/pipSettings.js +6 -0
  56. package/src/packagemanager/pip/runPipCommand.js +209 -0
  57. package/src/packagemanager/pipx/createPipXPackageManager.js +18 -0
  58. package/src/packagemanager/pipx/runPipXCommand.js +60 -0
  59. package/src/packagemanager/pnpm/createPackageManager.js +57 -0
  60. package/src/packagemanager/pnpm/dependencyScanner/commandArgumentScanner.js +35 -0
  61. package/src/packagemanager/pnpm/parsing/parsePackagesFromArguments.js +109 -0
  62. package/src/packagemanager/pnpm/runPnpmCommand.js +32 -0
  63. package/src/packagemanager/poetry/createPoetryPackageManager.js +72 -0
  64. package/src/packagemanager/uv/createUvPackageManager.js +18 -0
  65. package/src/packagemanager/uv/runUvCommand.js +66 -0
  66. package/src/packagemanager/uvx/createUvxPackageManager.js +18 -0
  67. package/src/packagemanager/yarn/createPackageManager.js +41 -0
  68. package/src/packagemanager/yarn/dependencyScanner/commandArgumentScanner.js +35 -0
  69. package/src/packagemanager/yarn/parsing/parsePackagesFromArguments.js +128 -0
  70. package/src/packagemanager/yarn/runYarnCommand.js +36 -0
  71. package/src/registryProxy/certBundle.js +203 -0
  72. package/src/registryProxy/certUtils.js +178 -0
  73. package/src/registryProxy/getConnectTimeout.js +13 -0
  74. package/src/registryProxy/http-utils.js +80 -0
  75. package/src/registryProxy/interceptors/createInterceptorForEcoSystem.js +25 -0
  76. package/src/registryProxy/interceptors/interceptorBuilder.js +179 -0
  77. package/src/registryProxy/interceptors/minimumPackageAgeExclusions.js +33 -0
  78. package/src/registryProxy/interceptors/npm/modifyNpmInfo.js +180 -0
  79. package/src/registryProxy/interceptors/npm/npmInterceptor.js +101 -0
  80. package/src/registryProxy/interceptors/npm/parseNpmPackageUrl.js +60 -0
  81. package/src/registryProxy/interceptors/pip/modifyPipInfo.js +167 -0
  82. package/src/registryProxy/interceptors/pip/modifyPipJsonResponse.js +176 -0
  83. package/src/registryProxy/interceptors/pip/parsePipPackageUrl.js +162 -0
  84. package/src/registryProxy/interceptors/pip/pipInterceptor.js +122 -0
  85. package/src/registryProxy/interceptors/pip/pipMetadataResponseUtils.js +27 -0
  86. package/src/registryProxy/interceptors/pip/pipMetadataVersionUtils.js +131 -0
  87. package/src/registryProxy/interceptors/suppressedVersionsState.js +21 -0
  88. package/src/registryProxy/isImdsEndpoint.js +13 -0
  89. package/src/registryProxy/mitmRequestHandler.js +240 -0
  90. package/src/registryProxy/plainHttpProxy.js +95 -0
  91. package/src/registryProxy/registryProxy.js +255 -0
  92. package/src/registryProxy/tunnelRequestHandler.js +213 -0
  93. package/src/scanning/audit/index.js +129 -0
  94. package/src/scanning/index.js +82 -0
  95. package/src/scanning/malwareDatabase.js +131 -0
  96. package/src/scanning/newPackagesDatabaseBuilder.js +71 -0
  97. package/src/scanning/newPackagesDatabaseWarnings.js +17 -0
  98. package/src/scanning/newPackagesListCache.js +126 -0
  99. package/src/scanning/packageNameVariants.js +29 -0
  100. package/src/shell-integration/helpers.js +296 -0
  101. package/src/shell-integration/path-wrappers/templates/unix-wrapper.template.sh +37 -0
  102. package/src/shell-integration/path-wrappers/templates/windows-wrapper.template.cmd +25 -0
  103. package/src/shell-integration/setup-ci.js +152 -0
  104. package/src/shell-integration/setup.js +110 -0
  105. package/src/shell-integration/shellDetection.js +39 -0
  106. package/src/shell-integration/startup-scripts/init-fish.fish +122 -0
  107. package/src/shell-integration/startup-scripts/init-posix.sh +112 -0
  108. package/src/shell-integration/startup-scripts/init-pwsh.ps1 +176 -0
  109. package/src/shell-integration/supported-shells/bash.js +222 -0
  110. package/src/shell-integration/supported-shells/fish.js +97 -0
  111. package/src/shell-integration/supported-shells/powershell.js +102 -0
  112. package/src/shell-integration/supported-shells/windowsPowershell.js +102 -0
  113. package/src/shell-integration/supported-shells/zsh.js +94 -0
  114. package/src/shell-integration/teardown.js +114 -0
  115. package/src/utils/safeSpawn.js +153 -0
  116. package/tsconfig.json +21 -0
@@ -0,0 +1,327 @@
1
+ import fs from "fs";
2
+ import path from "path";
3
+ import os from "os";
4
+ import { ui } from "../environment/userInteraction.js";
5
+ import { getEcoSystem } from "./settings.js";
6
+ import { getSafeChainBaseDir } from "./safeChainDir.js";
7
+
8
+ /**
9
+ * @typedef {Object} SafeChainConfig
10
+ *
11
+ * We cannot trust the input and should add the necessary validations
12
+ * @property {unknown | Number} scanTimeout
13
+ * @property {unknown | Number} minimumPackageAgeHours
14
+ * @property {unknown | string} malwareListBaseUrl
15
+ * @property {unknown | SafeChainRegistryConfiguration} npm
16
+ * @property {unknown | SafeChainRegistryConfiguration} pip
17
+ *
18
+ * @typedef {Object} SafeChainRegistryConfiguration
19
+ * We cannot trust the input and should add the necessary validations.
20
+ * @property {unknown | string[]} customRegistries
21
+ * @property {unknown | string[]} minimumPackageAgeExclusions
22
+ */
23
+
24
+ /**
25
+ * @returns {number}
26
+ */
27
+ export function getScanTimeout() {
28
+ const config = readConfigFile();
29
+
30
+ if (process.env.AIKIDO_SCAN_TIMEOUT_MS) {
31
+ const scanTimeout = validateTimeout(process.env.AIKIDO_SCAN_TIMEOUT_MS);
32
+ if (scanTimeout != null) {
33
+ return scanTimeout;
34
+ }
35
+ }
36
+
37
+ if (config.scanTimeout) {
38
+ const scanTimeout = validateTimeout(config.scanTimeout);
39
+ if (scanTimeout != null) {
40
+ return scanTimeout;
41
+ }
42
+ }
43
+
44
+ return 10000; // Default to 10 seconds
45
+ }
46
+
47
+ /**
48
+ *
49
+ * @param {any} value
50
+ * @returns {number?}
51
+ */
52
+ function validateTimeout(value) {
53
+ const timeout = Number(value);
54
+ if (!Number.isNaN(timeout) && timeout > 0) {
55
+ return timeout;
56
+ }
57
+ return null;
58
+ }
59
+
60
+ /**
61
+ * @param {any} value
62
+ * @returns {number | undefined}
63
+ */
64
+ function validateMinimumPackageAgeHours(value) {
65
+ const hours = Number(value);
66
+ if (!Number.isNaN(hours)) {
67
+ return hours;
68
+ }
69
+ return undefined;
70
+ }
71
+
72
+ /**
73
+ * Gets the minimum package age in hours from config file only
74
+ * @returns {number | undefined}
75
+ */
76
+ export function getMinimumPackageAgeHours() {
77
+ const config = readConfigFile();
78
+ if (config.minimumPackageAgeHours !== undefined) {
79
+ const validated = validateMinimumPackageAgeHours(
80
+ config.minimumPackageAgeHours
81
+ );
82
+ if (validated !== undefined) {
83
+ return validated;
84
+ }
85
+ }
86
+ return undefined;
87
+ }
88
+
89
+ /**
90
+ * Gets the malware list base URL from config file only
91
+ * @returns {string | undefined}
92
+ */
93
+ export function getMalwareListBaseUrl() {
94
+ const config = readConfigFile();
95
+ if (config.malwareListBaseUrl && typeof config.malwareListBaseUrl === "string") {
96
+ return config.malwareListBaseUrl;
97
+ }
98
+ return undefined;
99
+ }
100
+
101
+ /**
102
+ * Gets the custom npm registries from the config file (format parsing only, no validation)
103
+ * @returns {string[]}
104
+ */
105
+ export function getNpmCustomRegistries() {
106
+ const config = readConfigFile();
107
+
108
+ if (!config || !config.npm) {
109
+ return [];
110
+ }
111
+
112
+ // TypeScript needs help understanding that config.npm exists and has customRegistries
113
+ const npmConfig = /** @type {SafeChainRegistryConfiguration} */ (config.npm);
114
+ const customRegistries = npmConfig.customRegistries;
115
+
116
+ if (!Array.isArray(customRegistries)) {
117
+ return [];
118
+ }
119
+
120
+ return customRegistries.filter((item) => typeof item === "string");
121
+ }
122
+
123
+ /**
124
+ * Gets the custom npm registries from the config file (format parsing only, no validation)
125
+ * @returns {string[]}
126
+ */
127
+ export function getPipCustomRegistries() {
128
+ const config = readConfigFile();
129
+
130
+ if (!config || !config.pip) {
131
+ return [];
132
+ }
133
+
134
+ // TypeScript needs help understanding that config.pip exists and has customRegistries
135
+ const pipConfig = /** @type {SafeChainRegistryConfiguration} */ (config.pip);
136
+ const customRegistries = pipConfig.customRegistries;
137
+
138
+ if (!Array.isArray(customRegistries)) {
139
+ return [];
140
+ }
141
+
142
+ return customRegistries.filter((item) => typeof item === "string");
143
+ }
144
+
145
+ /**
146
+ * Gets the minimum package age exclusions from the config file for the current ecosystem
147
+ * @returns {string[]}
148
+ */
149
+ export function getMinimumPackageAgeExclusions() {
150
+ const config = readConfigFile();
151
+ const ecosystem = getEcoSystem();
152
+ const registryConfig = ecosystem === "py" ? config.pip : config.npm;
153
+
154
+ if (!config || !registryConfig) {
155
+ return [];
156
+ }
157
+
158
+ const typedRegistryConfig =
159
+ /** @type {SafeChainRegistryConfiguration} */ (registryConfig);
160
+ const exclusions = typedRegistryConfig.minimumPackageAgeExclusions;
161
+
162
+ if (!Array.isArray(exclusions)) {
163
+ return [];
164
+ }
165
+
166
+ return exclusions.filter((item) => typeof item === "string");
167
+ }
168
+
169
+ /**
170
+ * @param {import("../api/aikido.js").MalwarePackage[]} data
171
+ * @param {string | number} version
172
+ *
173
+ * @returns {void}
174
+ */
175
+ export function writeDatabaseToLocalCache(data, version) {
176
+ try {
177
+ const databasePath = getDatabasePath();
178
+ const versionPath = getDatabaseVersionPath();
179
+
180
+ fs.writeFileSync(databasePath, JSON.stringify(data));
181
+ fs.writeFileSync(versionPath, version.toString());
182
+ } catch {
183
+ ui.writeWarning(
184
+ "Failed to write malware database to local cache, next time the database will be fetched from the server again."
185
+ );
186
+ }
187
+ }
188
+
189
+ /**
190
+ * @returns {{malwareDatabase: import("../api/aikido.js").MalwarePackage[] | null, version: string | null}}
191
+ */
192
+ export function readDatabaseFromLocalCache() {
193
+ try {
194
+ const databasePath = getDatabasePath();
195
+ if (!fs.existsSync(databasePath)) {
196
+ return {
197
+ malwareDatabase: null,
198
+ version: null,
199
+ };
200
+ }
201
+ const data = fs.readFileSync(databasePath, "utf8");
202
+ const malwareDatabase = JSON.parse(data);
203
+ const versionPath = getDatabaseVersionPath();
204
+ let version = null;
205
+ if (fs.existsSync(versionPath)) {
206
+ version = fs.readFileSync(versionPath, "utf8").trim();
207
+ }
208
+ return {
209
+ malwareDatabase: malwareDatabase,
210
+ version: version,
211
+ };
212
+ } catch {
213
+ ui.writeWarning(
214
+ "Failed to read malware database from local cache. Continuing without local cache."
215
+ );
216
+ return {
217
+ malwareDatabase: null,
218
+ version: null,
219
+ };
220
+ }
221
+ }
222
+
223
+ /**
224
+ * @returns {SafeChainConfig}
225
+ */
226
+ function readConfigFile() {
227
+ /** @type {SafeChainConfig} */
228
+ const emptyConfig = {
229
+ scanTimeout: undefined,
230
+ minimumPackageAgeHours: undefined,
231
+ malwareListBaseUrl: undefined,
232
+ npm: {
233
+ customRegistries: undefined,
234
+ },
235
+ pip: {
236
+ customRegistries: undefined,
237
+ },
238
+ };
239
+
240
+ const configFilePath = getConfigFilePath();
241
+
242
+ if (!fs.existsSync(configFilePath)) {
243
+ return emptyConfig;
244
+ }
245
+
246
+ try {
247
+ const data = fs.readFileSync(configFilePath, "utf8");
248
+ return JSON.parse(data);
249
+ } catch {
250
+ return emptyConfig;
251
+ }
252
+ }
253
+
254
+ /**
255
+ * @returns {string}
256
+ */
257
+ function getDatabasePath() {
258
+ const aikidoDir = getAikidoDirectory();
259
+ const ecosystem = getEcoSystem();
260
+ return path.join(aikidoDir, `malwareDatabase_${ecosystem}.json`);
261
+ }
262
+
263
+ function getDatabaseVersionPath() {
264
+ const aikidoDir = getAikidoDirectory();
265
+ const ecosystem = getEcoSystem();
266
+ return path.join(aikidoDir, `version_${ecosystem}.txt`);
267
+ }
268
+
269
+ /**
270
+ * @returns {string}
271
+ */
272
+ export function getNewPackagesListPath() {
273
+ const safeChainDir = getSafeChainDirectory();
274
+ const ecosystem = getEcoSystem();
275
+ return path.join(safeChainDir, `newPackagesList_${ecosystem}.json`);
276
+ }
277
+
278
+ /**
279
+ * @returns {string}
280
+ */
281
+ export function getNewPackagesListVersionPath() {
282
+ const safeChainDir = getSafeChainDirectory();
283
+ const ecosystem = getEcoSystem();
284
+ return path.join(safeChainDir, `newPackagesList_version_${ecosystem}.txt`);
285
+ }
286
+
287
+ /**
288
+ * @returns {string}
289
+ */
290
+ function getConfigFilePath() {
291
+ const primaryPath = path.join(getSafeChainDirectory(), "config.json");
292
+ if (fs.existsSync(primaryPath)) {
293
+ return primaryPath;
294
+ }
295
+
296
+ const legacyPath = path.join(getAikidoDirectory(), "config.json");
297
+ if (fs.existsSync(legacyPath)) {
298
+ return legacyPath;
299
+ }
300
+
301
+ return primaryPath;
302
+ }
303
+
304
+ /**
305
+ * @returns {string}
306
+ */
307
+ export function getSafeChainDirectory() {
308
+ const safeChainDir = getSafeChainBaseDir();
309
+
310
+ if (!fs.existsSync(safeChainDir)) {
311
+ fs.mkdirSync(safeChainDir, { recursive: true });
312
+ }
313
+ return safeChainDir;
314
+ }
315
+
316
+ /**
317
+ * @returns {string}
318
+ */
319
+ function getAikidoDirectory() {
320
+ const homeDir = os.homedir();
321
+ const aikidoDir = path.join(homeDir, ".aikido");
322
+
323
+ if (!fs.existsSync(aikidoDir)) {
324
+ fs.mkdirSync(aikidoDir, { recursive: true });
325
+ }
326
+ return aikidoDir;
327
+ }
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Gets the minimum package age in hours from environment variable
3
+ * @returns {string | undefined}
4
+ */
5
+ export function getMinimumPackageAgeHours() {
6
+ return process.env.SAFE_CHAIN_MINIMUM_PACKAGE_AGE_HOURS;
7
+ }
8
+
9
+ /**
10
+ * Gets the custom npm registries from environment variable
11
+ * Expected format: comma-separated list of registry domains
12
+ * Example: "npm.company.com,registry.internal.net"
13
+ * @returns {string | undefined}
14
+ */
15
+ export function getNpmCustomRegistries() {
16
+ return process.env.SAFE_CHAIN_NPM_CUSTOM_REGISTRIES;
17
+ }
18
+
19
+ /**
20
+ * Gets the custom pip registries from environment variable
21
+ * Expected format: comma-separated list of registry domains
22
+ * Example: "pip.company.com,registry.internal.net"
23
+ * @returns {string | undefined}
24
+ */
25
+ export function getPipCustomRegistries() {
26
+ return process.env.SAFE_CHAIN_PIP_CUSTOM_REGISTRIES;
27
+ }
28
+
29
+ /**
30
+ * Gets the logging level from environment variable
31
+ * Valid values: "silent", "normal", "verbose"
32
+ * @returns {string | undefined}
33
+ */
34
+ export function getLoggingLevel() {
35
+ return process.env.SAFE_CHAIN_LOGGING;
36
+ }
37
+
38
+ /**
39
+ * Gets the minimum package age exclusions from environment variable
40
+ * Expected format: comma-separated list of package names
41
+ * Example: "react,@aikidosec/safe-chain,lodash"
42
+ * @returns {string | undefined}
43
+ */
44
+ export function getMinimumPackageAgeExclusions() {
45
+ return process.env.SAFE_CHAIN_MINIMUM_PACKAGE_AGE_EXCLUSIONS ||
46
+ process.env.SAFE_CHAIN_NPM_MINIMUM_PACKAGE_AGE_EXCLUSIONS;
47
+ }
48
+
49
+ /**
50
+ * Gets the malware list base URL from environment variable
51
+ * Expected format: full URL without trailing slash
52
+ * Example: "https://malware-list.aikido.dev"
53
+ * @returns {string | undefined}
54
+ */
55
+ export function getMalwareListBaseUrl() {
56
+ return process.env.SAFE_CHAIN_MALWARE_LIST_BASE_URL;
57
+ }
@@ -0,0 +1,71 @@
1
+ import os from "os";
2
+ import path from "path";
3
+ import { fileURLToPath } from "url";
4
+ import { getInstalledSafeChainDir } from "../installLocation.js";
5
+
6
+ /**
7
+ * @returns {string}
8
+ */
9
+ export function getSafeChainBaseDir() {
10
+ return getInstalledSafeChainDir() ?? path.join(os.homedir(), ".safe-chain");
11
+ }
12
+
13
+ /**
14
+ * @returns {string}
15
+ */
16
+ export function getBinDir() {
17
+ return path.join(getSafeChainBaseDir(), "bin");
18
+ }
19
+
20
+ /**
21
+ * @returns {string}
22
+ */
23
+ export function getShimsDir() {
24
+ return path.join(getSafeChainBaseDir(), "shims");
25
+ }
26
+
27
+ /**
28
+ * @returns {string}
29
+ */
30
+ export function getScriptsDir() {
31
+ return path.join(getSafeChainBaseDir(), "scripts");
32
+ }
33
+
34
+ /**
35
+ * @returns {string}
36
+ */
37
+ export function getCertsDir() {
38
+ return path.join(getSafeChainBaseDir(), "certs");
39
+ }
40
+
41
+ /**
42
+ * Resolves the directory of the calling module.
43
+ * Falls back to __dirname when import.meta.url is unavailable (pkg CJS binary).
44
+ * @param {string | undefined} moduleUrl
45
+ * @returns {string}
46
+ */
47
+ function resolveModuleDir(moduleUrl) {
48
+ if (moduleUrl) {
49
+ return path.dirname(fileURLToPath(moduleUrl));
50
+ }
51
+ // eslint-disable-next-line no-undef
52
+ return __dirname;
53
+ }
54
+
55
+ /**
56
+ * @param {string | undefined} moduleUrl
57
+ * @param {string} fileName
58
+ * @returns {string}
59
+ */
60
+ export function getStartupScriptSourcePath(moduleUrl, fileName) {
61
+ return path.join(resolveModuleDir(moduleUrl), "startup-scripts", fileName);
62
+ }
63
+
64
+ /**
65
+ * @param {string | undefined} moduleUrl
66
+ * @param {string} fileName
67
+ * @returns {string}
68
+ */
69
+ export function getPathWrapperTemplatePath(moduleUrl, fileName) {
70
+ return path.join(resolveModuleDir(moduleUrl), "path-wrappers", "templates", fileName);
71
+ }
@@ -0,0 +1,247 @@
1
+ import * as cliArguments from "./cliArguments.js";
2
+ import * as configFile from "./configFile.js";
3
+ import * as environmentVariables from "./environmentVariables.js";
4
+ import { ui } from "../environment/userInteraction.js";
5
+
6
+ export const LOGGING_SILENT = "silent";
7
+ export const LOGGING_NORMAL = "normal";
8
+ export const LOGGING_VERBOSE = "verbose";
9
+
10
+ export function getLoggingLevel() {
11
+ // Priority 1: CLI argument
12
+ const cliLevel = cliArguments.getLoggingLevel();
13
+ if (cliLevel === LOGGING_SILENT || cliLevel === LOGGING_VERBOSE) {
14
+ return cliLevel;
15
+ }
16
+ if (cliLevel) {
17
+ // CLI arg was set but invalid, default to normal for backwards compatibility.
18
+ return LOGGING_NORMAL;
19
+ }
20
+
21
+ // Priority 2: Environment variable
22
+ const envLevel = environmentVariables.getLoggingLevel()?.toLowerCase();
23
+ if (envLevel === LOGGING_SILENT || envLevel === LOGGING_VERBOSE) {
24
+ return envLevel;
25
+ }
26
+
27
+ return LOGGING_NORMAL;
28
+ }
29
+
30
+ export const ECOSYSTEM_JS = "js";
31
+ export const ECOSYSTEM_PY = "py";
32
+
33
+ // Default to JavaScript ecosystem
34
+ const ecosystemSettings = {
35
+ ecoSystem: ECOSYSTEM_JS,
36
+ };
37
+
38
+ /** @returns {string} - The current ecosystem setting (ECOSYSTEM_JS or ECOSYSTEM_PY) */
39
+ export function getEcoSystem() {
40
+ return ecosystemSettings.ecoSystem;
41
+ }
42
+ /**
43
+ * @param {string} setting - The ecosystem to set (ECOSYSTEM_JS or ECOSYSTEM_PY)
44
+ */
45
+ export function setEcoSystem(setting) {
46
+ ecosystemSettings.ecoSystem = setting;
47
+ }
48
+
49
+ const defaultMinimumPackageAge = 48;
50
+ /** @returns {number} */
51
+ export function getMinimumPackageAgeHours() {
52
+ // Priority 1: CLI argument
53
+ const cliValue = validateMinimumPackageAgeHours(
54
+ cliArguments.getMinimumPackageAgeHours()
55
+ );
56
+ if (cliValue !== undefined) {
57
+ return cliValue;
58
+ }
59
+
60
+ // Priority 2: Environment variable
61
+ const envValue = validateMinimumPackageAgeHours(
62
+ environmentVariables.getMinimumPackageAgeHours()
63
+ );
64
+ if (envValue !== undefined) {
65
+ return envValue;
66
+ }
67
+
68
+ // Priority 3: Config file
69
+ const configValue = configFile.getMinimumPackageAgeHours();
70
+ if (configValue !== undefined) {
71
+ return configValue;
72
+ }
73
+
74
+ return defaultMinimumPackageAge;
75
+ }
76
+
77
+ /**
78
+ * @param {string | undefined} value
79
+ * @returns {number | undefined}
80
+ */
81
+ function validateMinimumPackageAgeHours(value) {
82
+ if (!value) {
83
+ return undefined;
84
+ }
85
+
86
+ const numericValue = Number(value);
87
+ if (Number.isNaN(numericValue)) {
88
+ return undefined;
89
+ }
90
+
91
+ if (numericValue >= 0) {
92
+ return numericValue;
93
+ }
94
+
95
+ return undefined;
96
+ }
97
+
98
+ const defaultSkipMinimumPackageAge = false;
99
+ export function skipMinimumPackageAge() {
100
+ const cliValue = cliArguments.getSkipMinimumPackageAge();
101
+
102
+ if (cliValue === true) {
103
+ return true;
104
+ }
105
+
106
+ return defaultSkipMinimumPackageAge;
107
+ }
108
+
109
+ /**
110
+ * Normalizes a registry URL by removing protocol if present
111
+ * @param {string} registry
112
+ * @returns {string}
113
+ */
114
+ function normalizeRegistry(registry) {
115
+ // Remove protocol (http://, https://) if present
116
+ return registry.replace(/^https?:\/\//, "");
117
+ }
118
+
119
+ /**
120
+ * Parses comma-separated registries from environment variable
121
+ * @param {string | undefined} envValue
122
+ * @returns {string[]}
123
+ */
124
+ function parseRegistriesFromEnv(envValue) {
125
+ if (!envValue || typeof envValue !== "string") {
126
+ return [];
127
+ }
128
+
129
+ // Split by comma and trim whitespace
130
+ return envValue
131
+ .split(",")
132
+ .map((registry) => registry.trim())
133
+ .filter((registry) => registry.length > 0);
134
+ }
135
+
136
+ /**
137
+ * Gets the custom npm registries from both environment variable and config file (merged)
138
+ * @returns {string[]}
139
+ */
140
+ export function getNpmCustomRegistries() {
141
+ const envRegistries = parseRegistriesFromEnv(
142
+ environmentVariables.getNpmCustomRegistries()
143
+ );
144
+ const configRegistries = configFile.getNpmCustomRegistries();
145
+
146
+ // Merge both sources and remove duplicates
147
+ const allRegistries = [...envRegistries, ...configRegistries];
148
+ const uniqueRegistries = [...new Set(allRegistries)];
149
+
150
+ // Normalize each registry (remove protocol if any)
151
+ return uniqueRegistries.map(normalizeRegistry);
152
+ }
153
+
154
+ /**
155
+ * Gets the custom npm registries from both environment variable and config file (merged)
156
+ * @returns {string[]}
157
+ */
158
+ export function getPipCustomRegistries() {
159
+ const envRegistries = parseRegistriesFromEnv(
160
+ environmentVariables.getPipCustomRegistries()
161
+ );
162
+ const configRegistries = configFile.getPipCustomRegistries();
163
+
164
+ // Merge both sources and remove duplicates
165
+ const allRegistries = [...envRegistries, ...configRegistries];
166
+ const uniqueRegistries = [...new Set(allRegistries)];
167
+
168
+ // Normalize each registry (remove protocol if any)
169
+ return uniqueRegistries.map(normalizeRegistry);
170
+ }
171
+
172
+ /**
173
+ * Parses comma-separated exclusions from environment variable
174
+ * @param {string | undefined} envValue
175
+ * @returns {string[]}
176
+ */
177
+ function parseExclusionsFromEnv(envValue) {
178
+ if (!envValue || typeof envValue !== "string") {
179
+ return [];
180
+ }
181
+
182
+ return envValue
183
+ .split(",")
184
+ .map((exclusion) => exclusion.trim())
185
+ .filter((exclusion) => exclusion.length > 0);
186
+ }
187
+
188
+ /**
189
+ * Gets the minimum package age exclusions from both environment variable and config file (merged)
190
+ * @returns {string[]}
191
+ */
192
+ export function getMinimumPackageAgeExclusions() {
193
+ const envExclusions = parseExclusionsFromEnv(
194
+ environmentVariables.getMinimumPackageAgeExclusions()
195
+ );
196
+ const configExclusions = configFile.getMinimumPackageAgeExclusions();
197
+
198
+ // Merge both sources and remove duplicates
199
+ const allExclusions = [...envExclusions, ...configExclusions];
200
+ return [...new Set(allExclusions)];
201
+ }
202
+
203
+ /**
204
+ * Gets the malware list base URL with priority: CLI argument > environment variable > config file > default
205
+ * @returns {string}
206
+ */
207
+ export function getMalwareListBaseUrl() {
208
+ // Priority 1: CLI argument
209
+ const cliValue = cliArguments.getMalwareListBaseUrl();
210
+ if (cliValue) {
211
+ const url = removeTrailingSlashes(cliValue);
212
+ ui.writeVerbose(`Fetching malware lists from ${url} as defined by CLI argument --safe-chain-malware-list-base-url`);
213
+ return url;
214
+ }
215
+
216
+ // Priority 2: Environment variable
217
+ const envValue = environmentVariables.getMalwareListBaseUrl();
218
+ if (envValue) {
219
+ const url = removeTrailingSlashes(envValue);
220
+ ui.writeVerbose(`Fetching malware lists from ${url} as defined by environment variable SAFE_CHAIN_MALWARE_LIST_BASE_URL`);
221
+ return url;
222
+ }
223
+
224
+ // Priority 3: Config file
225
+ const configValue = configFile.getMalwareListBaseUrl();
226
+ if (configValue) {
227
+ const url = removeTrailingSlashes(configValue);
228
+ ui.writeVerbose(`Fetching malware lists from ${url} as defined by config file (malwareListBaseUrl)`);
229
+ return url;
230
+ }
231
+
232
+ // Default
233
+ return removeTrailingSlashes("https://malware-list.aikido.dev");
234
+ }
235
+
236
+ /**
237
+ * Removes trailing slashes from a URL-like string.
238
+ * @param {string} value
239
+ * @returns {string}
240
+ */
241
+ function removeTrailingSlashes(value) {
242
+ if (!value || typeof value !== "string") {
243
+ return value;
244
+ }
245
+
246
+ return value.replace(/\/+$/, "");
247
+ }
@@ -0,0 +1,14 @@
1
+ export function isCi() {
2
+ const ciEnvironments = [
3
+ "CI",
4
+ "TF_BUILD", // Azure devops does not set CI, but TF_BUILD
5
+ ];
6
+
7
+ for (const env of ciEnvironments) {
8
+ if (process.env[env]) {
9
+ return true;
10
+ }
11
+ }
12
+
13
+ return false;
14
+ }