Skip to content

Commit 10d694a

Browse files
deivid-rodriguezmatzbot
authored andcommitted
[rubygems/rubygems] Warn on insecure materialization
ruby/rubygems@bc2537de71
1 parent 7cb0bb4 commit 10d694a

File tree

8 files changed

+58
-13
lines changed

8 files changed

+58
-13
lines changed

lib/bundler/definition.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,15 @@ def materialize(dependencies)
651651
incomplete_specs = still_incomplete_specs
652652
end
653653

654+
insecurely_materialized_specs = specs.insecurely_materialized_specs
655+
656+
if insecurely_materialized_specs.any?
657+
Bundler.ui.warn "The following platform specific gems are getting installed, yet the lockfile includes only their generic ruby version:\n" \
658+
" * #{insecurely_materialized_specs.map(&:full_name).join("\n * ")}\n" \
659+
"Please run `bundle lock --normalize-platforms` and commit the resulting lockfile.\n" \
660+
"Alternatively, you may run `bundle lock --add-platform <list-of-platforms-that-you-want-to-support>`"
661+
end
662+
654663
bundler = sources.metadata_source.specs.search(["bundler", Bundler.gem_version]).last
655664
specs["bundler"] = bundler
656665

lib/bundler/endpoint_specification.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class EndpointSpecification < Gem::Specification
66
include MatchRemoteMetadata
77

88
attr_reader :name, :version, :platform, :checksum
9-
attr_accessor :remote, :dependencies
9+
attr_accessor :remote, :dependencies, :locked_platform
1010

1111
def initialize(name, version, platform, spec_fetcher, dependencies, metadata = nil)
1212
super()
@@ -18,10 +18,15 @@ def initialize(name, version, platform, spec_fetcher, dependencies, metadata = n
1818

1919
@loaded_from = nil
2020
@remote_specification = nil
21+
@locked_platform = nil
2122

2223
parse_metadata(metadata)
2324
end
2425

26+
def insecurely_materialized?
27+
@locked_platform.to_s != @platform.to_s
28+
end
29+
2530
def fetch_platform
2631
@platform
2732
end

lib/bundler/lazy_specification.rb

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,24 @@ def to_lock
9999
out
100100
end
101101

102-
def materialize_for_installation(most_specific_locked_platform = nil)
102+
def materialize_strictly
103103
source.local!
104104

105-
matching_specs = source.specs.search(use_exact_resolved_specifications?(most_specific_locked_platform) ? self : [name, version])
105+
matching_specs = source.specs.search(self)
106106
return self if matching_specs.empty?
107107

108-
candidates = if use_exact_resolved_specifications?(most_specific_locked_platform)
109-
matching_specs
108+
__materialize__(matching_specs)
109+
end
110+
111+
def materialize_for_installation(most_specific_locked_platform = nil)
112+
source.local!
113+
114+
if use_exact_resolved_specifications?(most_specific_locked_platform)
115+
materialize_strictly
110116
else
117+
matching_specs = source.specs.search([name, version])
118+
return self if matching_specs.empty?
119+
111120
target_platform = source.is_a?(Source::Path) ? platform : local_platform
112121

113122
installable_candidates = GemHelpers.select_best_platform_match(matching_specs, target_platform)
@@ -119,10 +128,8 @@ def materialize_for_installation(most_specific_locked_platform = nil)
119128
installable_candidates = GemHelpers.select_best_platform_match(matching_specs, platform)
120129
end
121130

122-
installable_candidates
131+
__materialize__(installable_candidates)
123132
end
124-
125-
__materialize__(candidates)
126133
end
127134

128135
# If in frozen mode, we fallback to a non-installable candidate because by
@@ -143,8 +150,12 @@ def __materialize__(candidates, fallback_to_non_installable: Bundler.frozen_bund
143150
# `bundler/setup` performance
144151
if search.is_a?(StubSpecification)
145152
search.dependencies = dependencies
146-
elsif !source.is_a?(Source::Path) && search.runtime_dependencies.sort != dependencies.sort
147-
raise IncorrectLockfileDependencies.new(self)
153+
else
154+
if !source.is_a?(Source::Path) && search.runtime_dependencies.sort != dependencies.sort
155+
raise IncorrectLockfileDependencies.new(self)
156+
end
157+
158+
search.locked_platform = platform if search.instance_of?(RemoteSpecification) || search.instance_of?(EndpointSpecification)
148159
end
149160
end
150161
search

lib/bundler/remote_specification.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class RemoteSpecification
1212

1313
attr_reader :name, :version, :platform
1414
attr_writer :dependencies
15-
attr_accessor :source, :remote
15+
attr_accessor :source, :remote, :locked_platform
1616

1717
def initialize(name, version, platform, spec_fetcher)
1818
@name = name
@@ -21,6 +21,11 @@ def initialize(name, version, platform, spec_fetcher)
2121
@platform = Gem::Platform.new(platform)
2222
@spec_fetcher = spec_fetcher
2323
@dependencies = nil
24+
@locked_platform = nil
25+
end
26+
27+
def insecurely_materialized?
28+
@locked_platform.to_s != @platform.to_s
2429
end
2530

2631
# Needed before installs, since the arch matters then and quick

lib/bundler/rubygems_ext.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ def required_ruby_version=(req)
150150
end
151151
end
152152

153+
def insecurely_materialized?
154+
false
155+
end
156+
153157
def groups
154158
@groups ||= []
155159
end

lib/bundler/spec_set.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def materialized_for_all_platforms
143143
@specs.map do |s|
144144
next s unless s.is_a?(LazySpecification)
145145
s.source.remote!
146-
spec = s.materialize_for_installation
146+
spec = s.materialize_strictly
147147
raise GemNotFound, "Could not find #{s.full_name} in any of the sources" unless spec
148148
spec
149149
end
@@ -162,6 +162,10 @@ def missing_specs
162162
@specs.select {|s| s.is_a?(LazySpecification) }
163163
end
164164

165+
def insecurely_materialized_specs
166+
@specs.select(&:insecurely_materialized?)
167+
end
168+
165169
def -(other)
166170
SpecSet.new(to_a - other.to_a)
167171
end

lib/bundler/stub_specification.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ def self.from_stub(stub)
99
spec
1010
end
1111

12+
def insecurely_materialized?
13+
false
14+
end
15+
1216
attr_reader :checksum
1317
attr_accessor :stub, :ignored
1418

spec/bundler/install/gemfile/specific_platform_spec.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
L
5959

6060
bundle "install --verbose"
61-
61+
expect(err).to include("The following platform specific gems are getting installed, yet the lockfile includes only their generic ruby version")
6262
expect(out).to include("Installing sass-embedded 1.72.0 (x86_64-darwin-15)")
6363

6464
expect(the_bundle).to include_gem("sass-embedded 1.72.0 x86_64-darwin-15")
@@ -130,6 +130,7 @@
130130
L
131131

132132
bundle "update"
133+
expect(err).to include("The following platform specific gems are getting installed, yet the lockfile includes only their generic ruby version")
133134

134135
checksums.checksum gem_repo2, "google-protobuf", "3.0.0.alpha.5.0.5.1"
135136

@@ -238,6 +239,7 @@
238239
L
239240

240241
bundle "install --verbose"
242+
expect(err).to include("The following platform specific gems are getting installed, yet the lockfile includes only their generic ruby version")
241243
expect(out).to include("Installing libv8 8.4.255.0 (universal-darwin)")
242244

243245
bundle "add mini_racer --verbose"
@@ -275,6 +277,7 @@
275277
L
276278

277279
bundle "install --verbose", artifice: "compact_index_precompiled_before"
280+
expect(err).to include("The following platform specific gems are getting installed, yet the lockfile includes only their generic ruby version")
278281
expect(out).to include("Installing grpc 1.50.0 (universal-darwin)")
279282
end
280283
end

0 commit comments

Comments
 (0)