Security Field On Operations Has An Empty Array (v3)

  • Query id: 663c442d-f918-4f62-b096-0bf5dcbeb655
  • Query name: Security Field On Operations Has An Empty Array (v3)
  • Platform: OpenAPI
  • Severity: High
  • Category: Access Control
  • URL: Github

Description

Security object for operations, if defined, must define a security scheme, otherwise it should be considered an error
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",
        "security": [],
        "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": [
    {
      "OAuth2": [
        "read"
      ]
    }
  ]
}
Positive test num. 2 - json file
{
  "openapi": "3.0.0",
  "info": {
    "title": "Simple API overview",
    "version": "1.0.0"
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "security": [
          {
            "OAuth2": [
              "admin"
            ]
          }
        ],
        "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"
                            }
                          ]
                        }
                      ]
                    }
                  }
                }
              }
            }
          }
        }
      },
      "patch": {
        "operationId": "validateVersionsPathv2",
        "summary": "Validate operation",
        "security": [],
        "responses": {
          "204": {
            "description": "204 response"
          }
        }
      }
    }
  },
  "security": [
    {
      "OAuth2": [
        "read"
      ]
    }
  ]
}
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",
        "security": [
          {
            "OAuth2": [
              "admin"
            ]
          }
        ],
        "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"
                            }
                          ]
                        }
                      ]
                    }
                  }
                }
              }
            }
          }
        }
      },
      "patch": {
        "operationId": "validateVersionsPathv2",
        "summary": "Validate operation",
        "security": [],
        "responses": {
          "204": {
            "description": "204 response"
          }
        }
      }
    }
  },
  "security": [
    {
      "OAuth2": [
        "read"
      ]
    }
  ]
}

Positive test num. 4 - json file
{
  "openapi": "3.0.0",
  "info": {
    "title": "Simple API overview",
    "version": "1.0.0"
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "security": [
          {
            "OAuth2": [
              "admin"
            ]
          }
        ],
        "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"
                            }
                          ]
                        }
                      ]
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/apis": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "security": [],
        "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": [
    {
      "OAuth2": [
        "read"
      ]
    }
  ]
}
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
      security: []
      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:
- OAuth2:
  - read
Positive test num. 6 - yaml file
openapi: 3.0.0
info:
  title: Simple API overview
  version: 1.0.0
paths:
  "/":
    get:
      operationId: listVersionsv2
      summary: List API versions
      security:
      - OAuth2:
        - admin
      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
    patch:
      operationId: validateVersionsPathv2
      summary: Validate operation
      security: []
      responses:
        '204':
          description: 204 response
security:
- OAuth2:
  - read
Positive test num. 7 - yaml file
openapi: 3.0.0
info:
  title: Simple API overview
  version: 1.0.0
paths:
  "/":
    get:
      operationId: listVersionsv2
      summary: List API versions
      security:
      - OAuth2:
        - admin
      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
    patch:
      operationId: validateVersionsPathv2
      summary: Validate operation
      security: []
      responses:
        '204':
          description: 204 response
security:
- OAuth2:
  - read
Positive test num. 8 - yaml file
openapi: 3.0.0
info:
  title: Simple API overview
  version: 1.0.0
paths:
  "/":
    get:
      operationId: listVersionsv2
      summary: List API versions
      security:
      - OAuth2:
        - admin
      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
  "/apis":
    get:
      operationId: listVersionsv2
      summary: List API versions
      security: []
      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:
- OAuth2:
  - read
Positive test num. 9 - yaml file
swagger: "2.0"
info:
  title: Simple API Overview
  version: 1.0.0
  contact:
    name: contact
    url: https://www.google.com/
    email: user@gmail.com
paths:
  "/":
    get:
      operationId: listVersionsv2
      summary: List API versions
      security: []
      responses:
        "200":
          description: 200 response
          schema:
            type: object
            properties:
              code:
                type: integer
                format: int32
              message:
                type: string
            required:
              - code
              - message
Positive test num. 10 - json file
{
  "swagger": "2.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0",
    "contact": {
      "name": "contact",
      "url": "https://www.google.com/",
      "email": "user@gmail.com"
    }
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "security": [],
        "responses": {
          "200": {
            "description": "200 response",
            "schema": {
              "type": "object",
              "properties": {
                "code": {
                  "type": "integer",
                  "format": "int32"
                },
                "message": {
                  "type": "string"
                }
              },
              "required": [
                "code",
                "message"
              ]
            }
          }
        }
      }
    }
  }
}

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",
        "security": [
          {
            "OAuth2": [
              "write"
            ]
          }
        ],
        "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": [
    {
      "OAuth2": [
        "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
      security:
        - OAuth2:
            - write
      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:
  - OAuth2:
      - read
Negative test num. 3 - yaml file
swagger: "2.0"
info:
  title: Simple API Overview
  version: 1.0.0
  contact:
    name: contact
    url: https://www.google.com/
    email: user@gmail.com
paths:
  "/":
    get:
      operationId: listVersionsv2
      summary: List API versions
      security:
        - OAuth2:
            - write
      responses:
        "200":
          description: 200 response
          schema:
            type: object
            properties:
              code:
                type: integer
                format: int32
              message:
                type: string
            required:
              - code
              - message

Negative test num. 4 - json file
{
  "swagger": "2.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0",
    "contact": {
      "name": "contact",
      "url": "https://www.google.com/",
      "email": "user@gmail.com"
    }
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "security": [
          {
            "OAuth2": [
              "write"
            ]
          }
        ],
        "responses": {
          "200": {
            "description": "200 response",
            "schema": {
              "type": "object",
              "properties": {
                "code": {
                  "type": "integer",
                  "format": "int32"
                },
                "message": {
                  "type": "string"
                }
              },
              "required": [
                "code",
                "message"
              ]
            }
          }
        }
      }
    }
  }
}