Constraining Enum Property

  • Query id: be1d8733-3731-40c7-a845-734741c6871d
  • Query name: Constraining Enum Property
  • Platform: OpenAPI
  • Severity: Info
  • Category: Best Practices
  • CWE: 20
  • URL: Github

Description

There is a constraining keyword in a property which is already restricted by enum values
Documentation

Code samples

Code samples with security vulnerabilities

Positive test num. 1 - 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"
          }
        },
        "parameters": [
          {
            "name": "category",
            "in": "body",
            "description": "max records to return",
            "required": true,
            "schema": {
              "$ref": "#/definitions/Category"
            }
          }
        ]
      }
    }
  },
  "definitions": {
    "Category": {
      "type": "object",
      "properties": {
        "id": {
          "type": "integer",
          "format": "int64",
          "minimum": 1,
          "enum": [
            2,
            3,
            4,
            5,
            6
          ]
        },
        "name": {
          "type": "string",
          "maxLength": 10,
          "enum": [
            "Foo",
            "Bar"
          ]
        }
      }
    }
  }
}
Positive test num. 2 - 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
      parameters:
      - name: category
        in: body
        description: max records to return
        required: true
        schema:
          "$ref": "#/definitions/Category"
definitions:
  Category:
    type: object
    properties:
      id:
        type: integer
        format: int64
        minimum: 1
        enum:
        - 2
        - 3
        - 4
        - 5
        - 6
      name:
        type: string
        maxLength: 10
        enum:
        - Foo
        - Bar

Code samples without security vulnerabilities

Negative test num. 1 - 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"
          }
        },
        "parameters": [
          {
            "name": "category",
            "in": "body",
            "description": "max records to return",
            "required": true,
            "schema": {
              "$ref": "#/definitions/Category"
            }
          }
        ]
      }
    }
  },
  "definitions": {
    "Category": {
      "type": "object",
      "properties": {
        "id": {
          "type": "integer",
          "format": "int64",
          "minimum": 1
        },
        "name": {
          "type": "string",
          "enum": [
            "Foo",
            "Bar"
          ]
        }
      }
    }
  }
}
Negative test num. 2 - 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
      parameters:
      - name: category
        in: body
        description: max records to return
        required: true
        schema:
          "$ref": "#/definitions/Category"
definitions:
  Category:
    type: object
    properties:
      id:
        type: integer
        format: int64
        minimum: 1
      name:
        type: string
        enum:
        - Foo
        - Bar