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

feat: add validation checks #358

Merged
merged 24 commits into from
Jul 21, 2023
Merged

feat: add validation checks #358

merged 24 commits into from
Jul 21, 2023

Conversation

nfriedly
Copy link
Member

@nfriedly nfriedly commented Jul 8, 2023

This is a first stab at the validation checks I talked about in #356

There's more that I want to do, but wanted to get this bit up for now.

package.json Show resolved Hide resolved
source/types.ts Outdated Show resolved Hide resolved
source/validations.ts Outdated Show resolved Hide resolved
@nfriedly nfriedly requested a review from gamemaker1 July 8, 2023 22:12
Copy link
Member

@gamemaker1 gamemaker1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good so far :D

Linter doesn't seem very happy though, let me try fixing that.

@gamemaker1
Copy link
Member

What if we made validation a boolean and allowed an onValidationError function to be passed with the options instead, so the user can decide what to do with the error?

- also suppress the annoying node warning, caused by jest using an experimental feature
@gamemaker1 gamemaker1 changed the title Initial validation checks feat: add validation checks Jul 9, 2023
@nfriedly
Copy link
Member Author

nfriedly commented Jul 9, 2023

What if we made validation a boolean and allowed an onValidationError function to be passed with the options instead, so the user can decide what to do with the error?

Yeah, related to that, it occurred to me that Throw will have different behavior for validations that run at startup (where throwing likely kills the server) vs in the context of a request (where throwing will be caught by express's error handler and the error message potentially passed to the end user in the HTTP response.) In light of that, I'm not sure I even want to support the Throw option.

What if validation had 3 potential values: false, a default function that calls console.warn, or a custom function. Although, then the question arises: should the custom function be expected to handle the request, or the regular path? And, should the regular rate-limiting still apply?

Honestly, that's starting to seem more confusing. Maybe we should just go with the validate setting being a boolean value for now: true = warn, false = skip all checks. We can always add a Throw option or an onValidationError setting later if there's demand for it.


On an unrelated note, I was thinking that each validation error should have a unique code, like ERR_ERL_INVALID_IP. We could set it in the error.code property, include it in the error message, and also include some information about it in our wiki. (Of course, error.code only matters if we throw or have a callback...)

@nfriedly
Copy link
Member Author

nfriedly commented Jul 9, 2023

Oh, and switching the config option to a boolean would also solve the "enum in the types file" confusion.

@gamemaker1
Copy link
Member

That makes sense! Should I change it to that then?

@nfriedly
Copy link
Member Author

That makes sense! Should I change it to that then?

Yeah, feel free. I'd like to do some more work on this, but I'm not sure when I'll get to it.

- also adds a `ValidationError` that can be thrown by the validators
@gamemaker1
Copy link
Member

Done.

@gamemaker1
Copy link
Member

Although now we have an Error class in the types file :/

source/types.ts Outdated Show resolved Hide resolved
source/validations.ts Outdated Show resolved Hide resolved
source/validations.ts Outdated Show resolved Hide resolved
source/lib.ts Show resolved Hide resolved
source/validations.ts Outdated Show resolved Hide resolved
@gamemaker1 gamemaker1 marked this pull request as ready for review July 12, 2023 16:46
source/lib.ts Outdated Show resolved Hide resolved
source/validations.ts Outdated Show resolved Hide resolved
source/validations.ts Outdated Show resolved Hide resolved
@nfriedly
Copy link
Member Author

ok, decorators are out, runCheck is back in. (although I renamed it to wrap() since half it's job is to not run the checks. build looks to be passing now.

@nfriedly
Copy link
Member Author

nfriedly commented Jul 13, 2023

It just occurred to me that all of these checks are run per-request and can potentially log something on every single request. We might want to limit that. The ones here are probably fine to run on only the first request and none after.

Maybe we should include the stack trace to make it stand out more if we limit it to one log, though.

@nfriedly
Copy link
Member Author

Ok, it now disables validations near the end of the first request, but logs a full stack trace if an issue is found.

I did a bit of testing and realized that the X-Forwarded-For check didn't work because the default trust proxy value is false. I updated things to account for this.

I also templatized the more info link part of the error message, since I initially forgot to update it when changing the error code.

I think it needs a little more testing (both manual and automated), and then I think it will be good to ship.

@gamemaker1
Copy link
Member

This looks great!!

readme.md Outdated Show resolved Hide resolved
@gamemaker1 gamemaker1 added enhancement Making the library better proxy Related to proxy changing IP address labels Jul 16, 2023
@nfriedly
Copy link
Member Author

Ok, I think this is good to go, but lets wait a day or two to see if we want to do a point release with #361 first. I'm leaning towards that, but I'd really like to hear back that it resolves the issue first.

@nfriedly
Copy link
Member Author

Alright, I think we've waited long enough. I'm going to ship this.

@nfriedly nfriedly merged commit fe1f16e into main Jul 21, 2023
44 checks passed
@gamemaker1 gamemaker1 deleted the checks branch July 21, 2023 03:28
@nfriedly nfriedly mentioned this pull request Aug 3, 2023
5 tasks
@SimonSchick
Copy link

Thank you for this, we did in-fact discover config issues due to this PR :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Making the library better proxy Related to proxy changing IP address
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants