API keys sent as cleartext in a cookie

Issue ID: v3-global-securityrequirement-apikey-incookie-clear

Average severity: Critical

Description

Your API accepts API keys sent in cleartext in a cookie over an unencrypted channel. Attackers can easily intercept API calls and retrieve the credentials. They can then use the credentials to make other API calls.

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. The security field defines that the API is protected with API keys. The server object sets HTTP as the supported transportation protocol. This means that the API accepts an API call over an unencrypted HTTP connection, and expects it to have the API key in the clear in a cookie:

{  
    "servers": [
        {
            "url": "http://my.api.server.com/",
            "description": "API server"
        }
    ],
    // ...  
    "components": {
        "securitySchemes": {
            "apiKey": {
                "type": "apiKey",
                "name": "X-API-Key",
                "in": "cookie"
            }
        }
    },
    // ...   
    "security": [
        {
            "apiKey": []
        }
    ]
}

Possible exploit scenario

Attackers can intercept the credentials simply by listening to the network traffic in a public WiFi network. They could also use a traffic logging tool on a smartphone, computer, or browser, or sniff the traffic in the network to get the credentials.

Remediation

At the very least, set the transport protocol to HTTPS only so that all traffic is encrypted. You could also improve the security of the authentication method. The OAuth 2.0 Authorization Code flow (Access Code flow in OAS v2) is considered the most secure way to provide API authorization.

{  
    "servers": [
        {
            "url": "https://my.api.server.com/",
            "description": "API server"
        }
    ],
    // ...  
    "components": {
        "securitySchemes": {
            "OAuth2": {
                "type": "oauth2",
                "flows": {
                    "authorizationCode": {
                        "scopes": {
                            "write": "modify objects in your account",
                            "read": "read objects in your account"
                        },
                        "authorizationUrl": "https://example.com/oauth/authorize",
                        "tokenUrl": "https://example.com/oauth/token" 
                    }
                }
            }
        }
    },
    // ...
    "security": [
        {
        "OAuth2": [
                "write",
                "read"
            ]
        }
    ]
}