Response on operations that should not have a body has declared content (v3)

  • Query id: 12a7210b-f4b4-47d0-acac-0a819e2a0ca3
  • Query name: Response on operations that should not have a body has declared content (v3)
  • Platform: OpenAPI
  • Severity: Medium
  • Category: Networking and Firewall
  • URL: Github

Description

If a response is head or its code is 204 or 304, it shouldn't have a content defined
Documentation

Code samples

Code samples with security vulnerabilities

Postitive 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.com"
    }
  },
  "paths": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "200": {
            "description": "200 response"
          }
        }
      },
      "delete": {
        "operationId": "deleteVersion",
        "summary": "Deletes API versions",
        "responses": {
          "204": {
            "description": "has content",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/ApiVersion"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "ApiVersion": {
        "type": "object",
        "discriminator": {
          "propertyName": "ApiVersion"
        },
        "properties": {
          "code": {
            "type": "integer",
            "format": "int32"
          },
          "version": {
            "type": "string"
          }
        }
      }
    }
  }
}
Postitive test num. 2 - 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.com"
    }
  },
  "paths": {
    "/": {
      "head": {
        "operationId": "headers",
        "summary": "headers",
        "responses": {
          "200": {
            "description": "has content",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/ApiVersion"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "ApiVersion": {
        "type": "object",
        "discriminator": {
          "propertyName": "ApiVersion"
        },
        "properties": {
          "code": {
            "type": "integer",
            "format": "int32"
          },
          "version": {
            "type": "string"
          }
        }
      }
    }
  }
}
Postitive test num. 3 - yaml 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.com
paths:
  "/":
    get:
      operationId: listVersionsv2
      summary: List API versions
      responses:
        "200":
          description: 200 response
    delete:
      operationId: deleteVersion
      summary: Deletes API versions
      responses:
        "204":
          description: wrong example
          content:
            application/json:
              schema:
                "$ref": "#/components/ApiVersion"
components:
  schemas:
    ApiVersion:
      type: object
      discriminator:
        propertyName: ApiVersion
      properties:
        code:
          type: integer
          format: int32
        version:
          type: string

Postitive test num. 4 - yaml 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.com
paths:
  "/":
    head:
      operationId: headers
      summary: headers
      responses:
        "200":
          description: wrong example
          content:
            application/json:
              schema:
                "$ref": "#/components/ApiVersion"
components:
  schemas:
    ApiVersion:
      type: object
      discriminator:
        propertyName: ApiVersion
      properties:
        code:
          type: integer
          format: int32
        version:
          type: string
Postitive test num. 5 - yaml file
swagger: "2.0"
info:
  title: Simple API Overview
  version: 1.0.0
paths:
  "/":
    head:
      operationId: headers
      summary: headers
      responses:
        "200":
          description: wrong example
          schema:
            type: object
            discriminator:
              propertyName: ApiVersion
            properties:
              code:
                type: integer
                format: int32
              version:
                type: string
Postitive test num. 6 - json file
{
  "swagger": "2.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "paths": {
    "/": {
      "head": {
        "operationId": "headers",
        "summary": "headers",
        "responses": {
          "200": {
            "description": "wrong example",
            "schema": {
              "type": "object",
              "discriminator": {
                "propertyName": "ApiVersion"
              },
              "properties": {
                "code": {
                  "type": "integer",
                  "format": "int32"
                },
                "version": {
                  "type": "string"
                }
              }
            }
          }
        }
      }
    }
  }
}

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.com"
    }
  },
  "paths": {
    "/": {
      "delete": {
        "operationId": "deleteVersion",
        "summary": "Deletes API versions",
        "responses": {
          "204": {
            "description": "no content"
          }
        }
      },
      "head": {
        "operationId": "headers",
        "summary": "headers",
        "responses": {
          "200": {
            "description": "no content"
          }
        }
      }
    }
  }
}
Negative test num. 2 - yaml 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.com
paths:
  "/":
    delete:
      operationId: deleteVersion
      summary: Deletes API versions
      responses:
        "204":
          description: no content
    head:
      operationId: headers
      summary: headers
      responses:
        "200":
          description: no content
Negative test num. 3 - json file
{
  "swagger": "2.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0"
  },
  "paths": {
    "/": {
      "delete": {
        "operationId": "deleteVersion",
        "summary": "Deletes API versions",
        "responses": {
          "204": {
            "description": "no content"
          }
        }
      },
      "head": {
        "operationId": "headers",
        "summary": "headers",
        "responses": {
          "200": {
            "description": "no content"
          }
        }
      }
    }
  }
}

Negative test num. 4 - yaml file
swagger: "2.0"
info:
  title: Simple API Overview
  version: 1.0.0
paths:
  "/":
    delete:
      operationId: deleteVersion
      summary: Deletes API versions
      responses:
        "204":
          description: no content
    head:
      operationId: headers
      summary: headers
      responses:
        "200":
          description: no content