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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
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.rbindex 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 OpenIDConnectparse_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]+ endrescue JWT::DecodeError => eFailure(e.message)rescue JSON::JWK::Set::KidNotFound@@ -84,9 +83,13 @@ module OpenIDConnectenddef 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)enddef 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.rbindex 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 doendlet(: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 doinstance_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 doit "correctly queries for the token's public key" doparse- 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")endcontext "when the provider signing the token is not known" do@@ -94,6 +92,18 @@ RSpec.describe OpenIDConnect::JwtParser doendend+ 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" dolet(:token) { Base64.encode64("banana").strip }@@ -159,4 +169,20 @@ RSpec.describe OpenIDConnect::JwtParser doit { is_expected.to be_success }endend++ 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+ endend