dependabot-npm_and_yarn 0.91.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +7 -0
  2. data/helpers/build +14 -0
  3. data/helpers/npm/.eslintrc +14 -0
  4. data/helpers/npm/bin/run.js +34 -0
  5. data/helpers/npm/lib/helpers.js +25 -0
  6. data/helpers/npm/lib/peer-dependency-checker.js +102 -0
  7. data/helpers/npm/lib/subdependency-updater.js +48 -0
  8. data/helpers/npm/lib/updater.js +101 -0
  9. data/helpers/npm/package-lock.json +8868 -0
  10. data/helpers/npm/package.json +17 -0
  11. data/helpers/npm/test/fixtures/npm-left-pad.json +1 -0
  12. data/helpers/npm/test/fixtures/updater/original/package-lock.json +16 -0
  13. data/helpers/npm/test/fixtures/updater/original/package.json +9 -0
  14. data/helpers/npm/test/fixtures/updater/updated/package-lock.json +16 -0
  15. data/helpers/npm/test/helpers.js +7 -0
  16. data/helpers/npm/test/updater.test.js +50 -0
  17. data/helpers/npm/yarn.lock +6176 -0
  18. data/helpers/yarn/.eslintrc +14 -0
  19. data/helpers/yarn/bin/run.js +36 -0
  20. data/helpers/yarn/lib/fix-duplicates.js +78 -0
  21. data/helpers/yarn/lib/helpers.js +5 -0
  22. data/helpers/yarn/lib/lockfile-parser.js +21 -0
  23. data/helpers/yarn/lib/peer-dependency-checker.js +130 -0
  24. data/helpers/yarn/lib/replace-lockfile-declaration.js +57 -0
  25. data/helpers/yarn/lib/subdependency-updater.js +69 -0
  26. data/helpers/yarn/lib/updater.js +266 -0
  27. data/helpers/yarn/package.json +17 -0
  28. data/helpers/yarn/test/fixtures/updater/original/package.json +6 -0
  29. data/helpers/yarn/test/fixtures/updater/original/yarn.lock +11 -0
  30. data/helpers/yarn/test/fixtures/updater/updated/yarn.lock +12 -0
  31. data/helpers/yarn/test/fixtures/updater/with-version-comments/package.json +5 -0
  32. data/helpers/yarn/test/fixtures/updater/with-version-comments/yarn.lock +13 -0
  33. data/helpers/yarn/test/fixtures/yarnpkg-is-positive.json +1 -0
  34. data/helpers/yarn/test/fixtures/yarnpkg-left-pad.json +1 -0
  35. data/helpers/yarn/test/helpers.js +7 -0
  36. data/helpers/yarn/test/updater.test.js +93 -0
  37. data/helpers/yarn/yarn.lock +4760 -0
  38. data/lib/dependabot/npm_and_yarn/file_fetcher/path_dependency_builder.rb +146 -0
  39. data/lib/dependabot/npm_and_yarn/file_fetcher.rb +332 -0
  40. data/lib/dependabot/npm_and_yarn/file_parser.rb +397 -0
  41. data/lib/dependabot/npm_and_yarn/file_updater/npm_lockfile_updater.rb +527 -0
  42. data/lib/dependabot/npm_and_yarn/file_updater/npmrc_builder.rb +190 -0
  43. data/lib/dependabot/npm_and_yarn/file_updater/package_json_preparer.rb +87 -0
  44. data/lib/dependabot/npm_and_yarn/file_updater/package_json_updater.rb +218 -0
  45. data/lib/dependabot/npm_and_yarn/file_updater/yarn_lockfile_updater.rb +471 -0
  46. data/lib/dependabot/npm_and_yarn/file_updater.rb +189 -0
  47. data/lib/dependabot/npm_and_yarn/metadata_finder.rb +217 -0
  48. data/lib/dependabot/npm_and_yarn/native_helpers.rb +28 -0
  49. data/lib/dependabot/npm_and_yarn/requirement.rb +145 -0
  50. data/lib/dependabot/npm_and_yarn/update_checker/latest_version_finder.rb +340 -0
  51. data/lib/dependabot/npm_and_yarn/update_checker/library_detector.rb +67 -0
  52. data/lib/dependabot/npm_and_yarn/update_checker/registry_finder.rb +224 -0
  53. data/lib/dependabot/npm_and_yarn/update_checker/requirements_updater.rb +193 -0
  54. data/lib/dependabot/npm_and_yarn/update_checker/subdependency_version_resolver.rb +223 -0
  55. data/lib/dependabot/npm_and_yarn/update_checker/version_resolver.rb +495 -0
  56. data/lib/dependabot/npm_and_yarn/update_checker.rb +282 -0
  57. data/lib/dependabot/npm_and_yarn/version.rb +34 -0
  58. data/lib/dependabot/npm_and_yarn.rb +11 -0
  59. metadata +226 -0
@@ -0,0 +1,146 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "dependabot/dependency_file"
5
+ require "dependabot/errors"
6
+ require "dependabot/npm_and_yarn/native_helpers"
7
+ require "dependabot/npm_and_yarn/file_fetcher"
8
+
9
+ module Dependabot
10
+ module NpmAndYarn
11
+ class FileFetcher
12
+ class PathDependencyBuilder
13
+ def initialize(dependency_name:, path:, directory:, package_lock:,
14
+ yarn_lock:)
15
+ @dependency_name = dependency_name
16
+ @path = path
17
+ @directory = directory
18
+ @package_lock = package_lock
19
+ @yarn_lock = yarn_lock
20
+ end
21
+
22
+ def dependency_file
23
+ filename = File.join(path, "package.json")
24
+
25
+ DependencyFile.new(
26
+ name: Pathname.new(filename).cleanpath.to_path,
27
+ content: build_path_dep_content(dependency_name),
28
+ directory: directory,
29
+ support_file: true
30
+ )
31
+ end
32
+
33
+ private
34
+
35
+ attr_reader :dependency_name, :path, :package_lock, :yarn_lock,
36
+ :directory
37
+
38
+ def details_from_yarn_lock
39
+ parsed_yarn_lock.to_a.
40
+ find do |n, _|
41
+ next false unless n.split(/(?<=\w)\@/).first == dependency_name
42
+
43
+ n.split(/(?<=\w)\@/).last.start_with?("file:")
44
+ end&.last
45
+ end
46
+
47
+ def details_from_npm_lock
48
+ parsed_package_lock.fetch("dependencies", []).to_a.
49
+ select { |_, v| v.fetch("version", "").start_with?("file:") }.
50
+ find { |n, _| n == dependency_name }&.
51
+ last
52
+ end
53
+
54
+ def build_path_dep_content(dependency_name)
55
+ unless details_from_yarn_lock || details_from_npm_lock
56
+ raise Dependabot::PathDependenciesNotReachable, [dependency_name]
57
+ end
58
+
59
+ if details_from_yarn_lock
60
+ {
61
+ name: dependency_name,
62
+ version: details_from_yarn_lock["version"] || "0.0.1",
63
+ dependencies:
64
+ replace_yarn_lock_file_paths(
65
+ details_from_yarn_lock["dependencies"]
66
+ ),
67
+ optionalDependencies:
68
+ replace_yarn_lock_file_paths(
69
+ details_from_yarn_lock["optionalDependencies"]
70
+ )
71
+ }.compact.to_json
72
+ else
73
+ {
74
+ name: dependency_name,
75
+ version: "0.0.1",
76
+ dependencies: details_from_npm_lock["requires"]
77
+ }.compact.to_json
78
+ end
79
+ end
80
+
81
+ # If an unfetchable path dependency itself has path dependencies
82
+ # then the paths in the yarn.lock for them will be absolute, not
83
+ # relative. Worse, they may point to the user's local cache.
84
+ # We work around this by constructing a relative path to the
85
+ # (second-level) path dependencies.
86
+ def replace_yarn_lock_file_paths(dependencies_hash)
87
+ return unless dependencies_hash
88
+
89
+ dependencies_hash.each_with_object({}) do |(k, v), obj|
90
+ obj[k] = v
91
+ next unless v.start_with?("file:")
92
+
93
+ path_from_base =
94
+ parsed_yarn_lock.to_a.
95
+ find do |n, _|
96
+ next false unless n.split(/(?<=\w)\@/).first == k
97
+
98
+ n.split(/(?<=\w)\@/).last.start_with?("file:")
99
+ end&.first&.split(/(?<=\w)\@/)&.last&.gsub("file:", "")
100
+
101
+ next unless path_from_base
102
+
103
+ obj[k] = "file:" + File.join(inverted_path, path_from_base)
104
+ end
105
+ end
106
+
107
+ def parsed_package_lock
108
+ return {} unless package_lock
109
+
110
+ JSON.parse(package_lock.content)
111
+ rescue JSON::ParserError
112
+ {}
113
+ end
114
+
115
+ def parsed_yarn_lock
116
+ return {} unless yarn_lock
117
+
118
+ @parsed_yarn_lock ||=
119
+ SharedHelpers.in_a_temporary_directory do
120
+ File.write("yarn.lock", yarn_lock.content)
121
+
122
+ SharedHelpers.run_helper_subprocess(
123
+ command: "node #{yarn_helper_path}",
124
+ function: "parseLockfile",
125
+ args: [Dir.pwd]
126
+ )
127
+ end
128
+ end
129
+
130
+ # The path back to the root lockfile
131
+ def inverted_path
132
+ path.split("/").map do |part|
133
+ next part if part == "."
134
+ next "tmp" if part == ".."
135
+
136
+ ".."
137
+ end.join("/")
138
+ end
139
+
140
+ def yarn_helper_path
141
+ NativeHelpers.yarn_helper_path
142
+ end
143
+ end
144
+ end
145
+ end
146
+ end
@@ -0,0 +1,332 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "dependabot/file_fetchers"
5
+ require "dependabot/file_fetchers/base"
6
+ require "dependabot/npm_and_yarn/file_parser"
7
+
8
+ module Dependabot
9
+ module NpmAndYarn
10
+ class FileFetcher < Dependabot::FileFetchers::Base
11
+ require_relative "file_fetcher/path_dependency_builder"
12
+
13
+ def self.required_files_in?(filenames)
14
+ filenames.include?("package.json")
15
+ end
16
+
17
+ def self.required_files_message
18
+ "Repo must contain a package.json."
19
+ end
20
+
21
+ private
22
+
23
+ # rubocop:disable Metrics/CyclomaticComplexity
24
+ # rubocop:disable Metrics/PerceivedComplexity
25
+ def fetch_files
26
+ fetched_files = []
27
+ fetched_files << package_json
28
+ fetched_files << package_lock if package_lock && !ignore_package_lock?
29
+ fetched_files << yarn_lock if yarn_lock
30
+ fetched_files << shrinkwrap if shrinkwrap
31
+ fetched_files << lerna_json if lerna_json
32
+ fetched_files << npmrc if npmrc
33
+ fetched_files << yarnrc if yarnrc
34
+ fetched_files += workspace_package_jsons
35
+ fetched_files += lerna_packages
36
+ fetched_files += path_dependencies(fetched_files)
37
+
38
+ fetched_files.uniq
39
+ end
40
+ # rubocop:enable Metrics/CyclomaticComplexity
41
+ # rubocop:enable Metrics/PerceivedComplexity
42
+
43
+ def package_json
44
+ @package_json ||= fetch_file_from_host("package.json")
45
+ end
46
+
47
+ def package_lock
48
+ @package_lock ||= fetch_file_if_present("package-lock.json")
49
+ end
50
+
51
+ def yarn_lock
52
+ @yarn_lock ||= fetch_file_if_present("yarn.lock")
53
+ end
54
+
55
+ def shrinkwrap
56
+ @shrinkwrap ||= fetch_file_if_present("npm-shrinkwrap.json")
57
+ end
58
+
59
+ def npmrc
60
+ @npmrc ||= fetch_file_if_present(".npmrc")&.
61
+ tap { |f| f.support_file = true }
62
+
63
+ return @npmrc if @npmrc || directory == "/"
64
+
65
+ # Loop through parent directories looking for an npmrc
66
+ (1..directory.split("/").count).each do |i|
67
+ @npmrc = fetch_file_from_host("../" * i + ".npmrc")&.
68
+ tap { |f| f.support_file = true }
69
+ break if @npmrc
70
+ rescue Dependabot::DependencyFileNotFound
71
+ # Ignore errors (.npmrc may not be present)
72
+ nil
73
+ end
74
+
75
+ @npmrc
76
+ end
77
+
78
+ def yarnrc
79
+ @yarnrc ||= fetch_file_if_present(".yarnrc")&.
80
+ tap { |f| f.support_file = true }
81
+
82
+ return @yarnrc if @yarnrc || directory == "/"
83
+
84
+ # Loop through parent directories looking for an yarnrc
85
+ (1..directory.split("/").count).each do |i|
86
+ @yarnrc = fetch_file_from_host("../" * i + ".yarnrc")&.
87
+ tap { |f| f.support_file = true }
88
+ break if @yarnrc
89
+ rescue Dependabot::DependencyFileNotFound
90
+ # Ignore errors (.yarnrc may not be present)
91
+ nil
92
+ end
93
+
94
+ @yarnrc
95
+ end
96
+
97
+ def lerna_json
98
+ @lerna_json ||= fetch_file_if_present("lerna.json")&.
99
+ tap { |f| f.support_file = true }
100
+ end
101
+
102
+ def workspace_package_jsons
103
+ @workspace_package_jsons ||= fetch_workspace_package_jsons
104
+ end
105
+
106
+ def lerna_packages
107
+ @lerna_packages ||= fetch_lerna_packages
108
+ end
109
+
110
+ def path_dependencies(fetched_files)
111
+ package_json_files = []
112
+ unfetchable_deps = []
113
+
114
+ path_dependency_details(fetched_files).each do |name, path|
115
+ path = path.sub(/^file:/, "").sub(/^link:/, "")
116
+ filename = File.join(path, "package.json")
117
+ cleaned_name = Pathname.new(filename).cleanpath.to_path
118
+ next if fetched_files.map(&:name).include?(cleaned_name)
119
+
120
+ begin
121
+ file = fetch_file_from_host(filename)
122
+ package_json_files << file
123
+ rescue Dependabot::DependencyFileNotFound
124
+ unfetchable_deps << [name, path]
125
+ end
126
+ end
127
+
128
+ package_json_files += build_unfetchable_deps(unfetchable_deps)
129
+
130
+ if package_json_files.any?
131
+ package_json_files +=
132
+ path_dependencies(fetched_files + package_json_files)
133
+ end
134
+
135
+ package_json_files.tap { |fs| fs.each { |f| f.support_file = true } }
136
+ end
137
+
138
+ def path_dependency_details(fetched_files)
139
+ package_json_path_deps = []
140
+
141
+ fetched_files.each do |file|
142
+ package_json_path_deps +=
143
+ path_dependency_details_from_manifest(file)
144
+ end
145
+
146
+ path_starts = %w(file: link:.)
147
+
148
+ package_lock_path_deps =
149
+ parsed_package_lock.fetch("dependencies", []).to_a.
150
+ select { |_, v| v.fetch("version", "").start_with?(*path_starts) }.
151
+ map { |k, v| [k, v.fetch("version")] }
152
+
153
+ shrinkwrap_path_deps =
154
+ parsed_shrinkwrap.fetch("dependencies", []).to_a.
155
+ select { |_, v| v.fetch("version", "").start_with?(*path_starts) }.
156
+ map { |k, v| [k, v.fetch("version")] }
157
+
158
+ [
159
+ *package_json_path_deps,
160
+ *package_lock_path_deps,
161
+ *shrinkwrap_path_deps
162
+ ].uniq
163
+ end
164
+
165
+ def path_dependency_details_from_manifest(file)
166
+ return [] unless file.name.end_with?("package.json")
167
+
168
+ current_dir = file.name.rpartition("/").first
169
+ current_dir = nil if current_dir == ""
170
+ path_dep_starts = %w(file: / ./ ../ ~/ link:.)
171
+
172
+ JSON.parse(file.content).
173
+ values_at(*NpmAndYarn::FileParser::DEPENDENCY_TYPES).
174
+ compact.flat_map(&:to_a).
175
+ select { |_, v| v.start_with?(*path_dep_starts) }.
176
+ map do |name, path|
177
+ path = path.sub(/^file:/, "").sub(/^link:/, "")
178
+ path = File.join(current_dir, path) unless current_dir.nil?
179
+ [name, Pathname.new(path).cleanpath.to_path]
180
+ end
181
+ rescue JSON::ParserError
182
+ raise Dependabot::DependencyFileNotParseable, file.path
183
+ end
184
+
185
+ def fetch_workspace_package_jsons
186
+ return [] unless parsed_package_json["workspaces"]
187
+
188
+ package_json_files = []
189
+
190
+ workspace_paths(parsed_package_json["workspaces"]).each do |workspace|
191
+ file = File.join(workspace, "package.json")
192
+
193
+ begin
194
+ package_json_files << fetch_file_from_host(file)
195
+ rescue Dependabot::DependencyFileNotFound
196
+ nil
197
+ end
198
+ end
199
+
200
+ package_json_files
201
+ end
202
+
203
+ def fetch_lerna_packages
204
+ return [] unless parsed_lerna_json["packages"]
205
+
206
+ dependency_files = []
207
+
208
+ workspace_paths(parsed_lerna_json["packages"]).each do |workspace|
209
+ dependency_files += fetch_lerna_packages_from_path(workspace)
210
+ end
211
+
212
+ dependency_files
213
+ end
214
+
215
+ def fetch_lerna_packages_from_path(path, nested = false)
216
+ dependency_files = []
217
+
218
+ package_json_path = File.join(path, "package.json")
219
+
220
+ begin
221
+ dependency_files << fetch_file_from_host(package_json_path)
222
+ dependency_files += [
223
+ fetch_file_if_present(File.join(path, "package-lock.json")),
224
+ fetch_file_if_present(File.join(path, "yarn.lock")),
225
+ fetch_file_if_present(File.join(path, "npm-shrinkwrap.json"))
226
+ ].compact
227
+ rescue Dependabot::DependencyFileNotFound
228
+ matches_double_glob =
229
+ parsed_lerna_json["packages"].any? do |globbed_path|
230
+ next false unless globbed_path.include?("**")
231
+
232
+ File.fnmatch?(globbed_path, path)
233
+ end
234
+
235
+ if matches_double_glob && !nested
236
+ dependency_files +=
237
+ expanded_paths(File.join(path, "*")).flat_map do |nested_path|
238
+ fetch_lerna_packages_from_path(nested_path, true)
239
+ end
240
+ end
241
+ end
242
+
243
+ dependency_files
244
+ end
245
+
246
+ def workspace_paths(workspace_object)
247
+ paths_array =
248
+ if workspace_object.is_a?(Hash)
249
+ workspace_object.values_at("packages", "nohoist").flatten.compact
250
+ elsif workspace_object.is_a?(Array) then workspace_object
251
+ else raise "Unexpected workspace object"
252
+ end
253
+
254
+ paths_array.flat_map do |path|
255
+ # The packages/!(not-this-package) syntax is unique to Yarn
256
+ if path.include?("*") || path.include?("!(")
257
+ expanded_paths(path)
258
+ else path
259
+ end
260
+ end
261
+ end
262
+
263
+ def expanded_paths(path)
264
+ ignored_paths = path.scan(/!\((.*?)\)/).flatten
265
+
266
+ dir = directory.gsub(%r{(^/|/$)}, "")
267
+ path = path.gsub(%r{^\./}, "").gsub(/!\(.*?\)/, "*")
268
+ unglobbed_path = path.split("*").first&.gsub(%r{(?<=/)[^/]*$}, "") ||
269
+ "."
270
+
271
+ repo_contents(dir: unglobbed_path, raise_errors: false).
272
+ select { |file| file.type == "dir" }.
273
+ map { |f| f.path.gsub(%r{^/?#{Regexp.escape(dir)}/?}, "") }.
274
+ select { |filename| File.fnmatch?(path, filename) }.
275
+ reject { |fn| ignored_paths.any? { |p| fn.include?(p) } }
276
+ end
277
+
278
+ def parsed_package_json
279
+ JSON.parse(package_json.content)
280
+ rescue JSON::ParserError
281
+ raise Dependabot::DependencyFileNotParseable, package_json.path
282
+ end
283
+
284
+ def parsed_package_lock
285
+ return {} unless package_lock
286
+
287
+ JSON.parse(package_lock.content)
288
+ rescue JSON::ParserError
289
+ {}
290
+ end
291
+
292
+ def parsed_shrinkwrap
293
+ return {} unless shrinkwrap
294
+
295
+ JSON.parse(shrinkwrap.content)
296
+ rescue JSON::ParserError
297
+ {}
298
+ end
299
+
300
+ def ignore_package_lock?
301
+ return false unless npmrc
302
+
303
+ npmrc.content.match?(/^package-lock\s*=\s*false/)
304
+ end
305
+
306
+ def build_unfetchable_deps(unfetchable_deps)
307
+ return [] unless package_lock || yarn_lock
308
+
309
+ unfetchable_deps.map do |name, path|
310
+ PathDependencyBuilder.new(
311
+ dependency_name: name,
312
+ path: path,
313
+ directory: directory,
314
+ package_lock: package_lock,
315
+ yarn_lock: yarn_lock
316
+ ).dependency_file
317
+ end
318
+ end
319
+
320
+ def parsed_lerna_json
321
+ return {} unless lerna_json
322
+
323
+ JSON.parse(lerna_json.content)
324
+ rescue JSON::ParserError
325
+ raise Dependabot::DependencyFileNotParseable, lerna_json.path
326
+ end
327
+ end
328
+ end
329
+ end
330
+
331
+ Dependabot::FileFetchers.
332
+ register("npm_and_yarn", Dependabot::NpmAndYarn::FileFetcher)