Skip to content

Hapi not rejecting non-URL-encoded data on route that allows 'application/x-www-form-urlencoded' content type #3422

@memughal

Description

@memughal

I am trying the following test and I am sending non-urlencoded data to a route that 'allows' url encoded data. Since content type of this request is set to 'application/x-www-form-urlencoded', it should encode the payload but it does not.

            context('with invalid payload (not URL-encoded)', () => {
                it('returns 400', () => {
                    return server.inject({
                        url: '/books',
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/x-www-form-urlencoded',
                            Authorization: internals.headers.Authorization
                        },
                        payload: 'title=💣'
                    }).then(res => {
                        expect(res.statusCode).to.equal(400);
                    });
                });
            });

and the following routes config:

       this.server.route({
            method: 'POST',
            path: '/books',
            config: {
                handler: (request, reply) => {
                    this.handleBooks(request, response);
                },
                description: 'Create a new book',
                payload: {
                    allow: 'application/x-www-form-urlencoded'
                },
                validate: {
                    payload: {
                        title: Joi.string().required().description('Title of the new book')
                    }
                }
            }
        })

This results in 200 status code instead of 400, since routes should not let users post not encoded characters by default.

Context

  • node version: v7.1.0
  • hapi version: 16.0.2
  • os: MacOS Sierra v 10.12

Metadata

Metadata

Assignees

Labels

non issueIssue is not a problem or requires changes

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions