otto 2.11.0 → 2.13.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/.github/workflows/ci.yml +1 -1
- data/.github/workflows/claude-code-review.yml +1 -1
- data/.github/workflows/claude.yml +1 -1
- data/.github/workflows/code-smells.yml +2 -2
- data/.github/workflows/release-gem.yml +1 -1
- data/.github/workflows/ruby-lint.yml +1 -1
- data/.github/workflows/yardoc.yml +1 -1
- data/.rubocop_todo.yml +2 -2
- data/AGENTS.md +4 -2
- data/CHANGELOG.rst +792 -0
- data/Gemfile +3 -2
- data/Gemfile.lock +13 -8
- data/README.md +6 -0
- data/docs/README.md +4 -0
- data/docs/audits/2026-09-28-v2.12.0-code-audit.md +732 -0
- data/docs/audits/README.md +63 -0
- data/docs/guides/authentication.md +329 -0
- data/docs/guides/forwarded-authority.md +85 -5
- data/docs/guides/mcp.md +40 -4
- data/docs/guides/privacy.md +28 -2
- data/docs/guides/routing.md +76 -9
- data/docs/guides/testing-guide.md +146 -5
- data/docs/migrating/v2.3.0.md +13 -7
- data/docs/reference/authentication.md +70 -22
- data/examples/lambda_handlers/README.md +4 -4
- data/examples/lambda_handlers/handlers.rb +4 -5
- data/examples/lambda_handlers/routes +2 -2
- data/examples/mcp_demo/README.md +65 -27
- data/examples/mcp_demo/app.rb +8 -5
- data/examples/mcp_demo/config.ru +50 -3
- data/examples/mcp_demo/routes +10 -6
- data/examples/security_features/README.md +2 -2
- data/lib/otto/caddy_tls/localhost_guard.rb +6 -6
- data/lib/otto/caddy_tls/server.rb +8 -4
- data/lib/otto/core/configuration.rb +5 -3
- data/lib/otto/core/option_hash.rb +124 -0
- data/lib/otto/core/redacted_inspect.rb +149 -0
- data/lib/otto/core/router.rb +57 -34
- data/lib/otto/core.rb +2 -0
- data/lib/otto/env_keys.rb +30 -6
- data/lib/otto/logging_helpers.rb +1 -1
- data/lib/otto/mcp/auth/token.rb +12 -1
- data/lib/otto/mcp/protocol.rb +3 -3
- data/lib/otto/mcp/registry.rb +28 -2
- data/lib/otto/mcp/route_parser.rb +20 -8
- data/lib/otto/mcp/server.rb +17 -9
- data/lib/otto/privacy/config.rb +31 -14
- data/lib/otto/privacy/ip_privacy.rb +109 -3
- data/lib/otto/privacy/redacted_fingerprint.rb +8 -10
- data/lib/otto/request.rb +34 -0
- data/lib/otto/response.rb +5 -2
- data/lib/otto/route.rb +1 -1
- data/lib/otto/route_definition.rb +3 -2
- data/lib/otto/route_handlers/base.rb +1 -1
- data/lib/otto/route_handlers/logic_class.rb +86 -34
- data/lib/otto/security/authentication/route_auth_wrapper.rb +10 -3
- data/lib/otto/security/config.rb +703 -345
- data/lib/otto/security/configurator.rb +82 -45
- data/lib/otto/security/core.rb +4 -2
- data/lib/otto/security/csp/report_middleware.rb +17 -1
- data/lib/otto/security/csrf.rb +17 -2
- data/lib/otto/security/csrf_validation.rb +13 -7
- data/lib/otto/security/middleware/csrf_middleware.rb +203 -37
- data/lib/otto/security/middleware/ip_privacy_middleware.rb +129 -42
- data/lib/otto/security/rate_limiter.rb +8 -5
- data/lib/otto/security/trusted_proxy_config.rb +396 -0
- data/lib/otto/static.rb +70 -6
- data/lib/otto/testing.rb +148 -0
- data/lib/otto/utils.rb +166 -40
- data/lib/otto/version.rb +1 -1
- data/lib/otto.rb +44 -11
- metadata +8 -2
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
# frozen_string_literal: true
|
|
4
4
|
|
|
5
5
|
require 'ipaddr'
|
|
6
|
+
require 'rack/utils'
|
|
6
7
|
require 'digest'
|
|
7
8
|
require 'openssl'
|
|
8
9
|
require 'socket'
|
|
@@ -136,13 +137,118 @@ class Otto
|
|
|
136
137
|
return value if value.nil? || masked_ip.nil? || masked_ip.empty?
|
|
137
138
|
|
|
138
139
|
replacement = masked_ip.include?(':') ? %("[#{masked_ip}]") : masked_ip
|
|
139
|
-
|
|
140
|
-
# semicolon) so a parameter merely ending in "for" is never touched.
|
|
141
|
-
value.gsub(/(\A|[,;]\s*)for\s*=\s*("[^"]*"|[^;,]+)/i) do
|
|
140
|
+
value.gsub(FORWARDED_FOR_PAIR) do
|
|
142
141
|
"#{Regexp.last_match(1)}for=#{replacement}"
|
|
143
142
|
end
|
|
144
143
|
end
|
|
145
144
|
|
|
145
|
+
# A `for=` pair in an RFC 7239 Forwarded value. Group 1 is the boundary
|
|
146
|
+
# before it, kept on rewrite.
|
|
147
|
+
#
|
|
148
|
+
# The boundaries are the ones Rack 3.2.7 (Rack::Utils.forwarded_values)
|
|
149
|
+
# accepts before a parameter name: the start of the value, a comma or
|
|
150
|
+
# semicolon, any whitespace (Rack skips it between pairs, after a quoted
|
|
151
|
+
# value, and before the first pair; a newline becomes a separator), or
|
|
152
|
+
# the closing quote of a quoted value, which Rack needs nothing after.
|
|
153
|
+
# A parameter merely ending in "for" is never touched. A quoted value
|
|
154
|
+
# may contain backslash escapes, as in Rack; an unquoted one runs to
|
|
155
|
+
# the next comma, semicolon or newline.
|
|
156
|
+
FORWARDED_FOR_PAIR = /(\A|[\s;,"])for\s*=\s*("(?:[^"\\]|\\.)*"|[^;,\n]*)/i
|
|
157
|
+
|
|
158
|
+
# Redact the `for=` values of an RFC 7239 Forwarded value: replace them
|
|
159
|
+
# with +replacement+ ({.mask_forwarded_for}), or remove them when
|
|
160
|
+
# +replacement+ is nil ({.strip_forwarded_for}).
|
|
161
|
+
#
|
|
162
|
+
# @param value [String] the Forwarded header value
|
|
163
|
+
# @param replacement [String, nil] an address, or nil
|
|
164
|
+
# @return [String] the rewritten value; empty when nothing is left
|
|
165
|
+
def self.redact_forwarded_for(value, replacement)
|
|
166
|
+
replacement ? mask_forwarded_for(value, replacement) : strip_forwarded_for(value)
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
# Guards the once-per-process warning for a deleted Forwarded header.
|
|
170
|
+
FORWARDED_DELETION_LOCK = Mutex.new
|
|
171
|
+
@forwarded_deletion_logged = false
|
|
172
|
+
|
|
173
|
+
# Log that IPPrivacyMiddleware deleted a Forwarded header whose `for=`
|
|
174
|
+
# rewrite it could not verify: at warn the first time in the process,
|
|
175
|
+
# at debug (when Otto.debug is on) after that.
|
|
176
|
+
#
|
|
177
|
+
# A header with an RFC 7239 extension parameter, which Rack rejects, is
|
|
178
|
+
# deleted on every request that carries it, and any client can send
|
|
179
|
+
# one, so a warning per request would be noise. The message never
|
|
180
|
+
# includes the header value, which may hold an address.
|
|
181
|
+
#
|
|
182
|
+
# @return [void]
|
|
183
|
+
def self.log_forwarded_deletion
|
|
184
|
+
message = '[IPPrivacyMiddleware] Deleted a Forwarded header: a for= value survived the ' \
|
|
185
|
+
'rewrite, or Rack cannot parse it (e.g. an extension parameter).'
|
|
186
|
+
first = FORWARDED_DELETION_LOCK.synchronize do
|
|
187
|
+
next false if @forwarded_deletion_logged
|
|
188
|
+
|
|
189
|
+
@forwarded_deletion_logged = true
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
if first
|
|
193
|
+
Otto.logger.warn("#{message} Later deletions in this process are logged at debug.")
|
|
194
|
+
elsif Otto.debug
|
|
195
|
+
Otto.logger.debug(message)
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
# Make the next Forwarded deletion warn again. Test support.
|
|
200
|
+
#
|
|
201
|
+
# @api private
|
|
202
|
+
# @return [void]
|
|
203
|
+
def self.reset_forwarded_deletion_log!
|
|
204
|
+
FORWARDED_DELETION_LOCK.synchronize { @forwarded_deletion_logged = false }
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
# Whether every `for=` value in an RFC 7239 Forwarded value is
|
|
208
|
+
# +replacement+ (or there is none, when +replacement+ is nil).
|
|
209
|
+
#
|
|
210
|
+
# The rewrite above is a pattern match; this is the check that it
|
|
211
|
+
# caught everything. It reads the header with Rack's parser, which
|
|
212
|
+
# decides Rack::Request#ip, and with Otto's depth-mode parser. A value
|
|
213
|
+
# Rack cannot parse (it rejects parameters other than by, for, host and
|
|
214
|
+
# proto) fails, since it cannot be verified.
|
|
215
|
+
#
|
|
216
|
+
# @param value [String] rewritten Forwarded header value
|
|
217
|
+
# @param replacement [String, nil] the address written into `for=`, or
|
|
218
|
+
# nil when the `for=` pairs were removed
|
|
219
|
+
# @return [Boolean]
|
|
220
|
+
def self.forwarded_for_only?(value, replacement)
|
|
221
|
+
expected = replacement&.include?(':') ? "[#{replacement}]" : replacement
|
|
222
|
+
parsed = Rack::Utils.forwarded_values(value)
|
|
223
|
+
return false if parsed.nil?
|
|
224
|
+
|
|
225
|
+
values = Array(parsed[:for]) + Otto::Utils.rfc7239_for_chain(value).reject(&:empty?)
|
|
226
|
+
values.all?(expected)
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
# Remove every `for=` pair from an RFC 7239 Forwarded header value.
|
|
230
|
+
#
|
|
231
|
+
# For a request with no resolvable client IP there is no address to put
|
|
232
|
+
# in `for=`. The pairs are removed rather than set to `unknown`: Rack
|
|
233
|
+
# returns a `for=` value as the client IP whenever it reads Forwarded,
|
|
234
|
+
# so `for=unknown` made Rack::Request#ip the string "unknown", while
|
|
235
|
+
# with no `for=` it falls back to REMOTE_ADDR as Otto::Request#ip does.
|
|
236
|
+
# RFC 7239 allows an element without `for=`, so `proto=`, `host=` and
|
|
237
|
+
# `by=` stay. An element left with no pairs is dropped.
|
|
238
|
+
#
|
|
239
|
+
# @param value [String, nil] the Forwarded header value
|
|
240
|
+
# @return [String, nil] the header without `for=` pairs; an empty string
|
|
241
|
+
# when nothing else remains, nil for nil input
|
|
242
|
+
def self.strip_forwarded_for(value)
|
|
243
|
+
return value if value.nil?
|
|
244
|
+
|
|
245
|
+
value.gsub(FORWARDED_FOR_PAIR) { Regexp.last_match(1) }
|
|
246
|
+
.split(',', -1)
|
|
247
|
+
.map { |element| element.gsub(/\s*;[\s;]*/, ';').gsub(/\A[\s;]+|[\s;]+\z/, '') }
|
|
248
|
+
.reject(&:empty?)
|
|
249
|
+
.join(', ')
|
|
250
|
+
end
|
|
251
|
+
|
|
146
252
|
# Mask IPv4 address
|
|
147
253
|
#
|
|
148
254
|
# @param addr [IPAddr] IPAddr object (must be IPv4)
|
|
@@ -26,16 +26,14 @@ class Otto
|
|
|
26
26
|
:request_path, :request_method, :referer
|
|
27
27
|
|
|
28
28
|
# IP-bearing forwarded headers overwritten with the masked IP in the
|
|
29
|
-
# geo-resolution env view
|
|
30
|
-
#
|
|
31
|
-
#
|
|
32
|
-
#
|
|
33
|
-
#
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
HTTP_X_CLIENT_IP
|
|
38
|
-
].freeze
|
|
29
|
+
# geo-resolution env view: the forwarded-for family and the vendor
|
|
30
|
+
# client-address headers (CF-Connecting-IP, True-Client-IP, ...). The
|
|
31
|
+
# same list IPPrivacyMiddleware#rewrite_forwarded_addresses rewrites,
|
|
32
|
+
# so a custom resolver reading env sees masked values everywhere the
|
|
33
|
+
# middleware would. The structured RFC 7239 Forwarded header
|
|
34
|
+
# (HTTP_FORWARDED) is handled separately in {#geo_env} (dropped, not
|
|
35
|
+
# swapped, to keep valid syntax).
|
|
36
|
+
GEO_MASKED_FORWARDED_HEADERS = Otto::Utils::ADDRESS_ONLY_HEADERS
|
|
39
37
|
|
|
40
38
|
# Create a new RedactedFingerprint from a Rack environment
|
|
41
39
|
#
|
data/lib/otto/request.rb
CHANGED
|
@@ -22,6 +22,33 @@ class Otto
|
|
|
22
22
|
#
|
|
23
23
|
# @see Otto#register_request_helpers
|
|
24
24
|
class Request < Rack::Request
|
|
25
|
+
# The Hash that #session installs in env['rack.session'] when no session
|
|
26
|
+
# middleware provided one. It is an ordinary, writable Hash; the class
|
|
27
|
+
# marks it as a stand-in, so RouteAuthWrapper can install a strategy's
|
|
28
|
+
# session over it while never replacing a session a middleware installed.
|
|
29
|
+
class DefaultSession < ::Hash
|
|
30
|
+
# Whether +session+ is no session a middleware installed: nil, or a
|
|
31
|
+
# DefaultSession. Checks the class only, so a lazy session object (for
|
|
32
|
+
# example rack-session's SessionHash) is not loaded.
|
|
33
|
+
#
|
|
34
|
+
# @param session [Object, nil] the value of env['rack.session']
|
|
35
|
+
# @return [Boolean]
|
|
36
|
+
def self.vacant?(session)
|
|
37
|
+
session.nil? || session.instance_of?(self)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Rack::Request#session installs the value of this private hook in
|
|
42
|
+
# env['rack.session'] when env has no session (rack 3.1 and 3.2:
|
|
43
|
+
# request.rb, `def default_session; {}; end`). Otto installs a
|
|
44
|
+
# {DefaultSession} instead of a plain Hash.
|
|
45
|
+
#
|
|
46
|
+
# @return [DefaultSession]
|
|
47
|
+
def default_session
|
|
48
|
+
DefaultSession.new
|
|
49
|
+
end
|
|
50
|
+
private :default_session
|
|
51
|
+
|
|
25
52
|
def user_agent
|
|
26
53
|
env['HTTP_USER_AGENT']
|
|
27
54
|
end
|
|
@@ -208,6 +235,13 @@ class Otto
|
|
|
208
235
|
canonical = env['otto.client_ip']
|
|
209
236
|
return canonical if canonical && !canonical.empty?
|
|
210
237
|
|
|
238
|
+
# otto.ip_match and otto.peer_relayed without a usable otto.client_ip
|
|
239
|
+
# mean the middleware ran and found no client IP (every pass writes
|
|
240
|
+
# otto.peer_relayed; otto.ip_match alone may be a stub). It deleted the
|
|
241
|
+
# forwarded address headers on that path, so re-resolving here would
|
|
242
|
+
# see only the peer and return a proxy as the client.
|
|
243
|
+
return canonical if env.key?('otto.ip_match') && env.key?('otto.peer_relayed')
|
|
244
|
+
|
|
211
245
|
Otto::Utils.resolve_client_ip(env, otto_security_config)
|
|
212
246
|
end
|
|
213
247
|
|
data/lib/otto/response.rb
CHANGED
|
@@ -91,8 +91,11 @@ class Otto
|
|
|
91
91
|
# Prevent MIME type sniffing
|
|
92
92
|
headers['x-content-type-options'] = 'nosniff'
|
|
93
93
|
|
|
94
|
-
# Add referrer policy
|
|
95
|
-
|
|
94
|
+
# Add the configured referrer policy without replacing an explicit
|
|
95
|
+
# response-level value.
|
|
96
|
+
configured_policy = request && request.env['otto.security_config']&.referrer_policy
|
|
97
|
+
policy = self['referrer-policy'] || configured_policy || Otto::Security::Config::DEFAULT_REFERRER_POLICY
|
|
98
|
+
headers['referrer-policy'] ||= policy
|
|
96
99
|
|
|
97
100
|
# Add frame options
|
|
98
101
|
headers['x-frame-options'] = 'DENY'
|
data/lib/otto/route.rb
CHANGED
|
@@ -187,7 +187,7 @@ class Otto
|
|
|
187
187
|
# Add security headers
|
|
188
188
|
if otto.respond_to?(:security_config) && otto.security_config
|
|
189
189
|
otto.security_config.security_headers.each do |header, value|
|
|
190
|
-
res.headers[header]
|
|
190
|
+
res.headers[header] ||= value
|
|
191
191
|
end
|
|
192
192
|
end
|
|
193
193
|
|
|
@@ -121,8 +121,9 @@ class Otto
|
|
|
121
121
|
end
|
|
122
122
|
|
|
123
123
|
# Parse a single whitespace-delimited `key=value` option token, applying
|
|
124
|
-
# the security-gating fail-fast rule
|
|
125
|
-
# RouteParser
|
|
124
|
+
# the security-gating fail-fast rule for normal routes (issue #191). The
|
|
125
|
+
# MCP RouteParser also calls this, after rejecting auth/role/csrf tokens
|
|
126
|
+
# outright because MCP and TOOL routes do not enforce them.
|
|
126
127
|
# @param part [String] a single option token, e.g. "auth=session"
|
|
127
128
|
# @param context [String] human-readable source description for the
|
|
128
129
|
# raised error message, e.g. "route definition \"GET /admin ...\""
|
|
@@ -148,7 +148,7 @@ class Otto
|
|
|
148
148
|
# Add security headers
|
|
149
149
|
if otto_instance.respond_to?(:security_config) && otto_instance.security_config
|
|
150
150
|
otto_instance.security_config.security_headers.each do |header, value|
|
|
151
|
-
res.headers[header]
|
|
151
|
+
res.headers[header] ||= value
|
|
152
152
|
end
|
|
153
153
|
end
|
|
154
154
|
|
|
@@ -10,14 +10,25 @@ class Otto
|
|
|
10
10
|
#
|
|
11
11
|
# Logic classes use a constrained signature: initialize(context, params, locale)
|
|
12
12
|
# - context: The authentication strategy result (user info, session data)
|
|
13
|
-
# - params: Merged request parameters
|
|
13
|
+
# - params: Merged request parameters. Path captures win over form and
|
|
14
|
+
# query parameters (Rack order between those two), and a JSON body sits
|
|
15
|
+
# below all of them.
|
|
14
16
|
# - locale: The locale string from env['otto.locale']
|
|
15
17
|
#
|
|
18
|
+
# A Logic class may also declare a +route_params:+ keyword on initialize
|
|
19
|
+
# to receive the path captures on their own, separate from anything the
|
|
20
|
+
# caller sent in the query string or body:
|
|
21
|
+
#
|
|
22
|
+
# def initialize(context, params, locale, route_params: {})
|
|
23
|
+
#
|
|
16
24
|
# IMPORTANT: Logic classes do NOT receive the Rack request or env hash.
|
|
17
25
|
# This is intentional - Logic classes work with clean, authenticated contexts.
|
|
18
26
|
# For endpoints requiring direct request access (sessions, cookies, headers,
|
|
19
27
|
# or logout flows), use controller handlers (Controller#action or Controller.action).
|
|
20
28
|
class LogicClassHandler < BaseHandler
|
|
29
|
+
# Parameter types (from Method#parameters) that name a keyword argument
|
|
30
|
+
ROUTE_PARAMS_KEYWORD_TYPES = %i[key keyreq].freeze
|
|
31
|
+
|
|
21
32
|
protected
|
|
22
33
|
|
|
23
34
|
# Invoke Logic class with constrained signature
|
|
@@ -36,8 +47,13 @@ class Otto
|
|
|
36
47
|
# Get locale
|
|
37
48
|
locale = env['otto.locale'] || 'en'
|
|
38
49
|
|
|
39
|
-
# Instantiate Logic class
|
|
40
|
-
|
|
50
|
+
# Instantiate Logic class. Path captures travel separately so a Logic
|
|
51
|
+
# class can read the router's value by name, whatever the body sent.
|
|
52
|
+
logic = if accepts_route_params?
|
|
53
|
+
target_class.new(strategy_result, logic_params, locale, route_params: route_params)
|
|
54
|
+
else
|
|
55
|
+
target_class.new(strategy_result, logic_params, locale)
|
|
56
|
+
end
|
|
41
57
|
|
|
42
58
|
# Execute standard Logic class lifecycle
|
|
43
59
|
logic.raise_concerns if logic.respond_to?(:raise_concerns)
|
|
@@ -57,50 +73,86 @@ class Otto
|
|
|
57
73
|
[result, context]
|
|
58
74
|
end
|
|
59
75
|
|
|
76
|
+
# Path captures matched by the router, keyed by the route's placeholder
|
|
77
|
+
# names. Always present in +params+ as well (where they take precedence);
|
|
78
|
+
# this copy is for Logic classes that must not confuse a path value with
|
|
79
|
+
# one the caller put in the query string or body.
|
|
80
|
+
#
|
|
81
|
+
# @return [Hash] Indifferent hash of route captures; empty for literal routes
|
|
82
|
+
def route_params
|
|
83
|
+
Otto::Static.indifferent_params((@extra_params || {}).dup)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Whether the Logic class constructor declares a +route_params:+ keyword
|
|
87
|
+
# (or accepts arbitrary keywords). Logic classes opt in by declaring it;
|
|
88
|
+
# the three-positional signature keeps working unchanged.
|
|
89
|
+
#
|
|
90
|
+
# @return [Boolean]
|
|
91
|
+
def accepts_route_params?
|
|
92
|
+
return @accepts_route_params if defined?(@accepts_route_params)
|
|
93
|
+
|
|
94
|
+
@accepts_route_params = target_class.instance_method(:initialize).parameters.any? do |type, name|
|
|
95
|
+
type == :keyrest || (ROUTE_PARAMS_KEYWORD_TYPES.include?(type) && name == :route_params)
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
60
99
|
# Extract logic parameters including JSON body parsing
|
|
100
|
+
#
|
|
101
|
+
# Starts from req.params, which already carries Rack's own precedence
|
|
102
|
+
# (form body over query string) with the path captures merged on top by
|
|
103
|
+
# setup_request_response. A JSON body sits below all of those: a JSON
|
|
104
|
+
# key can never replace a path capture, a query parameter, or a form
|
|
105
|
+
# field. JSON bodies are only read for methods that carry a body (never
|
|
106
|
+
# GET or HEAD).
|
|
107
|
+
#
|
|
61
108
|
# @param req [Rack::Request] Request object
|
|
62
109
|
# @param env [Hash] Rack environment
|
|
63
110
|
# @return [Hash] Parameters for Logic class
|
|
64
111
|
def extract_logic_params(req, env)
|
|
65
|
-
# req.params already has extra_params merged and indifferent_params applied
|
|
66
|
-
# by setup_request_response in BaseHandler
|
|
67
112
|
logic_params = req.params.dup
|
|
113
|
+
return logic_params unless json_body?(req)
|
|
68
114
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
115
|
+
json_params = parse_json_body(req, env)
|
|
116
|
+
return logic_params if json_params.empty?
|
|
117
|
+
|
|
118
|
+
Otto::Static.indifferent_params(json_params.merge(logic_params))
|
|
119
|
+
end
|
|
73
120
|
|
|
74
|
-
|
|
121
|
+
# Whether the request carries a JSON body worth parsing
|
|
122
|
+
# @param req [Rack::Request] Request object
|
|
123
|
+
# @return [Boolean]
|
|
124
|
+
def json_body?(req)
|
|
125
|
+
return false if req.get? || req.head?
|
|
126
|
+
return false unless req.content_type&.include?('application/json')
|
|
127
|
+
|
|
128
|
+
req.body&.size&.positive? || false
|
|
75
129
|
end
|
|
76
130
|
|
|
77
131
|
# Parse JSON request body with error handling
|
|
78
132
|
# @param req [Rack::Request] Request object
|
|
79
133
|
# @param env [Hash] Rack environment
|
|
80
|
-
# @
|
|
81
|
-
#
|
|
82
|
-
def parse_json_body(req, env
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
logic_params
|
|
134
|
+
# @return [Hash] Parsed JSON object, or an empty hash when the body is
|
|
135
|
+
# not a JSON object or fails to parse
|
|
136
|
+
def parse_json_body(req, env)
|
|
137
|
+
req.body.rewind
|
|
138
|
+
json_data = JSON.parse(req.body.read)
|
|
139
|
+
json_data.is_a?(Hash) ? json_data : {}
|
|
140
|
+
rescue JSON::ParserError => e
|
|
141
|
+
# Base context pattern: create once, reuse for correlation
|
|
142
|
+
log_context = Otto::LoggingHelpers.request_context(env)
|
|
143
|
+
|
|
144
|
+
Otto.structured_log(:error, 'JSON parsing error',
|
|
145
|
+
log_context.merge(
|
|
146
|
+
handler: handler_name,
|
|
147
|
+
error: e.message,
|
|
148
|
+
error_class: e.class.name,
|
|
149
|
+
duration: Otto::Utils.now_in_μs - @start_time
|
|
150
|
+
))
|
|
151
|
+
|
|
152
|
+
Otto::LoggingHelpers.log_backtrace(e,
|
|
153
|
+
log_context.merge(handler: handler_name))
|
|
154
|
+
|
|
155
|
+
{}
|
|
104
156
|
end
|
|
105
157
|
|
|
106
158
|
# Format handler name for Logic routes
|
|
@@ -209,9 +209,16 @@ class Otto
|
|
|
209
209
|
# Set environment variables for controllers/logic
|
|
210
210
|
env['otto.strategy_result'] = result
|
|
211
211
|
|
|
212
|
-
# SESSION PERSISTENCE:
|
|
213
|
-
#
|
|
214
|
-
|
|
212
|
+
# SESSION PERSISTENCE: never replace a session a middleware installed
|
|
213
|
+
# (e.g. rack-session's SessionHash); the middleware commits the
|
|
214
|
+
# object it installed. result.session (nil and false are not
|
|
215
|
+
# sessions) goes into env only when env has none, or only the
|
|
216
|
+
# Otto::Request::DefaultSession that Otto::Request#session installs
|
|
217
|
+
# when no middleware did (Otto's CSRF check runs before this wrapper
|
|
218
|
+
# and reads the session that way). The check reads only the class,
|
|
219
|
+
# so it does not load a lazy session.
|
|
220
|
+
session = result.session
|
|
221
|
+
env['rack.session'] = session if session && Otto::Request::DefaultSession.vacant?(env['rack.session'])
|
|
215
222
|
|
|
216
223
|
# Layer 1 Authorization: Check role requirements
|
|
217
224
|
auth_check = @role_authorizer.check(result, env)
|