Skip to content

Commit

Permalink
fix(npm): registry inferring should include the full registry path
Browse files Browse the repository at this point in the history
  • Loading branch information
yeikel committed Apr 10, 2023
1 parent ad21dc3 commit a28f712
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ coverage/
.tool-versions
.rspec_status
.rdbg_history
.idea/
23 changes: 12 additions & 11 deletions npm_and_yarn/lib/dependabot/npm_and_yarn/file_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class FileFetcher < Dependabot::FileFetchers::Base
# "yarn link", e.g. "link:react"
PATH_DEPENDENCY_STARTS = %w(file: link:. link:/ link:~/ / ./ ../ ~/).freeze
PATH_DEPENDENCY_CLEAN_REGEX = /^file:|^link:/
DEFAULT_NPM_REGISTRY = "https://registry.npmjs.org"

def self.required_files_in?(filenames)
filenames.include?("package.json")
Expand Down Expand Up @@ -85,25 +86,25 @@ def fetch_files

# If every entry in the lockfile uses the same registry, we can infer
# that there is a global .npmrc file, so add it here as if it were in the repo.

def inferred_npmrc
return @inferred_npmrc if defined?(@inferred_npmrc)
return @inferred_npmrc = nil unless npmrc.nil? && package_lock

known_registries = []
JSON.parse(package_lock.content).fetch("dependencies", {}).each do |_name, details|
resolved = details.fetch("resolved", "https://registry.npmjs.org")
begin
uri = URI.parse(resolved)
rescue URI::InvalidURIError
# Ignoring non-URIs since they're not registries.
# This can happen if resolved is false, for instance.
next
JSON.parse(package_lock.content).fetch("dependencies", {}).each do |dependency_name, details|
resolved = details.fetch("resolved", DEFAULT_NPM_REGISTRY)

next if !resolved.is_a?(String) || resolved.include?(DEFAULT_NPM_REGISTRY)

index = resolved.index(dependency_name)
unless index.nil?
registry_base_url = resolved[0...index]
known_registries << registry_base_url
end
# Check for scheme since path dependencies will not have one
known_registries << "#{uri.scheme}://#{uri.host}" if uri.scheme && uri.host
end

if known_registries.uniq.length == 1 && known_registries.first != "https://registry.npmjs.org"
if known_registries.uniq.length == 1 && known_registries.first != DEFAULT_NPM_REGISTRY
Dependabot.logger.info("Inferred global NPM registry is: #{known_registries.first}")
return @inferred_npmrc = Dependabot::DependencyFile.new(
name: ".npmrc",
Expand Down
32 changes: 31 additions & 1 deletion npm_and_yarn/spec/dependabot/npm_and_yarn/file_fetcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1787,7 +1787,37 @@
expect(file_fetcher_instance.files.map(&:name)).
to eq(%w(package.json package-lock.json .npmrc))
expect(file_fetcher_instance.files.find { |f| f.name == ".npmrc" }.content).
to eq("registry=https://npm.fury.io")
to eq("registry=https://npm.fury.io/dependabot/")
end
end

context "with no .npmrc but package-lock.json contains a artifactory repository" do
before do
allow(file_fetcher_instance).to receive(:commit).and_return("sha")

stub_request(:get, File.join(url, "package.json?ref=sha")).
with(headers: { "Authorization" => "token token" }).
to_return(
status: 200,
body: fixture_to_response("projects/npm6/private_artifactory_repository", "package.json"),
headers: json_header
)

stub_request(:get, File.join(url, "package-lock.json?ref=sha")).
with(headers: { "Authorization" => "token token" }).
to_return(
status: 200,
body: fixture_to_response("projects/npm6/private_artifactory_repository", "package-lock.json"),
headers: json_header
)
end

it "infers an npmrc file" do
expect(file_fetcher_instance.files.count).to eq(3)
expect(file_fetcher_instance.files.map(&:name)).
to eq(%w(package.json package-lock.json .npmrc))
expect(file_fetcher_instance.files.find { |f| f.name == ".npmrc" }.content).
to eq("registry=https://myRegistry/api/npm/npm/")
end
end
end
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "{{ name }}",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no\\ test\ specified\" && exit 1",
"prettify": "prettier --write \"{{packages/*/src,examples,cypress,scripts}/**/,}*.{js,jsx,ts,tsx,css,md}\""
},
"repository": {
"type": "git",
"url": "git+https://github.com/waltfy/PROTO_TEST.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/waltfy/PROTO_TEST/issues"
},
"homepage": "https://github.com/waltfy/PROTO_TEST#readme",
"dependencies": {
"fetch-factory": "^0.0.1"
},
"devDependencies": {
"etag" : "^1.0.0"
}}

0 comments on commit a28f712

Please sign in to comment.