API Key Exposed In Operation Security (v3)

  • Query id: 281b8071-6226-4a43-911d-fec246d422c2
  • Query name: API Key Exposed In Operation Security (v3)
  • Platform: OpenAPI
  • Severity: Low
  • Category: Access Control
  • URL: Github

Description

API Keys should be transported using a secure method such as HTTPS. Define a security scheme that uses a secure method to transport the API key.
Documentation

Code samples

Code samples with security vulnerabilities

Positive test num. 1 - json file
{
  "openapi": "3.0.0",
  "info": {
    "title": "Simple API overview",
    "version": "1.0.0"
  },
  "paths": {
    "/pets": {
      "post": {
        "description": "Creates a new pet in the store",
        "operationId": "addPet",
        "security": [
          {
            "apiKey1": [],
            "apiKey2": [],
            "apiKey3": []
          }
        ],
        "responses": {
          "200": {
            "description": "200 response"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "apiKey1": {
        "type": "apiKey",
        "name": "X-API-Key",
        "in": "header"
      },
      "apiKey2": {
        "type": "apiKey",
        "name": "X-API-Key",
        "in": "cookie"
      },
      "apiKey3": {
        "type": "apiKey",
        "name": "X-API-Key",
        "in": "query"
      }
    }
  }
}
Positive test num. 2 - yaml file
openapi: 3.0.0
info:
  title: Simple API overview
  version: 1.0.0
paths:
  /pets:
    post:
      description: Creates a new pet in the store
      operationId: addPet
      security:
        - apiKey1: []
          apiKey2: []
          apiKey3: []
      responses:
        "200":
          description: 200 response
components:
  securitySchemes:
    apiKey1:
      type: apiKey
      name: X-API-Key
      in: header
    apiKey2:
      type: apiKey
      name: X-API-Key
      in: cookie
    apiKey3:
      type: apiKey
      name: X-API-Key
      in: query
Positive test num. 3 - json file
{
  "swagger": "2.0",
  "info": {
    "title": "Simple API overview",
    "version": "1.0.0"
  },
  "paths": {
    "/pets": {
      "post": {
        "description": "Creates a new pet in the store",
        "operationId": "addPet",
        "security": [
          {
            "apiKey1": [],
            "apiKey3": []
          }
        ],
        "responses": {
          "200": {
            "description": "200 response"
          }
        }
      }
    }
  },
  "securityDefinitions": {
    "apiKey1": {
      "type": "apiKey",
      "name": "X-API-Key",
      "in": "header"
    },
    "apiKey3": {
      "type": "apiKey",
      "name": "X-API-Key",
      "in": "query"
    }
  }
}

Positive test num. 4 - yaml file
swagger: "2.0"
info:
  title: Simple API overview
  version: 1.0.0
paths:
  /pets:
    post:
      description: Creates a new pet in the store
      operationId: addPet
      security:
        - apiKey1: []
          apiKey3: []
      responses:
        "200":
          description: 200 response
securityDefinitions:
  apiKey1:
    type: apiKey
    name: X-API-Key
    in: header
  apiKey3:
    type: apiKey
    name: X-API-Key
    in: query

Code samples without security vulnerabilities

Negative test num. 1 - json file
{
  "openapi": "3.0.0",
  "info": {
    "version": "1.0.0",
    "title": "Simple API overview"
  },
  "paths": {
    "/pets": {
      "post": {
        "description": "Creates a new pet in the store",
        "operationId": "addPet",
        "security": [
          {
            "OAuth2": [
              "write",
              "read"
            ]
          }
        ],
        "responses": {
          "200": {
            "description": "200 response"
          }
        }
      }
    }
  },
  "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"
          }
        }
      }
    }
  }
}
Negative test num. 2 - yaml file
openapi: 3.0.0
info:
  title: Simple API overview
  version: 1.0.0
paths:
  /pets:
    post:
      description: Creates a new pet in the store
      operationId: addPet
      security:
        - OAuth2:
            - write
            - read
      responses:
        "200":
          description: 200 response
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
Negative test num. 3 - json file
{
  "swagger": "2.0",
  "info": {
    "version": "1.0.0",
    "title": "Simple API overview"
  },
  "paths": {
    "/pets": {
      "post": {
        "description": "Creates a new pet in the store",
        "operationId": "addPet",
        "security": [
          {
            "OAuth2": [
              "write",
              "read"
            ]
          }
        ],
        "responses": {
          "200": {
            "description": "200 response"
          }
        }
      }
    }
  },
  "securityDefinitions": {
    "OAuth2": {
      "type": "oauth2",
      "flow": "accessCode",
      "authorizationUrl": "https://example.com/oauth/authorize",
      "tokenUrl": "https://example.com/oauth/token",
      "scopes": {
        "read": "Grants read access",
        "write": "Grants write access"
      }
    }
  }
}

Negative test num. 4 - yaml file
swagger: "2.0"
info:
  title: Simple API overview
  version: 1.0.0
paths:
  /pets:
    post:
      description: Creates a new pet in the store
      operationId: addPet
      security:
        - OAuth2:
            - write
            - read
      responses:
        "200":
          description: 200 response
securityDefinitions:
  OAuth2:
    type: oauth2
    flow: accessCode
    authorizationUrl: https://example.com/oauth/authorize
    tokenUrl: https://example.com/oauth/token
    scopes:
      read: Grants read access
      write: Grants write access
Negative test num. 5 - yaml file
openapi: "3.0.1"
info:
  title: "test"
  version: "1.0"
servers:
  - url: "https://@API-GW-ID.execute-api.us-east-1.amazonaws.com/{basePath}"
    variables:
      basePath:
        default: "/api"

paths:
  /address:
    get:
      summary: test
      description: test
      security:
        - apiKey1: [ ]
        - authorizer: [ ]
      responses:
        '200':
          description: test
          content:
            text/plain:
              schema:
                type: string

components:
  securitySchemes:
    authorizer:
      type: "apiKey"
      name: "authorization"
      in: "header"