Pattern for string array item is too loose

Average severity: Medium

Description

One or more arrays with items of type string in your API have too loosely defined pattern for the strings. The pattern does not actually limit what gets passed to the API.

For more details, see the OpenAPI Specification.

Example

The following is an example of how this type of risk could look in your API definition. The array accepts items of the type string but the pattern of the items is not precise enough:

{
  "parameters": [
  {
    "name": "ids",
    "in": "query",
    "description": "IDs to filter by",
    "required": true,
    "type": "array",
    "items": {
       "type": "string",
       "pattern": ".*"
     }
  }
}

Possible exploit scenario

If you define too loose pattern for strings, you do not actually limit what is accepted as the input. This could open your backend server to various attacks, like SQL injections or buffer overflows.

Remediation

Set a well-defined regular expression in the pattern field of array items. This ensures that only arrays of strings matching the set pattern get passed to your API.

For example, the API below only accepts UUIDs that are compliant with RFC 4122:

{
  "parameters": [
  {
    "name": "ids",
    "in": "query",
    "description": "IDs to filter by",
    "required": true,
    "type": "array",
    "items": {
       "type": "string",
       "pattern": "/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89AB][0-9a-f]{3}-[0-9a-f]{12}$/i" 
    } 
  } 
}

Get API Security news directly in your Inbox.

By clicking Subscribe you agree to our Data Policy