Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependabot::UnexpectedExternalCode #3098

Merged
merged 3 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion bin/dry-run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
write: false,
clone: false,
lockfile_only: false,
reject_external_code: false,
requirements_update_strategy: nil,
commit: nil,
updater_options: {},
Expand Down Expand Up @@ -167,6 +168,10 @@
$options[:lockfile_only] = true
end

opts.on("--reject-external-code", "Reject external code") do |_value|
$options[:reject_external_code] = true
end

opts_req_desc = "Options: auto, widen_ranges, bump_versions or "\
"bump_versions_if_necessary"
opts.on("--requirements-update-strategy STRATEGY", opts_req_desc) do |value|
Expand Down Expand Up @@ -454,7 +459,8 @@ def handle_dependabot_error(error:, dependency:)
dependency_files: $files,
repo_contents_path: $repo_contents_path,
source: source,
credentials: $options[:credentials]
credentials: $options[:credentials],
reject_external_code: $options[:reject_external_code],
)

dependencies = cached_read("dependencies") { parser.parse }
Expand Down
15 changes: 15 additions & 0 deletions bundler/lib/dependabot/bundler/file_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,26 @@ def parse
dependency_set += gemfile_dependencies
dependency_set += gemspec_dependencies
dependency_set += lockfile_dependencies
check_external_code(dependency_set.dependencies)
dependency_set.dependencies
end

private

def check_external_code(dependencies)
return unless @reject_external_code
return unless git_source?(dependencies)

# A git source dependency might contain a .gemspec that is evaluated
raise ::Dependabot::UnexpectedExternalCode
end

def git_source?(dependencies)
dependencies.any? do |dep|
dep.requirements.any? { |req| req.fetch(:source)&.fetch(:type) == "git" }
end
end

def gemfile_dependencies
dependencies = DependencySet.new

Expand Down
39 changes: 38 additions & 1 deletion bundler/spec/dependabot/bundler/file_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,21 @@
let(:lockfile) do
Dependabot::DependencyFile.new(name: "Gemfile.lock", content: lockfile_body)
end
let(:parser) { described_class.new(dependency_files: files, source: source) }
let(:parser) do
described_class.new(
dependency_files: files,
source: source,
reject_external_code: reject_external_code
)
end
let(:source) do
Dependabot::Source.new(
provider: "github",
repo: "gocardless/bump",
directory: "/"
)
end
let(:reject_external_code) { false }
let(:gemfile_body) { fixture("ruby", "gemfiles", gemfile_fixture_name) }
let(:lockfile_body) { fixture("ruby", "lockfiles", lockfile_fixture_name) }
let(:gemfile_fixture_name) { "version_specified" }
Expand Down Expand Up @@ -281,6 +288,36 @@
end
end

context "rejecting external code" do
let(:reject_external_code) { true }

context "with no git sources" do
let(:gemfile_fixture_name) { "version_specified" }

it "does not raise exception" do
expect { parser.parse }.not_to raise_error
end
end

context "with a git source" do
let(:gemfile_fixture_name) { "git_source" }
let(:lockfile_fixture_name) { "git_source.lock" }

it "raises exception" do
expect { parser.parse }.to raise_error(::Dependabot::UnexpectedExternalCode)
end
end

context "with a subdependency of a git source" do
let(:lockfile_fixture_name) { "git_source_undeclared.lock" }
let(:gemfile_fixture_name) { "git_source_undeclared" }

it "raises exception" do
expect { parser.parse }.to raise_error(::Dependabot::UnexpectedExternalCode)
end
end
end

context "with a dependency that only appears in the lockfile" do
let(:gemfile_fixture_name) { "subdependency" }
let(:lockfile_fixture_name) { "subdependency.lock" }
Expand Down
3 changes: 3 additions & 0 deletions common/lib/dependabot/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,7 @@ def initialize(go_mod, declared_path, discovered_path)

# Raised by UpdateChecker if all candidate updates are ignored
class AllVersionsIgnored < DependabotError; end

# Raised by FileParser if processing may execute external code in the update context
class UnexpectedExternalCode < DependabotError; end
end
3 changes: 2 additions & 1 deletion common/lib/dependabot/file_parsers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ class Base
attr_reader :dependency_files, :repo_contents_path, :credentials, :source

def initialize(dependency_files:, repo_contents_path: nil, source:,
credentials: [])
credentials: [], reject_external_code: false)
@dependency_files = dependency_files
@repo_contents_path = repo_contents_path
@credentials = credentials
@source = source
@reject_external_code = reject_external_code

check_required_files
end
Expand Down