The personal finance app for everyone (by everyone)
Ruby
fix(oidc): honor http issuer scheme during OIDC discovery (#2853)
Will Wilsonb6cd8437a52d
diff --git a/config/initializers/openid_connect_http_discovery.rb b/config/initializers/openid_connect_http_discovery.rbnew file mode 100644index 0000000000..884ec730db--- /dev/null+++ b/config/initializers/openid_connect_http_discovery.rb@@ -0,0 +1,53 @@+# frozen_string_literal: true++# Patch: honor the issuer's scheme during OIDC discovery.+#+# The openid_connect/swd gems hardcode discovery to HTTPS (SWD.url_builder defaults to+# URI::HTTPS, and Config::Resource drops the issuer's scheme), so an http:// issuer -+# a self-hosted IdP without SSL - is upgraded to https:443 and fails to connect.+#+# Only the discovery request is affected: the endpoints it returns are absolute, and+# rack-oauth2 keeps a scheme that is already present, so the rest of the flow follows.+# The port and cache-key handling below are also made scheme-aware so an http:// issuer+# never drops an explicit port or reuses an https:// issuer's cached metadata.+#+# Verified against openid_connect 2.3.1 / swd 2.0.3 - revisit if those are upgraded.+# See https://github.com/we-promise/sure/issues/2844.+require "openid_connect"++module OpenIDConnect+ module Discovery+ module Provider+ class Config+ class Resource+ def initialize(uri)+ @scheme = uri.scheme+ @host = uri.host+ # Only omit the scheme's own default port, so an explicitly configured+ # non-default port (e.g. http on 443, https on 80) is preserved.+ @port = uri.port unless uri.port == uri.default_port+ @path = File.join uri.path, ".well-known/openid-configuration"+ attr_missing!+ end++ def endpoint+ url_builder = @scheme == "http" ? URI::HTTP : URI::HTTPS+ url_builder.build [ nil, host, port, path, nil, nil ]+ rescue URI::Error => e+ raise SWD::Exception.new(e.message)+ end++ private++ # The gem keys the discovery cache on host alone. Now that a single host+ # can be reached over more than one scheme/port/path, include those so an+ # http:// issuer never reuses an https:// issuer's cached metadata.+ def cache_key+ digest = OpenSSL::Digest::SHA256.hexdigest [ @scheme, host, port, path ].join(" ")+ "swd:resource:opneid-conf:#{digest}"+ end+ end+ end+ end+ end+enddiff --git a/test/initializers/openid_connect_http_discovery_test.rb b/test/initializers/openid_connect_http_discovery_test.rbnew file mode 100644index 0000000000..3fc5da6836--- /dev/null+++ b/test/initializers/openid_connect_http_discovery_test.rb@@ -0,0 +1,56 @@+# frozen_string_literal: true++require "test_helper"++# Regression coverage for https://github.com/we-promise/sure/issues/2844 - a+# self-hosted IdP served over plain HTTP was upgraded to https:443 during discovery.+class OpenIDConnectHttpDiscoveryTest < ActiveSupport::TestCase+ Resource = OpenIDConnect::Discovery::Provider::Config::Resource++ test "http issuer builds an http discovery endpoint" do+ endpoint = Resource.new(URI.parse("http://auth.example")).endpoint++ assert_equal "http://auth.example/.well-known/openid-configuration", endpoint.to_s+ end++ test "https issuer still builds an https discovery endpoint" do+ endpoint = Resource.new(URI.parse("https://auth.example")).endpoint++ assert_equal "https://auth.example/.well-known/openid-configuration", endpoint.to_s+ end++ test "custom http port is preserved" do+ endpoint = Resource.new(URI.parse("http://auth.example:9000")).endpoint++ assert_equal "http://auth.example:9000/.well-known/openid-configuration", endpoint.to_s+ end++ test "explicit non-default port is preserved per scheme" do+ http_443 = Resource.new(URI.parse("http://auth.example:443")).endpoint+ https_80 = Resource.new(URI.parse("https://auth.example:80")).endpoint++ assert_equal "http://auth.example:443/.well-known/openid-configuration", http_443.to_s+ assert_equal "https://auth.example:80/.well-known/openid-configuration", https_80.to_s+ end++ test "cache key varies by scheme, port, and path on the same host" do+ baseline = cache_key_for("http://auth.example")+ other_scheme = cache_key_for("https://auth.example")+ other_port = cache_key_for("http://auth.example:9000")+ other_path = cache_key_for("http://auth.example/application/o/sure")++ keys = [ baseline, other_scheme, other_port, other_path ]+ assert_equal keys.length, keys.uniq.length, "each of scheme/port/path must produce a distinct cache key"+ end++ test "issuer path prefix is preserved" do+ endpoint = Resource.new(URI.parse("http://auth.example/application/o/sure")).endpoint++ assert_equal "http://auth.example/application/o/sure/.well-known/openid-configuration", endpoint.to_s+ end++ private+ def cache_key_for(issuer)+ Resource.new(URI.parse(issuer)).send(:cache_key)+ end+end