geminabox 4.0.0 → 4.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 758c29c033252b87dc6fbe24ee015340f6f0c365ecc01aed02b7809e15ff3a6e
4
- data.tar.gz: 321199cded3cc8b60cf4ecf7f9b04c2a14ec5cc5246a0f072e3d4d12307106cd
3
+ metadata.gz: 6027465d974d436749b9a1741ac8992b4780a385cfb0e9fa9d5f2af93e25b78d
4
+ data.tar.gz: ca7c1aef11d03cc9d9d0c6b4b1d7b595e356b665f024f4a09a6f3e05575c99e0
5
5
  SHA512:
6
- metadata.gz: b726d61fe2807fb6c2d0d19544c9e52d8b3cef10be0dd8d772e72f866d286017609034e54ff15ac222cb5bca6fac9394e57b964ae8cb939c7a4e34bc73431f89
7
- data.tar.gz: 0d84ea5560b6cc1bd32de94dd0b6952256f820e98ca5fdfbda9656c47eea1fecac25c8f3177518fc6ca59ba2fc293f072f75c18154f69d1e50cb70131208c05a
6
+ metadata.gz: fc1b26de298e094e06e96a689cf7335fa4caa32fdc6739602f911bbdf182cc7a6722cda9cbf8f0305b42229feca9fac59b3927deee9e03dc006a4750f7249ab5
7
+ data.tar.gz: bff3b578986ded188c3ae43a37821b98e1eb0e6f31255be700beef5d90f838889b39be5ec8ea1f0a9ee82eeec9603726e12296a66dd3743aa6f7cc70318ab7a7
data/README.md CHANGED
@@ -40,6 +40,12 @@ Create a config.ru as follows:
40
40
 
41
41
  run Geminabox::Server
42
42
 
43
+ Uploads whose gemspec metadata exceeds 1 MiB uncompressed are refused with a
44
+ 400. RubyGems parses that YAML into many times its size in memory, so the cap
45
+ keeps one upload from exhausting a worker. Typical gems use a few KiB. To
46
+ change it, set `Geminabox.max_metadata_size` (in bytes) in config.ru; `nil`
47
+ leaves only RubyGems' own 10 MiB limit.
48
+
43
49
  Start your gem server with `rackup`, or hook up the config.ru as you normally would ([passenger](https://www.phusionpassenger.com/), [puma](https://puma.io/), [unicorn](https://yhbt.net/unicorn/), whatever floats your boat).
44
50
 
45
51
  ## Using Geminabox alongside rubygems.org
@@ -150,6 +156,32 @@ The Geminabox server makes no outbound HTTP requests. The `gem inabox`
150
156
  client uploads gems with the [HTTPClient](https://github.com/nahi/httpclient)
151
157
  gem, which honors the `http_proxy` / `HTTP_PROXY` environment variables.
152
158
 
159
+ ### TLS certificates
160
+
161
+ Since 4.1, `gem inabox` verifies the server's TLS certificate (earlier
162
+ versions accepted any certificate). It trusts what `gem install` trusts: the
163
+ system trust store plus the TLS settings in your gemrc. If your server uses a
164
+ self-signed certificate or a private CA that the system does not trust,
165
+ either add the CA to the system store, name it in `~/.gemrc`:
166
+
167
+ ```yaml
168
+ :ssl_ca_cert: /path/to/your-ca.pem # a file, or a directory of hashed certs
169
+ ```
170
+
171
+ or point a single push at it:
172
+
173
+ SSL_CERT_FILE=/path/to/your-ca.pem gem inabox pkg/my-gem-1.0.0.gem
174
+
175
+ `SSL_CERT_DIR` works too. A gemrc `:ssl_client_cert:` (one PEM holding the
176
+ certificate and its key) is presented to servers that ask for one, and a
177
+ non-zero `:ssl_verify_mode:` is applied as-is, both as RubyGems does.
178
+
179
+ To skip verification anyway, set `GEMINABOX_INSECURE_SKIP_TLS_VERIFY=1`, or
180
+ `:ssl_verify_mode: 0` in gemrc (which also turns verification off for
181
+ `gem install`). The client prints a warning on every push while either is
182
+ set: without verification, anyone on the network path can read your
183
+ credentials and the gem, and fake a successful upload.
184
+
153
185
  If you drive `GeminaboxClient` from Ruby (a Rake task, say), you can configure
154
186
  its HTTP layer through `Geminabox.http_adapter` before creating the client:
155
187
 
@@ -9,7 +9,10 @@ module Geminabox
9
9
 
10
10
  def serve
11
11
  headers["Cache-Control"] = 'no-transform'
12
- send_file(File.expand_path(File.join(Geminabox.data, *request.path_info)), :type => response['Content-Type'])
12
+ path = File.expand_path(File.join(Geminabox.data, *request.path_info))
13
+ # Rack::Protection normally strips ".." first; this holds without it.
14
+ halt 404 unless path.start_with?(File.expand_path(Geminabox.data) + File::SEPARATOR)
15
+ send_file(path, :type => response['Content-Type'])
13
16
  end
14
17
 
15
18
  %w[/specs.4.8.gz
@@ -27,7 +27,7 @@ module Geminabox
27
27
  @http_client ||= HTTPClient.new(ENV['http_proxy']).tap do |http_client|
28
28
  http_client.transparent_gzip_decompression = true
29
29
  http_client.keep_alive_timeout = 32 # sec
30
- http_client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
30
+ configure_tls(http_client.ssl_config)
31
31
  http_client.send_timeout = 0
32
32
  http_client.receive_timeout = 0
33
33
  end
@@ -36,5 +36,24 @@ module Geminabox
36
36
  def http_client=(client)
37
37
  @http_client = client
38
38
  end
39
+
40
+ private
41
+
42
+ def configure_tls(ssl_config)
43
+ # httpclient otherwise trusts only its own bundled CA file (from 2015,
44
+ # without Let's Encrypt's root) and ignores SSL_CERT_FILE.
45
+ ssl_config.set_default_paths
46
+ ssl_config.add_trust_ca(rubygems_ssl_ca_cert) if rubygems_ssl_ca_cert
47
+ cert, key = rubygems_ssl_client_cert
48
+ if cert
49
+ ssl_config.client_cert = cert
50
+ ssl_config.client_key = key
51
+ end
52
+ if !verify_tls?
53
+ ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
54
+ elsif rubygems_ssl_verify_mode
55
+ ssl_config.verify_mode = rubygems_ssl_verify_mode
56
+ end
57
+ end
39
58
  end
40
59
  end
@@ -19,11 +19,12 @@ module Geminabox
19
19
  adapter.post(*args)
20
20
  end
21
21
 
22
- # Note that this configuration turns SSL certificate verification off.
22
+ # Note that this configuration verifies SSL certificates unless
23
+ # GEMINABOX_INSECURE_SKIP_TLS_VERIFY is set.
23
24
  # To set up the adapter for your environment see:
24
25
  # https://github.com/lostisland/faraday/wiki/Setting-up-SSL-certificates
25
26
  def set_auth(uri, username = nil, password = nil)
26
- connection = Faraday.new url: uri, ssl: {verify: false} do |faraday|
27
+ connection = Faraday.new url: uri, ssl: ssl_options do |faraday|
27
28
  faraday.adapter http_engine
28
29
  faraday.proxy(ENV['http_proxy']) if ENV['http_proxy']
29
30
  end
@@ -51,6 +52,17 @@ module Geminabox
51
52
 
52
53
  private
53
54
 
55
+ def ssl_options
56
+ options = { verify: verify_tls? }
57
+ options[:verify_mode] = rubygems_ssl_verify_mode if options[:verify] && rubygems_ssl_verify_mode
58
+ if (ca_cert = rubygems_ssl_ca_cert)
59
+ options[File.directory?(ca_cert) ? :ca_path : :ca_file] = ca_cert
60
+ end
61
+ cert, key = rubygems_ssl_client_cert
62
+ options.merge!(client_cert: cert, client_key: key) if cert
63
+ options
64
+ end
65
+
54
66
  def set_username_and_password(connection, username, password)
55
67
  # Remove this condition when Faraday 1.x dropped (when Ruby 2.5 dropped)
56
68
  if Gem::Version.new(Faraday::VERSION) < Gem::Version.new("2.0")
@@ -4,6 +4,40 @@ require_relative 'http_adapter_config_error'
4
4
 
5
5
  module Geminabox
6
6
  class HttpAdapter
7
+ INSECURE_SKIP_TLS_VERIFY_ENV = 'GEMINABOX_INSECURE_SKIP_TLS_VERIFY'
8
+
9
+ # Certificates are verified unless the user deliberately opts out, and
10
+ # the opt-out announces itself every time a client is built.
11
+ def verify_tls?
12
+ if %w[1 true].include?(ENV[INSECURE_SKIP_TLS_VERIFY_ENV].to_s.downcase)
13
+ warn_unverified "#{INSECURE_SKIP_TLS_VERIFY_ENV} is set"
14
+ false
15
+ elsif rubygems_ssl_verify_mode == OpenSSL::SSL::VERIFY_NONE
16
+ warn_unverified "ssl_verify_mode: 0 is set in your gemrc"
17
+ false
18
+ else
19
+ true
20
+ end
21
+ end
22
+
23
+ # The gemrc TLS settings RubyGems itself uses for gem install and gem push,
24
+ # so a machine that can install from a server can also push to it.
25
+ def rubygems_ssl_verify_mode
26
+ Gem.configuration.ssl_verify_mode
27
+ end
28
+
29
+ def rubygems_ssl_ca_cert
30
+ Gem.configuration.ssl_ca_cert
31
+ end
32
+
33
+ # Like RubyGems, ssl_client_cert names one PEM holding the certificate and its key.
34
+ def rubygems_ssl_client_cert
35
+ path = Gem.configuration.ssl_client_cert
36
+ return unless path
37
+
38
+ pem = File.read(path)
39
+ [OpenSSL::X509::Certificate.new(pem), OpenSSL::PKey.read(pem)]
40
+ end
7
41
 
8
42
  def get_content(*args)
9
43
  raise HttpAdapterConfigError.new(:get_content, 'the response body')
@@ -21,6 +55,14 @@ module Geminabox
21
55
  raise HttpAdapterConfigError.new(:set_auth, 'true')
22
56
  end
23
57
 
58
+ private
59
+
60
+ def warn_unverified(reason)
61
+ warn "WARNING: #{reason}, so TLS certificates are not verified. Anyone on the " \
62
+ "network path can read your credentials and the gem you push, and fake a " \
63
+ "successful upload."
64
+ end
65
+
24
66
  end
25
67
  end
26
68
 
@@ -3,6 +3,10 @@
3
3
  module Geminabox
4
4
 
5
5
  class IncomingGem
6
+ CANONICAL_VERSION = /\A#{Gem::Version::VERSION_PATTERN}\z/
7
+ PLATFORM_CHARS = /\A[a-zA-Z0-9_.-]+\z/
8
+ private_constant :CANONICAL_VERSION, :PLATFORM_CHARS
9
+
6
10
  def initialize(gem_data, root_path = Geminabox.data)
7
11
  unless gem_data.respond_to? :read
8
12
  raise ArgumentError, "Expected an instance of IO"
@@ -26,9 +30,11 @@ module Geminabox
26
30
  File.open(@tempfile.path, "rb")
27
31
  end
28
32
 
33
+ # spec comes from the uploaded archive and RubyGems does not validate it on
34
+ # read, so the name must be checked before it becomes part of a path.
29
35
  def valid?
30
- spec && spec.name && spec.version
31
- rescue Gem::Package::Error
36
+ metadata_within_limit? && safe_filename? && indexable?
37
+ rescue Gem::Package::Error, Zlib::Error
32
38
  false
33
39
  end
34
40
 
@@ -53,6 +59,94 @@ module Geminabox
53
59
  def hexdigest
54
60
  @sha1
55
61
  end
62
+
63
+ private
64
+
65
+ def safe_filename?
66
+ spec && spec.name && spec.version &&
67
+ spec.name.match?(Gem::Specification::VALID_NAME_PATTERN) &&
68
+ File.basename(name) == name
69
+ end
70
+
71
+ def indexable?
72
+ loadable_version?(spec.version) &&
73
+ indexable_platform? &&
74
+ spec.dependencies.all? { |dep| valid_dependency?(dep) } &&
75
+ valid_requirement?(spec.required_ruby_version) &&
76
+ valid_requirement?(spec.required_rubygems_version)
77
+ end
78
+
79
+ # Gem::Package#spec parses the spec YAML into roughly 50x its size in
80
+ # memory and allows up to 10 MiB of it, once per upload and again on every
81
+ # full reindex, so the cap is checked before anything is parsed. RubyGems
82
+ # parses every metadata entry, so all of them are measured.
83
+ def metadata_within_limit?
84
+ limit = Geminabox.max_metadata_size
85
+ return true if limit.nil?
86
+ # The pre-2007 format keeps the YAML inline; the file size bounds it.
87
+ return File.size(@tempfile.path) <= limit if Gem::Package.new(@tempfile.path).is_a?(Gem::Package::Old)
88
+
89
+ File.open(@tempfile.path, "rb") do |io|
90
+ Gem::Package::TarReader.new(io).each do |entry|
91
+ case entry.full_name
92
+ when "metadata"
93
+ return false if entry.size > limit
94
+ when "metadata.gz"
95
+ return false unless inflates_within?(entry, limit)
96
+ end
97
+ end
98
+ end
99
+ true
100
+ end
101
+
102
+ def inflates_within?(entry, limit)
103
+ Zlib::GzipReader.wrap(entry) do |gz|
104
+ total = 0
105
+ while (chunk = gz.read(64 * 1024))
106
+ total += chunk.bytesize
107
+ return false if total > limit
108
+ end
109
+ end
110
+ true
111
+ end
112
+
113
+ # Gem::Version re-parses its string on Marshal.load, so one bad version in
114
+ # the specs index makes every later load of the index raise. The string is
115
+ # also written raw into filenames and compact-index lines, so whitespace
116
+ # that Gem::Version.correct? tolerates (and Gem::Version.new strips) is
117
+ # rejected too.
118
+ def loadable_version?(version)
119
+ version.is_a?(Gem::Version) && version.version.is_a?(String) &&
120
+ version.version.match?(CANONICAL_VERSION)
121
+ end
122
+
123
+ # The gem is stored under its normalised platform, but the indexer records
124
+ # the original string, so a legacy alias (i686-linux, mswin32, jruby)
125
+ # points clients at a file that does not exist.
126
+ def indexable_platform?
127
+ spec.original_platform.is_a?(String) &&
128
+ spec.original_platform.match?(PLATFORM_CHARS) &&
129
+ name == "#{spec.original_name}.gem"
130
+ end
131
+
132
+ # nil is what an older spec without the field reads back as.
133
+ def valid_requirement?(requirement)
134
+ return true if requirement.nil?
135
+
136
+ requirement.is_a?(Gem::Requirement) &&
137
+ requirement.requirements.all? do |op, version|
138
+ Gem::Requirement::OPS.key?(op) && loadable_version?(version)
139
+ end
140
+ end
141
+
142
+ # Dependency names and requirements are written verbatim into the
143
+ # line-oriented compact index and Marshalled into the specs index.
144
+ def valid_dependency?(dep)
145
+ dep_name = dep.name.is_a?(Array) ? dep.name.first : dep.name
146
+ dep_name.is_a?(String) &&
147
+ dep_name.match?(Gem::Specification::VALID_NAME_PATTERN) &&
148
+ valid_requirement?(dep.requirement)
149
+ end
56
150
  end
57
151
 
58
152
  end
@@ -174,8 +174,9 @@ module Geminabox
174
174
  error_response(403, 'Gem deletion is disabled - see https://github.com/geminabox/geminabox/issues/115')
175
175
  end
176
176
 
177
+ path = file_path
177
178
  serialize_update do
178
- File.delete file_path if File.exist? file_path
179
+ File.delete path if File.exist? path
179
180
  self.class.reindex(:force_rebuild)
180
181
  redirect url("/")
181
182
  end
@@ -309,7 +310,10 @@ HTML
309
310
  end
310
311
 
311
312
  def file_path
312
- File.expand_path(File.join(Geminabox.data, *request.path_info))
313
+ path = File.expand_path(File.join(Geminabox.data, *request.path_info))
314
+ # Rack::Protection normally strips ".." first; this holds without it.
315
+ halt 404 unless path.start_with?(File.expand_path(Geminabox.data) + File::SEPARATOR)
316
+ path
313
317
  end
314
318
 
315
319
  def dependency_cache
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Geminabox
4
- VERSION = '4.0.0' unless defined? VERSION
4
+ VERSION = '4.1.0' unless defined? VERSION
5
5
  end
data/lib/geminabox.rb CHANGED
@@ -48,7 +48,8 @@ module Geminabox
48
48
  :lockfile,
49
49
  :retry_interval,
50
50
  :allow_upload,
51
- :on_gem_received
51
+ :on_gem_received,
52
+ :max_metadata_size
52
53
  )
53
54
 
54
55
  def set_defaults(defaults)
@@ -79,7 +80,8 @@ module Geminabox
79
80
  lockfile: File.join(ENV.fetch('TMPDIR', Dir.tmpdir), 'geminabox.lockfile'),
80
81
  retry_interval: 60,
81
82
  allow_upload: true,
82
- on_gem_received: nil
83
+ on_gem_received: nil,
84
+ max_metadata_size: 1024 * 1024
83
85
  )
84
86
 
85
87
  # Removed in 4.0 with the RubyGems proxy. Warn-and-ignore (rather than
@@ -27,7 +27,7 @@ class GeminaboxClient
27
27
  end
28
28
 
29
29
  def push(gemfile, options = {})
30
- response = http_client.post(url_for(:upload), { 'file' => File.open(gemfile, "rb"), 'overwrite' => !!options[:overwrite] }, { 'Accept' => 'text/plain' })
30
+ response = post_gem(gemfile, options)
31
31
 
32
32
  if response.status < 300
33
33
  response.body
@@ -36,6 +36,36 @@ class GeminaboxClient
36
36
  end
37
37
  end
38
38
 
39
+ private
40
+
41
+ def post_gem(gemfile, options)
42
+ http_client.post(url_for(:upload), { 'file' => File.open(gemfile, "rb"), 'overwrite' => !!options[:overwrite] }, { 'Accept' => 'text/plain' })
43
+ rescue StandardError => e
44
+ raise unless certificate_failure?(e)
45
+
46
+ raise GeminaboxClient::Error, untrusted_certificate_message(e)
47
+ end
48
+
49
+ # Faraday wraps OpenSSL's error, so match on the message rather than the class.
50
+ def certificate_failure?(error)
51
+ error.message.include?("certificate verify failed")
52
+ end
53
+
54
+ def untrusted_certificate_message(error)
55
+ <<~MESSAGE
56
+ Could not verify the TLS certificate of #{url}
57
+ #{error.message}
58
+
59
+ If the server uses a self-signed certificate or a private CA, trust that CA by either:
60
+ - SSL_CERT_FILE=/path/to/ca.pem gem inabox ...
61
+ - adding ":ssl_ca_cert: /path/to/ca.pem" to ~/.gemrc (gem install reads it too)
62
+ - adding the CA to the system trust store
63
+
64
+ To push without verifying (anyone on the network path can then read your
65
+ credentials and the gem), set GEMINABOX_INSECURE_SKIP_TLS_VERIFY=1.
66
+ MESSAGE
67
+ end
68
+
39
69
  end
40
70
 
41
71
  class GeminaboxClient::Error < RuntimeError
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geminabox
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0
4
+ version: 4.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Lea
@@ -211,7 +211,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
211
211
  - !ruby/object:Gem::Version
212
212
  version: 3.2.3
213
213
  requirements: []
214
- rubygems_version: 4.0.3
214
+ rubygems_version: 4.0.20
215
215
  specification_version: 4
216
216
  summary: Really simple private RubyGems hosting
217
217
  test_files: []