класс ActionDispatch::SSL
Этот промежуточный программный модуль добавляется в стек, когда `config.force_ssl = true`, и получает опции, заданные в `config.ssl_options`. Он выполняет три задачи для принудительного использования защищённых HTTP-запросов:
1. TLS redirect: Permanently redirects http:// requests to https://
with the same URL host, path, etc. Enabled by default. Set `config.ssl_options`
to modify the destination URL
(e.g. `redirect: { host: "secure.widgets.com", port: 8080 }`), or set
`redirect: false` to disable this feature.
Cookies will not be flagged as secure for excluded requests.
2. Secure cookies: Sets the `secure` flag on cookies to tell browsers they
mustn't be sent along with http:// requests. Enabled by default. Set
`config.ssl_options` with `secure_cookies: false` to disable this feature.
3. HTTP Strict Transport Security (HSTS): Tells the browser to remember
this site as TLS-only and automatically redirect non-TLS requests.
Enabled by default. Configure `config.ssl_options` with `hsts: false` to disable. Установите `config.ssl_options` с `hsts: { … }` для настройки HSTS:
* `expires`: How long, in seconds, these settings will stick. The minimum required to qualify for browser preload lists is `18.weeks`. Defaults to `180.days` (recommended). * `subdomains`: Set to `true` to tell the browser to apply these settings to all subdomains. This protects your cookies from interception by a vulnerable site on a subdomain. Defaults to `true`. * `preload`: Advertise that this site may be included in browsers' preloaded HSTS lists. HSTS protects your site on every visit *except the first visit* since it hasn't seen your HSTS header yet. To close this gap, browser vendors include a baked-in list of HSTS-enabled sites. Go to https://hstspreload.appspot.com to submit your site for inclusion. Defaults to `false`.
Чтобы отключить HSTS, простого пропуска заголовка недостаточно. Браузеры будут помнить первоначальную директиву HSTS до её истечения. Вместо этого используйте заголовок, чтобы указать браузерам немедленно истечь HSTS. Установка `hsts: false` является сокращением для `hsts: { expires: 0 }`.
Запросы могут отказаться от перенаправления с помощью `exclude`:
config.ssl_options = { redirect: { exclude: -> request { request.path =~ /healthcheck/ } } }
Константы
- HSTS_EXPIRES_IN
-
По умолчанию установлено на 180 дней, что является минимальным значением для www.ssllabs.com/ssltest/ и больше, чем требование в 18 недель для списков предварительной загрузки браузера.
Публичные методы класса
# File actionpack/lib/action_dispatch/middleware/ssl.rb, line 49
def self.default_hsts_options
{ expires: HSTS_EXPIRES_IN, subdomains: true, preload: false }
end # File actionpack/lib/action_dispatch/middleware/ssl.rb, line 53
def initialize(app, redirect: {}, hsts: {}, secure_cookies: true)
@app = app
@redirect = redirect
@exclude = @redirect && @redirect[:exclude] || proc { !@redirect }
@secure_cookies = secure_cookies
@hsts_header = build_hsts_header(normalize_hsts_options(hsts))
end Публичные методы экземпляра
# File actionpack/lib/action_dispatch/middleware/ssl.rb, line 64
def call(env)
request = Request.new env
if request.ssl?
@app.call(env).tap do |status, headers, body|
set_hsts_header! headers
flag_cookies_as_secure! headers if @secure_cookies && !@exclude.call(request)
end
else
return redirect_to_https request unless @exclude.call(request)
@app.call(env)
end
end
© 2004–2018 David Heinemeier Hansson
Licensed under the MIT License.