rails-cloudflare-origin 0.1.3 → 0.2.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: 8519b6aa4b8ee1fdd5c0dcbb3a3d5b201736b82da7e1efefce42b8acdc1add3c
4
- data.tar.gz: 346c75c26d1086a1720e4b9a421196781a984c031d19f75ac23d4236cf377ab1
3
+ metadata.gz: 72b9b84b3e47b2fb08dd10e6a08dc4b53c0968cac0e061e71284d4d1d7d1803c
4
+ data.tar.gz: e28b96297476ff9c47b66339b03e5ee9d9f4eb2a9d3b022de8d396d184a46e91
5
5
  SHA512:
6
- metadata.gz: 103666a5b8d8f5b59c8b51004712aba5688af3d40396321993745c68aed2a2887b83c1ecc441f42de4572681b133f4b0b7c28c69e60bad24ad5ce76554ab6250
7
- data.tar.gz: 637ab7816db20a640f853aa3232005445884644a1e710f8624bc4e3cf3b18a58b1e92bc28ab75397697a8e1c332e52803def2216391567c493c5f3e77193e9bc
6
+ metadata.gz: 4f9cddd7b35f2f64a58c3a1736e98075fe7246a131ea6b4c8745dc4a40817c81d8c34f67fa7725d37e629974e1c87ea16f0908989aeaa9358bbf105a87b482a0
7
+ data.tar.gz: df7b8c6f28d3af0f772d0c54589c22b1803e4e39c368ea94086651aa2075946baec6c2d56bd63f57047eddf9090cd06625d643e6c8fd30ab5382da3f772f616a
data/README.md CHANGED
@@ -14,7 +14,7 @@ A Railtie installs the middleware at the top of the stack, above anything the ap
14
14
 
15
15
  ## What it does on each request
16
16
 
17
- 1. Removes `Forwarded`, `Client-IP`, `X-Forwarded-For`, `X-Real-IP` and `X-Forwarded-Host`. A visitor can send any of these, and conflicting values make Rails raise `ActionDispatch::RemoteIp::IpSpoofAttackError`, which surfaces as a 500. `X-Forwarded-Proto` and `X-Forwarded-Port` stay.
17
+ 1. Removes `Forwarded`, `Client-IP`, `X-Forwarded-For`, `X-Real-IP`, `X-Forwarded-Host`, `X-Forwarded-Ssl` and `X-Forwarded-Scheme`. A visitor can send any of these: conflicting client addresses make Rails raise `ActionDispatch::RemoteIp::IpSpoofAttackError`, which surfaces as a 500, and Rack reads `X-Forwarded-Ssl` and `X-Forwarded-Scheme` before `X-Forwarded-Proto`, so a visitor could make a plain HTTP request look like HTTPS. `X-Forwarded-Proto` and `X-Forwarded-Port` stay.
18
18
  2. Takes the `X-Origin-Secret` header out of the request, so it never reaches the app, its logs, or an error tracker.
19
19
  3. Lets a `GET` or `HEAD` of exactly `/up` through without the secret when it carries no `CF-Ray` header. This is the platform's health check, which reaches the app directly instead of through Cloudflare. A `/up` that came through Cloudflare still needs the secret.
20
20
  4. Answers anything else without a matching secret with a `403` and `cache-control: no-store`.
@@ -37,20 +37,20 @@ The gem reads two secrets, each from an environment variable first and from the
37
37
 
38
38
  | Secret | Environment variable | Credential |
39
39
  | --- | --- | --- |
40
- | Current, required to switch the gem on | `CLOUDFLARE_ORIGIN_SECRET` | `cloudflare_origin_secret` |
40
+ | Current, required outside `development` and `test` | `CLOUDFLARE_ORIGIN_SECRET` | `cloudflare_origin_secret` |
41
41
  | Previous, being rotated out, optional | `CLOUDFLARE_ORIGIN_SECRET_PREVIOUS` | `cloudflare_origin_secret_previous` |
42
42
 
43
43
  Each secret is resolved on its own: a non-blank environment variable wins over the credential. A variable left set on the host therefore overrides the credential without any warning, so a stale one keeps an old value in force after you rotate the credential.
44
44
 
45
45
  Both secrets are accepted, so the value rotates without an outage: set the new value as the current secret and the old one as the previous secret, deploy, switch the Transform Rule to the new value, then remove the previous one.
46
46
 
47
- A secret that is present but shorter than 32 characters fails the boot, so a broken value stops a deploy instead of answering every request with a 403. A blank secret counts as absent.
47
+ A secret that is present but shorter than 32 characters, or that starts or ends with whitespace (such as the trailing newline of a value read from a file), fails the boot, so a broken value stops a deploy instead of answering every request with a 403. A blank secret counts as absent.
48
48
 
49
49
  ## When it is on
50
50
 
51
- The middleware is installed only when a current secret is present, and never in the `development` and `test` environments, even when a secret is set, so local requests and specs see the headers they send.
51
+ The middleware is installed in every environment except `development` and `test`, where it is never installed, even when a secret is set, so local requests and specs see the headers they send.
52
52
 
53
- When no current secret is present, the gem installs nothing. Outside `development` and `test` it logs a warning at boot saying the middleware is not installed. An asset build that boots without credentials has no secret, so it is simply off.
53
+ Outside `development` and `test`, a boot without a current secret fails, because the app would otherwise answer requests that skipped Cloudflare. So does a boot with a previous secret but no current one, which is a rotation that removed the wrong value. The one exception is a boot with `SECRET_KEY_BASE_DUMMY` set and neither secret configured, which Rails uses for steps that run without credentials, such as an asset build: there the gem installs nothing and logs a warning saying so. A previous secret without a current one still fails the boot. Set `SECRET_KEY_BASE_DUMMY` on any other credential-less boot that serves no requests.
54
54
 
55
55
  ## Requirements
56
56
 
@@ -28,6 +28,8 @@ module CloudflareOrigin
28
28
  # With these gone and REMOTE_ADDR set to the visitor, Rack's request.ip and Rails' remote_ip
29
29
  # both return REMOTE_ADDR, and Rails' spoofing check has nothing to compare, so a forged header
30
30
  # can neither choose the client IP nor raise IpSpoofAttackError. X-Real-IP goes too, because some error trackers take the client IP from it.
31
+ # X-Forwarded-Ssl and X-Forwarded-Scheme go because Rack reads them before X-Forwarded-Proto, so
32
+ # a visitor could otherwise make a plain HTTP request look like HTTPS.
31
33
  # X-Forwarded-Proto and X-Forwarded-Port stay, since Cloudflare and the proxy server in front of
32
34
  # the app set them.
33
35
  FORWARDING_HEADERS = %w[
@@ -35,6 +37,8 @@ module CloudflareOrigin
35
37
  HTTP_FORWARDED
36
38
  HTTP_X_FORWARDED_FOR
37
39
  HTTP_X_FORWARDED_HOST
40
+ HTTP_X_FORWARDED_SCHEME
41
+ HTTP_X_FORWARDED_SSL
38
42
  HTTP_X_REAL_IP
39
43
  ].freeze
40
44
 
@@ -54,6 +58,11 @@ module CloudflareOrigin
54
58
  raise ArgumentError, "the current Cloudflare origin secret is blank" if current.blank?
55
59
 
56
60
  secrets = [current, previous].compact_blank
61
+ # A header value reaches the app with its edge whitespace trimmed, so a padded secret (one read
62
+ # from a file with its trailing newline, say) could never match and would 403 every page.
63
+ if secrets.any? { it != it.strip }
64
+ raise ArgumentError, "a Cloudflare origin secret has leading or trailing whitespace"
65
+ end
57
66
  if secrets.any? { it.length < MINIMUM_LENGTH }
58
67
  raise ArgumentError, "a Cloudflare origin secret is shorter than #{MINIMUM_LENGTH} characters"
59
68
  end
@@ -6,14 +6,25 @@ module CloudflareOrigin
6
6
  next if Rails.env.local?
7
7
 
8
8
  current = Railtie.secret app, :cloudflare_origin_secret
9
+ previous = Railtie.secret app, :cloudflare_origin_secret_previous
9
10
  if current.blank?
10
- Rails.logger.warn "rails-cloudflare-origin: middleware not installed because no origin secret is configured"
11
+ # A rotation slip, never a build: the current value was dropped instead of the previous one.
12
+ if previous.present?
13
+ raise ArgumentError, "rails-cloudflare-origin: a previous origin secret is configured without a current one"
14
+ end
15
+ # An app booted without its secret would answer requests that skipped Cloudflare, so only a
16
+ # boot that has no credentials on purpose, such as an asset build, may go on without one.
17
+ unless ENV["SECRET_KEY_BASE_DUMMY"].present?
18
+ raise ArgumentError, "rails-cloudflare-origin: no origin secret is configured; " \
19
+ "set CLOUDFLARE_ORIGIN_SECRET or the cloudflare_origin_secret credential, or, for a step that " \
20
+ "serves no requests such as an asset build, set SECRET_KEY_BASE_DUMMY"
21
+ end
22
+
23
+ Rails.logger.warn "rails-cloudflare-origin: middleware not installed because SECRET_KEY_BASE_DUMMY is set"
11
24
  next
12
25
  end
13
26
 
14
- app.config.middleware.insert_before 0, Middleware,
15
- current:,
16
- previous: Railtie.secret(app, :cloudflare_origin_secret_previous)
27
+ app.config.middleware.insert_before 0, Middleware, current:, previous:
17
28
  end
18
29
 
19
30
  # The environment variable wins when set, so a host can supply the secret without editing
@@ -1,4 +1,4 @@
1
1
  module CloudflareOrigin
2
2
  # Release builds stamp the tag's version over this placeholder.
3
- VERSION = "0.1.3"
3
+ VERSION = "0.2.0"
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-cloudflare-origin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel López Prat