Invalid Media Type Value (v3)

  • Query id: cf4a5f45-a27b-49df-843a-9911dbfe71d4
  • Query name: Invalid Media Type Value (v3)
  • Platform: OpenAPI
  • Severity: Info
  • Category: Best Practices
  • CWE: 710
  • URL: Github

Description

The Media Type value should match the following format: <type>/<subtype>[+suffix][;parameters]
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": "200 response",
            "content": {
              "application/json": {
                "encoding": {
                  "code": {
                    "contentType": "image/png, image/jpeg"
                  }
                }
              }
            }
          }
        },
        "requestBody": {
          "content": {
            "multipart/form- data": {
              "encoding": {
                "code": {
                  "contentType": "image/png, image/jpeg"
                }
              }
            }
          }
        }
      }
    }
  }
}
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: 200 response
          content:
            application/json:
              encoding:
                code:
                  contentType: image/png, image/jpeg
      requestBody:
        content:
          multipart/form- data:
            encoding:
              code:
                contentType: image/png, image/jpeg

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": "200 response",
            "content": {
              "application/json": {
                "encoding": {
                  "code": {
                    "contentType": "image/png, image/jpeg"
                  }
                },
                "schema": {
                  "properties": {
                    "code": {
                      "type": "string",
                      "format": "binary"
                    },
                    "message": {
                      "type": "string"
                    }
                  },
                  "type": "object",
                  "discriminator": {
                    "propertyName": "petType"
                  }
                }
              }
            }
          }
        },
        "requestBody": {
          "content": {
            "multipart/form-data": {
              "schema": {
                "type": "string",
                "format": "binary",
                "properties": {
                  "code": {
                    "type": "string",
                    "format": "binary"
                  }
                }
              },
              "encoding": {
                "code": {
                  "contentType": "image/png, image/jpeg"
                }
              }
            }
          }
        }
      }
    }
  }
}
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: 200 response
          content:
            application/json:
              schema:
                type: object
                discriminator:
                  propertyName: petType
                properties:
                  code:
                    type: string
                    format: binary
                  message:
                    type: string
              encoding:
                code:
                  contentType: image/png, image/jpeg
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: string
              format: binary
              properties:
                code:
                  type: string
                  format: binary
            encoding:
              code:
                contentType: image/png, image/jpeg
Negative test num. 3 - yaml file
openapi: 3.0.0
info:
  title: Pet Store API
  version: 1.0.0
servers:
  - url: https://api.example.com/v1
paths:
  /pets:
    get:
      summary: Get a list of pets
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              example: { "pets": ["dog", "cat"] }
    post:
      summary: Add a new pet
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                name:
                  type: string
                  example: "Fido"
                  pattern: "^[A-Za-z]{1,20}$"
                species:
                  type: string
                  example: "dog"
                color:
                  type: string
                  enum:
                    - brown
                    - grey
                    - black
                    - white
                birthDate:
                  type: string
                  format: date
                weight:
                  type: integer
                  format: int32
                someSubType:
                  type: 'object'
                  properties:
                    content:
                      type: string
      responses:
        '201':
          description: Pet added successfully
  /pets/{petId}:
    get:
      summary: Get details of a specific pet
      parameters:
        - name: petId
          in: path
          required: true
          schema:
            type: integer
            format: int64
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              example: { "name": "Fido", "species": "dog" }

Negative test num. 4 - json file
{
  "openapi": "3.0.0",
  "info": {
    "title": "Pet Store API",
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "https://api.example.com/v1"
    }
  ],
  "paths": {
    "/pets": {
      "get": {
        "summary": "Get a list of pets",
        "responses": {
          "200": {
            "description": "Successful response",
            "content": {
              "application/json": {
                "example": { "pets": ["dog", "cat"] }
              }
            }
          }
        }
      },
      "post": {
        "summary": "Add a new pet",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "name": {
                    "type": "string",
                    "example": "Fido",
                    "pattern": "^[A-Za-z]{1,20}$"
                  },
                  "species": {
                    "type": "string",
                    "example": "dog"
                  },
                  "color": {
                    "type": "string",
                    "enum": [
                      "brown",
                      "grey",
                      "black",
                      "white"
                    ]
                  },
                  "birthDate": {
                    "type": "string",
                    "format": "date"
                  },
                  "weight": {
                    "type": "integer",
                    "format": "int32"
                  },
                  "someSubType": {
                    "type": "object",
                    "properties": {
                      "content": {
                        "type": "string"
                      }
                    }
                  }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Pet added successfully"
          }
        }
      }
    },
    "/pets/{petId}": {
      "get": {
        "summary": "Get details of a specific pet",
        "parameters": [
          {
            "name": "petId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "integer",
              "format": "int64"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Successful response",
            "content": {
              "application/json": {
                "example": { "name": "Fido", "species": "dog" }
              }
            }
          }
        }
      }
    }
  }
}
Negative test num. 5 - yaml file
openapi: 3.0.0
info:
  title: Pet Store API
  version: 1.0.0
servers:
  - url: https://api.example.com/v1
paths:
  /pets:
    get:
      summary: Get a list of pets
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              example: { "pets": ["dog", "cat"] }
    post:
      summary: Add a new pet
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                name:
                  type: string
                  example: "Fido"
                  pattern: "^[A-Za-z]{1,20}$"
                species:
                  type: string
                  example: "dog"
                color:
                  type: string
                  enum:
                    - brown
                    - grey
                    - black
                    - white
                birthDate:
                  type: string
                  format: date
                weight:
                  type: integer
                  format: int32
                someSubType:
                  type: 'object'
                  properties:
                    content:
                      type: 'object'
                      properties:
                        content:
                          type: object
                          properties:
                            key1:
                              type: string
                            key2:
                              type: integer
      responses:
        '201':
          description: Pet added successfully
  /pets/{petId}:
    get:
      summary: Get details of a specific pet
      parameters:
        - name: petId
          in: path
          required: true
          schema:
            type: integer
            format: int64
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              example: { "name": "Fido", "species": "dog" }
Negative test num. 6 - json file
{
  "openapi": "3.0.0",
  "info": {
    "title": "Pet Store API",
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "https://api.example.com/v1"
    }
  ],
  "paths": {
    "/pets": {
      "get": {
        "summary": "Get a list of pets",
        "responses": {
          "200": {
            "description": "Successful response",
            "content": {
              "application/json": {
                "example": { "pets": ["dog", "cat"] }
              }
            }
          }
        }
      },
      "post": {
        "summary": "Add a new pet",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "name": {
                    "type": "string",
                    "example": "Fido",
                    "pattern": "^[A-Za-z]{1,20}$"
                  },
                  "species": {
                    "type": "string",
                    "example": "dog"
                  },
                  "color": {
                    "type": "string",
                    "enum": ["brown", "grey", "black", "white"]
                  },
                  "birthDate": {
                    "type": "string",
                    "format": "date"
                  },
                  "weight": {
                    "type": "integer",
                    "format": "int32"
                  },
                  "someSubType": {
                    "type": "object",
                    "properties": {
                      "content": {
                        "type": "object",
                        "properties": {
                          "content": {
                            "type": "object",
                            "properties": {
                              "key1": {
                                "type": "string"
                              },
                              "key2": {
                                "type": "integer"
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Pet added successfully"
          }
        }
      }
    },
    "/pets/{petId}": {
      "get": {
        "summary": "Get details of a specific pet",
        "parameters": [
          {
            "name": "petId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "integer",
              "format": "int64"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Successful response",
            "content": {
              "application/json": {
                "example": { "name": "Fido", "species": "dog" }
              }
            }
          }
        }
      }
    }
  }
}