openreceive-rails 0.4.35 → 0.5.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: 4d379815d45b9bcea47477d77267b201f05506b2bbe822d5cf7ca128d4b00ff7
4
- data.tar.gz: f9be4e02ea2ca4d022022e306e6912510d033de5cac53d4d881afc8e2924efdb
3
+ metadata.gz: 04b374d4260a8185bd012b7cfa4591f0132fee90de24e80d230b43afec9642a2
4
+ data.tar.gz: 97b049bd0b41d7efc4b1eddd862adc48328b94a5d0b606d8f8eb8191fb05a59e
5
5
  SHA512:
6
- metadata.gz: 1a890a9900f16000a933c4daa16c7e42ba59f7c8a6a48ecd86332ab53efd84d27759ff2842d14d59da5cef2b2f5cbfc9ff4584a31359e4bb7a1ffafb0d17323c
7
- data.tar.gz: 425abbe2117ebb6c491fdceacf1dda92f5ba5afbe00d4e6139accde6b2ceccd179b2729497c151394a17695d7af289a1e6b0411e981aa1de3f65752d8087b76a
6
+ metadata.gz: 31f7e710ed4b526591f56b2b83354151bc4284d7ef238b617e837b2c2ef1658da2821d8da1fb21051935a4ef4d22eade92721d995b2283ca1413a2f942876549
7
+ data.tar.gz: c8ea1864f564fd6218dc93a869e52466f596f4395ea3255cceeffca2b3bac630f5770ee9570dae65ec1f2cf5512b0b5c2c876e520a2639a1679f282fc0d44eca
data/CHANGELOG.md CHANGED
@@ -1,5 +1,28 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.5.0 - 2026-11-16
4
+
5
+ No breaking API change in this gem. A Rails app with a custom ("headless")
6
+ checkout built on `@openreceive/browser/headless` must read the breaking
7
+ changes in the root changelog: a coin tile no longer starts a swap, and the
8
+ payment breakdown has no "Total" row. A page that renders the packaged checkout
9
+ (`@openreceive/elements`, `@openreceive/react` or the standalone checkout of
10
+ the same version) needs no change.
11
+
12
+ - Fixed: the default client address, used by `config.rate_limiting` and stored
13
+ on each payment, now follows `config.action_dispatch.trusted_proxies`. It was
14
+ Rack's `request.ip`, which ignores that setting, so an app behind a load
15
+ balancer or CDN with a public address counted the proxy as every payer even
16
+ after listing it. When the peer that connected is a trusted proxy, the
17
+ address is the one it forwarded; otherwise it is the peer itself, whatever
18
+ the forwarding headers say. A `config.client_ip` of your own still wins. An
19
+ app behind a proxy will see different `client_ip` values on new payments.
20
+ - Changed: the installer's initializer explains which address the per-IP
21
+ limit counts and when to list a proxy in
22
+ `config.action_dispatch.trusted_proxies`.
23
+ - The swap request guard and the rate limiting changes of 0.5.0 come from
24
+ `openreceive-server`; see its changelog.
25
+
3
26
  ## 0.4.35 - 2026-11-12
4
27
 
5
28
  Release with the complete 0.4.35 package family. No Ruby runtime changes
@@ -84,8 +84,13 @@ OpenReceive.configure do |config|
84
84
 
85
85
  # Recommended for public web shops: cap invoice creation per client IP,
86
86
  # counted from the engine-owned openreceive_payments rows. Leave it off for
87
- # point-of-sale deployments where many payers share one IP. Behind a proxy,
88
- # configure Rails' trusted proxies so request.ip is the payer.
87
+ # point-of-sale deployments where many payers share one IP.
88
+ #
89
+ # The address counted is the one that connected to your app. When that is
90
+ # one of your trusted proxies, it is the client the proxy forwarded
91
+ # (request.remote_ip) instead. Rails trusts private addresses already; list
92
+ # a proxy with a public address (a cloud load balancer, a CDN) in
93
+ # config.action_dispatch.trusted_proxies, or all its payers share one limit.
89
94
  # config.rate_limiting = true
90
95
 
91
96
  # Settlement discovery is opportunistic by default: every engine request
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "ipaddr"
3
4
  require "openreceive/server"
4
5
 
5
6
  module OpenReceive
@@ -63,9 +64,10 @@ module OpenReceive
63
64
  # engine-owned openreceive_payments rows; or a Hash with
64
65
  # limit_per_hour / limit_per_day. Mutually exclusive with rate_limit.
65
66
  @rate_limiting = false
66
- # Client IP attribution: a proc receiving the framework request.
67
- # Defaults to ActionDispatch::Request#ip (which honors the app's
68
- # trusted-proxy configuration).
67
+ # Client IP attribution: a proc receiving the framework request. nil
68
+ # (the default) takes the address that connected, or the client a
69
+ # trusted proxy forwarded (config.action_dispatch.trusted_proxies); see
70
+ # default_client_ip. A proc set here replaces that default.
69
71
  @client_ip = nil
70
72
  # nil (the default) mirrors the JS createOpenReceive defaults: the
71
73
  # price provider becomes the built-in cached live price feed (with
@@ -140,7 +142,7 @@ module OpenReceive
140
142
  end
141
143
 
142
144
  def reset_runtime!
143
- %i[@service @request_handler @settlement_hook @resolved_nwc_client].each do |name|
145
+ %i[@service @request_handler @settlement_hook @resolved_nwc_client @rails_trusted_proxies].each do |name|
144
146
  remove_instance_variable(name) if instance_variable_defined?(name)
145
147
  end
146
148
  self
@@ -205,15 +207,10 @@ module OpenReceive
205
207
  DEFAULT_RATE_LIMIT_PER_HOUR = 60
206
208
  HOUR_SECONDS = 3_600
207
209
  DAY_SECONDS = 86_400
210
+ REFUSAL_WARNING_SECONDS = 60
208
211
 
209
212
  def resolved_client_ip
210
- extractor = @client_ip || lambda do |request|
211
- if request.respond_to?(:ip)
212
- request.ip
213
- elsif request.is_a?(Hash)
214
- request["REMOTE_ADDR"]
215
- end
216
- end
213
+ extractor = @client_ip || method(:default_client_ip)
217
214
  # Mirrors the JS handler: the extracted IP (custom hook or framework
218
215
  # default) is normalized into the bucket the limiter counts with (IPv6
219
216
  # /64, v4-mapped collapsed) and that same bucket is what gets stamped on
@@ -221,6 +218,66 @@ module OpenReceive
221
218
  ->(request) { OpenReceive::Server::ClientIp.attributed(extractor.call(request)) }
222
219
  end
223
220
 
221
+ # The client address when the host set no config.client_ip. The engine's
222
+ # controllers pass a Rails request; bare handler calls pass a Rack env
223
+ # Hash or nothing.
224
+ #
225
+ # For a Rails request the address that connected (REMOTE_ADDR) decides.
226
+ # When it is one of the app's trusted proxies, the client is the one that
227
+ # proxy forwarded: request.remote_ip. Any other address is the client
228
+ # itself, whatever X-Forwarded-For, Client-Ip or Forwarded say: a client
229
+ # that connects directly cannot choose its address, and a public proxy
230
+ # the app has not listed is counted as one client.
231
+ def default_client_ip(request)
232
+ if defined?(::ActionDispatch::Request) && request.is_a?(::ActionDispatch::Request)
233
+ rails_client_ip(request)
234
+ elsif request.respond_to?(:ip)
235
+ request.ip
236
+ elsif request.is_a?(Hash)
237
+ request["REMOTE_ADDR"]
238
+ end
239
+ end
240
+
241
+ def rails_client_ip(request)
242
+ peer = connecting_address(request)
243
+ # No address connected (a Unix socket): attributed as before, by Rack.
244
+ return request.ip if peer.nil?
245
+ return peer unless rails_trusted_proxies.any? { |proxy| proxy === peer }
246
+
247
+ begin
248
+ request.remote_ip
249
+ rescue StandardError
250
+ # RemoteIp raises when Client-Ip and X-Forwarded-For disagree (its
251
+ # spoofing check). Such a request is attributed as before, by Rack.
252
+ request.ip
253
+ end
254
+ end
255
+
256
+ # REMOTE_ADDR when it is one IP address, nil otherwise. An IPv4-mapped
257
+ # IPv6 address is read as its IPv4, so Rails' IPv4 ranges match it.
258
+ def connecting_address(request)
259
+ raw = request.remote_addr.to_s.strip
260
+ address = IPAddr.new(raw)
261
+ range = address.to_range
262
+ return nil unless range.begin == range.end
263
+
264
+ address.ipv4_mapped? ? address.native.to_s : raw
265
+ rescue ArgumentError
266
+ nil
267
+ end
268
+
269
+ # The app's trusted proxies, resolved by Rails itself: its RemoteIp
270
+ # middleware turns config.action_dispatch.trusted_proxies into the list
271
+ # it filters with, which is Rails' own private ranges when the app sets
272
+ # none. Matched the way that middleware matches (proxy === address).
273
+ def rails_trusted_proxies
274
+ @rails_trusted_proxies ||= begin
275
+ application = defined?(::Rails) && ::Rails.respond_to?(:application) ? ::Rails.application : nil
276
+ configured = application&.config&.action_dispatch&.trusted_proxies
277
+ ::ActionDispatch::RemoteIp.new(nil, true, configured).proxies
278
+ end
279
+ end
280
+
224
281
  # The built-in limiter (config.rate_limiting): a COUNT over the
225
282
  # engine-owned rows' client_ip within the rolling window, throttling only
226
283
  # the invoice-minting actions — identical semantics to the JS handler.
@@ -240,6 +297,24 @@ module OpenReceive
240
297
  limit_per_day = positive_rate_limit(limit_per_day, "limit_per_day") unless limit_per_day.nil?
241
298
  extract_ip = resolved_client_ip
242
299
  warned_unattributable = false
300
+ warned_not_a_payer = false
301
+ last_refusal_warning = nil
302
+ # Say which address was refused, at most once a minute. When that
303
+ # address is the host's own proxy, every payer shares its budget and
304
+ # this line is the only place the operator can see why checkouts
305
+ # stopped. The wording follows the JS limiter's.
306
+ refuse = lambda do |ip, counted, limit, window, at|
307
+ if last_refusal_warning.nil? || at - last_refusal_warning >= REFUSAL_WARNING_SECONDS
308
+ last_refusal_warning = at
309
+ rails_logger&.warn(
310
+ "[openreceive] rate limiting refused a new payment from #{ip}: #{counted} made in the " \
311
+ "last #{window}, limit #{limit}. If #{ip} is your proxy, load balancer or CDN " \
312
+ "rather than one payer, every payer is sharing this limit: pass the payer's own " \
313
+ "address — see https://openreceive.org/guides/rate-limiting.md"
314
+ )
315
+ end
316
+ raise OpenReceive::Server::RateLimitedError, BUILT_IN_RATE_LIMIT_MESSAGE
317
+ end
243
318
  lambda do |context|
244
319
  next true unless %w[checkout.create swap.create].include?(context[:action].to_s)
245
320
  ip = extract_ip.call(context[:request]).to_s
@@ -256,13 +331,30 @@ module OpenReceive
256
331
  end
257
332
  next true
258
333
  end
334
+ unless OpenReceive::Server::ClientIp.payer_address?(ip)
335
+ # A loopback or private address is the host's own proxy or network,
336
+ # never a payer on the internet. Counting it would put every payer
337
+ # in one bucket and stop the whole site at the limit, so it is not
338
+ # counted. Said once per process.
339
+ unless warned_not_a_payer
340
+ warned_not_a_payer = true
341
+ rails_logger&.warn(
342
+ "[openreceive] rate limiting is on, but this request's client address is #{ip}, a " \
343
+ "private or loopback address: your own proxy or network, not the payer. Limiting " \
344
+ "it would make every payer share one limit, so the request was allowed and such " \
345
+ "requests are not limited. Pass the payer's own address — see " \
346
+ "https://openreceive.org/guides/rate-limiting.md"
347
+ )
348
+ end
349
+ next true
350
+ end
259
351
  now = Time.now
260
- over_hour = OpenReceivePayment.count_attempts_from_ip(ip, now - HOUR_SECONDS) >= limit_per_hour
261
- raise OpenReceive::Server::RateLimitedError, BUILT_IN_RATE_LIMIT_MESSAGE if over_hour
352
+ last_hour = OpenReceivePayment.count_attempts_from_ip(ip, now - HOUR_SECONDS)
353
+ refuse.call(ip, last_hour, limit_per_hour, "hour", now) if last_hour >= limit_per_hour
262
354
 
263
- if limit_per_day &&
264
- OpenReceivePayment.count_attempts_from_ip(ip, now - DAY_SECONDS) >= limit_per_day
265
- raise OpenReceive::Server::RateLimitedError, BUILT_IN_RATE_LIMIT_MESSAGE
355
+ if limit_per_day
356
+ last_day = OpenReceivePayment.count_attempts_from_ip(ip, now - DAY_SECONDS)
357
+ refuse.call(ip, last_day, limit_per_day, "24 hours", now) if last_day >= limit_per_day
266
358
  end
267
359
  true
268
360
  end
@@ -5,6 +5,6 @@ module OpenReceive
5
5
  # top-level `::Rails` framework constant — engine code always references the framework as
6
6
  # `::Rails` to avoid shadowing.
7
7
  module Rails
8
- VERSION = "0.4.35"
8
+ VERSION = "0.5.0"
9
9
  end
10
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openreceive-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.35
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - OpenReceive
@@ -15,28 +15,28 @@ dependencies:
15
15
  requirements:
16
16
  - - '='
17
17
  - !ruby/object:Gem::Version
18
- version: 0.4.35
18
+ version: 0.5.0
19
19
  type: :runtime
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
23
  - - '='
24
24
  - !ruby/object:Gem::Version
25
- version: 0.4.35
25
+ version: 0.5.0
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: openreceive-server
28
28
  requirement: !ruby/object:Gem::Requirement
29
29
  requirements:
30
30
  - - '='
31
31
  - !ruby/object:Gem::Version
32
- version: 0.4.35
32
+ version: 0.5.0
33
33
  type: :runtime
34
34
  prerelease: false
35
35
  version_requirements: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - '='
38
38
  - !ruby/object:Gem::Version
39
- version: 0.4.35
39
+ version: 0.5.0
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: rails
42
42
  requirement: !ruby/object:Gem::Requirement