Undefined OAuth2 scope in an OpenAPI 3 operation

An OpenAPI 3.0 operation requires a scope absent from its OAuth2 definition.

Description

An OpenAPI 3.0 operation’s security replaces global requirements. Match its OAuth2 scope names to the flow’s scopes under components.securitySchemes and to the authorization server.

Potential impact

A client can request the wrong permission and be denied access to the operation.

Remediation

If a required scope is missing, define it and keep it in the operation’s requirement. Test the operation with and without the required scope. For OpenID Connect, verify scope names with the provider.

Examples

The second GET / keeps read:api and removes the undefined error:api.

Before

json
{
  "openapi": "3.0.0",
  "paths": {
    "/": {
      "get": {
        "security": [
          {
            "oAuth2AuthCode": [
              "read:api",
              "error:api"
            ]
          }
        ]
      }
    }
  },
  "components": {
    "securitySchemes": {
      "oAuth2AuthCode": {
        "type": "oauth2",
        "flows": {
          "authorizationCode": {
            "authorizationUrl": "https://api.example.com/oauth/authorize",
            "tokenUrl": "https://api.example.com/oauth/token",
            "scopes": {
              "read:api": "read your apis"
            }
          }
        }
      }
    }
  }
}

After

json
{
  "openapi": "3.0.0",
  "paths": {
    "/": {
      "get": {
        "security": [
          {
            "oAuth2AuthCode": [
              "read:api"
            ]
          }
        ]
      }
    }
  },
  "components": {
    "securitySchemes": {
      "oAuth2AuthCode": {
        "type": "oauth2",
        "flows": {
          "authorizationCode": {
            "authorizationUrl": "https://api.example.com/oauth/authorize",
            "tokenUrl": "https://api.example.com/oauth/token",
            "scopes": {
              "read:api": "read your apis"
            }
          }
        }
      }
    }
  }
}

References