snyk 1.1122.0 → 1.1124.0

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.
@@ -3695,7 +3695,7 @@ exports.getExtraProjectCount = getExtraProjectCount;
3695
3695
  "use strict";
3696
3696
 
3697
3697
  Object.defineProperty(exports, "__esModule", ({ value: true }));
3698
- exports.filterOutProcessedWorkspaces = exports.getMultiPluginResult = void 0;
3698
+ exports.filterOutProcessedNpmWorkspaces = exports.filterOutProcessedYarnWorkspaces = exports.getMultiPluginResult = void 0;
3699
3699
  const cloneDeep = __webpack_require__(83465);
3700
3700
  const pathLib = __webpack_require__(85622);
3701
3701
  const cliInterface = __webpack_require__(65266);
@@ -3708,15 +3708,18 @@ const convert_single_splugin_res_to_multi_custom_1 = __webpack_require__(99695);
3708
3708
  const convert_multi_plugin_res_to_multi_custom_1 = __webpack_require__(23110);
3709
3709
  const errors_1 = __webpack_require__(55191);
3710
3710
  const yarn_workspaces_parser_1 = __webpack_require__(27326);
3711
+ const npm_workspaces_parser_1 = __webpack_require__(31744);
3711
3712
  const debug = debugModule('snyk-test');
3712
3713
  async function getMultiPluginResult(root, options, targetFiles) {
3713
3714
  var _a;
3714
3715
  const allResults = [];
3715
3716
  const failedResults = [];
3716
- // process any yarn workspaces first
3717
+ // process any workspaces first
3717
3718
  // the files need to be proceeded together as they provide context to each other
3718
- const { scannedProjects, unprocessedFiles, } = await processYarnWorkspacesProjects(root, options, targetFiles);
3719
- allResults.push(...scannedProjects);
3719
+ const { scannedProjects: scannedYarnResults, unprocessedFiles: unprocessedFilesFromYarn, } = await processYarnWorkspacesProjects(root, options, targetFiles);
3720
+ allResults.push(...scannedYarnResults);
3721
+ const { scannedProjects: scannedNpmResults, unprocessedFiles, } = await processNpmWorkspacesProjects(root, options, unprocessedFilesFromYarn);
3722
+ allResults.push(...scannedNpmResults);
3720
3723
  debug(`Not part of a workspace: ${unprocessedFiles.join(', ')}}`);
3721
3724
  // process the rest 1 by 1 sent to relevant plugins
3722
3725
  for (const targetFile of unprocessedFiles) {
@@ -3768,7 +3771,7 @@ async function processYarnWorkspacesProjects(root, options, targetFiles) {
3768
3771
  strictOutOfSync: options.strictOutOfSync,
3769
3772
  dev: options.dev,
3770
3773
  }, targetFiles);
3771
- const unprocessedFiles = filterOutProcessedWorkspaces(root, scannedProjects, targetFiles);
3774
+ const unprocessedFiles = filterOutProcessedYarnWorkspaces(root, scannedProjects, targetFiles);
3772
3775
  return { scannedProjects, unprocessedFiles };
3773
3776
  }
3774
3777
  catch (e) {
@@ -3776,7 +3779,21 @@ async function processYarnWorkspacesProjects(root, options, targetFiles) {
3776
3779
  return { scannedProjects: [], unprocessedFiles: targetFiles };
3777
3780
  }
3778
3781
  }
3779
- function filterOutProcessedWorkspaces(root, scannedProjects, allTargetFiles) {
3782
+ async function processNpmWorkspacesProjects(root, options, targetFiles) {
3783
+ try {
3784
+ const { scannedProjects } = await npm_workspaces_parser_1.processNpmWorkspaces(root, {
3785
+ strictOutOfSync: options.strictOutOfSync,
3786
+ dev: options.dev,
3787
+ }, targetFiles);
3788
+ const unprocessedFiles = filterOutProcessedNpmWorkspaces(root, scannedProjects, targetFiles);
3789
+ return { scannedProjects, unprocessedFiles };
3790
+ }
3791
+ catch (e) {
3792
+ debug('Error during detecting or processing Npm Workspaces: ', e);
3793
+ return { scannedProjects: [], unprocessedFiles: targetFiles };
3794
+ }
3795
+ }
3796
+ function filterOutProcessedYarnWorkspaces(root, scannedProjects, allTargetFiles) {
3780
3797
  const targetFiles = [];
3781
3798
  const scanned = scannedProjects
3782
3799
  .map((p) => p.targetFile)
@@ -3801,7 +3818,33 @@ function filterOutProcessedWorkspaces(root, scannedProjects, allTargetFiles) {
3801
3818
  }
3802
3819
  return targetFiles;
3803
3820
  }
3804
- exports.filterOutProcessedWorkspaces = filterOutProcessedWorkspaces;
3821
+ exports.filterOutProcessedYarnWorkspaces = filterOutProcessedYarnWorkspaces;
3822
+ function filterOutProcessedNpmWorkspaces(root, scannedProjects, allTargetFiles) {
3823
+ const targetFiles = [];
3824
+ const scanned = scannedProjects
3825
+ .map((p) => p.targetFile)
3826
+ .map((p) => pathLib.resolve(process.cwd(), root, p));
3827
+ const all = allTargetFiles.map((p) => ({
3828
+ path: pathLib.resolve(process.cwd(), root, p),
3829
+ original: p,
3830
+ }));
3831
+ for (const entry of all) {
3832
+ const { path, original } = entry;
3833
+ const { base } = pathLib.parse(path);
3834
+ if (!['package.json', 'package-lock.json'].includes(base)) {
3835
+ targetFiles.push(original);
3836
+ continue;
3837
+ }
3838
+ // standardise to package.json
3839
+ // we discover the lockfiles but targetFile is package.json
3840
+ if (!scanned.includes(path.replace('package-lock.json', 'package.json'))) {
3841
+ targetFiles.push(original);
3842
+ continue;
3843
+ }
3844
+ }
3845
+ return targetFiles;
3846
+ }
3847
+ exports.filterOutProcessedNpmWorkspaces = filterOutProcessedNpmWorkspaces;
3805
3848
 
3806
3849
 
3807
3850
  /***/ }),
@@ -3915,8 +3958,9 @@ const modulesParser = __webpack_require__(85994);
3915
3958
  const lockParser = __webpack_require__(82791);
3916
3959
  const analytics = __webpack_require__(82744);
3917
3960
  const missing_targetfile_error_1 = __webpack_require__(56775);
3961
+ const snyk_nodejs_lockfile_parser_1 = __webpack_require__(423);
3962
+ const path = __webpack_require__(85622);
3918
3963
  async function inspect(root, targetFile, options = {}) {
3919
- var _a;
3920
3964
  if (!targetFile) {
3921
3965
  throw missing_targetfile_error_1.MissingTargetFileError(root);
3922
3966
  }
@@ -3928,17 +3972,30 @@ async function inspect(root, targetFile, options = {}) {
3928
3972
  : await modulesParser.parse(root, targetFile, options);
3929
3973
  let scannedProjects = [];
3930
3974
  if (isResDepGraph(depRes)) {
3931
- if (depRes.pkgManager.version) {
3932
- analytics.add('lockfileVersion', depRes.pkgManager.version);
3933
- }
3934
3975
  scannedProjects = [{ depGraph: depRes }];
3935
3976
  }
3936
3977
  else {
3937
- if ((_a = depRes.meta) === null || _a === void 0 ? void 0 : _a.lockfileVersion) {
3938
- analytics.add('lockfileVersion', depRes.meta.lockfileVersion);
3939
- }
3940
3978
  scannedProjects = [{ depTree: depRes }];
3941
3979
  }
3980
+ if (isLockFileBased) {
3981
+ const lockFileFullPath = path.resolve(root, targetFile);
3982
+ const lockfileVersion = snyk_nodejs_lockfile_parser_1.getLockfileVersionFromFile(lockFileFullPath);
3983
+ switch (lockfileVersion) {
3984
+ case snyk_nodejs_lockfile_parser_1.NodeLockfileVersion.NpmLockV1:
3985
+ case snyk_nodejs_lockfile_parser_1.NodeLockfileVersion.YarnLockV1:
3986
+ analytics.add('lockfileVersion', 1);
3987
+ break;
3988
+ case snyk_nodejs_lockfile_parser_1.NodeLockfileVersion.NpmLockV2:
3989
+ case snyk_nodejs_lockfile_parser_1.NodeLockfileVersion.YarnLockV2:
3990
+ analytics.add('lockfileVersion', 2);
3991
+ break;
3992
+ case snyk_nodejs_lockfile_parser_1.NodeLockfileVersion.NpmLockV3:
3993
+ analytics.add('lockfileVersion', 3);
3994
+ break;
3995
+ default:
3996
+ break;
3997
+ }
3998
+ }
3942
3999
  return {
3943
4000
  plugin: {
3944
4001
  name: 'snyk-nodejs-lockfile-parser',
@@ -3998,7 +4055,9 @@ async function parse(root, targetFile, options) {
3998
4055
  const strictOutOfSync = options.strictOutOfSync !== false;
3999
4056
  const lockfileVersion = lockFileParser.getLockfileVersionFromFile(lockFileFullPath);
4000
4057
  if (lockfileVersion === snyk_nodejs_lockfile_parser_1.NodeLockfileVersion.YarnLockV1 ||
4001
- lockfileVersion === snyk_nodejs_lockfile_parser_1.NodeLockfileVersion.YarnLockV2) {
4058
+ lockfileVersion === snyk_nodejs_lockfile_parser_1.NodeLockfileVersion.YarnLockV2 ||
4059
+ lockfileVersion === snyk_nodejs_lockfile_parser_1.NodeLockfileVersion.NpmLockV2 ||
4060
+ lockfileVersion === snyk_nodejs_lockfile_parser_1.NodeLockfileVersion.NpmLockV3) {
4002
4061
  return await buildDepGraph(root, manifestFileFullPath, lockFileFullPath, lockfileVersion, {
4003
4062
  includeDevDeps: options.dev || false,
4004
4063
  includeOptionalDeps: true,
@@ -4032,6 +4091,9 @@ async function buildDepGraph(root, manifestFilePath, lockfilePath, lockfileVersi
4032
4091
  return await lockFileParser.parseYarnLockV1Project(manifestFileContents, lockFileContents, options);
4033
4092
  case snyk_nodejs_lockfile_parser_1.NodeLockfileVersion.YarnLockV2:
4034
4093
  return lockFileParser.parseYarnLockV2Project(manifestFileContents, lockFileContents, options);
4094
+ case snyk_nodejs_lockfile_parser_1.NodeLockfileVersion.NpmLockV2:
4095
+ case snyk_nodejs_lockfile_parser_1.NodeLockfileVersion.NpmLockV3:
4096
+ return lockFileParser.parseNpmLockV2Project(manifestFileContents, lockFileContents, options);
4035
4097
  }
4036
4098
  throw new Error('Failed to build dep graph from current project');
4037
4099
  }
@@ -4111,6 +4173,138 @@ async function parse(root, targetFile, options) {
4111
4173
  exports.parse = parse;
4112
4174
 
4113
4175
 
4176
+ /***/ }),
4177
+
4178
+ /***/ 31744:
4179
+ /***/ ((__unused_webpack_module, exports, __webpack_require__) => {
4180
+
4181
+ "use strict";
4182
+
4183
+ Object.defineProperty(exports, "__esModule", ({ value: true }));
4184
+ exports.packageJsonBelongsToWorkspace = exports.getWorkspacesMap = exports.processNpmWorkspaces = void 0;
4185
+ const baseDebug = __webpack_require__(15158);
4186
+ const pathUtil = __webpack_require__(85622);
4187
+ const sortBy = __webpack_require__(58254);
4188
+ const groupBy = __webpack_require__(20276);
4189
+ const micromatch = __webpack_require__(70850);
4190
+ const debug = baseDebug('snyk-npm-workspaces');
4191
+ const lockFileParser = __webpack_require__(423);
4192
+ const get_file_contents_1 = __webpack_require__(84210);
4193
+ const errors_1 = __webpack_require__(55191);
4194
+ async function processNpmWorkspaces(root, settings, targetFiles) {
4195
+ // the order of npmTargetFiles folders is important
4196
+ // must have the root level most folders at the top
4197
+ const mappedAndFiltered = targetFiles
4198
+ .map((p) => ({ path: p, ...pathUtil.parse(p) }))
4199
+ .filter((res) => ['package.json', 'package-lock.json'].includes(res.base));
4200
+ const sorted = sortBy(mappedAndFiltered, 'dir');
4201
+ const grouped = groupBy(sorted, 'dir');
4202
+ const npmTargetFiles = grouped;
4203
+ debug(`Processing potential Npm workspaces (${targetFiles.length})`);
4204
+ if (settings.yarnWorkspaces && Object.keys(npmTargetFiles).length === 0) {
4205
+ throw errors_1.NoSupportedManifestsFoundError([root]);
4206
+ }
4207
+ let npmWorkspacesMap = {};
4208
+ const workspacesFilesMap = {};
4209
+ const result = {
4210
+ plugin: {
4211
+ name: 'snyk-nodejs-npm-workspaces',
4212
+ runtime: process.version,
4213
+ },
4214
+ scannedProjects: [],
4215
+ };
4216
+ // the folders must be ordered highest first
4217
+ for (const directory of Object.keys(npmTargetFiles)) {
4218
+ debug(`Processing ${directory} as a potential npm workspace`);
4219
+ let isWorkspacePackage = false;
4220
+ let isRootPackageJson = false;
4221
+ const packageJsonFileName = pathUtil.join(directory, 'package.json');
4222
+ const packageJson = get_file_contents_1.getFileContents(root, packageJsonFileName);
4223
+ npmWorkspacesMap = {
4224
+ ...npmWorkspacesMap,
4225
+ ...getWorkspacesMap(packageJson),
4226
+ };
4227
+ for (const workspaceRoot of Object.keys(npmWorkspacesMap)) {
4228
+ const match = packageJsonBelongsToWorkspace(packageJsonFileName, npmWorkspacesMap, workspaceRoot);
4229
+ if (match) {
4230
+ debug(`${packageJsonFileName} matches an existing workspace pattern`);
4231
+ workspacesFilesMap[packageJsonFileName] = {
4232
+ root: workspaceRoot,
4233
+ };
4234
+ isWorkspacePackage = true;
4235
+ }
4236
+ if (packageJsonFileName === workspaceRoot) {
4237
+ isRootPackageJson = true;
4238
+ }
4239
+ }
4240
+ if (!(isWorkspacePackage || isRootPackageJson)) {
4241
+ debug(`${packageJsonFileName} is not part of any detected workspace, skipping`);
4242
+ continue;
4243
+ }
4244
+ try {
4245
+ const rootDir = isWorkspacePackage
4246
+ ? pathUtil.dirname(workspacesFilesMap[packageJsonFileName].root)
4247
+ : pathUtil.dirname(packageJsonFileName);
4248
+ const rootLockfileName = pathUtil.join(rootDir, 'package-lock.json');
4249
+ const lockContent = get_file_contents_1.getFileContents(root, rootLockfileName);
4250
+ const res = lockFileParser.parseNpmLockV2Project(packageJson.content, lockContent.content, {
4251
+ includeDevDeps: settings.dev || false,
4252
+ strictOutOfSync: settings.strictOutOfSync || false,
4253
+ includeOptionalDeps: false,
4254
+ pruneCycles: true,
4255
+ });
4256
+ const project = {
4257
+ packageManager: 'npm',
4258
+ targetFile: pathUtil.relative(root, packageJson.fileName),
4259
+ depGraph: res,
4260
+ plugin: {
4261
+ name: 'snyk-nodejs-lockfile-parser',
4262
+ runtime: process.version,
4263
+ },
4264
+ };
4265
+ result.scannedProjects.push(project);
4266
+ }
4267
+ catch (e) {
4268
+ if (settings.yarnWorkspaces) {
4269
+ throw e;
4270
+ }
4271
+ debug(`Error process workspace: ${packageJsonFileName}. ERROR: ${e}`);
4272
+ }
4273
+ }
4274
+ if (!result.scannedProjects.length) {
4275
+ debug(`No npm workspaces detected in any of the ${targetFiles.length} target files.`);
4276
+ }
4277
+ return result;
4278
+ }
4279
+ exports.processNpmWorkspaces = processNpmWorkspaces;
4280
+ function getWorkspacesMap(file) {
4281
+ const workspacesMap = {};
4282
+ if (!file) {
4283
+ return workspacesMap;
4284
+ }
4285
+ try {
4286
+ const rootFileWorkspacesDefinitions = JSON.parse(file.content).workspaces || false;
4287
+ if (rootFileWorkspacesDefinitions && rootFileWorkspacesDefinitions.length) {
4288
+ workspacesMap[file.fileName] = {
4289
+ workspaces: rootFileWorkspacesDefinitions,
4290
+ };
4291
+ }
4292
+ }
4293
+ catch (e) {
4294
+ debug('Failed to process a workspace', e.message);
4295
+ }
4296
+ return workspacesMap;
4297
+ }
4298
+ exports.getWorkspacesMap = getWorkspacesMap;
4299
+ function packageJsonBelongsToWorkspace(packageJsonFileName, workspacesMap, workspaceRoot) {
4300
+ const workspaceRootFolder = pathUtil.dirname(workspaceRoot.replace(/\\/g, '/'));
4301
+ const workspacesGlobs = (workspacesMap[workspaceRoot].workspaces || []).map((workspace) => pathUtil.join(workspaceRootFolder, workspace));
4302
+ const match = micromatch.isMatch(packageJsonFileName.replace(/\\/g, '/'), workspacesGlobs.map((p) => pathUtil.normalize(pathUtil.join(p, 'package.json')).replace(/\\/g, '/')));
4303
+ return match;
4304
+ }
4305
+ exports.packageJsonBelongsToWorkspace = packageJsonBelongsToWorkspace;
4306
+
4307
+
4114
4308
  /***/ }),
4115
4309
 
4116
4310
  /***/ 27326: