Skip to content

[QUESTION] how to properly sanitize API request payloads #1089

@tezvi

Description

@tezvi

Just a general question what do you guys recommend as a best practice to sanitize user request payloads for XSS and other potential harmful data.

There are several ideas that come on my mind, like:

  • Entity fields validation using Symfony Validator and annotations
  • Decorating deserializer / denormalizer / decoder and filtering string data from payload requests
  • Additional Apache / Nginx handling and filtering

Any other ideas? Does api-platform have it's own way of handling this? It seems to me that data is persisted straight from the request.

Activity

soyuka

soyuka commented on Apr 4, 2019

@soyuka
Member

Data is validated after the denormalization phase. To avoid type errors you can disable type checking in the normalizers.

tezvi

tezvi commented on Apr 4, 2019

@tezvi
Author

I understand. However I was aiming more into a security kind of checking.
For an example I would like to prevent malicious javascript data from even reaching a validation phase and deserialization to an entity.

I've come up with a simple implementation by decorating a default decoder and manually inspecting string values from a raw JSON payload and throwing HTTP 400 if funky business was detected. It's a bit defensive I know so i was wondering about alternatives.

teohhanhui

teohhanhui commented on Apr 5, 2019

@teohhanhui
Contributor
  1. Always use parameterized (database) queries.
  2. Always escape (for the correct context) untrusted data (anything that came from user input) on the client.
tezvi

tezvi commented on Apr 5, 2019

@tezvi
Author

Hi, thanks for input.
You are absolutely right, but how do you suggest to accomplish 2nd rule if you are using a default api POST endpoint configuration without custom controller or any custom listener and only default constraint validators on entity (no fancy custom filtering).
If you send a HTTP POST payload with malicious XSS it will be persisted to database. It means it will go through ReadListener -> DeserializeListener -> ValidationListener -> WriteListener and end up in database.

sclausen

sclausen commented on Apr 23, 2019

@sclausen

If a string containing javascript is stored in the database, it’s not harmful per se. You shouldnt evaluate the javascript on output though.

tezvi

tezvi commented on Apr 23, 2019

@tezvi
Author

Of course, that might be the solution. However, by using a default ApiPlatform implementation the XSS content will be returned to client. So you'll have to tweak around that.
Concerning security in general, a proper course of action should be to prevent any malicious data from entering into persisting layer. Better safe than sorry :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @teohhanhui@sclausen@soyuka@tezvi

        Issue actions

          [QUESTION] how to properly sanitize API request payloads · Issue #1089 · api-platform/api-platform