rails-cloudflare-origin 0.1.2 → 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 +4 -4
- data/README.md +6 -6
- data/lib/cloudflare_origin/middleware.rb +9 -0
- data/lib/cloudflare_origin/railtie.rb +15 -4
- data/lib/cloudflare_origin/version.rb +2 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 72b9b84b3e47b2fb08dd10e6a08dc4b53c0968cac0e061e71284d4d1d7d1803c
|
|
4
|
+
data.tar.gz: e28b96297476ff9c47b66339b03e5ee9d9f4eb2a9d3b022de8d396d184a46e91
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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-
|
|
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,24 +37,24 @@ 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
|
|
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
|
|
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
|
-
|
|
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
|
|
|
57
|
-
Ruby 4.0 or newer and Rails 8.1 or newer. The gem targets the latest Ruby and Rails only, and these floors move up with each release.
|
|
57
|
+
Ruby 4.0.10 or newer and Rails 8.1 or newer. The gem targets the latest Ruby and Rails only, and these floors move up with each release.
|
|
58
58
|
|
|
59
59
|
## Development
|
|
60
60
|
|
|
@@ -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
|
-
|
|
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
|
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.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Daniel López Prat
|
|
@@ -68,7 +68,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
68
68
|
requirements:
|
|
69
69
|
- - ">="
|
|
70
70
|
- !ruby/object:Gem::Version
|
|
71
|
-
version:
|
|
71
|
+
version: 4.0.10
|
|
72
72
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
73
73
|
requirements:
|
|
74
74
|
- - ">="
|