Review transport security for global API keys (OpenAPI 3.0)

Global API key authentication requires HTTPS

Description

When global security requires an API key, use HTTPS for every request that sends it.

Potential impact

Plaintext HTTP can expose a key in transit. A key in a URL may also appear in logs, even over HTTPS.

Remediation

Require HTTPS on the server and clients. Send keys in a header and redact it from logs. Set an HTTPS URL in OpenAPI 3.0 servers, or https in OpenAPI 2.0 schemes.

Examples

The After example also switches to OAuth2 with the read scope. You can keep API key authentication with the same HTTPS protection.

Before

json
{
  "openapi": "3.0.0",
  "servers": [{"url": "http://api.example.com"}],
  "security": [
    {
      "apiKeyAuth": []
    }
  ],
  "components": {
    "securitySchemes": {
      "apiKeyAuth": {
        "type": "apiKey",
        "name": "X-API-Key",
        "in": "query"
      }
    }
  }
}

After

json
{
  "openapi": "3.0.0",
  "servers": [{"url": "https://api.example.com"}],
  "security": [
    {
      "OAuth2": [
        "read"
      ]
    }
  ],
  "components": {
    "securitySchemes": {
      "OAuth2": {
        "type": "oauth2",
        "flows": {
          "authorizationCode": {
            "authorizationUrl": "https://example.com/oauth/authorize",
            "tokenUrl": "https://example.com/oauth/token",
            "scopes": {
              "read": "read objects in your account"
            }
          }
        }
      }
    }
  }
}

References