OAuth2 With Implicit Flow

  • Query id: 39cb32f2-3a42-4af0-8037-82a7a9654b6c
  • Query name: OAuth2 With Implicit Flow
  • Platform: OpenAPI
  • Severity: Medium
  • Category: Access Control
  • CWE: 284
  • URL: Github

Description

OAuth2 implicit flow is vulnerable to access token leakage and access token replay
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",
    "contact": {
      "name": "contact",
      "url": "https://www.google.com/",
      "email": "user@gmail.c"
    }
  },
  "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": {
      "api_key": {
        "type": "apiKey",
        "name": "api_key",
        "in": "header"
      },
      "petstore_auth": {
        "type": "oauth2",
        "flows": {
          "implicit": {
            "authorizationUrl": "http://example.org/api/oauth/dialog",
            "scopes": {
              "write:pets": "modify pets in your account",
              "read:pets": "read your pets"
            }
          }
        }
      }
    }
  }
}
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
components:
  securitySchemes:
    api_key:
      type: apiKey
      name: api_key
      in: header
    petstore_auth:
      type: oauth2
      flows:
        implicit:
          authorizationUrl: http://example.org/api/oauth/dialog
          scopes:
            write:pets: modify pets in your account
            read:pets: read your pets
Positive test num. 3 - 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": null
              }
            }
          }
        }
      }
    },
    "components": {
      "securitySchemes": {
        "oAuth2AuthCode": {
          "type": "oauth2",
          "description": "For more information, see https://api.my.company.com/docs/oauth",
          "flows": {
            "authorizationCode": {
              "authorizationUrl": "https://api.my.company.com/oauth/authorize",
              "tokenUrl": "https://api.my.company.com/oauth/token",
              "scopes": {
                "write:api": "modify apis in your account",
                "read:api": "read your apis"
              }
            },
            "implicit": {
              "authorizationUrl": "https://api.invalid.company.com/oauth/authorize",
              "scopes": {
                "write:api": "modify apis in your account",
                "read:api": "read your apis"
              }
            }
          }
        }
      }
    }
  }

Positive test num. 4 - 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:
components:
  securitySchemes:
    oAuth2AuthCode:
      type: oauth2
      description: For more information, see https://api.my.company.com/docs/oauth
      flows:
        authorizationCode:
          authorizationUrl: https://api.my.company.com/oauth/authorize
          tokenUrl: https://api.my.company.com/oauth/token
          scopes:
            write:api: modify apis in your account
            read:api: read your apis
        implicit:
          authorizationUrl: https://api.invalid.company.com/oauth/authorize
          scopes:
            write:api: modify apis in your account
            read:api: read your apis
Positive test num. 5 - 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:
components:
  securitySchemes:
    oAuth2AuthCode:
      type: oauth2
      description: For more information, see https://api.my.company.com/docs/oauth
      flows:
        authorizationCode:
          authorizationUrl: https://api.my.company.com/oauth/authorize
          tokenUrl: https://api.my.company.com/oauth/token
          scopes:
            write:api: modify apis in your account
            read:api: read your apis
    oAuth2AuthCode2:
      type: oauth2
      description: For more information, see https://api.my.company.com/docs/oauth
      flows:
        implicit:
          authorizationUrl: https://api.invalid.company.com/oauth/authorize
          scopes:
            write:api: modify apis in your account
            read:api: read your apis

Code samples without security vulnerabilities

Negative test num. 1 - json file
{
  "openapi": "3.0.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0",
    "contact": {
      "name": "contact",
      "url": "https://www.google.com/",
      "email": "user@gmail.c"
    }
  },
  "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": {
      "api_key": {
        "type": "apiKey",
        "name": "api_key",
        "in": "header"
      },
      "petstore_auth": {
        "type": "oauth2",
        "flows": {
          "authorizationCode": {
            "tokenUrl": "https://example.com/api/oauth/token",
            "authorizationUrl": "http://example.org/api/oauth/dialog",
            "scopes": {
              "write:pets": "modify pets in your account",
              "read:pets": "read your pets"
            }
          }
        }
      }
    }
  }
}
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:
    api_key:
      type: apiKey
      name: api_key
      in: header
    petstore_auth:
      type: oauth2
      flows:
        authorizationCode:
          tokenUrl: https://example.com/api/oauth/token
          authorizationUrl: http://example.org/api/oauth/dialog
          scopes:
            write:pets: modify pets in your account
            read:pets: read your pets