Pattern Undefined (v3)

  • Query id: 00b78adf-b83f-419c-8ed8-c6018441dd3a
  • Query name: Pattern Undefined (v3)
  • Platform: OpenAPI
  • Severity: Medium
  • Category: Insecure Configurations
  • CWE: 20
  • URL: Github

Description

String schema should have 'pattern' defined.
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",
    "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",
            "content": {
              "application/json": {
                "examples": {
                  "foo": {
                    "value": {
                      "versions": [
                        {
                          "status": "CURRENT",
                          "updated": "2011-01-21T11:33:21Z",
                          "id": "v2.0",
                          "links": [
                            {
                              "href": "http://127.0.0.1:8774/v2/",
                              "rel": "self"
                            }
                          ]
                        }
                      ]
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "GeneralError": {
        "type": "object",
        "discriminator": {
          "propertyName": "petType"
        },
        "additionalProperties": false,
        "properties": {
          "code": {
            "type": "string",
            "maxLength": 15,
            "format": "int32"
          },
          "message": {
            "type": "string",
            "maxLength": 15
          }
        },
        "required": [
          "petType"
        ]
      }
    },
    "requestBodies": {
      "NewItem": {
        "description": "A JSON object containing item data",
        "required": true,
        "content": {
          "multipart/form-data": {
            "schema": {
              "$ref": "#/components/schemas/GeneralError"
            }
          }
        }
      }
    }
  }
}
Positive 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": {
    "/": {
      "get": {
        "responses": {
          "200": {
            "description": "200 response",
            "content": {
              "application/json": {
                "schema": {
                  "discriminator": {
                    "propertyName": "petType"
                  },
                  "additionalProperties": false,
                  "properties": {
                    "code": {
                      "type": "string",
                      "maxLength": 15,
                      "format": "int32"
                    },
                    "message": {
                      "type": "string",
                      "maxLength": 15
                    }
                  },
                  "required": [
                    "petType"
                  ],
                  "type": "object"
                },
                "examples": {
                  "foo": {
                    "value": {
                      "versions": [
                        {
                          "status": "CURRENT",
                          "updated": "2011-01-21T11:33:21Z",
                          "id": "v2.0",
                          "links": [
                            {
                              "href": "http://127.0.0.1:8774/v2/",
                              "rel": "self"
                            }
                          ]
                        }
                      ]
                    }
                  }
                }
              }
            }
          }
        },
        "operationId": "listVersionsv2",
        "summary": "List API versions"
      }
    }
  }
}
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":
          description: 200 response
          content:
            application/json:
              examples:
                foo:
                  value:
                    versions:
                      - status: CURRENT
                        updated: "2011-01-21T11:33:21Z"
                        id: v2.0
                        links:
                          - href: http://127.0.0.1:8774/v2/
                            rel: self
components:
  schemas:
    GeneralError:
      type: object
      discriminator:
        propertyName: petType
      additionalProperties: false
      properties:
        code:
          type: string
          maxLength: 15
          format: int32
        message:
          type: string
          maxLength: 15
      required:
        - petType
  requestBodies:
    NewItem:
      description: A JSON object containing item data
      required: true
      content:
        multipart/form-data:
          schema:
            $ref: "#/components/schemas/GeneralError"

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: 200 response
          content:
            application/json:
              schema:
                type: object
                discriminator:
                  propertyName: petType
                additionalProperties: false
                properties:
                  code:
                    type: string
                    maxLength: 15
                    format: int32
                  message:
                    type: string
                    maxLength: 15
              examples:
                foo:
                  value:
                    versions:
                      - status: CURRENT
                        updated: "2011-01-21T11:33:21Z"
                        id: v2.0
                        links:
                          - href: http://127.0.0.1:8774/v2/
                            rel: self
Positive test num. 5 - json file
{
  "swagger": "2.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0",
    "contact": {
      "name": "contact",
      "url": "https://www.google.com/",
      "email": "user@gmail.com"
    }
  },
  "paths": {
    "/": {
      "get": {
        "responses": {
          "200": {
            "description": "200 response",
            "schema": {
              "discriminator": "petType",
              "additionalProperties": false,
              "properties": {
                "code": {
                  "type": "string",
                  "maxLength": 15,
                  "format": "int32"
                },
                "message": {
                  "type": "string",
                  "maxLength": 15
                }
              },
              "required": [
                "petType"
              ],
              "type": "object"
            }
          }
        }
      }
    }
  }
}
Positive test num. 6 - 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
          schema:
            type: object
            discriminator: petType
            additionalProperties: false
            properties:
              code:
                type: string
                maxLength: 15
                format: int32
              message:
                type: string
                maxLength: 15

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": {
    "/": {
      "get": {
        "operationId": "listVersionsv2",
        "summary": "List API versions",
        "responses": {
          "200": {
            "description": "200 response",
            "content": {
              "application/json": {
                "examples": {
                  "foo": {
                    "value": {
                      "versions": [
                        {
                          "status": "CURRENT",
                          "updated": "2011-01-21T11:33:21Z",
                          "id": "v2.0",
                          "links": [
                            {
                              "href": "http://127.0.0.1:8774/v2/",
                              "rel": "self"
                            }
                          ]
                        }
                      ]
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "GeneralError": {
        "type": "object",
        "discriminator": {
          "propertyName": "petType"
        },
        "additionalProperties": false,
        "properties": {
          "code": {
            "type": "string",
            "maxLength": 15,
            "format": "int32",
            "pattern": "^[0-9a-z]{15}$"
          },
          "message": {
            "type": "string",
            "maxLength": 15,
            "pattern": "^[0-9a-z]{15}$"
          }
        },
        "required": [
          "petType"
        ]
      }
    },
    "requestBodies": {
      "NewItem": {
        "description": "A JSON object containing item data",
        "required": true,
        "content": {
          "multipart/form-data": {
            "schema": {
              "$ref": "#/components/schemas/GeneralError"
            }
          }
        }
      }
    }
  }
}
Negative 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": {
    "/": {
      "get": {
        "responses": {
          "200": {
            "description": "200 response",
            "content": {
              "application/json": {
                "schema": {
                  "discriminator": {
                    "propertyName": "petType"
                  },
                  "additionalProperties": false,
                  "properties": {
                    "code": {
                      "type": "string",
                      "maxLength": 15,
                      "format": "int32",
                      "pattern": "^[0-9a-z]{15}$"
                    },
                    "message": {
                      "type": "string",
                      "maxLength": 15,
                      "pattern": "^[0-9a-z]{15}$"
                    }
                  },
                  "required": [
                    "petType"
                  ],
                  "type": "object"
                },
                "examples": {
                  "foo": {
                    "value": {
                      "versions": [
                        {
                          "status": "CURRENT",
                          "updated": "2011-01-21T11:33:21Z",
                          "id": "v2.0",
                          "links": [
                            {
                              "href": "http://127.0.0.1:8774/v2/",
                              "rel": "self"
                            }
                          ]
                        }
                      ]
                    }
                  }
                }
              }
            }
          }
        },
        "operationId": "listVersionsv2",
        "summary": "List API versions"
      }
    }
  }
}
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":
          description: 200 response
          content:
            application/json:
              examples:
                foo:
                  value:
                    versions:
                      - status: CURRENT
                        updated: "2011-01-21T11:33:21Z"
                        id: v2.0
                        links:
                          - href: http://127.0.0.1:8774/v2/
                            rel: self
components:
  schemas:
    GeneralError:
      type: object
      discriminator:
        propertyName: petType
      additionalProperties: false
      properties:
        code:
          type: string
          maxLength: 15
          format: int32
          pattern: ^[0-9a-z]{15}$
        message:
          type: string
          maxLength: 15
          pattern: ^[0-9a-z]{15}$
      required:
        - petType
  requestBodies:
    NewItem:
      description: A JSON object containing item data
      required: true
      content:
        multipart/form-data:
          schema:
            $ref: "#/components/schemas/GeneralError"

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: 200 response
          content:
            application/json:
              schema:
                type: object
                discriminator:
                  propertyName: petType
                additionalProperties: false
                properties:
                  code:
                    type: string
                    maxLength: 15
                    format: int32
                    pattern: ^[0-9a-z]{15}$
                  message:
                    type: string
                    maxLength: 15
                    pattern: ^[0-9a-z]{15}$
              examples:
                foo:
                  value:
                    versions:
                      - status: CURRENT
                        updated: "2011-01-21T11:33:21Z"
                        id: v2.0
                        links:
                          - href: http://127.0.0.1:8774/v2/
                            rel: self
Negative test num. 5 - json file
{
  "swagger": "2.0",
  "info": {
    "title": "Simple API Overview",
    "version": "1.0.0",
    "contact": {
      "name": "contact",
      "url": "https://www.google.com/",
      "email": "user@gmail.com"
    }
  },
  "paths": {
    "/": {
      "get": {
        "responses": {
          "200": {
            "description": "200 response",
            "schema": {
              "discriminator": "petType",
              "additionalProperties": false,
              "properties": {
                "code": {
                  "type": "string",
                  "maxLength": 15,
                  "format": "int32",
                  "pattern": "^[0-9a-z]{15}$"
                },
                "message": {
                  "type": "string",
                  "maxLength": 15,
                  "pattern": "^[0-9a-z]{15}$"
                }
              },
              "required": [
                "petType"
              ],
              "type": "object"
            }
          }
        }
      }
    }
  }
}
Negative test num. 6 - 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
          schema:
            type: object
            discriminator: petType
            additionalProperties: false
            properties:
              code:
                type: string
                maxLength: 15
                format: int32
                pattern: ^[0-9a-z]{15}$
              message:
                type: string
                maxLength: 15
                pattern: ^[0-9a-z]{15}$