Description
Top-level security sets the API’s default authentication requirement. Without it, each protected operation needs its own requirement.
Potential impact
If an operation also omits security, clients have no documented reason to send credentials.
Remediation
Put shared requirements in top-level security and exceptions on the relevant operations. Define the schemes in OpenAPI 3.0 components.securitySchemes or 2.0 securityDefinitions. Enforce the same policy on the server.
Examples
Here GET / inherits petstore_auth. Replace the example OAuth2 URLs with your provider’s endpoints. The pet scopes are illustrative. Require only the scopes each operation needs.
Before
json
{
"openapi": "3.0.0",
"info": {
"title": "Simple API overview",
"version": "1.0.0"
},
"paths": {
"/": {
"get": {
"responses": {
"200": {
"description": "ok"
}
}
}
}
}
}
After
json
{
"openapi": "3.0.0",
"info": {
"title": "Simple API overview",
"version": "1.0.0"
},
"paths": {
"/": {
"get": {
"responses": {
"200": {
"description": "ok"
}
}
}
}
},
"security": [
{
"petstore_auth": [
"write:pets",
"read:pets"
]
}
],
"components": {
"securitySchemes": {
"petstore_auth": {
"type": "oauth2",
"flows": {
"authorizationCode": {
"authorizationUrl": "https://example.com/oauth/authorize",
"tokenUrl": "https://example.com/oauth/token",
"scopes": {
"write:pets": "modify pets in your account",
"read:pets": "read your pets"
}
}
}
}
}
}
}