String schema has no maximum length defined

Average severity: Medium

Description

A string schema does not specify the maximum length for the accepted strings.

Example

The following is an example of how this type of risk could look in your API definition:

"post": {
  "description": "Creates a new pet in the store",
  "operationId": "addPet",
  "parameters": [
    {
      "name": "pet",
      "in": "body",
      "description": "Pet to add to the store",
      "required": true,
      "schema": {
        "type": "object",
        "required": [
          "name"
        ],
        "properties": {
        "name": {
          "type": "string"
        }       
      }
    }
   ],

Possible exploit scenario

If you do not limit the length of strings, attackers can send longer strings to your API than what your backend server can handle. This could overload your backend server and make it crash. In some cases, this could cause a buffer overflow and allow for executing arbitrary code. Long strings are also more prone to injection attacks.

Remediation

Set the maxLength property in string schemas to ensure that only strings of the expected size get passed to your API:

"post": {
  "description": "Creates a new pet in the store",
  "operationId": "addPet",
  "parameters": [
    {
      "name": "pet",
      "in": "body",
      "description": "Pet to add to the store",
      "required": true,
      "schema": {
        "type": "object",
        "required": [
          "name"
        ],
        "properties": {
        "name": {
          "type": "string",
          "maxLength": "10"
        }       
      }
    }
   ],

We also recommend that you specify a regular expression to further lock down the expected string format.


Get API Security news directly in your Inbox.

By clicking Subscribe you agree to our Data Policy