snyk 1.1055.0 → 1.1056.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.
@@ -142568,7 +142568,7 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
142568
142568
  "use strict";
142569
142569
 
142570
142570
  Object.defineProperty(exports, "__esModule", ({ value: true }));
142571
- exports.readRawBuildInfo = exports.extractModuleInformation = exports.GoBinary = void 0;
142571
+ exports.determinePaths = exports.readRawBuildInfo = exports.extractModuleInformation = exports.GoBinary = void 0;
142572
142572
  const depGraph = __webpack_require__(26326);
142573
142573
  const event_loop_spinner_1 = __webpack_require__(77158);
142574
142574
  const path = __webpack_require__(85622);
@@ -142608,36 +142608,29 @@ class GoBinary {
142608
142608
  // matchFilesToModules goes through all files, extracts the package name and
142609
142609
  // adds it to the relevant module in the GoBinary.
142610
142610
  matchFilesToModules(files) {
142611
- // goModCachePath is the path at which the modules are downloaded to. When
142612
- // building a Go binary, this is usually either $GOMODCACHE or
142613
- // $GOROOT/pkg/mod. Binaries built with `-trimpath` will have that module
142614
- // cache path trimmed away, meaning that the goModCachePath will always be
142615
- // empty.
142616
- let goModCachePath = "";
142617
- let moduleName = (mod) => {
142618
- return mod.fullName();
142619
- };
142611
+ const { modCachePath, vendorPath } = determinePaths(this.modules, files);
142620
142612
  for (const fileName of files) {
142621
142613
  if (fileName === "<autogenerated>") {
142622
142614
  continue;
142623
142615
  }
142624
- // As long as the goModCachePath is not set, try finding it. We do not
142625
- // have a way of knowing whether we're checking a `-trimpath` binary, so
142626
- // for `-trimpath` binaries we will be wasting a couple of cycles by
142627
- // trying to find the goModCachePath for every file.
142628
- if (goModCachePath === "") {
142629
- [goModCachePath, moduleName] = this.determineModuleCachePath(fileName);
142630
- }
142631
- // Assuming that goModCachePath is set, this is true for files outside the
142632
- // modPath, which is usually the developers files (the built module) and
142633
- // Go source files (stdlib, runtime etc). If it is not set, this check is
142634
- // meaningless.
142635
- if (!fileName.startsWith(goModCachePath)) {
142616
+ let moduleName = (mod) => mod.fullName();
142617
+ let pkgFile = "";
142618
+ if (vendorPath && fileName.startsWith(vendorPath)) {
142619
+ moduleName = (mod) => mod.name;
142620
+ pkgFile = trimPrefix(fileName, vendorPath);
142621
+ }
142622
+ else if (modCachePath && fileName.startsWith(modCachePath)) {
142623
+ moduleName = (mod) => mod.fullName();
142624
+ pkgFile = trimPrefix(fileName, modCachePath);
142625
+ }
142626
+ else if (!vendorPath && !modCachePath) {
142627
+ // is trimmed
142628
+ pkgFile = fileName;
142629
+ }
142630
+ else {
142631
+ // skip file, probably a file from the Go source.
142636
142632
  continue;
142637
142633
  }
142638
- // remove the go module cache path at the beginning of the file name,
142639
- // leaving only the module + file paths.
142640
- const pkgFile = fileName.slice(goModCachePath.length);
142641
142634
  // Try to find the module that matches our file name, and if found,
142642
142635
  // extract the package name out of it.
142643
142636
  // Go source files will not be matched by any module, so they will be
@@ -142673,40 +142666,6 @@ class GoBinary {
142673
142666
  }
142674
142667
  }
142675
142668
  }
142676
- determineModuleCachePath(fileName) {
142677
- // this is the module name as it appears in the directory on *non-vendored*
142678
- // dependencies, e.g. "/go/pkg/mod/github.com/my/repo@v1.0.0".
142679
- const fullName = (m) => {
142680
- return m.fullName();
142681
- };
142682
- for (const [, mod] of Object.entries(this.modules)) {
142683
- if (!fileName.includes(mod.name)) {
142684
- continue;
142685
- }
142686
- const parts = fileName.split(fullName(mod));
142687
- if (parts.length === 2) {
142688
- return [parts[0], fullName];
142689
- }
142690
- else {
142691
- const parts = fileName.split(path.join("vendor", mod.name));
142692
- if (parts.length === 2) {
142693
- return [
142694
- // trailing slash is important to properly separate module name from
142695
- // directory. parts[0] would always include it, so we also need to
142696
- // add it to "vendor".
142697
- path.join(parts[0], "vendor/"),
142698
- // for vendored modules, the directory does not contain the version,
142699
- // e.g. /app/vendor/github.com/my/repo/. As such, the "full name" of
142700
- // the module should also not contain the version string.
142701
- (m) => {
142702
- return m.name;
142703
- },
142704
- ];
142705
- }
142706
- }
142707
- }
142708
- return ["", fullName];
142709
- }
142710
142669
  }
142711
142670
  exports.GoBinary = GoBinary;
142712
142671
  function extractModuleInformation(binary) {
@@ -142902,6 +142861,72 @@ function readString(binaryFile, ptrSize, readPtr, addr) {
142902
142861
  }
142903
142862
  return data.toString("binary");
142904
142863
  }
142864
+ function isTrimmed(files) {
142865
+ // the easiest way to detect trimmed binaries: the filenames will all be
142866
+ // relative.
142867
+ // There usually is a `build` line in the Go binary's metadata that denotes
142868
+ // whether `trimpath` has been used or not, but there are binaries out there
142869
+ // that have trimmed paths without that annotation (for example
142870
+ // kyverno@v1.8.1).
142871
+ return files.every((file) => !file.startsWith("/"));
142872
+ }
142873
+ // determinePaths returns the modCachePath and vendorPath for a binary.
142874
+ // goModCachePath is the path at which the modules are downloaded to. When
142875
+ // building a Go binary, this is usually either $GOMODCACHE or
142876
+ // $GOROOT/pkg/mod.
142877
+ // The vendorPath is the path where vendored files are located, which is usually
142878
+ // the main module's location + "/vendor".
142879
+ //
142880
+ // Binaries built with `-trimpath` will have all paths trimmed away, meaning
142881
+ // that both returned values will be empty.
142882
+ function determinePaths(modules, files) {
142883
+ if (isTrimmed(files)) {
142884
+ return { modCachePath: "", vendorPath: "" };
142885
+ }
142886
+ return {
142887
+ modCachePath: determineGoModCachePath(modules, files),
142888
+ vendorPath: determineVendorPath(modules, files),
142889
+ };
142890
+ }
142891
+ exports.determinePaths = determinePaths;
142892
+ function determineVendorPath(modules, files) {
142893
+ // to determine the vendor path, we search for a file that contains
142894
+ // `vendor/<module name>`. This file also contains the "root" of the
142895
+ // repository, e.g. `/app/vendor/<module-name>`. This means that the `main`
142896
+ // package is located somewhere in `/app/...`.
142897
+ // We check for other files in that root to make sure that we really got the
142898
+ // right vendor folder, and not just a random folder named `vendor` somewhere.
142899
+ for (const [, mod] of Object.entries(modules)) {
142900
+ const vendoredModulePath = path.join("vendor", mod.name) + "/";
142901
+ const file = files.find((file) => file.includes(vendoredModulePath));
142902
+ if (file) {
142903
+ // make sure that we find other files in that path not in the vendor
142904
+ // folder.
142905
+ const mainModulePath = file.split(vendoredModulePath)[0];
142906
+ const success = files.find((file) => file.includes(mainModulePath) && !file.includes(vendoredModulePath));
142907
+ if (success) {
142908
+ return path.join(mainModulePath, "vendor") + "/";
142909
+ }
142910
+ }
142911
+ }
142912
+ return "";
142913
+ }
142914
+ function determineGoModCachePath(modules, files) {
142915
+ // files in the go mod cache path always contain the module name and version.
142916
+ for (const [, mod] of Object.entries(modules)) {
142917
+ const file = files.find((file) => file.includes("/" + mod.fullName()));
142918
+ if (file) {
142919
+ return file.split(mod.fullName())[0];
142920
+ }
142921
+ }
142922
+ return "";
142923
+ }
142924
+ function trimPrefix(s, prefix) {
142925
+ if (s.startsWith(prefix)) {
142926
+ return s.substring(prefix.length);
142927
+ }
142928
+ return s;
142929
+ }
142905
142930
  //# sourceMappingURL=go-binary.js.map
142906
142931
 
142907
142932
  /***/ }),