Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validator crashes fastify application upon invalid / undefined field value #26

Closed
2 tasks done
callmeteus opened this issue Jul 26, 2022 · 4 comments
Closed
2 tasks done
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@callmeteus
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.3.0

Plugin version

No response

Node.js version

16.15.1

Operating system

Windows

Operating system version (i.e. 20.04, 11.3, 10)

10

Description

When the fastify validator calls the validator function (https://github.com/fastify/fastify/blob/66fc397212531c2565a9cb86a2c0f4a2f06a8c77/lib/validation.js#L79), it checks if a return is type or has an error on it.

The TypeBox provider actually throws an excetion (

throw new TypeBoxValidationError([...typeCheck.Errors(value)])
), which causes fastify to crash the entire application since there is no try-catch on this request phase.

image

Steps to Reproduce

  • Create a simple application using the TypeBox provider
  • Register a route with a given schema (I have tested with a body param)
  • Try to make a request to the route with a missing field
fastify.route({
    method: "POST",
    url: "/user/sign-up",
    schema: {
        body: Type.Object({
            email: Type.String()
        })
    },
    handler() { }
})

Expected Behavior

No response

@callmeteus callmeteus changed the title Validator crashes upon invalid field value Validator crashes fastify application upon invalid / undefined field value Jul 26, 2022
@callmeteus
Copy link
Author

My personal solution was to make the provider do what is is actually commented to do: return validation errors:

export const TypeBoxValidatorCompiler: FastifySchemaCompiler<TSchema> = ({ schema }) => {
    const typeCheck = TypeCompiler.Compile(schema);

    return (value): any => {
        if (typeCheck.Check(value)) {
            return;
        }

        const errors = [...typeCheck.Errors(value)];

        return {
            error: errors.map((firstError) => ({
                message: firstError.message,
                schemaPath: firstError.path
            } as FastifySchemaValidationError))
        };
    }
}

@mcollina
Copy link
Member

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

@mcollina mcollina added bug Something isn't working good first issue Good for newcomers labels Jul 26, 2022
@sinclairzx81
Copy link
Contributor

Fix submitted on PR #28

@ikad95
Copy link

ikad95 commented Aug 11, 2022

I have the similar issue. Is funny that this week I decided to try fastify although I did the PoC in express just because I had a bit of time in my hand and I saw you guys accept PR of company SVG for your website. I hope you guys are as reliable as you are in benchmarks.
PS : When my service goes live, I'd love to represent my company as I said before. I work at Goldman Sachs, NodeJS services are written in express and internal libraries like for auth, etc are made to work on top of express. Please do accept my PR :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants