geminabox 4.0.1 → 4.1.1

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: a7be0ebee3d899057907a90084837978a25915b3a82683e8ea74e0d1227d3125
4
- data.tar.gz: 12a531294faf2cb0745b44ac1c9191a759f0d82ba05acabfa33d05637e4d551f
3
+ metadata.gz: 9ba631efd6619a144c24a0ea39b1bc580fdcdd3ddb868988708353fa34f8ff13
4
+ data.tar.gz: 9aa5fbe6e570c477e84bf362df1c2d790b2f4809a537e696353e0aac67414c7e
5
5
  SHA512:
6
- metadata.gz: 149594466d61a7637ec9e308aba64b4b076588f9e01523fac0cded7465f7bfeb257a9ea6b051e17a408580775f64210201b87d1fc5a81bb1d9555b28caab978d
7
- data.tar.gz: c9ff3b58f3c76ca47330a9949ad6d01c98a88a5970d06eb06c0e0c417122315e3fadefc9b69e6e777c5b4adfc5a53b6675b014a53c2d9849cf455a547051fe6d
6
+ metadata.gz: 5e23c6674441cb09c7b9a869cb2cb9d617b080278191f2e7ce4cb49e64e81d310e01a294fe14c3162ce34668eadef49c9092ca31c2c161d6e094a1837511d0e9
7
+ data.tar.gz: a6e548b70fdfd5b4bc9b42ea43ab295d947bac989e694be8607e7c913e350110d66701e9aa6017b0f44b18b8a2b16f5a796bdbfe0c982329fa405769dfbfea8a
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
 
@@ -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 (before 2.10,
44
+ # a 2015 one 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"
@@ -29,10 +33,8 @@ module Geminabox
29
33
  # spec comes from the uploaded archive and RubyGems does not validate it on
30
34
  # read, so the name must be checked before it becomes part of a path.
31
35
  def valid?
32
- spec && spec.name && spec.version &&
33
- spec.name.match?(Gem::Specification::VALID_NAME_PATTERN) &&
34
- File.basename(name) == name
35
- rescue Gem::Package::Error
36
+ metadata_within_limit? && safe_filename? && indexable?
37
+ rescue Gem::Package::Error, Zlib::Error
36
38
  false
37
39
  end
38
40
 
@@ -57,6 +59,96 @@ module Geminabox
57
59
  def hexdigest
58
60
  @sha1
59
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
+ # checksums.yaml.gz is YAML-parsed the same way, before the spec, so it
84
+ # gets the same cap; a real one is a few hundred bytes.
85
+ def metadata_within_limit?
86
+ limit = Geminabox.max_metadata_size
87
+ return true if limit.nil?
88
+ # The pre-2007 format keeps the YAML inline; the file size bounds it.
89
+ return File.size(@tempfile.path) <= limit if Gem::Package.new(@tempfile.path).is_a?(Gem::Package::Old)
90
+
91
+ File.open(@tempfile.path, "rb") do |io|
92
+ Gem::Package::TarReader.new(io).each do |entry|
93
+ case entry.full_name
94
+ when "metadata"
95
+ return false if entry.size > limit
96
+ when "metadata.gz", "checksums.yaml.gz"
97
+ return false unless inflates_within?(entry, limit)
98
+ end
99
+ end
100
+ end
101
+ true
102
+ end
103
+
104
+ def inflates_within?(entry, limit)
105
+ Zlib::GzipReader.wrap(entry) do |gz|
106
+ total = 0
107
+ while (chunk = gz.read(64 * 1024))
108
+ total += chunk.bytesize
109
+ return false if total > limit
110
+ end
111
+ end
112
+ true
113
+ end
114
+
115
+ # Gem::Version re-parses its string on Marshal.load, so one bad version in
116
+ # the specs index makes every later load of the index raise. The string is
117
+ # also written raw into filenames and compact-index lines, so whitespace
118
+ # that Gem::Version.correct? tolerates (and Gem::Version.new strips) is
119
+ # rejected too.
120
+ def loadable_version?(version)
121
+ version.is_a?(Gem::Version) && version.version.is_a?(String) &&
122
+ version.version.match?(CANONICAL_VERSION)
123
+ end
124
+
125
+ # The gem is stored under its normalised platform, but the indexer records
126
+ # the original string, so a legacy alias (i686-linux, mswin32, jruby)
127
+ # points clients at a file that does not exist.
128
+ def indexable_platform?
129
+ spec.original_platform.is_a?(String) &&
130
+ spec.original_platform.match?(PLATFORM_CHARS) &&
131
+ name == "#{spec.original_name}.gem"
132
+ end
133
+
134
+ # nil is what an older spec without the field reads back as.
135
+ def valid_requirement?(requirement)
136
+ return true if requirement.nil?
137
+
138
+ requirement.is_a?(Gem::Requirement) &&
139
+ requirement.requirements.all? do |op, version|
140
+ Gem::Requirement::OPS.key?(op) && loadable_version?(version)
141
+ end
142
+ end
143
+
144
+ # Dependency names and requirements are written verbatim into the
145
+ # line-oriented compact index and Marshalled into the specs index.
146
+ def valid_dependency?(dep)
147
+ dep_name = dep.name.is_a?(Array) ? dep.name.first : dep.name
148
+ dep_name.is_a?(String) &&
149
+ dep_name.match?(Gem::Specification::VALID_NAME_PATTERN) &&
150
+ valid_requirement?(dep.requirement)
151
+ end
60
152
  end
61
153
 
62
154
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Geminabox
4
- VERSION = '4.0.1' unless defined? VERSION
4
+ VERSION = '4.1.1' 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.1
4
+ version: 4.1.1
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: []