stoplightio / prism

Turn any OpenAPI2/3 and Postman Collection file into an API server with mocking, transformations and validations.
https://stoplight.io/open-source/prism
Apache License 2.0
4.2k stars 344 forks source link

Prism fails to send a response if validation pattern contains non-ascii characters #1934

Open RomanKornev opened 2 years ago

RomanKornev commented 2 years ago

Describe the bug

Prism fails to send a response if specification's validation pattern contains non-ascii characters. Request is stuck forever.

To Reproduce

  1. Given this OpenAPI document spec.yaml
    swagger: '2.0'
    info:
    title: bug
    version: 1.0.2
    schemes:
    - https
    basePath: /
    produces:
    - application/json
    consumes:
    - application/json
    definitions:
    GenericError:
    type: object
    required:
      - httpCode
    properties:
      httpCode:
        type: string
        example: '400'
        maxLength: 3
    additionalProperties: false
    Info:
    additionalProperties: false
    properties:
      city:
        type: string
        pattern: '^[А-ЯЁа-яё]+([- ][А-ЯЁа-яё]+)*$'
    type: object
    paths:
    /:
    post:
      consumes:
        - application/json
      produces:
        - application/json
      parameters:
        - in: body
          name: body
          description: body
          required: true
          schema:
            $ref: '#/definitions/Info'
      responses:
        '422':
          schema:
            $ref: '#/definitions/GenericError'
      operationId: create
  2. Run this CLI command prism mock spec.yaml
  3. Send POST to localhost:4010
    curl --request POST \
    --url http://localhost:4010/ \
    --header 'accept: application/json' \
    --header 'content-type: application/json' \
    --data '{
    "city": "fdgfg"
    }'
  4. Request is stuck forever
  5. See error message in logs
    [10:29:45 AM] › [HTTP SERVER] post /applications ℹ  info      Request received
    [10:29:45 AM] ›     [NEGOTIATOR] ℹ  info      Request contains an accept header: application/json
    [10:29:45 AM] ›     [VALIDATOR] ⚠  warning   Request did not pass the validation rules
    [10:29:45 AM] ›     [NEGOTIATOR] ✔  success   Found response 422. I'll try with it.
    [10:29:45 AM] ›     [NEGOTIATOR] ⬤  debug     Unable to find a content with an example defined for the response 422
    [10:29:45 AM] ›     [NEGOTIATOR] ✔  success   The response 422 has a schema. I'll keep going with this one
    [10:29:45 AM] ›     [NEGOTIATOR] ✔  success   Responding with the requested status code 422
    (node:22298) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_CHAR]: Invalid character in header content ["sl-violations"]
    at ServerResponse.setHeader (_http_outgoing.js:561:3)
    at /usr/local/lib/node_modules/@stoplight/prism-cli/node_modules/@stoplight/prism-http-server/dist/server.js:69:23
    at /usr/local/lib/node_modules/@stoplight/prism-cli/node_modules/fp-ts/lib/function.js:184:30
    at /usr/local/lib/node_modules/@stoplight/prism-cli/node_modules/fp-ts/lib/EitherT.js:52:116
    at /usr/local/lib/node_modules/@stoplight/prism-cli/node_modules/fp-ts/lib/Task.js:142:37
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    (node:22298) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 6)

Expected behavior

An error message response was sent to the client.

Environment (remove any that are not applicable):

Lingihn commented 11 months ago

I confirm the problem When receiving JSON with non-acsii characters from the server, For example, something like this [ { "id": 1, "short_name": "Техноэнерго", "name": "Техноэнерго 1 1 ", "series": "1", "type": "1", "number_of_phases": 3, "type_of_inclusion": "прямого", "rated_current": "1(10)", "execution": "электронный", "reporting_device": "ЖКИ", "energy_type": "активный", "number_of_directions": "однонаправленный", "accuracy_class": "1", "value_before_comma": 6, "zero_discharges_before_comma": 0, "value_after_comma": 1, "zero_discharges_after_comma": 0, "intertest_interval": 16, "form_factor_body": "на щиток", "stores_load_profile": 0, "price_category": "только 1ЦК", "meets_isu_requirements": 0, "possibility_using_oreim": 0, "offer_sitel": 0, "description": "Может служить в качестве ОДПУ. После устранения замечаний, потребителей с ", "ordinal_number": 14, "end_at": null, "status": 1, "interface": "[{\"id\":1,\"name\":\"оптический порт2\"}]", "created_at": "2022-08-25T19:28:06Z", "created_by": 1, "created_by_name": "Тестов Тестер Тестерович", "updated_at": "2023-01-23T09:23:31Z", "updated_by": 3, "updated_by_name": "Белов Максим Александрович" },

- Prism proxy crashes with the following error:

TypeError: Invalid character in header content ["sl-violations"]
    at ServerResponse.setHeader (node:_http_outgoing:606:3)
    at addViolationHeader (C:\Users\Offlu\AppData\Roaming\nvm\v16.17.0\node_modules\@stoplight\prism-cli\node_modules\@stoplight\prism-http-server\dist\server.js:39:11)
    at C:\Users\Offlu\AppData\Roaming\nvm\v16.17.0\node_modules\@stoplight\prism-cli\node_modules\@stoplight\prism-http-server\dist\server.js:77:17
    at C:\Users\Offlu\AppData\Roaming\nvm\v16.17.0\node_modules\@stoplight\prism-cli\node_modules\fp-ts\lib\function.js:197:30
    at C:\Users\Offlu\AppData\Roaming\nvm\v16.17.0\node_modules\@stoplight\prism-cli\node_modules\fp-ts\lib\EitherT.js:83:97
    at C:\Users\Offlu\AppData\Roaming\nvm\v16.17.0\node_modules\@stoplight\prism-cli\node_modules\fp-ts\lib\Task.js:133:41
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
Lingihn commented 11 months ago

I understood the problem a little better. Such characters are contained in enum and cannot be rejected or described with ACSII characters. This makes it impossible to use Proxy on OpenApi documents with Russian characters in regex patterns, and the already mentioned Enums.