snyk 1.765.0 → 1.766.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.
@@ -207592,9 +207592,19 @@ async function fetchNugetInformationFromPackages(flattenedPackageList, targetFra
207592
207592
  // begin collecting information from .nuget files on installed packages
207593
207593
  debug('Trying to analyze .nuspec files');
207594
207594
  for (const name of Object.keys(flattenedPackageList)) {
207595
- const dep = flattenedPackageList[name];
207596
- debug('...' + name);
207597
- nugetPackageInformation.push(await nuspec_parser_1.parseNuspec(dep, targetFramework));
207595
+ try {
207596
+ const dep = flattenedPackageList[name];
207597
+ debug('...' + name);
207598
+ const resolved = await nuspec_parser_1.parseNuspec(dep, targetFramework);
207599
+ nugetPackageInformation.push(resolved);
207600
+ }
207601
+ catch (e) {
207602
+ debug('Failed parsing nuspec file');
207603
+ debug(e);
207604
+ //log but make sure to rethrow the error
207605
+ //why? if we cannot parse nuspec file, we got nothing to do!
207606
+ throw e;
207607
+ }
207598
207608
  }
207599
207609
  return nugetPackageInformation;
207600
207610
  }
@@ -207826,7 +207836,7 @@ exports.getMinimumTargetFrameworkFromPackagesConfig = getMinimumTargetFrameworkF
207826
207836
  "use strict";
207827
207837
 
207828
207838
  Object.defineProperty(exports, "__esModule", ({ value: true }));
207829
- exports.parseNuspec = void 0;
207839
+ exports._parsedNuspec = exports.parseNuspec = void 0;
207830
207840
  const JSZip = __webpack_require__(9576);
207831
207841
  const fs = __webpack_require__(35747);
207832
207842
  const path = __webpack_require__(85622);
@@ -207835,61 +207845,113 @@ const debugModule = __webpack_require__(15158);
207835
207845
  const debug = debugModule('snyk');
207836
207846
  const targetFrameworkRegex = /([.a-zA-Z]+)([.0-9]+)/;
207837
207847
  async function parseNuspec(dep, targetFramework) {
207838
- return Promise.resolve()
207839
- .then(() => {
207840
- const nupkgPath = path.resolve(dep.path, dep.name + '.' + dep.version + '.nupkg');
207841
- const nupkgData = fs.readFileSync(nupkgPath);
207842
- return JSZip.loadAsync(nupkgData);
207843
- })
207844
- .then(nuspecZipData => {
207845
- const nuspecFiles = Object.keys(nuspecZipData.files).filter(file => {
207846
- return path.extname(file) === '.nuspec';
207847
- });
207848
- return nuspecZipData.files[nuspecFiles[0]].async('text');
207849
- })
207850
- .then(nuspecContent => {
207851
- return new Promise((resolve, reject) => {
207852
- parseXML.parseString(nuspecContent, (err, result) => {
207853
- if (err) {
207854
- return reject(err);
207855
- }
207856
- let ownDeps = [];
207857
- // We are only going to check the first targetFramework we encounter
207858
- // in the future we may want to support multiple, but only once
207859
- // we have dependency version conflict resolution implemented
207860
- result.package.metadata.forEach(metadata => {
207861
- metadata.dependencies.forEach(rawDependency => {
207862
- // Find and add target framework version specific dependencies
207863
- const depsForTargetFramework = extractDepsForTargetFramework(rawDependency, targetFramework);
207864
- if (depsForTargetFramework && depsForTargetFramework.group) {
207865
- ownDeps = ownDeps.concat(extractDepsFromRaw(depsForTargetFramework.group.dependency));
207866
- }
207867
- // Find all groups with no targetFramework attribute
207868
- // add their deps
207869
- const depsFromPlainGroups = extractDepsForPlainGroups(rawDependency);
207870
- if (depsFromPlainGroups) {
207871
- depsFromPlainGroups.forEach(depGroup => {
207872
- ownDeps = ownDeps.concat(extractDepsFromRaw(depGroup.dependency));
207873
- });
207874
- }
207875
- // Add the default dependencies
207876
- ownDeps = ownDeps.concat(extractDepsFromRaw(rawDependency.dependency));
207877
- });
207878
- });
207879
- return resolve({
207880
- children: ownDeps,
207881
- name: dep.name,
207882
- });
207883
- });
207884
- });
207885
- })
207886
- .catch(err => {
207887
- // parsing problems are coerced into an empty nuspec
207888
- debug('Error parsing dependency', JSON.stringify(dep), err);
207848
+ //precaution
207849
+ if (!dep) {
207850
+ throw new Error('expected DependencyInfo parameter to have value but found it undefined');
207851
+ }
207852
+ //another precaution
207853
+ if (!targetFramework) {
207854
+ throw new Error('expected TargetFramework parameter to have value but found it undefined');
207855
+ }
207856
+ const nuspecContent = await loadNuspecFromAsync(dep);
207857
+ if (nuspecContent === null) {
207858
+ debug('failed to load nuspec content');
207889
207859
  return null;
207890
- });
207860
+ }
207861
+ return await _parsedNuspec(nuspecContent, targetFramework, dep.name);
207891
207862
  }
207892
207863
  exports.parseNuspec = parseNuspec;
207864
+ async function loadNuspecFromAsync(dep) {
207865
+ const nupkgPath = path.resolve(dep.path, dep.name + '.' + dep.version + '.nupkg');
207866
+ let nupkgData;
207867
+ try {
207868
+ nupkgData = fs.readFileSync(nupkgPath);
207869
+ }
207870
+ catch (err) {
207871
+ if (err.code == 'ENOENT') {
207872
+ debug('No nupkg file found at ' + nupkgPath);
207873
+ return null; //this is needed not to break existing code flow
207874
+ }
207875
+ else {
207876
+ throw err;
207877
+ }
207878
+ }
207879
+ const nuspecZipData = await JSZip.loadAsync(nupkgData);
207880
+ const nuspecFile = Object.keys(nuspecZipData.files).find(file => {
207881
+ return path.extname(file) === '.nuspec';
207882
+ });
207883
+ if (!nuspecFile) {
207884
+ throw new Error('`failed to read nupkg file from: ${nupkgPath}`');
207885
+ }
207886
+ if (!nuspecZipData) {
207887
+ throw new Error(`failed to open nupkg file as an archive from: ${nupkgPath}`);
207888
+ }
207889
+ return await nuspecZipData.files[nuspecFile].async('text');
207890
+ }
207891
+ //this is exported for testing, but should not executed directly. Hence the '_' in the name.
207892
+ async function _parsedNuspec(nuspecContent, targetFramework, depName) {
207893
+ var _a;
207894
+ const parsedNuspec = await parseXML.parseStringPromise(nuspecContent);
207895
+ let ownDeps = [];
207896
+ //note: this will throw if assertion fails
207897
+ assertNuspecSchema(parsedNuspec);
207898
+ for (const metadata of parsedNuspec.package.metadata) {
207899
+ (_a = metadata.dependencies) === null || _a === void 0 ? void 0 : _a.forEach(rawDependency => {
207900
+ // Find and add target framework version specific dependencies
207901
+ const depsForTargetFramework = extractDepsForTargetFramework(rawDependency, targetFramework);
207902
+ if (depsForTargetFramework && depsForTargetFramework.group) {
207903
+ ownDeps = ownDeps.concat(extractDepsFromRaw(depsForTargetFramework.group.dependency));
207904
+ }
207905
+ // Find all groups with no targetFramework attribute
207906
+ // add their deps
207907
+ const depsFromPlainGroups = extractDepsForPlainGroups(rawDependency);
207908
+ if (depsFromPlainGroups) {
207909
+ depsFromPlainGroups.forEach(depGroup => {
207910
+ ownDeps = ownDeps.concat(extractDepsFromRaw(depGroup.dependency));
207911
+ });
207912
+ }
207913
+ // Add the default dependencies
207914
+ ownDeps = ownDeps.concat(extractDepsFromRaw(rawDependency.dependency));
207915
+ });
207916
+ }
207917
+ return {
207918
+ children: ownDeps,
207919
+ name: depName,
207920
+ };
207921
+ }
207922
+ exports._parsedNuspec = _parsedNuspec;
207923
+ function assertNuspecSchema(parsedNuspec) {
207924
+ var _a;
207925
+ if (!((_a = parsedNuspec.package) === null || _a === void 0 ? void 0 : _a.metadata)) {
207926
+ throw new Error('This is an invalid nuspec file. Package or Metadata xml section is missing. This is a required element. See https://docs.microsoft.com/en-us/nuget/reference/nuspec');
207927
+ }
207928
+ //just in case, this should *not* happen
207929
+ if (!Array.isArray(parsedNuspec.package.metadata)) {
207930
+ throw new Error('This is an invalid nuspec file; the metadata tag is supposed to be a collection of objects but it is not!');
207931
+ }
207932
+ for (const metadata of parsedNuspec.package.metadata) {
207933
+ //just in case, this shouldn't happen as this would indicate invalid/malformed nuspec file
207934
+ if (metadata == null || typeof metadata !== 'object') {
207935
+ throw new Error('Expected elements in a "metadata" tag to be objects, but they were ' +
207936
+ typeof metadata +
207937
+ ', this is not supposed to happen and is likely due to malformed nuspec file.');
207938
+ }
207939
+ if (metadata.dependencies) {
207940
+ //just in case, error would indicate malformed nuspec
207941
+ if (!Array.isArray(metadata.dependencies)) {
207942
+ throw new Error('Expected that "dependencies" tag would be an array but it isn\'t. This is not supposed to happen and is likely due to malformed nuspec file!');
207943
+ }
207944
+ for (const rawDependency of metadata.dependencies) {
207945
+ //just in case, shouldn't happen as this would indicate malformed nuspec format
207946
+ if (typeof rawDependency !== 'object') {
207947
+ throw new Error('Unexpected dependency value. Expected it to be object but it was ' +
207948
+ typeof rawDependency +
207949
+ ', this is likely a malformed nuspec');
207950
+ }
207951
+ }
207952
+ }
207953
+ }
207954
+ }
207893
207955
  function extractDepsForPlainGroups(rawDependency) {
207894
207956
  if (!rawDependency.group) {
207895
207957
  return [];