puma 8.1.3 → 8.2.0.pre1

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.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +120 -0
  3. data/README.md +4 -3
  4. data/docs/GLOSSARY.md +160 -0
  5. data/docs/architecture.md +41 -42
  6. data/docs/binding.md +3 -0
  7. data/docs/cluster_mode.md +21 -21
  8. data/docs/control_server.md +4 -4
  9. data/docs/deployment/nginx.md +2 -0
  10. data/docs/deployment/systemd.md +15 -0
  11. data/docs/puma_support.md +12 -0
  12. data/docs/restart.md +2 -2
  13. data/docs/signals.md +3 -3
  14. data/docs/ssl.md +65 -0
  15. data/docs/upgrading/5.0-Upgrade.md +1 -1
  16. data/docs/upgrading/6.0-Upgrade.md +1 -1
  17. data/docs/upgrading/7.0-Upgrade.md +1 -1
  18. data/docs/upgrading/8.0-Upgrade.md +1 -1
  19. data/ext/puma_http11/extconf.rb +7 -5
  20. data/ext/puma_http11/http11_parser.c +66 -84
  21. data/ext/puma_http11/http11_parser.h +0 -5
  22. data/ext/puma_http11/mini_ssl.c +31 -68
  23. data/ext/puma_http11/puma_http11.c +125 -7
  24. data/lib/puma/app/status.rb +22 -7
  25. data/lib/puma/binder.rb +0 -1
  26. data/lib/puma/cli.rb +1 -3
  27. data/lib/puma/client.rb +133 -57
  28. data/lib/puma/client_env.rb +9 -11
  29. data/lib/puma/cluster/worker.rb +0 -6
  30. data/lib/puma/cluster.rb +3 -14
  31. data/lib/puma/cluster_accept_loop_delay.rb +18 -7
  32. data/lib/puma/commonlogger.rb +3 -4
  33. data/lib/puma/configuration.rb +4 -1
  34. data/lib/puma/const.rb +5 -5
  35. data/lib/puma/control_cli.rb +5 -7
  36. data/lib/puma/detect.rb +1 -1
  37. data/lib/puma/dsl.rb +70 -14
  38. data/lib/puma/http_parser_error.rb +8 -0
  39. data/lib/puma/launcher/bundle_pruner.rb +3 -21
  40. data/lib/puma/launcher.rb +55 -25
  41. data/lib/puma/minissl/context_builder.rb +3 -2
  42. data/lib/puma/minissl.rb +12 -16
  43. data/lib/puma/plugin/systemd.rb +69 -3
  44. data/lib/puma/rack/urlmap.rb +2 -2
  45. data/lib/puma/reactor.rb +138 -25
  46. data/lib/puma/response.rb +65 -38
  47. data/lib/puma/runner.rb +4 -1
  48. data/lib/puma/sd_notify.rb +21 -0
  49. data/lib/puma/server.rb +14 -14
  50. data/lib/puma/server_plugin_control.rb +2 -1
  51. data/lib/puma/single.rb +0 -1
  52. data/lib/puma/thread_pool.rb +87 -14
  53. data/lib/puma/util.rb +5 -16
  54. data/lib/puma.rb +22 -5
  55. data/lib/rack/handler/puma.rb +1 -5
  56. metadata +5 -2
@@ -1,14 +1,11 @@
1
- #define RSTRING_NOT_MODIFIED 1
2
-
3
1
  #include <ruby.h>
4
- #include <ruby/version.h>
5
2
  #include <ruby/io.h>
6
3
 
7
- #ifdef HAVE_OPENSSL_BIO_H
8
-
9
4
  #include <openssl/bio.h>
10
5
  #include <openssl/ssl.h>
6
+ #ifndef HAVE_SSL_CTX_SET_DH_AUTO
11
7
  #include <openssl/dh.h>
8
+ #endif
12
9
  #include <openssl/err.h>
13
10
  #include <openssl/x509.h>
14
11
 
@@ -44,15 +41,20 @@ void raise_param_error(const char* caller, const char *param) {
44
41
 
45
42
  void engine_free(void *ptr) {
46
43
  ms_conn *conn = ptr;
47
- ms_cert_buf* cert_buf = (ms_cert_buf*)SSL_get_app_data(conn->ssl);
48
- if(cert_buf) {
49
- OPENSSL_free(cert_buf->buf);
50
- free(cert_buf);
44
+ if(conn->ssl) {
45
+ ms_cert_buf* cert_buf = (ms_cert_buf*)SSL_get_app_data(conn->ssl);
46
+ if(cert_buf) {
47
+ OPENSSL_free(cert_buf->buf);
48
+ free(cert_buf);
49
+ }
50
+ SSL_free(conn->ssl);
51
+ } else {
52
+ BIO_free(conn->read);
53
+ BIO_free(conn->write);
51
54
  }
52
- SSL_free(conn->ssl);
53
55
  SSL_CTX_free(conn->ctx);
54
56
 
55
- free(conn);
57
+ ruby_xfree(conn);
56
58
  }
57
59
 
58
60
  const rb_data_type_t engine_data_type = {
@@ -104,17 +106,17 @@ DH *get_dh2048(void) {
104
106
  static unsigned char dh2048_g[] = { 0x02 };
105
107
 
106
108
  DH *dh;
107
- #if !(OPENSSL_VERSION_NUMBER < 0x10100005L)
109
+ #if OPENSSL_VERSION_NUMBER >= 0x10100005L
108
110
  BIGNUM *p, *g;
109
111
  #endif
110
112
 
111
113
  dh = DH_new();
114
+ if (dh == NULL) return NULL;
112
115
 
113
116
  #if OPENSSL_VERSION_NUMBER < 0x10100005L
114
117
  dh->p = BN_bin2bn(dh2048_p, sizeof(dh2048_p), NULL);
115
118
  dh->g = BN_bin2bn(dh2048_g, sizeof(dh2048_g), NULL);
116
-
117
- if ((dh->p == NULL) || (dh->g == NULL)) {
119
+ if (dh->p == NULL || dh->g == NULL) {
118
120
  DH_free(dh);
119
121
  return NULL;
120
122
  }
@@ -213,7 +215,6 @@ sslctx_alloc(VALUE klass) {
213
215
 
214
216
  #ifdef HAVE_TLS_SERVER_METHOD
215
217
  ctx = SSL_CTX_new(TLS_method());
216
- // printf("\nctx using TLS_method security_level %d\n", SSL_CTX_get_security_level(ctx));
217
218
  #else
218
219
  ctx = SSL_CTX_new(SSLv23_method());
219
220
  #endif
@@ -229,7 +230,10 @@ VALUE
229
230
  sslctx_initialize(VALUE self, VALUE mini_ssl_ctx) {
230
231
  SSL_CTX* ctx;
231
232
  int ssl_options, private_key_result;
232
- VALUE key, cert, ca, verify_mode, ssl_cipher_filter, ssl_ciphersuites, no_tlsv1, no_tlsv1_1,
233
+ #ifdef HAVE_SSL_CTX_SET_CIPHERSUITES
234
+ VALUE ssl_ciphersuites;
235
+ #endif
236
+ VALUE key, cert, ca, verify_mode, ssl_cipher_filter, no_tlsv1, no_tlsv1_1,
233
237
  verification_flags, session_id_bytes, cert_pem, key_pem, key_password_command, key_password;
234
238
  BIO *bio;
235
239
  X509 *x509 = NULL;
@@ -269,7 +273,9 @@ sslctx_initialize(VALUE self, VALUE mini_ssl_ctx) {
269
273
 
270
274
  ssl_cipher_filter = rb_funcall(mini_ssl_ctx, rb_intern_const("ssl_cipher_filter"), 0);
271
275
 
276
+ #ifdef HAVE_SSL_CTX_SET_CIPHERSUITES
272
277
  ssl_ciphersuites = rb_funcall(mini_ssl_ctx, rb_intern_const("ssl_ciphersuites"), 0);
278
+ #endif
273
279
 
274
280
  no_tlsv1 = rb_funcall(mini_ssl_ctx, rb_intern_const("no_tlsv1"), 0);
275
281
 
@@ -377,8 +383,10 @@ sslctx_initialize(VALUE self, VALUE mini_ssl_ctx) {
377
383
  pkey = PEM_read_bio_PrivateKey(bio, NULL, password_cb, (void *) password);
378
384
 
379
385
  if (SSL_CTX_use_PrivateKey(ctx, pkey) != 1) {
386
+ /* don't echo the key material into the exception message/log */
387
+ EVP_PKEY_free(pkey);
380
388
  BIO_free(bio);
381
- raise_file_error("SSL_CTX_use_PrivateKey", RSTRING_PTR(key_pem));
389
+ raise_param_error("SSL_CTX_use_PrivateKey", "key_pem");
382
390
  }
383
391
  EVP_PKEY_free(pkey);
384
392
  BIO_free(bio);
@@ -413,15 +421,12 @@ sslctx_initialize(VALUE self, VALUE mini_ssl_ctx) {
413
421
  }
414
422
 
415
423
  SSL_CTX_set_min_proto_version(ctx, min);
416
-
417
424
  #else
418
- /* As of 1.0.2f, SSL_OP_SINGLE_DH_USE key use is always on */
419
425
  ssl_options |= SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_SINGLE_DH_USE;
420
-
421
426
  if (RTEST(no_tlsv1)) {
422
427
  ssl_options |= SSL_OP_NO_TLSv1;
423
428
  }
424
- if(RTEST(no_tlsv1_1)) {
429
+ if (RTEST(no_tlsv1_1)) {
425
430
  ssl_options |= SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1;
426
431
  }
427
432
  #endif
@@ -457,7 +462,7 @@ sslctx_initialize(VALUE self, VALUE mini_ssl_ctx) {
457
462
  SSL_CTX_set_cipher_list(ctx, "HIGH:!aNULL@STRENGTH");
458
463
  }
459
464
 
460
- #if HAVE_SSL_CTX_SET_CIPHERSUITES
465
+ #ifdef HAVE_SSL_CTX_SET_CIPHERSUITES
461
466
  // Only override OpenSSL default ciphersuites if config option is supplied.
462
467
  if (!NIL_P(ssl_ciphersuites)) {
463
468
  StringValue(ssl_ciphersuites);
@@ -466,7 +471,6 @@ sslctx_initialize(VALUE self, VALUE mini_ssl_ctx) {
466
471
  #endif
467
472
 
468
473
  #if OPENSSL_VERSION_NUMBER < 0x10002000L
469
- // Remove this case if OpenSSL 1.0.1 (now EOL) support is no longer needed.
470
474
  ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
471
475
  if (ecdh) {
472
476
  SSL_CTX_set_tmp_ecdh(ctx, ecdh);
@@ -476,9 +480,7 @@ sslctx_initialize(VALUE self, VALUE mini_ssl_ctx) {
476
480
  SSL_CTX_set_ecdh_auto(ctx, 1);
477
481
  #endif
478
482
 
479
- if (NIL_P(verify_mode)) {
480
- /* SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, NULL); */
481
- } else {
483
+ if (!NIL_P(verify_mode)) {
482
484
  SSL_CTX_set_verify(ctx, NUM2INT(verify_mode), engine_verify_callback);
483
485
  }
484
486
 
@@ -491,8 +493,6 @@ sslctx_initialize(VALUE self, VALUE mini_ssl_ctx) {
491
493
  (unsigned char *) RSTRING_PTR(session_id_bytes),
492
494
  SSL_MAX_SSL_SESSION_ID_LENGTH);
493
495
 
494
- // printf("\ninitialize end security_level %d\n", SSL_CTX_get_security_level(ctx));
495
-
496
496
  #ifdef HAVE_SSL_CTX_SET_DH_AUTO
497
497
  // https://www.openssl.org/docs/man3.0/man3/SSL_CTX_set_dh_auto.html
498
498
  SSL_CTX_set_dh_auto(ctx, 1);
@@ -769,10 +769,12 @@ void Init_mini_ssl(VALUE puma) {
769
769
  puma = rb_define_module("Puma");
770
770
  #endif
771
771
 
772
+ #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
772
773
  SSL_library_init();
773
774
  OpenSSL_add_ssl_algorithms();
774
775
  SSL_load_error_strings();
775
776
  ERR_load_crypto_strings();
777
+ #endif
776
778
 
777
779
  mod = rb_define_module_under(puma, "MiniSSL");
778
780
 
@@ -790,34 +792,13 @@ void Init_mini_ssl(VALUE puma) {
790
792
  /* Version of OpenSSL that Puma was compiled with */
791
793
  rb_define_const(mod, "OPENSSL_VERSION", rb_str_new2(OPENSSL_VERSION_TEXT));
792
794
 
793
- #if !defined(LIBRESSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000
794
795
  /* Version of OpenSSL that Puma loaded with */
796
+ #if !defined(LIBRESSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
795
797
  rb_define_const(mod, "OPENSSL_LIBRARY_VERSION", rb_str_new2(OpenSSL_version(OPENSSL_VERSION)));
796
798
  #else
797
799
  rb_define_const(mod, "OPENSSL_LIBRARY_VERSION", rb_str_new2(SSLeay_version(SSLEAY_VERSION)));
798
800
  #endif
799
801
 
800
- #if defined(OPENSSL_NO_SSL3) || defined(OPENSSL_NO_SSL3_METHOD)
801
- /* True if SSL3 is not available */
802
- rb_define_const(mod, "OPENSSL_NO_SSL3", Qtrue);
803
- #else
804
- rb_define_const(mod, "OPENSSL_NO_SSL3", Qfalse);
805
- #endif
806
-
807
- #if defined(OPENSSL_NO_TLS1) || defined(OPENSSL_NO_TLS1_METHOD)
808
- /* True if TLS1 is not available */
809
- rb_define_const(mod, "OPENSSL_NO_TLS1", Qtrue);
810
- #else
811
- rb_define_const(mod, "OPENSSL_NO_TLS1", Qfalse);
812
- #endif
813
-
814
- #if defined(OPENSSL_NO_TLS1_1) || defined(OPENSSL_NO_TLS1_1_METHOD)
815
- /* True if TLS1_1 is not available */
816
- rb_define_const(mod, "OPENSSL_NO_TLS1_1", Qtrue);
817
- #else
818
- rb_define_const(mod, "OPENSSL_NO_TLS1_1", Qfalse);
819
- #endif
820
-
821
802
  rb_define_singleton_method(mod, "check", noop, 0);
822
803
 
823
804
  eError = rb_define_class_under(mod, "SSLError", rb_eStandardError);
@@ -843,21 +824,3 @@ void Init_mini_ssl(VALUE puma) {
843
824
 
844
825
  rb_define_method(eng, "ssl_vers_st", engine_ssl_vers_st, 0);
845
826
  }
846
-
847
- #else
848
-
849
- NORETURN(VALUE raise_error(VALUE self));
850
-
851
- VALUE raise_error(VALUE self) {
852
- rb_raise(rb_eStandardError, "SSL not available in this build");
853
- }
854
-
855
- void Init_mini_ssl(VALUE puma) {
856
- VALUE mod;
857
-
858
- mod = rb_define_module_under(puma, "MiniSSL");
859
- rb_define_class_under(mod, "SSLError", rb_eStandardError);
860
-
861
- rb_define_singleton_method(mod, "check", raise_error, 0);
862
- }
863
- #endif
@@ -4,13 +4,10 @@
4
4
  * License 3-clause BSD
5
5
  */
6
6
 
7
- #define RSTRING_NOT_MODIFIED 1
8
-
9
7
  #include "ruby.h"
10
8
  #include "ruby/encoding.h"
11
9
  #include <assert.h>
12
10
  #include <string.h>
13
- #include <ctype.h>
14
11
  #include "http11_parser.h"
15
12
 
16
13
  #ifdef HAVE_PRCTL
@@ -30,6 +27,7 @@ static VALUE global_fragment;
30
27
  static VALUE global_query_string;
31
28
  static VALUE global_server_protocol;
32
29
  static VALUE global_request_path;
30
+ static VALUE global_http_host;
33
31
 
34
32
  /** Defines common length and error messages for input length validation. */
35
33
  #define QUOTE(s) #s
@@ -135,16 +133,120 @@ static void init_common_fields(void)
135
133
  } else {
136
134
  memcpy(tmp + HTTP_PREFIX_LEN, cf->name, cf->len + 1);
137
135
  cf->value = rb_enc_interned_str(tmp, HTTP_PREFIX_LEN + cf->len, rb_utf8_encoding());
136
+ if (cf->len == 4 && !memcmp(cf->name, "HOST", 4)) {
137
+ global_http_host = cf->value;
138
+ }
139
+ }
140
+ }
141
+ }
142
+
143
+ static char snake_upcase_table[256];
144
+ static unsigned char host_char_table[256];
145
+
146
+ static void init_snake_upcase_table(void)
147
+ {
148
+ unsigned i;
149
+ for (i = 0; i < 256; i++) {
150
+ if (i >= 'a' && i <= 'z')
151
+ snake_upcase_table[i] = (char)(i & ~0x20);
152
+ else if (i == '_')
153
+ snake_upcase_table[i] = ',';
154
+ else if (i == '-')
155
+ snake_upcase_table[i] = '_';
156
+ else
157
+ snake_upcase_table[i] = (char)i;
158
+
159
+ unsigned char flags = 0;
160
+ if ((i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z') ||
161
+ (i >= '0' && i <= '9') || i == '.' || i == '_' || i == '~' || i == '-') {
162
+ flags |= 1;
163
+ }
164
+ if ((i >= '0' && i <= '9') || (i >= 'a' && i <= 'f') || (i >= 'A' && i <= 'F')) {
165
+ flags |= 2;
166
+ }
167
+ if (i >= '0' && i <= '9') {
168
+ flags |= 4;
138
169
  }
170
+ host_char_table[i] = flags;
139
171
  }
140
172
  }
141
173
 
174
+ static void validate_ascii(const char *at, size_t length)
175
+ {
176
+ const unsigned char *p = (const unsigned char *)at;
177
+ unsigned char acc = 0;
178
+ size_t i;
179
+ for (i = 0; i < length; i++) {
180
+ acc |= p[i];
181
+ }
182
+ if (acc & 0x80) {
183
+ rb_raise(eHttpParserError, "Invalid HTTP format, non-ASCII byte in request URI");
184
+ }
185
+ }
186
+
187
+ static void validate_host(const char *s, size_t len)
188
+ {
189
+ size_t i = 0;
190
+ if (s[0] == '[') {
191
+ i = 1;
192
+ while (i < len && s[i] != ']') {
193
+ unsigned char c = (unsigned char)s[i];
194
+ if (!(host_char_table[c] & 2) && c != ':' && c != '.') {
195
+ rb_raise(eHttpParserError, "Invalid Host header");
196
+ }
197
+ i++;
198
+ }
199
+ if (i == 1 || i >= len || s[i] != ']') {
200
+ rb_raise(eHttpParserError, "Invalid Host header");
201
+ }
202
+ i++;
203
+ } else {
204
+ while (i < len && s[i] != ':') {
205
+ unsigned char c = (unsigned char)s[i];
206
+ if (host_char_table[c] & 1) {
207
+ i++;
208
+ } else if (c == '%' && i + 2 < len &&
209
+ (host_char_table[(unsigned char)s[i + 1]] & 2) &&
210
+ (host_char_table[(unsigned char)s[i + 2]] & 2)) {
211
+ i += 3;
212
+ } else {
213
+ rb_raise(eHttpParserError, "Invalid Host header");
214
+ }
215
+ }
216
+ if (i == 0) {
217
+ rb_raise(eHttpParserError, "Invalid Host header");
218
+ }
219
+ }
220
+ if (i < len) {
221
+ if (s[i] != ':') {
222
+ rb_raise(eHttpParserError, "Invalid Host header");
223
+ }
224
+ i++;
225
+ while (i < len) {
226
+ if (!(host_char_table[(unsigned char)s[i]] & 4)) {
227
+ rb_raise(eHttpParserError, "Invalid Host header");
228
+ }
229
+ i++;
230
+ }
231
+ }
232
+ }
233
+
234
+ static inline int field_name_eql(const char *expected, const char *raw, size_t len)
235
+ {
236
+ size_t i;
237
+ for (i = 0; i < len; i++) {
238
+ if (expected[i] != snake_upcase_table[(unsigned char)raw[i]])
239
+ return 0;
240
+ }
241
+ return 1;
242
+ }
243
+
142
244
  static VALUE find_common_field_value(const char *field, size_t flen)
143
245
  {
144
246
  unsigned i;
145
247
  struct common_field *cf = common_http_fields;
146
248
  for(i = 0; i < ARRAY_SIZE(common_http_fields); i++, cf++) {
147
- if (cf->len == flen && !memcmp(cf->name, field, flen))
249
+ if (cf->len == flen && field_name_eql(cf->name, field, flen))
148
250
  return cf->value;
149
251
  }
150
252
  return Qnil;
@@ -168,14 +270,18 @@ static void http_field(puma_parser* hp, const char *field, size_t flen,
168
270
  if (f == Qnil) {
169
271
  /*
170
272
  * We got a strange header that we don't have a memoized value for.
171
- * Fallback to creating a new string to use as a hash key.
273
+ * Normalize the field name into the parser's scratch buffer, after the
274
+ * HTTP_ prefix, leaving the caller's String untouched.
172
275
  */
173
-
276
+ size_t i;
174
277
  size_t new_size = HTTP_PREFIX_LEN + flen;
278
+ char *name = hp->buf + HTTP_PREFIX_LEN;
175
279
  assert(new_size < BUFFER_LEN);
176
280
 
177
281
  memcpy(hp->buf, HTTP_PREFIX, HTTP_PREFIX_LEN);
178
- memcpy(hp->buf + HTTP_PREFIX_LEN, field, flen);
282
+ for (i = 0; i < flen; i++) {
283
+ name[i] = snake_upcase_table[(unsigned char)field[i]];
284
+ }
179
285
 
180
286
  f = rb_enc_interned_str(hp->buf, new_size, rb_utf8_encoding());
181
287
  }
@@ -189,6 +295,15 @@ static void http_field(puma_parser* hp, const char *field, size_t flen,
189
295
  /* check for duplicate header */
190
296
  v = rb_hash_aref(hp->request, f);
191
297
 
298
+ if (f == global_http_host) {
299
+ if (v != Qnil) {
300
+ rb_raise(eHttpParserError, "Invalid Host header");
301
+ }
302
+ if (vlen > 0) {
303
+ validate_host(value, vlen);
304
+ }
305
+ }
306
+
192
307
  if (v == Qnil) {
193
308
  v = rb_str_new(value, vlen);
194
309
  rb_hash_aset(hp->request, f, v);
@@ -212,6 +327,7 @@ static void request_uri(puma_parser* hp, const char *at, size_t length)
212
327
  VALUE val = Qnil;
213
328
 
214
329
  VALIDATE_MAX_LENGTH(length, REQUEST_URI);
330
+ validate_ascii(at, length);
215
331
 
216
332
  val = rb_str_new(at, length);
217
333
  rb_hash_aset(hp->request, global_request_uri, val);
@@ -222,6 +338,7 @@ static void fragment(puma_parser* hp, const char *at, size_t length)
222
338
  VALUE val = Qnil;
223
339
 
224
340
  VALIDATE_MAX_LENGTH(length, FRAGMENT);
341
+ validate_ascii(at, length);
225
342
 
226
343
  val = rb_str_new(at, length);
227
344
  rb_hash_aset(hp->request, global_fragment, val);
@@ -516,6 +633,7 @@ RUBY_FUNC_EXPORTED void Init_puma_http11(void)
516
633
  rb_define_method(cHttpParser, "finished?", HttpParser_is_finished, 0);
517
634
  rb_define_method(cHttpParser, "nread", HttpParser_nread, 0);
518
635
  rb_define_method(cHttpParser, "body", HttpParser_body, 0);
636
+ init_snake_upcase_table();
519
637
  init_common_fields();
520
638
 
521
639
  #ifdef HAVE_OPENSSL_BIO_H
@@ -12,23 +12,31 @@ module Puma
12
12
  # @param launcher [::Puma::Launcher]
13
13
  # @param token [String, nil] the token used for authentication
14
14
  # @param data_only [Boolean] if true, restrict to read-only data commands
15
+ # @param data_token [String, Symbol, nil] optional token (or 'none' / :none) for read-only data commands
15
16
  #
16
- def initialize(launcher, token: nil, data_only: false)
17
+ def initialize(launcher, token: nil, data_only: false, data_token: nil)
17
18
  @launcher = launcher
18
19
  @auth_token = token
19
20
  @enabled_commands = READ_ONLY_COMMANDS if data_only
21
+ @data_token =
22
+ case data_token
23
+ when '' then nil
24
+ when 'none' then :none
25
+ else data_token
26
+ end
20
27
  end
21
28
 
22
29
  # most commands call methods in `::Puma::Launcher` based on command in
23
30
  # `env['PATH_INFO']`
24
31
  def call(env)
25
- unless authenticate(env)
26
- return rack_response(403, 'Invalid auth token', 'text/plain')
27
- end
28
-
29
32
  # resp_type is processed by following case statement, return
30
33
  # is a number (status) or a string used as the body of a 200 response
31
34
  command = env['PATH_INFO'][/\/([^\/]+)$/, 1]
35
+
36
+ unless authenticate(env, command)
37
+ return rack_response(403, 'Invalid auth token', 'text/plain')
38
+ end
39
+
32
40
  if @enabled_commands && !@enabled_commands.include?(command)
33
41
  return rack_response(404, "Command #{command.inspect} unavailable", 'text/plain')
34
42
  end
@@ -86,9 +94,16 @@ module Puma
86
94
 
87
95
  private
88
96
 
89
- def authenticate(env)
97
+ def authenticate(env, command)
90
98
  return true unless @auth_token
91
- env['QUERY_STRING'].to_s.split(/[&;]/).include? "token=#{@auth_token}"
99
+ return true if token_matches?(env, @auth_token)
100
+ return false unless @data_token && READ_ONLY_COMMANDS.include?(command)
101
+
102
+ @data_token == :none || token_matches?(env, @data_token)
103
+ end
104
+
105
+ def token_matches?(env, token)
106
+ env['QUERY_STRING'].to_s.split(/[&;]/).include?("token=#{token}")
92
107
  end
93
108
 
94
109
  def rack_response(status, body, content_type='application/json')
data/lib/puma/binder.rb CHANGED
@@ -234,7 +234,6 @@ module Puma
234
234
  # If key and certs are not defined and localhost gem is required.
235
235
  # localhost gem will be used for self signed
236
236
  # Load localhost authority if not loaded.
237
- # Ruby 3 `values_at` accepts an array, earlier do not
238
237
  if params.values_at(*cert_key).all? { |v| v.to_s.empty? }
239
238
  ctx = localhost_authority && localhost_authority_context(params)
240
239
  end
data/lib/puma/cli.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'optparse'
4
- require 'uri'
5
4
 
6
5
  require_relative '../puma'
7
6
  require_relative 'configuration'
@@ -25,7 +24,6 @@ module Puma
25
24
  # arguments.
26
25
  #
27
26
  def initialize(argv, log_writer = LogWriter.stdio, events = Events.new, env: ENV)
28
- @debug = false
29
27
  @argv = argv.dup
30
28
  @log_writer = log_writer
31
29
  @events = events
@@ -92,7 +90,7 @@ module Puma
92
90
 
93
91
  def setup_options(env = ENV)
94
92
  @conf = Configuration.new({}, { events: @events }, env) do |user_config, file_config|
95
- @parser = OptionParser.new do |o|
93
+ OptionParser.new do |o|
96
94
  o.on "-b", "--bind URI", "URI to bind to (tcp://, unix://, ssl://)" do |arg|
97
95
  user_config.bind arg
98
96
  end