fix: OAuth flow validation contradicts the specification - #10
Merged
Conversation
The OAuth Flow Object marks only scopes as REQUIRED. authorizationUrl applies to implicit and authorizationCode, tokenUrl to password, clientCredentials and authorizationCode, and refreshUrl to none of them. Treating all four as mandatory made an implicit flow fail with a TypeError on the official example from the specification.
The specification calls bearerFormat 'a hint to the client [...] primarily for documentation purposes'. It is not REQUIRED, so a bearer scheme without it is valid and must be accepted.
The OAuth Flows Object marks no field as REQUIRED, so defining only the flows an API actually supports is valid. Requiring all four rejected almost every real-world document, including the specification's own example. Unknown flow keys are still rejected, and an oauth2 scheme with no flows at all remains invalid.
Each flow type has its own required fields: implicit needs authorizationUrl, password and clientCredentials need tokenUrl, and authorizationCode needs both. refreshUrl is never required. SecurityScheme performs this check because it knows the flow type of each entry, which OAuthFlow itself does not.
This document is the OAuth Flow Object Example from OAS 3.1.1. Before the preceding fixes it could not be parsed at all.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The library rejects OAuth security schemes that the specification permits — including the official OAuth Flow Object Example from the specification itself:
Three defects cause this. All three contradict tables that are identical in OAS 3.0.4 and 3.1.1, so 3.0 documents are affected too.
1.
OAuthFlowtreated every field as mandatory. Onlyscopesis REQUIRED; for the URLs the "Applies To" column narrows the rest:authorizationUrlimplicit,authorizationCodeonlytokenUrlpassword,clientCredentials,authorizationCodeonlyrefreshUrloauth2scopesoauth2— "The map MAY be empty"All four were declared
stringand read unconditionally, so an implicit flow — which has no token endpoint by definition — could not be represented at all.2.
oauth2required all four flow types at once. The OAuth Flows Object marks no field as REQUIRED, yet a scheme supporting onlyauthorizationCodewas rejected withAttribute "flows" is missing required key "implicit".3.
bearerFormatwas required forhttp/bearerschemes. The specification calls it a hint "primarily for documentation purposes". It is not REQUIRED, yet omitting it threw.Changes
OAuthFlow— the three URL fields become nullable andtoArray()emits only what is set.scopesstays mandatory, read unguarded like the required fields ofInfo,ServerandTag.SecurityScheme::setFlows()— accepts any non-empty subset of known flow types and validates each flow's URLs against its own type:implicitneedsauthorizationUrl,passwordandclientCredentialsneedtokenUrl,authorizationCodeneeds both,refreshUrlis never required. Unknown flow keys are still rejected; anoauth2scheme with no flows at all is still invalid.SecurityScheme::setBearerFormat()— validation removed.FLOW_*constants added, following the existingTYPE_*/IN_*pattern.FLOWSkeeps its value and order.Per-flow validation lives in
SecuritySchemebecause only it knows which flow type a givenOAuthFlowrepresents.Two limits are deliberate: the "Applies to" column is enforced in one direction only (a flow carrying a URL it does not need is still accepted, since tightening that breaks the existing
testRequiredprovider), andflows: {}still throws even though an empty map validates against the official schema — both are BC decisions of their own.Backward compatibility
getAuthorizationUrl(),getTokenUrl()andgetRefreshUrl()now return?string, so callers feeding them into astringparameter need a null check. Unavoidable — the fields are genuinely optional. Everything else is source-compatible: constructor parameter order is unchanged and all four arguments now have defaults.Tests
Eleven tests added, each failing against the unmodified code. Two tests that asserted the removed behaviour were replaced (
testMissingBearerFormat→testBearerFormatIsOptional;testMissingFlow→testSingleFlowIsAccepted+testUnknownFlowTypeIsRejected).testMissingFlowsis retained — that case is still invalid. The specification's OAuth Flow Object Example is added as a round-trip fixture, verified to fail against the pre-fix code.