Required Property With Default Value (v3)

  • Query id: 013bdb4b-9246-4248-b0c3-7fb0fee42a29
  • Query name: Required Property With Default Value (v3)
  • Platform: OpenAPI
  • Severity: Info
  • Category: Best Practices
  • CWE: 710
  • URL: Github

Description

Required properties receive value from requests, which makes unnecessary declare a default value
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": {
            "$ref": "#/components/schemas/MyObject"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "MyObject": {
        "type": "object",
        "required": [
          "id"
        ],
        "properties": {
          "id": {
            "type": "string",
            "default": "4056684e4e1347579362617ad82e5b4e"
          },
          "name": {
            "type": "string",
            "default": "guest"
          }
        }
      }
    }
  }
}
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",
        "responses": {
          "200": {
            "description": "success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": [
                    "id"
                  ],
                  "properties": {
                    "id": {
                      "type": "string",
                      "default": "4056684e4e1347579362617ad82e5b4e"
                    },
                    "name": {
                      "type": "string",
                      "default": "guest"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
Positive test num. 3 - 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':
          "$ref": "#/components/schemas/MyObject"
components:
  schemas:
    MyObject:
      type: object
      required:
      - id
      properties:
        id:
          type: string
          default: 4056684e4e1347579362617ad82e5b4e
        name:
          type: string
          default: guest

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: success
          content:
            application/json:
              schema:
                type: object
                required:
                - id
                properties:
                  id:
                    type: string
                    default: 4056684e4e1347579362617ad82e5b4e
                  name:
                    type: string
                    default: guest
Positive test num. 5 - json file
{
  "swagger": "2.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "paths": {
    "/": {
      "post": {
        "summary": "Add a new item",
        "parameters": [
          {
            "in": "body",
            "name": "item",
            "schema": {
              "type": "object",
              "required": [
                "id"
              ],
              "properties": {
                "id": {
                  "type": "string",
                  "default": "4056684e4e1347579362617ad82e5b4e"
                },
                "name": {
                  "type": "string",
                  "default": "guest"
                }
              }
            }
          }
        ],
        "responses": {
          "200": {
            "description": "200 response"
          }
        }
      }
    }
  }
}
Positive test num. 6 - yaml file
swagger: "2.0"
info:
  title: Simple API Overview
  version: 1.0.0
paths:
  "/":
    post:
      summary: Add a new item
      parameters:
        - in: body
          name: item
          schema:
            type: object
            required:
              - id
            properties:
              id:
                type: string
                default: 4056684e4e1347579362617ad82e5b4e
              name:
                type: string
                default: guest
      responses:
        "200":
          description: 200 response

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": {
            "$ref": "#/components/schemas/MyObject"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "MyObject": {
        "type": "object",
        "required": [
          "id"
        ],
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string",
            "default": "guest"
          }
        }
      }
    }
  }
}
Negative 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",
        "responses": {
          "200": {
            "description": "success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": [
                    "id"
                  ],
                  "properties": {
                    "id": {
                      "type": "string"
                    },
                    "name": {
                      "type": "string",
                      "default": "guest"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
Negative test num. 3 - 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':
          "$ref": "#/components/schemas/MyObject"
components:
  schemas:
    MyObject:
      type: object
      required:
      - id
      properties:
        id:
          type: string
        name:
          type: string
          default: guest

Negative 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: success
          content:
            application/json:
              schema:
                type: object
                required:
                - id
                properties:
                  id:
                    type: string
                  name:
                    type: string
                    default: guest
Negative test num. 5 - json file
{
  "swagger": "2.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "paths": {
    "/": {
      "post": {
        "summary": "Add a new item",
        "responses": {
          "200": {
            "description": "200 response"
          }
        },
        "parameters": [
          {
            "in": "body",
            "name": "item",
            "schema": {
              "type": "object",
              "required": [
                "id"
              ],
              "properties": {
                "id": {
                  "type": "string"
                },
                "name": {
                  "type": "string",
                  "default": "guest"
                }
              }
            }
          }
        ]
      }
    }
  }
}
Negative test num. 6 - yaml file
swagger: "2.0"
info:
  title: Simple API Overview
  version: 1.0.0
paths:
  "/":
    post:
      summary: Add a new item
      responses:
        "200":
          description: 200 response
      parameters:
        - in: body
          name: item
          schema:
            type: object
            required:
              - id
            properties:
              id:
                type: string
              name:
                type: string
                default: guest