Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Only try to parse JWT if provider has a JWKS URL

Jan Sandbrink436d70c3fea5
diff --git a/modules/openid_connect/app/services/openid_connect/jwt_parser.rb b/modules/openid_connect/app/services/openid_connect/jwt_parser.rb
index 60f941b2c5f..d468c3d53dd 100644
--- a/modules/openid_connect/app/services/openid_connect/jwt_parser.rb
+++ b/modules/openid_connect/app/services/openid_connect/jwt_parser.rb
@@ -48,23 +48,22 @@ module OpenIDConnect
parse_unverified_iss_alg_kid(token).bind do |issuer, alg, kid|
return Failure("Token signature algorithm #{alg} is not supported") if SUPPORTED_JWT_ALGORITHMS.exclude?(alg)
- provider = fetch_provider(issuer)
- return Failure("The access token issuer is unknown") if provider.blank?
-
- verified_payload, = JWT.decode(
- token,
- fetch_key(provider:, kid:),
- true,
- {
- algorithm: alg,
- verify_expiration: @verify_expiration,
- verify_aud: @verify_audience,
- aud: provider.client_id,
- required_claims: all_required_claims
- }
- )
-
- Success([verified_payload, provider])
+ fetch_provider(issuer).fmap do |provider|
+ verified_payload, = JWT.decode(
+ token,
+ fetch_key(provider:, kid:),
+ true,
+ {
+ algorithm: alg,
+ verify_expiration: @verify_expiration,
+ verify_aud: @verify_audience,
+ aud: provider.client_id,
+ required_claims: all_required_claims
+ }
+ )
+
+ [verified_payload, provider]
+ end
rescue JWT::DecodeError => e
Failure(e.message)
rescue JSON::JWK::Set::KidNotFound
@@ -84,9 +83,13 @@ module OpenIDConnect
end
def fetch_provider(issuer)
- return nil if issuer.blank?
+ return Failure("The token has no issuer") if issuer.blank?
- OpenIDConnect::Provider.where(available: true).where("options->>'issuer' = ?", issuer).first
+ provider = OpenIDConnect::Provider.where(available: true).where("options->>'issuer' = ?", issuer).first
+ return Failure("The access token issuer is unknown") if provider.blank?
+ return Failure("Unable to validate issuer signature, can't fetch JWKS") if provider.jwks_uri.blank?
+
+ Success(provider)
end
def fetch_key(provider:, kid:)
diff --git a/modules/openid_connect/spec/services/openid_connect/jwt_parser_spec.rb b/modules/openid_connect/spec/services/openid_connect/jwt_parser_spec.rb
index 8d2508e498b..a5e0bf9b181 100644
--- a/modules/openid_connect/spec/services/openid_connect/jwt_parser_spec.rb
+++ b/modules/openid_connect/spec/services/openid_connect/jwt_parser_spec.rb
@@ -38,9 +38,10 @@ RSpec.describe OpenIDConnect::JwtParser do
end
let(:token) { JWT.encode(payload, private_key, "RS256", { kid: "key-identifier" }) }
- let!(:provider) { create(:oidc_provider) }
+ let!(:provider) { create(:oidc_provider, issuer: known_issuer, client_id: our_client_id, jwks_uri:) }
let(:known_issuer) { "International Space Station" }
let(:our_client_id) { "openproject.org" }
+ let(:jwks_uri) { "https://example.com/certs" }
let(:expiration) { 1.minute.from_now.to_i }
before do
@@ -48,9 +49,6 @@ RSpec.describe OpenIDConnect::JwtParser do
instance_double(JSON::JWK, to_key: private_key.public_key)
)
- provider.options["issuer"] = known_issuer
- provider.options["client_id"] = our_client_id
- provider.options["jwks_uri"] = "https://example.com/certs"
provider.save!
end
@@ -69,7 +67,7 @@ RSpec.describe OpenIDConnect::JwtParser do
it "correctly queries for the token's public key" do
parse
- expect(JSON::JWK::Set::Fetcher).to have_received(:fetch).with("https://example.com/certs", kid: "key-identifier")
+ expect(JSON::JWK::Set::Fetcher).to have_received(:fetch).with(jwks_uri, kid: "key-identifier")
end
context "when the provider signing the token is not known" do
@@ -94,6 +92,18 @@ RSpec.describe OpenIDConnect::JwtParser do
end
end
+ context "when the token does not specify an issuer" do
+ let(:payload) do
+ { "exp" => expiration, "sub" => "M. Curie", "aud" => our_client_id }
+ end
+
+ it { is_expected.to be_failure }
+
+ it "indicates the problem" do
+ expect(parse.failure).to match(/token has no issuer/)
+ end
+ end
+
context "when the token is not a valid JWT" do
let(:token) { Base64.encode64("banana").strip }
@@ -159,4 +169,20 @@ RSpec.describe OpenIDConnect::JwtParser do
it { is_expected.to be_success }
end
end
+
+ context "when the provider has no jwks_url set" do
+ let(:jwks_uri) { "" }
+
+ it { is_expected.to be_failure }
+
+ it "indicates the problem that occured" do
+ expect(parse.failure).to match(/Unable to validate issuer signature/)
+ end
+
+ it "does not try to fetch a public key" do
+ parse
+
+ expect(JSON::JWK::Set::Fetcher).not_to have_received(:fetch)
+ end
+ end
end