-
Notifications
You must be signed in to change notification settings - Fork 811
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
fix: "an unknown value was passed to the validate function" #1873
Comments
+1 |
1 similar comment
+1 |
This comment was marked as outdated.
This comment was marked as outdated.
as you're using nestjs, in your main.ts you can update your ValidationPipe creation as follow : |
+1 In NestJs, I got the same error but I was sending correct payload, without Unkown values, that makes no sense. |
I agree with @marlon-chagas - even with correct payload I had to disable |
please add a 👍 in the PR description instead of spamming with +1 comments :-) |
I am not able to reproduce this. Here's my attempt at a reproduction. I have commented the results of the console.logs: import { IsOptional, IsPositive, validate } from "class-validator";
export class PaginationInfo {
before: string | undefined;
after: string | undefined;
@IsOptional()
@IsPositive()
first: number | undefined;
@IsOptional()
@IsPositive()
last: number | undefined;
}
const pInfo = new PaginationInfo();
pInfo.first = 2;
pInfo.last = 4;
const errors = await validate(pInfo);
console.log(errors); // []
pInfo.before = "before";
pInfo.after = "after";
const errors2 = await validate(pInfo);
console.log(errors2); // [] I see this is a GraphQL input object. Have you considered the possiblity that the |
I am seeing this in a nestjs app as well after upgrading. Strange that it seems to work locally but fail with this error in higher remote environments 🤷🏻 |
Have you double checked if your payload contains |
I suppose it is possible that production builds is attaching something. Need to check the output. |
We have several microservices using NestJS and this was happening to us for one particular endpoint. In our scenario, we had a property on our DTO that had no validation decorators on it. So for example:
became:
Which resolved the issue. Hope that helps |
Hello guys, i found i guess the solution, in my DTO after add a class-validator this work very well. wihout the validator in the DTO fields, i receve the error.
|
Why the value of |
how is possibile this issue is not fixed yet, it basically breaks the library |
Yes. It is total mess, and solutions are available only for NestJS. Downgrade to:
fix problem. |
See Semantic Versioning's specification for version zero:
On top of that I believe there is an unwritten convention for version zero releases to use minor version changes (0.13 -> 0.14) to reflect breaking changes, and patch versions (0.13.1 -> 0.13.2) for non-breaking changes. This gives users some predictability. @Hatko – does the issue still persist even if your validation classes are set up to respect |
In my case, my DTO doesn't have "isOptional" from the class-validator; when I'm using the 0.13.2 version, I'm not facing an error even though I don't pass some params, but after upgrading to the 0.14.0 version, I got "an unknown value was passed to the validate function" error. So my simple solution is to add "isOptional" in my DTO (nestjs) |
In my case, my class property has a nested validation. Typing with class transformer @ValidateNested()
@ApiProperty()
@Type(() => InstanceConfig) // here
instances: Array<InstanceConfig>; |
@eneilson thank you for pointing the way to dig. Your suggestion fixes the issue and seems like it's because by default In my case, the fix was a bit more complicated because I have a field containing a JSON string that I parse to an object using another decorator based on The simplest way to debug if the nested objects were parsed correctly is to log the parent DTO to the console and examine the value of the nested field. If it's just a plain object then |
seems updating @nestjs/common to 9.3.9 will fix the problem |
what about non-NestJs users, it still makes no sense to set |
@eneilson Thanks that was my issue as well, I just needed to include |
Due to [a deserialization issue](typestack/class-validator#1873) caused by `class-validator` `0.14.0`, disable `forbidUnknownValues` for a short-term solution.
I am using Apollo and |
I'm having the same error when using version In my case I'm using NestJS with class-validator and when the backend tries to validate the object an error with the message Thanks! |
Also make sure that if you create your DTO in the test with the ... operator based on another object, you are not providing the correct instance anymore. In my case, I had tried Instead I had to use |
I have still not seen a reproduction of a supposed bug with Until a bug can be reproduced, there is nothing to patch. If no reproduction is brought forth I am considering closing this issue, as letting it stay up is perpetuating a misconception (that the breaking change is a bug). |
It doesn't make sense to close the issue when there are plenty of people having this issue. I understand the semantic version but I didn't know that increasing a version would break something that was working correctly. It seems you're stating that this breaking change was on purpose, if so, could you please point out which one is it and how to address it? I'll try to create a sample of an app emulating this issue and I'll share it later here. Thanks! |
Minor version increases within major version 0 (0.13.X -> 0.14.0) should not be considered non-breaking. I have linked to the relevant description of this in the SemVer spec previously in this thread. I believe it is common practice during version 0 that projects will use minor version bumps (0.13.X -> 0.14.0) for breaking changes and patch version bumps for non-breaking changers (0.13.0 -> 0.13.1). This breaking change was made on purpose to fix a security vulnerability, see the release notes for a description of this.
That would be great, thanks! |
Hey @braaar, Analyzing this issue in my application and it happens that when an endpoint receives a payload using the Do you think turning Thanks! |
1. Fix correct ajv schema for enum type of type orm 2. Add forbidUnknownValues for class-validator(typestack/class-validator#1873)
Nice find! I'm no security expert, but the implication of allowing unknown values is that attackers could attach some sneaky extra stuff into an otherwise valid payload. Depending on the architecture of your application this could potentially allow them to give themselves heightened access or just simply corrupt the integrity of your database. Document databases like MongoDB and Firestore will typically allow you to save anything in there since there's no strict schema to adhere to. const errors = await validate(input);
if(errors.length == 0) {
await saveUserToDatabase(input)
} Of course, it's entirely possible that your application discards any extra fields from the input and I don't see a security issue in that case:, but in a way it's a bit scary to have to rely on future developers to remember this quirk when designing the code and it is arguably quite tedious: const errors = await validate(input);
if(errors.length == 0) {
await saveUserToDatabase({
name: input.name,
role: input.role
})
} |
Similarly, if you are doing some mapping from the input types onto another type after validation, you will have to be careful never to use the spread operator: // vulnerable
const user = {
name: input.name,
type: input.hasSubscription ? 'subscriber' : 'regular',
...input // this could contain anything!
}
await saveUserToDatabase(user);
// safe
const user = {
name: input.name,
type: input.hasSubscription ? 'subscriber' : 'regular',
role: input.role
}
await saveUserToDatabase(user); I think the preferable option is to forbid unknown values since you will know that the shape of the object is what you think it is after validation. |
I was facing this issue too, on NestJS, here is what was wrong My issue was using a custom MongoDB ID pipe to transform params, here is the implementation ` @Injectable() ` this returns a string because that's how it arrives at the controller handler, but I wrote the type as Types.ObjectId, like this `
` |
👏🏼 🎉 |
I have a similar problem when uploading files in Nestjs and get the same errors when I downgrade the version of the class validator from 0.14.0 -> 0.13.2, the problem was solved. I'm using Nestjs version 9.x.x |
I was in a similar situation as many here upgrading this package in a nestJS app due to the security vulnerability. Would be great if they could just patch 0.13.x (show me the vulnerability fix and I'll make a PR). For me part of the problem was in the internal handling of the TypeOrmCrudController. I didn't dig too far, in my case a PATCH update request was failing because not all properties that are passed on create/update were included. By manually defining a PATCH controller method and DTO to match the problem was fixed. Hope you find this helpful let me know if I can clarify. |
Hi, I am getting the same error while I am using "class-validator": "^0.12.2", But just wonder this is working fine on local env. but getting below error on production env. |
Thank you mate, you saved me |
Quoting myself from earlier in this issue:
I suspect this issue has gotten enough traction that people seeking guidance on how to make the breaking change in 0.13 work with NestJS will find it and see the tips and tricks people have posted in here. I will be closing this issue. |
I had similar issue. I fixed it changing param type from Types.ObjectId to string. |
Thanks, it helped! |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Description
After updating
class-validator
from0.13.2
to0.14.0
, we get an error when consuming GraphQL API:Minimal code-snippet showcasing the problem
Expected behavior
We expect to get a response, as with 0.13.2
Actual behavior
An error described above is thrown
The text was updated successfully, but these errors were encountered: