standard_id 0.26.4 → 0.28.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: 80484283cdfb54ecc2ead53d1498b5cd9ee88813e755b972db2e2d15561362f7
4
- data.tar.gz: 9f9aaea4b18b29a61c3d3e70d7be8f1c2016455b3866181b9bea0cd272617405
3
+ metadata.gz: c3aa81db933d1f506926c8c400e2bc57f60a7b29529f7bfa514e71b68fe028c1
4
+ data.tar.gz: 47a3be048ef63b02ee3819354dae0e4af75cb21a88d740e5b72e754c55471a2a
5
5
  SHA512:
6
- metadata.gz: 203398f53eacaf60b272dbaf84794c71d2a975fbd1f49669d37dc3375de6fa7d24f00b36f352a9bb09909e82e1947f4513082b397b61a664e22f0180aa34f665
7
- data.tar.gz: 737d854a5784cf508954f343482e0476e2f5ad7a40322a26d7e0360c6910af6333a954bab7e9f490fb66ffc77ee15946dff62e2afd4a5d67387c9aca60e04cf4
6
+ metadata.gz: e4b6618506a9428d7a426ad4d095bd95fd2aa9c3f7b58e8727f20e8300d8dc3c6f622da1e07829961474c392d35a292775f4596b36e39c0961f66a28cca2db7a
7
+ data.tar.gz: 585a4b2d8c65e4d9de10eb8466d99c9323ea6279459edf21fe72705f829688cded440133faaa50317a01bcf4d2544ecf48b01749527287be73cc042eb833fe6a
@@ -7,6 +7,23 @@ module StandardId
7
7
 
8
8
  layout "public"
9
9
 
10
+ # Rate limit reset-request generation by IP (10 per hour). The endpoint
11
+ # emails a reset token, so without a limit it's an email-flooding vector.
12
+ rate_limit to: StandardId.config.rate_limits.password_reset_start_per_ip,
13
+ within: 1.hour,
14
+ name: "reset-password-ip",
15
+ only: :create,
16
+ store: StandardId::RateLimitHandling::RATE_LIMIT_STORE
17
+
18
+ # Rate limit reset requests by email target (3 per 15 minutes) to blunt
19
+ # account-enumeration probing of individual addresses.
20
+ rate_limit to: StandardId.config.rate_limits.password_reset_start_per_target,
21
+ within: 15.minutes,
22
+ by: -> { "reset-password:#{params[:email].to_s.strip.downcase}" },
23
+ name: "reset-password-target",
24
+ only: :create,
25
+ store: StandardId::RateLimitHandling::RATE_LIMIT_STORE
26
+
10
27
  skip_before_action :require_browser_session!, only: [:show, :create]
11
28
 
12
29
  def show
@@ -9,6 +9,13 @@ module StandardId
9
9
 
10
10
  layout "public"
11
11
 
12
+ # Throttle account-creation spam by IP (10 per hour).
13
+ rate_limit to: StandardId.config.rate_limits.signup_per_ip,
14
+ within: 1.hour,
15
+ name: "signup-ip",
16
+ only: :create,
17
+ store: StandardId::RateLimitHandling::RATE_LIMIT_STORE
18
+
12
19
  skip_before_action :require_browser_session!, only: [:show, :create]
13
20
 
14
21
  before_action :redirect_if_authenticated, only: [:show]
@@ -2,6 +2,15 @@ module StandardId
2
2
  module Web
3
3
  module VerifyEmail
4
4
  class ConfirmController < BaseController
5
+ # Per-IP limit on code confirmation (20 per 15 minutes, shared with OTP
6
+ # verify). The per-challenge attempt cap alone doesn't stop distributed
7
+ # guessing across many challenges; both show and update probe a code.
8
+ rate_limit to: StandardId.config.rate_limits.otp_verify_per_ip,
9
+ within: 15.minutes,
10
+ name: "verify-email-confirm-ip",
11
+ only: [:show, :update],
12
+ store: StandardId::RateLimitHandling::RATE_LIMIT_STORE
13
+
5
14
  before_action :prepare_code_challenge
6
15
 
7
16
  def show
@@ -2,6 +2,15 @@ module StandardId
2
2
  module Web
3
3
  module VerifyPhone
4
4
  class ConfirmController < BaseController
5
+ # Per-IP limit on code confirmation (20 per 15 minutes, shared with OTP
6
+ # verify). The per-challenge attempt cap alone doesn't stop distributed
7
+ # guessing across many challenges; both show and update probe a code.
8
+ rate_limit to: StandardId.config.rate_limits.otp_verify_per_ip,
9
+ within: 15.minutes,
10
+ name: "verify-phone-confirm-ip",
11
+ only: [:show, :update],
12
+ store: StandardId::RateLimitHandling::RATE_LIMIT_STORE
13
+
5
14
  before_action :prepare_code_challenge
6
15
 
7
16
  def show
@@ -34,6 +34,11 @@ module StandardId
34
34
  scope :public_clients, -> { where(client_type: "public") }
35
35
  scope :for_owner, ->(owner) { where(owner: owner) }
36
36
 
37
+ # Loopback interface hosts per RFC 8252 §7.3 (native apps). "localhost" is
38
+ # included for compatibility but RFC 8252 §8.3 recommends clients use
39
+ # 127.0.0.1/::1 instead, since "localhost" can be remapped by the OS.
40
+ LOOPBACK_HOSTS = %w[127.0.0.1 ::1 localhost].freeze
41
+
37
42
  # Callbacks
38
43
  before_create :generate_client_id
39
44
  before_update :set_deactivated_at, if: :will_save_change_to_active?
@@ -99,6 +104,12 @@ module StandardId
99
104
  # (or, worse, a different path segment like /cb/evil).
100
105
  #
101
106
  # Subdomain wildcards are NOT supported — host must match exactly.
107
+ #
108
+ # Exception — loopback redirects for native apps (RFC 8252 §7.3): when this
109
+ # client is public + PKCE-required and BOTH the registered and requested
110
+ # URIs are http loopback URIs, the port is ignored (native apps bind an
111
+ # ephemeral port on a local listener at authorization time, so it cannot be
112
+ # known at registration). See #loopback_redirect_uri? below.
102
113
  def valid_redirect_uri?(uri)
103
114
  requested = self.class.parse_redirect_uri(uri)
104
115
  return false unless requested
@@ -107,6 +118,23 @@ module StandardId
107
118
  registered = self.class.parse_redirect_uri(registered_uri)
108
119
  next false unless registered
109
120
 
121
+ # RFC 8252 §7.3: for loopback interface redirects, "the authorization
122
+ # server MUST allow any port to be specified at the time of the request".
123
+ # Only host + path are compared; scheme is already pinned to "http" by
124
+ # the loopback predicate. Host equality is still required, so a client
125
+ # registered with 127.0.0.1 does not match localhost (or vice versa) —
126
+ # per §8.3, "localhost" is less trustworthy than the literal loopback
127
+ # IPs because the OS can remap it. This relaxation is gated to public
128
+ # PKCE clients: the redirect lands on an ephemeral listener on the
129
+ # user's own machine and PKCE binds the code to the initiating client,
130
+ # whereas confidential clients have stable callback URLs and keep
131
+ # strict port matching.
132
+ if public? && require_pkce? &&
133
+ self.class.loopback_redirect_uri?(registered) &&
134
+ self.class.loopback_redirect_uri?(requested)
135
+ next registered.host == requested.host && registered.path == requested.path
136
+ end
137
+
110
138
  registered.scheme == requested.scheme &&
111
139
  registered.host == requested.host &&
112
140
  registered.port == requested.port &&
@@ -114,6 +142,17 @@ module StandardId
114
142
  end
115
143
  end
116
144
 
145
+ # True when the parsed URI is an http URI targeting a loopback interface
146
+ # literal (RFC 8252 §7.3). IPv6 loopback hosts are normalized: URI.parse
147
+ # yields "[::1]" on some Ruby versions and "::1" on others, so surrounding
148
+ # brackets are stripped before comparison.
149
+ def self.loopback_redirect_uri?(parsed_uri)
150
+ return false unless parsed_uri.scheme == "http"
151
+
152
+ host = parsed_uri.host.to_s.delete_prefix("[").delete_suffix("]")
153
+ LOOPBACK_HOSTS.include?(host)
154
+ end
155
+
117
156
  # Parse a redirect URI string into a URI object suitable for comparison.
118
157
  # Returns nil for unparseable, relative, or scheme-less URIs.
119
158
  def self.parse_redirect_uri(value)
@@ -153,7 +153,10 @@ StandardId.configure do |c|
153
153
  # for backwards compatibility; new installs should set the newer key.
154
154
  # c.passwordless.max_attempts = 3
155
155
 
156
- # Default: 30 — seconds before a new challenge can be requested.
156
+ # Default: 30 — OTP-resend cooldown: minimum seconds before the same target
157
+ # can request another code. Enforced in the passwordless start path; a resend
158
+ # inside the window is rejected (the already-sent code stays valid). Set to 0
159
+ # to disable the cooldown.
157
160
  # c.passwordless.retry_delay = 30
158
161
 
159
162
  # Custom username validator; return nil/false to accept, or an error message
@@ -352,6 +352,15 @@ StandardId::ConfigSchema.define do
352
352
  field :verification_start_per_target, type: :integer, default: 3 # per 15 minutes
353
353
  field :verification_start_per_ip, type: :integer, default: 10 # per hour
354
354
 
355
+ # Password-reset request. The start endpoint emails a reset token, so an
356
+ # unthrottled endpoint is an email-flooding + account-enumeration vector.
357
+ # Mirrors the verification_start shape (per-IP hourly + per-target 15-min).
358
+ field :password_reset_start_per_ip, type: :integer, default: 10 # per hour
359
+ field :password_reset_start_per_target, type: :integer, default: 3 # per 15 minutes
360
+
361
+ # Password signup — throttle account-creation spam by IP.
362
+ field :signup_per_ip, type: :integer, default: 10 # per hour
363
+
355
364
  # API equivalents
356
365
  field :api_passwordless_start_per_ip, type: :integer, default: 10 # per hour
357
366
  field :api_passwordless_start_per_target, type: :integer, default: 5 # per 15 minutes
@@ -23,6 +23,7 @@ module StandardId
23
23
 
24
24
  validate_username!(username)
25
25
  run_username_validator!(username)
26
+ enforce_retry_delay!(username)
26
27
  emit_code_requested(username)
27
28
  challenge = create_challenge!(
28
29
  username,
@@ -43,6 +44,29 @@ module StandardId
43
44
 
44
45
  protected
45
46
 
47
+ # Reject a resend that arrives within passwordless.retry_delay seconds of
48
+ # the most recent still-active code for this target. A soft cooldown that
49
+ # complements the coarser per-target rate limits: it does not consume the
50
+ # active challenge, so the code already sent remains usable. Raises
51
+ # InvalidRequestError (rescued by the web login + API OAuth error handlers).
52
+ def enforce_retry_delay!(username)
53
+ delay = StandardId::Passwordless.retry_delay
54
+ return if delay <= 0
55
+
56
+ last = StandardId::CodeChallenge.active
57
+ .where(realm: @realm, channel: connection_type, target: username)
58
+ .order(created_at: :desc)
59
+ .first
60
+ return if last.nil?
61
+
62
+ elapsed = Time.current - last.created_at
63
+ return if elapsed >= delay
64
+
65
+ wait = (delay - elapsed).ceil
66
+ raise StandardId::InvalidRequestError,
67
+ "Please wait #{wait} #{'second'.pluralize(wait)} before requesting another code"
68
+ end
69
+
46
70
  def create_challenge!(username, code_length: nil, expires_in: nil, metadata: {})
47
71
  ActiveRecord::Base.transaction do
48
72
  invalidate_active_challenges!(username)
@@ -83,6 +83,14 @@ module StandardId
83
83
  legacy = StandardId.config.passwordless.max_attempts.to_i
84
84
  legacy.positive? ? legacy : 5
85
85
  end
86
+
87
+ # Minimum seconds that must elapse between successive code requests for
88
+ # the same target (the OTP-resend cooldown). Reads passwordless.retry_delay;
89
+ # 0 or negative disables the cooldown. Enforced in the passwordless strategy
90
+ # start path (see BaseStrategy#enforce_retry_delay!).
91
+ def retry_delay
92
+ StandardId.config.passwordless.retry_delay.to_i
93
+ end
86
94
  end
87
95
  end
88
96
  end
@@ -1,3 +1,3 @@
1
1
  module StandardId
2
- VERSION = "0.26.4"
2
+ VERSION = "0.28.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: standard_id
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.26.4
4
+ version: 0.28.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jaryl Sim