Operation accepts API keys transported in query parameters over network

Issue ID: operation-securityrequirement-apikey-inquery

Average severity: Medium

Description

The API operation accepts API keys transported in query parameters over the network instead of the more secure tokens. The credentials are sent over the network on each API call, over and over again, and are exposed to attack attempts to retrieve them. In addition, the API key could end up being visible in the logs that web servers and proxies produce on URLs.

For more details, see the OpenAPI Specification.

Example

The following is an example of how this type of risk could look in your API definition:

{
    "schemes": [
        "https"
    ],        
    // ...
    "securityDefinitions": {
        "apiKey": {
            "type": "apiKey",
            "name": "X-API-Key",
            "in": "query"
        }
    },
    // ...
    "paths": {
        "/pets": {
            "post": {
                "description": "Creates a new pet in the store",
                "operationId": "addPet",
                "security": [
                    {
                        "apiKey": []
                    }
                ]
                // ...
            }
        }
    }
}

Possible exploit scenario

Attackers can eavesdrop credentials transported over the network and try to intercept the traffic between the API consumer and your API to retrieve them. If they succeed, they can access all API operations protected with the same security requirement until the credentials are specifically revoked or changed. API keys have long lifetime providing extended opportunity for exploits.

Remediation

Use a more secure authentication method, like OAuth 2.0. It uses access tokens with limited lifetime and authorizations (the scopes) granted that the resource owner grants from an authorization server.

An API operation can be consumed only if the request includes an access token and the scopes of the token match the scopes that the API operation requires. Even if attackers successfully retrieved access tokens, they can only use the token on a subset of the API operation and for a limited time.

{
    "schemes": [
        "https"
    ],
    // ...
    "securityDefinitions": {
        "OAuth2": {
            "type": "oauth2",
            "flow": "accessCode",
            "scopes": {
                "read": "read objects in your account",
                "write": "modify objects in your account"
            },
            "authorizationUrl": "https://example.com/oauth/authorize",
            "tokenUrl": "https://example.com/oauth/token"
        }
    },
    // ...
    "paths": {
        "/pets": {
            "post": {
                "description": "Creates a new pet in the store",
                "operationId": "addPet",
                "security": [
                    {
                        "OAuth2": [
                            "write",
                            "read"
                        ]
                    }
                ]
                // ...
            }
        }
    }
}