Cleartext API Key In Global Security (v3)

  • Query id: 9c238c97-1991-4c0b-9c7d-6c7912e1dc7c
  • Query name: Cleartext API Key In Global Security (v3)
  • Platform: OpenAPI
  • Severity: Medium
  • Category: Access Control
  • URL: Github

Description

API Keys should not be sent as cleartext over an unencrypted channel
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": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "200": {
            "description": "200 response",
            "content": {
              "application/json": {
                "examples": {
                  "foo": {
                    "value": {
                      "versions": [
                        {
                          "status": "CURRENT",
                          "updated": "2011-01-21T11:33:21Z",
                          "id": "v2.0",
                          "links": [
                            {
                              "href": "http://127.0.0.1:8774/v2/",
                              "rel": "self"
                            }
                          ]
                        }
                      ]
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "security": [
    {
      "apiKey2": [],
      "apiKey3": [],
      "apiKey1": []
    }
  ],
  "components": {
    "securitySchemes": {
      "apiKey1": {
        "type": "apiKey",
        "name": "X-API-Key",
        "in": "header"
      },
      "apiKey2": {
        "type": "apiKey",
        "name": "X-API-Key",
        "in": "cookie"
      },
      "apiKey3": {
        "name": "X-API-Key",
        "in": "query",
        "type": "apiKey"
      }
    }
  }
}
Positive test num. 2 - yaml file
openapi: 3.0.0
info:
  title: Simple API Overview
  version: 1.0.0
paths:
  "/":
    get:
      operationId: listVersionsv2
      summary: List API versions
      responses:
        "200":
          description: 200 response
          content:
            application/json:
              examples:
                foo:
                  value:
                    versions:
                      - status: CURRENT
                        updated: "2011-01-21T11:33:21Z"
                        id: v2.0
                        links:
                          - href: http://127.0.0.1:8774/v2/
                            rel: self
security:
  - apiKey1: []
    apiKey2: []
    apiKey3: []
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": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "200": {
            "description": "200 response"
          }
        }
      }
    }
  },
  "security": [
    {
      "apiKey3": [],
      "apiKey1": []
    }
  ],
  "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:
  "/":
    get:
      operationId: listVersionsv2
      summary: List API versions
      responses:
        "200":
          description: 200 response
security:
  - apiKey1: []
    apiKey3: []
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": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "200": {
            "description": "200 response",
            "content": {
              "application/json": {
                "examples": {
                  "foo": {
                    "value": {
                      "versions": [
                        {
                          "status": "CURRENT",
                          "updated": "2011-01-21T11:33:21Z",
                          "id": "v2.0",
                          "links": [
                            {
                              "href": "http://127.0.0.1:8774/v2/",
                              "rel": "self"
                            }
                          ]
                        }
                      ]
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "OAuth2": {
        "type": "oauth2",
        "flows": {
          "authorizationCode": {
            "authorizationUrl": "https://example.com/oauth/authorize",
            "tokenUrl": "https://example.com/oauth/token",
            "scopes": {
              "write": "modify objects in your account",
              "read": "read objects in your account"
            }
          }
        }
      }
    }
  },
  "security": [
    {
      "OAuth2": [
        "write",
        "read"
      ]
    }
  ]
}
Negative test num. 2 - yaml file
openapi: 3.0.0
info:
  title: Simple API Overview
  version: 1.0.0
paths:
  "/":
    get:
      operationId: listVersionsv2
      summary: List API versions
      responses:
        "200":
          description: 200 response
          content:
            application/json:
              examples:
                foo:
                  value:
                    versions:
                      - status: CURRENT
                        updated: "2011-01-21T11:33:21Z"
                        id: v2.0
                        links:
                          - href: http://127.0.0.1:8774/v2/
                            rel: self
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
Negative test num. 3 - json file
{
  "swagger": "2.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "200": {
            "description": "200 response"
          }
        }
      }
    }
  },
  "security": [
    {
      "OAuth2": [
        "write",
        "read"
      ]
    }
  ],
  "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:
  "/":
    get:
      operationId: listVersionsv2
      summary: List API versions
      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
security:
  - OAuth2:
      - write
      - read