Skip to content

A platform for community discussion. Free, open, simple.

DEV: Fix auth redirect validation in user API key flow (#40459)

Penar Musaraj72a7ae59b619
diff --git a/app/controllers/user_api_keys_controller.rb b/app/controllers/user_api_keys_controller.rb
index c8dfe0139bd..aa12b5e9ce1 100644
--- a/app/controllers/user_api_keys_controller.rb
+++ b/app/controllers/user_api_keys_controller.rb
@@ -137,7 +137,9 @@ class UserApiKeysController < ApplicationController
def otp
require_params_otp
+ find_client_by_public_key
validate_params_otp
+ validate_auth_redirect
unless current_user
cookies[:destination_url] = request.fullpath
@@ -158,6 +160,7 @@ class UserApiKeysController < ApplicationController
def create_otp
require_params_otp
+ find_client_by_public_key
validate_params_otp
validate_auth_redirect
@@ -197,6 +200,10 @@ class UserApiKeysController < ApplicationController
@client = UserApiKeyClient.find_by(client_id: params[:client_id])
end
+ def find_client_by_public_key
+ @client = UserApiKeyClient.find_by(public_key: params[:public_key])
+ end
+
def require_params
%i[nonce scopes client_id].each { |p| params.require(p) }
end
@@ -234,7 +241,12 @@ class UserApiKeysController < ApplicationController
def validate_auth_redirect
return unless params.key?(:auth_redirect)
- if UserApiKeyClient.invalid_auth_redirect?(params[:auth_redirect], client: @client)
+
+ if @client&.auth_redirect.present? && params[:auth_redirect] != @client.auth_redirect
+ raise Discourse::InvalidAccess
+ end
+
+ if UserApiKeyClient.invalid_auth_redirect?(params[:auth_redirect])
raise Discourse::InvalidAccess
end
end
diff --git a/app/models/user_api_key_client.rb b/app/models/user_api_key_client.rb
index 22913089ee0..e3923f608d2 100644
--- a/app/models/user_api_key_client.rb
+++ b/app/models/user_api_key_client.rb
@@ -11,8 +11,7 @@ class UserApiKeyClient < ActiveRecord::Base
Set.new(scopes.map(&:name))
end
- def self.invalid_auth_redirect?(auth_redirect, client: nil)
- return false if client&.auth_redirect == auth_redirect
+ def self.invalid_auth_redirect?(auth_redirect)
SiteSetting
.allowed_user_api_auth_redirects
.split("|")
diff --git a/app/services/wildcard_url_checker.rb b/app/services/wildcard_url_checker.rb
index 5f9621c27b5..0f7f91250f9 100644
--- a/app/services/wildcard_url_checker.rb
+++ b/app/services/wildcard_url_checker.rb
@@ -1,21 +1,59 @@
# frozen_string_literal: true
module WildcardUrlChecker
+ HOST_WILDCARD_PATTERN = "[A-Za-z0-9.-]*"
+ HOST_WILDCARD_VALUE_REGEXP = /\A[A-Za-z0-9.-]+\z/
+ URL_COMPONENT_WILDCARD_PATTERN = ".*"
+
def self.check_url(url, url_to_check)
- return false if !valid_url?(url_to_check)
+ uri_to_check = parse_valid_url(url_to_check)
+ return false if uri_to_check.blank?
+ return true if url == "*"
- escaped_url = Regexp.escape(url).sub("\\*", '\S*')
- url_regex = Regexp.new("\\A#{escaped_url}\\z", "i")
+ uri = parse_valid_url(url)
+ return false if uri.blank?
- url_to_check.match?(url_regex)
+ uri.scheme.casecmp?(uri_to_check.scheme) && uri.port == uri_to_check.port &&
+ uri.userinfo.to_s.casecmp?(uri_to_check.userinfo.to_s) &&
+ hosts_match?(uri.host, uri_to_check.host) &&
+ wildcard_match?(path_query(uri), path_query(uri_to_check), URL_COMPONENT_WILDCARD_PATTERN) &&
+ wildcard_match?(uri.fragment.to_s, uri_to_check.fragment.to_s, URL_COMPONENT_WILDCARD_PATTERN)
end
private
- def self.valid_url?(url)
+ def self.parse_valid_url(url)
uri = URI.parse(url)
- uri&.scheme.present? && uri&.host.present?
+ return uri if uri&.scheme.present? && uri&.host.present?
+
+ nil
rescue URI::InvalidURIError
- false
+ nil
+ end
+
+ def self.hosts_match?(host, host_to_check)
+ host = host.downcase
+ host_to_check = host_to_check.downcase
+
+ if host.start_with?("*.") && host.count("*") == 1
+ suffix = host.delete_prefix("*.")
+ return(
+ host_to_check.end_with?(".#{suffix}") &&
+ host_to_check.delete_suffix(".#{suffix}").match?(HOST_WILDCARD_VALUE_REGEXP)
+ )
+ end
+
+ wildcard_match?(host, host_to_check, HOST_WILDCARD_PATTERN)
+ end
+
+ def self.path_query(uri)
+ path_query = uri.path.to_s
+ path_query += "?#{uri.query}" if uri.query.present?
+ path_query
+ end
+
+ def self.wildcard_match?(pattern, value, wildcard_pattern)
+ escaped_pattern = Regexp.escape(pattern).gsub("\\*", wildcard_pattern)
+ Regexp.new("\\A#{escaped_pattern}\\z", "i").match?(value)
end
end
diff --git a/spec/requests/user_api_keys_controller_spec.rb b/spec/requests/user_api_keys_controller_spec.rb
index ca2cc333e10..b9b722a31c9 100644
--- a/spec/requests/user_api_keys_controller_spec.rb
+++ b/spec/requests/user_api_keys_controller_spec.rb
@@ -265,6 +265,17 @@ RSpec.describe UserApiKeysController do
expect(response.status).to eq(302)
end
+ it "rejects wildcard auth_redirect matches outside the candidate host" do
+ SiteSetting.allowed_user_api_auth_redirects = "https://*.example.com/callback"
+ sign_in(Fabricate(:user, refresh_auto_groups: true))
+
+ post "/user-api-key.json",
+ params: args.merge(auth_redirect: "https://evil.com/path/.example.com/callback")
+
+ expect(response.status).to eq(403)
+ expect(response.body).to include("errors")
+ end
+
it "preserves query params in auth_redirect" do
SiteSetting.user_api_key_allowed_groups = Group::AUTO_GROUPS[:trust_level_0]
SiteSetting.allowed_user_api_auth_redirects = args[:auth_redirect] + "/*"
@@ -287,11 +298,18 @@ RSpec.describe UserApiKeysController do
)
end
- before { sign_in(user) }
+ before do
+ sign_in(user)
+ SiteSetting.allowed_user_api_auth_redirects = args[:auth_redirect]
+ end
+
+ it "requires registered client auth_redirect to stay on the global allowlist" do
+ SiteSetting.allowed_user_api_auth_redirects = "https://good.example.com/callback"
- it "does not require allowed_user_api_auth_redirects site setting" do
post "/user-api-key.json", params: args
- expect(response.status).to eq(302)
+
+ expect(response.status).to eq(403)
+ expect(response.body).to include("errors")
end
it "does not require application_name or public_key params" do
@@ -372,6 +390,21 @@ RSpec.describe UserApiKeysController do
expect(response.status).to eq(400)
end
+ it "does not show the form when auth_redirect differs from a registered client's redirect" do
+ SiteSetting.allowed_user_api_auth_redirects = "https://*.example.com/callback"
+
+ Fabricate(
+ :user_api_key_client,
+ public_key: public_key,
+ auth_redirect: "https://legitimate.example.com/callback",
+ )
+
+ get "/user-api-key/otp",
+ params: otp_args.merge(auth_redirect: "https://evil.example.com/callback")
+
+ expect(response.status).to eq(403)
+ end
+
it "rejects a non-RSA public key with 400" do
SiteSetting.allowed_user_api_auth_redirects = otp_args[:auth_redirect]
ec_key = OpenSSL::PKey::EC.generate("prime256v1")
@@ -392,6 +425,22 @@ RSpec.describe UserApiKeysController do
expect(response.status).to eq(403)
end
+ it "does not allow auth_redirect that differs from a registered client's redirect" do
+ SiteSetting.allowed_user_api_auth_redirects = "https://*.example.com/callback"
+ sign_in(Fabricate(:user, refresh_auto_groups: true))
+
+ Fabricate(
+ :user_api_key_client,
+ public_key: public_key,
+ auth_redirect: "https://legitimate.example.com/callback",
+ )
+
+ post "/user-api-key/otp",
+ params: otp_args.merge(auth_redirect: "https://evil.example.com/callback")
+
+ expect(response.status).to eq(403)
+ end
+
it "allows OTP for staff without meeting TL requirement" do
SiteSetting.user_api_key_allowed_groups = Group::AUTO_GROUPS[:trust_level_2]
SiteSetting.allowed_user_api_auth_redirects = otp_args[:auth_redirect]
diff --git a/spec/services/wildcard_url_checker_spec.rb b/spec/services/wildcard_url_checker_spec.rb
index 16988d0a425..8b4c3d3bdcd 100644
--- a/spec/services/wildcard_url_checker_spec.rb
+++ b/spec/services/wildcard_url_checker_spec.rb
@@ -2,74 +2,133 @@
RSpec.describe WildcardUrlChecker do
describe ".check_url" do
- context "when url is valid" do
- it "returns true" do
- result1 =
- described_class.check_url(
- "https://*.discourse.org",
- "https://anything.is.possible.discourse.org",
- )
- expect(result1).to eq(true)
+ subject(:check) { described_class.check_url(pattern, url) }
- result2 =
- described_class.check_url("https://www.discourse.org", "https://www.discourse.org")
- expect(result2).to eq(true)
+ context "when pattern is *" do
+ let(:pattern) { "*" }
+ let(:url) { "https://hello.discourse.org" }
- result3 = described_class.check_url("*", "https://hello.discourse.org")
- expect(result3).to eq(true)
+ it { is_expected.to eq(true) }
+ end
- result4 =
- described_class.check_url("discourse://auth_redirect", "discourse://auth_redirect")
- expect(result4).to eq(true)
+ context "with exact URL match" do
+ let(:pattern) { "https://www.discourse.org" }
- result5 =
- described_class.check_url(
- "customprotocol://www.discourse.org",
- "customprotocol://www.discourse.org",
- )
- expect(result5).to eq(true)
+ it "matches the same URL" do
+ expect(described_class.check_url(pattern, "https://www.discourse.org")).to eq(true)
end
- end
- context "when url is invalid" do
- it "returns false" do
- result1 =
- described_class.check_url(
- "https://*.discourse.org",
- "https://bad-domain.discourse.org.evil.com",
- )
- expect(result1).to eq(false)
+ it "does not match a different subdomain" do
+ expect(described_class.check_url(pattern, "https://www.www.discourse.org")).to eq(false)
+ end
- result2 =
- described_class.check_url(
- "https://www.discourse.org",
- "https://www.discourse.org.evil.com",
- )
- expect(result2).to eq(false)
+ it "does not match when the domain is used as a path component" do
+ expect(described_class.check_url(pattern, "https://evil.com/www.discourse.org")).to eq(
+ false,
+ )
+ end
- result3 =
- described_class.check_url("https://www.discourse.org", "https://www.www.discourse.org")
- expect(result3).to eq(false)
+ it "does not match a URL with the domain appended to an attacker domain" do
+ expect(described_class.check_url(pattern, "https://www.discourse.org.evil.com")).to eq(
+ false,
+ )
+ end
- result4 =
+ it "does not match a URL with an embedded newline" do
+ expect(
described_class.check_url(
- "https://www.discourse.org",
+ pattern,
"https://www.discourse.org\nwww.discourse.org.evil.com",
- )
- expect(result4).to eq(false)
+ ),
+ ).to eq(false)
+ end
+ end
+
+ context "with a subdomain wildcard pattern (*.example.com)" do
+ let(:pattern) { "https://*.example.com/callback" }
+
+ it "matches a direct subdomain" do
+ expect(described_class.check_url(pattern, "https://app.example.com/callback")).to eq(true)
+ end
+
+ it "matches a deeply nested subdomain" do
+ expect(described_class.check_url(pattern, "https://deep.sub.example.com/callback")).to eq(
+ true,
+ )
+ end
+
+ it "does not match the apex domain" do
+ expect(described_class.check_url(pattern, "https://example.com/callback")).to eq(false)
+ end
+
+ it "does not match a different path" do
+ expect(described_class.check_url(pattern, "https://app.example.com/other")).to eq(false)
+ end
+
+ it "does not match when the domain appears only in the path" do
+ expect(
+ described_class.check_url(pattern, "https://evil.com/path/.example.com/callback"),
+ ).to eq(false)
+ end
- result5 = described_class.check_url("https://", "https://")
- expect(result5).to eq(false)
+ it "does not match a domain that ends with example.com but is not a subdomain" do
+ expect(described_class.check_url(pattern, "https://notexample.com/callback")).to eq(false)
+ end
+
+ it "does not match a domain that uses example.com as a suffix on an attacker domain" do
+ expect(
+ described_class.check_url(pattern, "https://evil.com.example.com.attacker.com/callback"),
+ ).to eq(false)
+ end
+
+ it "does not match a different scheme" do
+ expect(described_class.check_url(pattern, "http://app.example.com/callback")).to eq(false)
+ end
+
+ it "does not match a different port" do
+ expect(described_class.check_url(pattern, "https://app.example.com:8080/callback")).to eq(
+ false,
+ )
+ end
+ end
+
+ context "with a wildcard in the path" do
+ let(:pattern) { "https://*.example.com/*" }
+
+ it "matches any path on a valid subdomain" do
+ expect(described_class.check_url(pattern, "https://app.example.com/any/path")).to eq(true)
+ end
- result6 =
+ it "still does not match a different host even with path wildcards" do
+ expect(described_class.check_url(pattern, "https://evil.com/any.example.com/path")).to eq(
+ false,
+ )
+ end
+ end
+
+ context "with a custom protocol" do
+ let(:pattern) { "discourse://auth_redirect" }
+ let(:url) { "discourse://auth_redirect" }
+
+ it { is_expected.to eq(true) }
+ end
+
+ context "with invalid URLs" do
+ it "returns false for URLs with no host" do
+ expect(described_class.check_url("https://", "https://")).to eq(false)
+ end
+
+ it "returns false for URLs with no scheme" do
+ expect(described_class.check_url("noscheme", "noscheme")).to eq(false)
+ end
+
+ it "returns false for URLs with an invalid scheme character in the pattern" do
+ expect(
described_class.check_url(
"invalid$protocol://www.discourse.org",
"invalid$protocol://www.discourse.org",
- )
- expect(result6).to eq(false)
-
- result7 = described_class.check_url("noscheme", "noscheme")
- expect(result7).to eq(false)
+ ),
+ ).to eq(false)
end
end
end