Schema does not define the type of JSON values

Description

The API defines a schema for a JSON payload, but the schema does not actually limit what is accepted. This can happen for two reasons:

  • The schema is for an object (a set of properties) but it does not have type set to object. The properties are matched to the ones defined in the schema, but no limit to their types is enforced.
  • Some of the properties themselves do not have type defined so any type is accepted for them.

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 schema NewPet is defined in #/definitions and referenced as the schema for the API method. However, the type of the schema is not set to object and the type is missing from the property tag:

1post:
2  description: Creates a new pet in the store
3  operationId: addPet
4  parameters:
5    name: pet
6    in: body
7    description: pet to add to the system
8    required: true
9    schema:
10      $ref: "#/definitions/NewPet"
11# ...
12NewPet:
13  required:
14    - name
15  properties:
16    name:
17      description: Pet name
18      type: string
19    tag:
20      description: Pet tag
21

Possible exploit scenario

If you do not clearly define the schema and you leave properties of a JSON payload empty, you effectively allow attackers to pass in any data. This means that you are opening your backend to various attacks, such as SQL injection.

This also lets attackers to try various unexpected inputs. Unexpected inputs may cause the backend server to crash or behave in an unexpected way. This in turn may cause the server to potentially leak stack trace that can be used for further attacks, or even data.

If no restrictions to the set of properties in the JSON payload are enforced, the API might also accept more fields than expected. The received payloads could be blindly transformed into an object and stored, overwriting sensitive internal data. For more details, see API6:2019 — Mass assignment in OWASP API Security Top 10.

Remediation

Make sure you define the types of all properties in the JSON payload and correctly set the type for objects to enforce limitations to what the schema accepts.

1post:
2  description: Creates a new pet in the store
3  operationId: addPet
4  parameters:
5    name: pet
6    in: body
7    description: pet to add to the system
8    required: true
9    schema:
10      $ref: "#/definitions/NewPet"
11# ...
12NewPet:
13  type: object
14  required:
15    - name
16  properties:
17    name:
18      type: string
19      description: Pet name
20    tag:
21      type: string
22      description: Pet tag
23

Copyright 42Crunch 2021