Header JSON Reference Does Not Exists

  • Query id: 376c9390-7e9e-4cb8-a067-fd31c05451fd
  • Query name: Header JSON Reference Does Not Exists
  • Platform: OpenAPI
  • Severity: Info
  • Category: Structure and Semantics
  • URL: Github

Description

Header reference should exists on components field
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": {
            "description": "success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/MyObject"
                  }
                },
                "headers": {
                  "X-Pages": {
                    "$ref": "#/components/headers/wPages"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "MyObject": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          }
        }
      }
    },
    "headers": {
      "xPages": {
        "schema": {
          "type": "integer",
          "description": "number of pages"
        }
      }
    }
  }
}
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: success
          content:
            application/json:
              schema:
                type: array
                items:
                  "$ref": "#/components/schemas/MyObject"
              headers:
                X-Pages:
                  "$ref": "#/components/headers/wPages"
components:
  schemas:
    MyObject:
      type: object
      properties:
        id:
          type: string
        name:
          type: string
  headers:
    xPages:
      schema:
        type: integer
        description: number of pages

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": {
            "description": "success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/MyObject"
                  }
                },
                "headers": {
                  "X-Pages": {
                    "$ref": "#/components/headers/xPages"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "MyObject": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "name": {
            "type": "string"
          }
        }
      }
    },
    "headers": {
      "xPages": {
        "schema": {
          "type": "integer",
          "description": "number of pages"
        }
      }
    }
  }
}
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: success
          content:
            application/json:
              schema:
                type: array
                items:
                  "$ref": "#/components/schemas/MyObject"
              headers:
                X-Pages:
                  "$ref": "#/components/headers/xPages"
components:
  schemas:
    MyObject:
      type: object
      properties:
        id:
          type: string
        name:
          type: string
  headers:
    xPages:
      schema:
        type: integer
        description: number of pages