@socketsecurity/cli-with-sentry 0.15.22 → 0.15.23

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.
package/dist/utils.js CHANGED
@@ -1846,30 +1846,11 @@ const ALERT_SEVERITY_ORDER = createEnum({
1846
1846
  none: 4
1847
1847
  });
1848
1848
  const {
1849
- CVE_ALERT_PROPS_FIRST_PATCHED_VERSION_IDENTIFIER,
1850
1849
  NPM: NPM$3
1851
1850
  } = constants;
1852
1851
  const MIN_ABOVE_THE_FOLD_COUNT = 3;
1853
1852
  const MIN_ABOVE_THE_FOLD_ALERT_COUNT = 1;
1854
1853
  const format = new ColorOrMarkdown(false);
1855
- function alertsHaveBlocked(alerts) {
1856
- return alerts.find(a => a.blocked) !== undefined;
1857
- }
1858
- function alertsHaveSeverity(alerts, severity) {
1859
- return alerts.find(a => a.raw.severity === severity) !== undefined;
1860
- }
1861
- function alertSeverityComparator(a, b) {
1862
- return getAlertSeverityOrder(a) - getAlertSeverityOrder(b);
1863
- }
1864
- function getAlertSeverityOrder(alert) {
1865
- const {
1866
- severity
1867
- } = alert.raw;
1868
- return severity === ALERT_SEVERITY.critical ? 0 : severity === ALERT_SEVERITY.high ? 1 : severity === ALERT_SEVERITY.middle ? 2 : severity === ALERT_SEVERITY.low ? 3 : 4;
1869
- }
1870
- function getAlertsSeverityOrder(alerts) {
1871
- return alertsHaveBlocked(alerts) || alertsHaveSeverity(alerts, ALERT_SEVERITY.critical) ? 0 : alertsHaveSeverity(alerts, ALERT_SEVERITY.high) ? 1 : alertsHaveSeverity(alerts, ALERT_SEVERITY.middle) ? 2 : alertsHaveSeverity(alerts, ALERT_SEVERITY.low) ? 3 : 4;
1872
- }
1873
1854
  function getHiddenRiskCounts(hiddenAlerts) {
1874
1855
  const riskCounts = {
1875
1856
  critical: 0,
@@ -1911,9 +1892,6 @@ function getHiddenRisksDescription(riskCounts) {
1911
1892
  }
1912
1893
  return `(${descriptions.join('; ')})`;
1913
1894
  }
1914
- function getSeverityLabel(severity) {
1915
- return severity === 'middle' ? 'moderate' : severity;
1916
- }
1917
1895
  async function addArtifactToAlertsMap(artifact, alertsByPkgId, options) {
1918
1896
  // Make TypeScript happy.
1919
1897
  if (!artifact.name || !artifact.version || !artifact.alerts?.length) {
@@ -1927,6 +1905,8 @@ async function addArtifactToAlertsMap(artifact, alertsByPkgId, options) {
1927
1905
  __proto__: null,
1928
1906
  ...options
1929
1907
  };
1908
+ const socketYml = findSocketYmlSync();
1909
+ const localRules = socketYml?.parsed.issueRules;
1930
1910
  const include = {
1931
1911
  __proto__: null,
1932
1912
  blocked: true,
@@ -1942,10 +1922,9 @@ async function addArtifactToAlertsMap(artifact, alertsByPkgId, options) {
1942
1922
  } = artifact;
1943
1923
  const pkgId = `${name}@${version}`;
1944
1924
  const major = vendor.semverExports.major(version);
1945
- const socketYml = findSocketYmlSync();
1946
1925
  const enabledState = {
1947
1926
  __proto__: null,
1948
- ...socketYml?.parsed.issueRules
1927
+ ...localRules
1949
1928
  };
1950
1929
  let sockPkgAlerts = [];
1951
1930
  for (const alert of artifact.alerts) {
@@ -1987,19 +1966,28 @@ async function addArtifactToAlertsMap(artifact, alertsByPkgId, options) {
1987
1966
  const alert = sockPkgAlert.raw;
1988
1967
  const fixType = alert.fix?.type ?? '';
1989
1968
  if (fixType === ALERT_FIX_TYPE.cve) {
1990
- const patchedVersion = alert.props[CVE_ALERT_PROPS_FIRST_PATCHED_VERSION_IDENTIFIER];
1991
- const patchedMajor = vendor.semverExports.major(patchedVersion);
1992
- const oldHighest = highestForCve.get(patchedMajor);
1993
- const highest = oldHighest?.version ?? '0.0.0';
1994
- if (vendor.semverExports.gt(patchedVersion, highest)) {
1995
- highestForCve.set(patchedMajor, {
1996
- alert: sockPkgAlert,
1997
- version: patchedVersion
1998
- });
1969
+ // An alert with alert.fix.type of 'cve' should have a
1970
+ // alert.props.firstPatchedVersionIdentifier property value.
1971
+ // We're just being cautious.
1972
+ const firstPatchedVersionIdentifier = alert.props?.firstPatchedVersionIdentifier;
1973
+ if (firstPatchedVersionIdentifier) {
1974
+ // Consolidate to the highest "first patched version" by each major
1975
+ // version number.
1976
+ const patchedMajor = vendor.semverExports.major(firstPatchedVersionIdentifier);
1977
+ const highest = highestForCve.get(patchedMajor)?.version ?? '0.0.0';
1978
+ if (vendor.semverExports.gt(firstPatchedVersionIdentifier, highest)) {
1979
+ highestForCve.set(patchedMajor, {
1980
+ alert: sockPkgAlert,
1981
+ version: firstPatchedVersionIdentifier
1982
+ });
1983
+ }
1984
+ } else {
1985
+ unfixableAlerts.push(sockPkgAlert);
1999
1986
  }
2000
1987
  } else if (fixType === ALERT_FIX_TYPE.upgrade) {
2001
- const oldHighest = highestForUpgrade.get(major);
2002
- const highest = oldHighest?.version ?? '0.0.0';
1988
+ // For Socket Optimize upgrades we assume the highest version available
1989
+ // is compatible. This may change in the future.
1990
+ const highest = highestForUpgrade.get(major)?.version ?? '0.0.0';
2003
1991
  if (vendor.semverExports.gt(version, highest)) {
2004
1992
  highestForUpgrade.set(major, {
2005
1993
  alert: sockPkgAlert,
@@ -2010,26 +1998,47 @@ async function addArtifactToAlertsMap(artifact, alertsByPkgId, options) {
2010
1998
  unfixableAlerts.push(sockPkgAlert);
2011
1999
  }
2012
2000
  }
2013
- sockPkgAlerts = [...unfixableAlerts, ...[...highestForCve.values()].map(d => d.alert), ...[...highestForUpgrade.values()].map(d => d.alert)];
2001
+ sockPkgAlerts = [
2002
+ // Sort CVE alerts by severity: critical, high, middle, then low.
2003
+ ...[...highestForCve.values()].map(d => d.alert).sort(alertSeverityComparator), ...[...highestForUpgrade.values()].map(d => d.alert), ...unfixableAlerts];
2004
+ } else {
2005
+ sockPkgAlerts.sort((a, b) => sorts.naturalCompare(a.type, b.type));
2014
2006
  }
2015
2007
  if (sockPkgAlerts.length) {
2016
- sockPkgAlerts.sort((a, b) => sorts.naturalCompare(a.type, b.type));
2017
2008
  alertsByPkgId.set(pkgId, sockPkgAlerts);
2018
2009
  }
2019
2010
  return alertsByPkgId;
2020
2011
  }
2021
- function getCveInfoFromAlertsMap(alertsMap, options) {
2012
+ function alertsHaveBlocked(alerts) {
2013
+ return alerts.find(a => a.blocked) !== undefined;
2014
+ }
2015
+ function alertsHaveSeverity(alerts, severity) {
2016
+ return alerts.find(a => a.raw.severity === severity) !== undefined;
2017
+ }
2018
+ function alertSeverityComparator(a, b) {
2019
+ // Put the most severe first.
2020
+ return getAlertSeverityOrder(a) - getAlertSeverityOrder(b);
2021
+ }
2022
+ function getAlertSeverityOrder(alert) {
2023
+ // The more severe, the lower the sort number.
2022
2024
  const {
2023
- exclude: _exclude,
2024
- limit = Infinity
2025
- } = {
2025
+ severity
2026
+ } = alert.raw;
2027
+ return severity === ALERT_SEVERITY.critical ? 0 : severity === ALERT_SEVERITY.high ? 1 : severity === ALERT_SEVERITY.middle ? 2 : severity === ALERT_SEVERITY.low ? 3 : 4;
2028
+ }
2029
+ function getAlertsSeverityOrder(alerts) {
2030
+ return alertsHaveBlocked(alerts) || alertsHaveSeverity(alerts, ALERT_SEVERITY.critical) ? 0 : alertsHaveSeverity(alerts, ALERT_SEVERITY.high) ? 1 : alertsHaveSeverity(alerts, ALERT_SEVERITY.middle) ? 2 : alertsHaveSeverity(alerts, ALERT_SEVERITY.low) ? 3 : 4;
2031
+ }
2032
+ function getCveInfoFromAlertsMap(alertsMap, options_) {
2033
+ const options = {
2026
2034
  __proto__: null,
2027
- ...options
2035
+ exclude: undefined,
2036
+ limit: Infinity,
2037
+ ...options_
2028
2038
  };
2029
- const exclude = {
2039
+ options.exclude = {
2030
2040
  __proto__: null,
2031
- upgradable: true,
2032
- ..._exclude
2041
+ ...options.exclude
2033
2042
  };
2034
2043
  let count = 0;
2035
2044
  let infoByPkgName = null;
@@ -2038,7 +2047,7 @@ function getCveInfoFromAlertsMap(alertsMap, options) {
2038
2047
  const name = packages.resolvePackageName(purlObj);
2039
2048
  sockPkgAlertsLoop: for (const sockPkgAlert of sockPkgAlerts) {
2040
2049
  const alert = sockPkgAlert.raw;
2041
- if (alert.fix?.type !== ALERT_FIX_TYPE.cve || exclude.upgradable && registry.getManifestData(NPM$3, name)) {
2050
+ if (alert.fix?.type !== ALERT_FIX_TYPE.cve || options.exclude.upgradable && registry.getManifestData(NPM$3, name)) {
2042
2051
  continue sockPkgAlertsLoop;
2043
2052
  }
2044
2053
  if (!infoByPkgName) {
@@ -2053,33 +2062,44 @@ function getCveInfoFromAlertsMap(alertsMap, options) {
2053
2062
  key
2054
2063
  } = alert;
2055
2064
  if (!infos.has(key)) {
2056
- const {
2057
- firstPatchedVersionIdentifier,
2058
- vulnerableVersionRange
2059
- } = alert.props;
2060
- try {
2061
- infos.set(key, {
2062
- firstPatchedVersionIdentifier,
2063
- vulnerableVersionRange: new vendor.semverExports.Range(
2064
- // Replace ', ' in a range like '>= 1.0.0, < 1.8.2' with ' ' so that
2065
- // semver.Range will parse it without erroring.
2066
- vulnerableVersionRange.replace(/, +/g, ' ')).format()
2067
- });
2068
- if (++count >= limit) {
2069
- break alertsMapLoop;
2065
+ // An alert with alert.fix.type of 'cve' should have a
2066
+ // alert.props.firstPatchedVersionIdentifier property value.
2067
+ // We're just being cautious.
2068
+ const firstPatchedVersionIdentifier = alert.props?.firstPatchedVersionIdentifier;
2069
+ const vulnerableVersionRange = alert.props?.vulnerableVersionRange;
2070
+ let error;
2071
+ if (firstPatchedVersionIdentifier && vulnerableVersionRange) {
2072
+ try {
2073
+ infos.set(key, {
2074
+ firstPatchedVersionIdentifier,
2075
+ vulnerableVersionRange: new vendor.semverExports.Range(
2076
+ // Replace ', ' in a range like '>= 1.0.0, < 1.8.2' with ' ' so that
2077
+ // semver.Range will parse it without erroring.
2078
+ vulnerableVersionRange.replace(/, +/g, ' ')).format()
2079
+ });
2080
+ if (++count >= options.limit) {
2081
+ break alertsMapLoop;
2082
+ }
2083
+ continue sockPkgAlertsLoop;
2084
+ } catch (e) {
2085
+ error = e;
2086
+ }
2087
+ }
2088
+ if (debug.isDebug()) {
2089
+ logger.logger.log('Unexpected condition: Invalid SocketPackageAlert in getCveInfoFromAlertsMap.');
2090
+ logger.logger.dir(alert);
2091
+ if (error) {
2092
+ logger.logger.log(error);
2070
2093
  }
2071
- } catch (e) {
2072
- debug.debugLog('getCveInfoFromAlertsMap', {
2073
- firstPatchedVersionIdentifier,
2074
- vulnerableVersionRange
2075
- });
2076
- debug.debugLog(e);
2077
2094
  }
2078
2095
  }
2079
2096
  }
2080
2097
  }
2081
2098
  return infoByPkgName;
2082
2099
  }
2100
+ function getSeverityLabel(severity) {
2101
+ return severity === 'middle' ? 'moderate' : severity;
2102
+ }
2083
2103
  function logAlertsMap(alertsMap, options) {
2084
2104
  const {
2085
2105
  hideAt = 'middle',
@@ -2284,14 +2304,7 @@ function getMajor(version) {
2284
2304
  return null;
2285
2305
  }
2286
2306
 
2287
- async function getAlertsMapFromPnpmLockfile(lockfile, options_) {
2288
- const options = {
2289
- __proto__: null,
2290
- consolidate: false,
2291
- limit: Infinity,
2292
- nothrow: false,
2293
- ...options_
2294
- };
2307
+ async function getAlertsMapFromPnpmLockfile(lockfile, options) {
2295
2308
  const purls = await extractPurlsFromPnpmLockfile(lockfile);
2296
2309
  return await getAlertsMapFromPurls(purls, {
2297
2310
  overrides: lockfile.overrides,
@@ -2302,12 +2315,15 @@ async function getAlertsMapFromPurls(purls, options_) {
2302
2315
  const options = {
2303
2316
  __proto__: null,
2304
2317
  consolidate: false,
2318
+ include: undefined,
2305
2319
  nothrow: false,
2306
2320
  ...options_
2307
2321
  };
2308
- const include = {
2322
+ options.include = {
2309
2323
  __proto__: null,
2310
- actions: undefined,
2324
+ // Leave 'actions' unassigned so it can be given a default value in
2325
+ // subsequent functions where `options` is passed.
2326
+ // actions: undefined,
2311
2327
  blocked: true,
2312
2328
  critical: true,
2313
2329
  cve: true,
@@ -2334,19 +2350,19 @@ async function getAlertsMapFromPurls(purls, options_) {
2334
2350
  throw new Error('Auth error: Try to run `socket login` first');
2335
2351
  }
2336
2352
  const sockSdk = sockSdkResult.data;
2337
- const toAlertsMapOptions = {
2353
+ const alertsMapOptions = {
2338
2354
  overrides: options.overrides,
2339
2355
  consolidate: options.consolidate,
2340
- include,
2356
+ include: options.include,
2341
2357
  spinner
2342
2358
  };
2343
2359
  for await (const batchResult of sockSdk.batchPackageStream({
2344
2360
  alerts: 'true',
2345
2361
  compact: 'true',
2346
- ...(include.actions ? {
2347
- actions: include.actions.join(',')
2362
+ ...(options.include.actions ? {
2363
+ actions: options.include.actions.join(',')
2348
2364
  } : {}),
2349
- ...(include.unfixable ? {} : {
2365
+ ...(options.include.unfixable ? {} : {
2350
2366
  fixable: 'true'
2351
2367
  })
2352
2368
  }, {
@@ -2355,7 +2371,7 @@ async function getAlertsMapFromPurls(purls, options_) {
2355
2371
  }))
2356
2372
  })) {
2357
2373
  if (batchResult.success) {
2358
- await addArtifactToAlertsMap(batchResult.data, alertsByPkgId, toAlertsMapOptions);
2374
+ await addArtifactToAlertsMap(batchResult.data, alertsByPkgId, alertsMapOptions);
2359
2375
  } else if (!options.nothrow) {
2360
2376
  const statusCode = batchResult.status ?? 'unknown';
2361
2377
  const statusMessage = batchResult.error ?? 'No status message';
@@ -2935,5 +2951,5 @@ exports.supportedConfigKeys = supportedConfigKeys;
2935
2951
  exports.updateConfigValue = updateConfigValue;
2936
2952
  exports.validationFlags = validationFlags;
2937
2953
  exports.walkNestedMap = walkNestedMap;
2938
- //# debugId=b0a949b5-7c7b-46ea-b147-73f0f9c6007a
2954
+ //# debugId=5078b465-1ba3-4edc-a65d-343294db93ef
2939
2955
  //# sourceMappingURL=utils.js.map