Skip to content

fix: honor include_request: false in all errors - #1729

Merged
olleolleolle merged 1 commit into
lostisland:mainfrom
yykamei:fix-parsing-error-include-request
Oct 26, 2026
Merged

olleolleolle merged 1 commit into
lostisland:mainfrom
yykamei:fix-parsing-error-include-request

Conversation

@yykamei

@yykamei yykamei commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Description

response :raise_error, include_request: false only stripped request data from the exceptions that RaiseError raises itself. When a 200 response fails to parse, the json middleware raises Faraday::ParsingError initialized with the live Faraday::Response, whose env still exposes request_headers and request_body:

conn = Faraday.new do |f|
  f.response :json
  f.response :raise_error, include_request: false
  f.adapter :test do |stub|
    stub.get('/secret') { [200, { 'Content-Type' => 'application/json' }, 'not-json'] }
  end
end

conn.get('/secret') { |req| req.headers['Authorization'] = 'Bearer SECRET' }
# e.response.env.request_headers still contains "Authorization" => "Bearer SECRET"

Applications forwarding such an exception (e.g. as a cause) to error trackers can leak credentials such as OAuth bearer tokens. This behavior has existed since the option was introduced in #1526.

With this change, RaiseError marks the opt-out on the request Env from on_request, and Faraday::Error replaces a flagged Faraday::Response (or flagged Faraday::Env) with a request-less { status:, headers:, body: } hash — the same shape #response_values already produces for 4xx/5xx errors under include_request: false. The option now applies consistently to errors raised by other middlewares, while the default (include_request: true) is unchanged.

Todos

  • Tests
  • Documentation

Additional Notes

  • The flag is stored as a custom Env member (env[:include_request]), the same mechanism the json middleware uses for raw_body, so no public Env API changes.
  • Behavior change under include_request: false only: ParsingError#response is now a Hash without request data instead of a Faraday::Response carrying it, matching the Hash shape 4xx/5xx errors already use under the same option. Default mode is unchanged.
  • Errors built from caller-supplied Hashes are unaffected: a Hash cannot carry the flag.

The option was only read by RaiseError#response_values, so a
ParsingError raised by the Json middleware still carried the full
request Env, leaking credentials such as Authorization headers to
error trackers.

RaiseError now marks the shared Env in on_request, and Error replaces
a flagged Response or Env with a request-less {status, headers, body}
hash, the same shape 4xx/5xx errors already use under the option.
@olleolleolle
olleolleolle merged commit 573827e into lostisland:main Oct 26, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants