Rails Rails Mime::Type::InvalidMimeType 的异常在本地是返回 406 错误,但是线上是 500 错误

Sylor-huang · 2020年12月12日 · 最后由 xinyuewaisong 回复于 2021年01月02日 · 648 次阅读

各位大佬好,我现在碰到了 Mime::Type::InvalidMimeType 的错误,尝试查看了一些资料,但是还没有找到解决办法。

Rails 下的评论,说是 406 为正常的: https://github.com/rails/rails/issues/37620#issuecomment-549192459

1. 在本地运行的时候,是返回正常的 406:

curl --verbose -H "Content-Type: %{#context['com.opensymphony.xwork2.dispatcher.HttpServletResponse'].addHeader('X-Hacker','Bounty Plz')}.multipart/form-data" http://127.0.0.1:3000

# 提示信息

*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 3000 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1:3000
> User-Agent: curl/7.64.1
> Accept: */*
> Content-Type: %{#context['com.opensymphony.xwork2.dispatcher.HttpServletResponse'].addHeader('X-Hacker','Bounty Plz')}.multipart/form-data
>
< HTTP/1.1 406 Not Acceptable
< Content-Type: text/html; charset=UTF-8
< X-Request-Id: 00ed739d-d934-40d7-8d70-460c4603adc5
< X-Runtime: 9.716673
< Content-Length: 0
<
* Connection #0 to host 127.0.0.1 left intact
* Closing connection 0

2. 但是线上环境运行的时候,返回的却是 500 错误

curl --verbose -H "Content-Type: %{#context['com.opensymphony.xwork2.dispatcher.HttpServletResponse'].addHeader('X-Hacker','Bounty Plz')}.multipart/form-data" https://xxxx.com/

#提示信息

* TLSv1.2 (OUT), TLS handshake, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* ALPN, server accepted to use h2
* Server certificate:
*  subject: CN=*.xxx.info
*  start date: Aug  9 00:00:00 2020 GMT
*  expire date: Sep  8 12:00:00 2021 GMT
*  subjectAltName: host "xxxx.info" matched cert's "*.xxxx.info"
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Using Stream ID: 1 (easy handle 0x7f8f9300aa00)
> GET / HTTP/2
> Host: xxxx.info
> User-Agent: curl/7.64.1
> Accept: */*
> Content-Type: %{#context['com.opensymphony.xwork2.dispatcher.HttpServletResponse'].addHeader('X-Hacker','Bounty Plz')}.multipart/form-data
>
* Connection state changed (MAX_CONCURRENT_STREAMS == 128)!
< HTTP/2 500
< date: Sat, 12 Dec 2020 05:46:41 GMT
< content-length: 77
<
An unhandled lowlevel error occurred. The application logs may have details.
* Connection #0 to host xxxx.info left intact
* Closing connection 0
# production 的log
[d939ca07-6d06-49f0-a26e-858d387244d4] Mime::Type::InvalidMimeType ("%{#context['com.opensymphony.xwork2.dispatcher.httpservletresponse'].addheader('x-hacker'" is not a valid MIME type):
[d939ca07-6d06-49f0-a26e-858d387244d4]
[d939ca07-6d06-49f0-a26e-858d387244d4] actionpack (6.0.2.1) lib/action_dispatch/http/mime_type.rb:235:in `initialize'
[d939ca07-6d06-49f0-a26e-858d387244d4] actionpack (6.0.2.1) lib/action_dispatch/http/mime_type.rb:143:in `new'
[d939ca07-6d06-49f0-a26e-858d387244d4] actionpack (6.0.2.1) lib/action_dispatch/http/mime_type.rb:143:in `lookup'
[d939ca07-6d06-49f0-a26e-858d387244d4] actionpack (6.0.2.1) lib/action_dispatch/http/mime_negotiation.rb:23:in `block in content_mime_type'
[d939ca07-6d06-49f0-a26e-858d387244d4] rack (2.2.2) lib/rack/request.rb:69:in `fetch'
[d939ca07-6d06-49f0-a26e-858d387244d4] rack (2.2.2) lib/rack/request.rb:69:in `fetch_header'
[d939ca07-6d06-49f0-a26e-858d387244d4] actionpack (6.0.2.1) lib/action_dispatch/http/mime_negotiation.rb:21:in `content_mime_type'
[d939ca07-6d06-49f0-a26e-858d387244d4] actionpack (6.0.2.1) lib/action_controller/metal/params_wrapper.rb:278:in `_wrapper_enabled?'
[d939ca07-6d06-49f0-a26e-858d387244d4] actionpack (6.0.2.1) lib/action_controller/metal/params_wrapper.rb:244:in `process_action'
[d939ca07-6d06-49f0-a26e-858d387244d4] activerecord (6.0.2.1) lib/active_record/railties/controller_runtime.rb:27:in `process_action'
[d939ca07-6d06-49f0-a26e-858d387244d4] actionpack (6.0.2.1) lib/abstract_controller/base.rb:136:in `process'
[d939ca07-6d06-49f0-a26e-858d387244d4] actionview (6.0.2.1) lib/action_view/rendering.rb:39:in `process'
[d939ca07-6d06-49f0-a26e-858d387244d4] actionpack (6.0.2.1) lib/action_controller/metal.rb:191:in `dispatch'
[d939ca07-6d06-49f0-a26e-858d387244d4] actionpack (6.0.2.1) lib/action_controller/metal.rb:252:in `dispatch'
[d939ca07-6d06-49f0-a26e-858d387244d4] actionpack (6.0.2.1) lib/action_dispatch/routing/route_set.rb:51:in `dispatch'
[d939ca07-6d06-49f0-a26e-858d387244d4] actionpack (6.0.2.1) lib/action_dispatch/routing/route_set.rb:33:in `serve'
[d939ca07-6d06-49f0-a26e-858d387244d4] actionpack (6.0.2.1) lib/action_dispatch/journey/router.rb:49:in `block in serve'
[d939ca07-6d06-49f0-a26e-858d387244d4] actionpack (6.0.2.1) lib/action_dispatch/journey/router.rb:32:in `each'
[d939ca07-6d06-49f0-a26e-858d387244d4] actionpack (6.0.2.1) lib/action_dispatch/journey/router.rb:32:in `serve'
[d939ca07-6d06-49f0-a26e-858d387244d4] actionpack (6.0.2.1) lib/action_dispatch/routing/route_set.rb:837:in `call'
[d939ca07-6d06-49f0-a26e-858d387244d4] apipie-rails (0.5.17) lib/apipie/static_dispatcher.rb:66:in `call'
[d939ca07-6d06-49f0-a26e-858d387244d4] apipie-rails (0.5.17) lib/apipie/extractor/recorder.rb:137:in `call'
[d939ca07-6d06-49f0-a26e-858d387244d4] warden (1.2.8) lib/warden/manager.rb:36:in `block in call'
[d939ca07-6d06-49f0-a26e-858d387244d4] warden (1.2.8) lib/warden/manager.rb:34:in `catch'
[d939ca07-6d06-49f0-a26e-858d387244d4] warden (1.2.8) lib/warden/manager.rb:34:in `call'
[d939ca07-6d06-49f0-a26e-858d387244d4] rack (2.2.2) lib/rack/tempfile_reaper.rb:15:in `call'
[d939ca07-6d06-49f0-a26e-858d387244d4] rack (2.2.2) lib/rack/etag.rb:27:in `call'
[d939ca07-6d06-49f0-a26e-858d387244d4] rack (2.2.2) lib/rack/conditional_get.rb:27:in `call'
[d939ca07-6d06-49f0-a26e-858d387244d4] rack (2.2.2) lib/rack/head.rb:12:in `call'
[d939ca07-6d06-49f0-a26e-858d387244d4] actionpack (6.0.2.1) lib/action_dispatch/http/content_security_policy.rb:18:in `call'
[d939ca07-6d06-49f0-a26e-858d387244d4] rack (2.2.2) lib/rack/session/abstract/id.rb:266:in `context'
[d939ca07-6d06-49f0-a26e-858d387244d4] rack (2.2.2) lib/rack/session/abstract/id.rb:260:in `call'
[d939ca07-6d06-49f0-a26e-858d387244d4] actionpack (6.0.2.1) lib/action_dispatch/middleware/cookies.rb:648:in `call'
[d939ca07-6d06-49f0-a26e-858d387244d4] actionpack (6.0.2.1) lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
[d939ca07-6d06-49f0-a26e-858d387244d4] activesupport (6.0.2.1) lib/active_support/callbacks.rb:101:in `run_callbacks'
[d939ca07-6d06-49f0-a26e-858d387244d4] actionpack (6.0.2.1) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
[d939ca07-6d06-49f0-a26e-858d387244d4] actionpack (6.0.2.1) lib/action_dispatch/middleware/actionable_exceptions.rb:17:in `call'
[d939ca07-6d06-49f0-a26e-858d387244d4] actionpack (6.0.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:32:in `call'
[d939ca07-6d06-49f0-a26e-858d387244d4] actionpack (6.0.2.1) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
[d939ca07-6d06-49f0-a26e-858d387244d4] railties (6.0.2.1) lib/rails/rack/logger.rb:38:in `call_app'
[d939ca07-6d06-49f0-a26e-858d387244d4] railties (6.0.2.1) lib/rails/rack/logger.rb:26:in `block in call'
[d939ca07-6d06-49f0-a26e-858d387244d4] activesupport (6.0.2.1) lib/active_support/tagged_logging.rb:80:in `block in tagged'
[d939ca07-6d06-49f0-a26e-858d387244d4] activesupport (6.0.2.1) lib/active_support/tagged_logging.rb:28:in `tagged'
[d939ca07-6d06-49f0-a26e-858d387244d4] activesupport (6.0.2.1) lib/active_support/tagged_logging.rb:80:in `tagged'
[d939ca07-6d06-49f0-a26e-858d387244d4] railties (6.0.2.1) lib/rails/rack/logger.rb:26:in `call'
[d939ca07-6d06-49f0-a26e-858d387244d4] feedmob-ahoy (4a0807c1c2c3) lib/ahoy/engine.rb:20:in `call_with_quiet_ahoy'
[d939ca07-6d06-49f0-a26e-858d387244d4] actionpack (6.0.2.1) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
[d939ca07-6d06-49f0-a26e-858d387244d4] request_store (1.5.0) lib/request_store/middleware.rb:19:in `call'
[d939ca07-6d06-49f0-a26e-858d387244d4] actionpack (6.0.2.1) lib/action_dispatch/middleware/request_id.rb:27:in `call'
[d939ca07-6d06-49f0-a26e-858d387244d4] rack (2.2.2) lib/rack/method_override.rb:24:in `call'
[d939ca07-6d06-49f0-a26e-858d387244d4] rack (2.2.2) lib/rack/runtime.rb:22:in `call'
[d939ca07-6d06-49f0-a26e-858d387244d4] activesupport (6.0.2.1) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
[d939ca07-6d06-49f0-a26e-858d387244d4] actionpack (6.0.2.1) lib/action_dispatch/middleware/executor.rb:14:in `call'
[d939ca07-6d06-49f0-a26e-858d387244d4] actionpack (6.0.2.1) lib/action_dispatch/middleware/static.rb:126:in `call'
[d939ca07-6d06-49f0-a26e-858d387244d4] rack (2.2.2) lib/rack/sendfile.rb:110:in `call'
[d939ca07-6d06-49f0-a26e-858d387244d4] actionpack (6.0.2.1) lib/action_dispatch/middleware/host_authorization.rb:77:in `call'
[d939ca07-6d06-49f0-a26e-858d387244d4] sentry-raven (2.13.0) lib/raven/integrations/rack.rb:51:in `call'
[d939ca07-6d06-49f0-a26e-858d387244d4] elastic-apm (3.5.0) lib/elastic_apm/middleware.rb:19:in `call'
[d939ca07-6d06-49f0-a26e-858d387244d4] railties (6.0.2.1) lib/rails/engine.rb:526:in `call'
[d939ca07-6d06-49f0-a26e-858d387244d4] puma (3.12.2) lib/puma/configuration.rb:227:in `call'
[d939ca07-6d06-49f0-a26e-858d387244d4] puma (3.12.2) lib/puma/server.rb:674:in `handle_request'
[d939ca07-6d06-49f0-a26e-858d387244d4] puma (3.12.2) lib/puma/server.rb:476:in `process_client'
[d939ca07-6d06-49f0-a26e-858d387244d4] puma (3.12.2) lib/puma/server.rb:334:in `block in run'
[d939ca07-6d06-49f0-a26e-858d387244d4] puma (3.12.2) lib/puma/thread_pool.rb:135:in `block in spawn_thread'
I, [2020-12-12T05:49:12.674045 #28849]  INFO -- : Sending event ce0d31e86e664d35baf24b7c772f8183 to Sentry

想请问下大家,线上环境报错 500 的可能原因是什么呢?谢谢大家~

406 是正常的,Rails 的安全保护

@huacnlee 嗯嗯,是的,但是我现在的问题是 production 是报 500 错误,不是 406,还没找到是哪里有问题,好像 rescue 还不能捕获这个错误,不知道华顺大哥您那有没有建议,可以指导我下,有可能是哪里会出现问题,导致 500 的,非常感谢~

@huacnlee 嗯嗯,好的,我稍后尝试下,非常感谢~

需要 登录 后方可回复, 如果你还没有账号请 注册新账号