Skip to content

Commit b3f51cc

Browse files
hotfix(): fix validation pipe (strip proto properties)
1 parent f333022 commit b3f51cc

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nestjs",
3-
"version": "6.2.2",
3+
"version": "6.2.3",
44
"description": "Modern, fast, powerful node.js web framework",
55
"scripts": {
66
"coverage": "nyc report --reporter=text-lcov | coveralls",

packages/common/pipes/validation.pipe.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { Optional } from '../decorators';
22
import { Injectable } from '../decorators/core';
3-
import { ArgumentMetadata, BadRequestException, ValidationError } from '../index';
3+
import {
4+
ArgumentMetadata,
5+
BadRequestException,
6+
ValidationError,
7+
} from '../index';
48
import { ClassTransformOptions } from '../interfaces/external/class-transform-options.interface';
59
import { ValidatorOptions } from '../interfaces/external/validator-options.interface';
610
import { PipeTransform } from '../interfaces/features/pipe-transform.interface';
@@ -57,10 +61,13 @@ export class ValidationPipe implements PipeTransform<any> {
5761
if (!metatype || !this.toValidate(metadata)) {
5862
return value;
5963
}
64+
value = this.toEmptyIfNil(value);
65+
66+
this.stripProtoKeys(value);
6067
const entity = classTransformer.plainToClass(
6168
metatype,
62-
this.toEmptyIfNil(value),
63-
this.transformOptions
69+
value,
70+
this.transformOptions,
6471
);
6572
const errors = await classValidator.validate(entity, this.validatorOptions);
6673
if (errors.length > 0) {
@@ -69,8 +76,8 @@ export class ValidationPipe implements PipeTransform<any> {
6976
return this.isTransformEnabled
7077
? entity
7178
: Object.keys(this.validatorOptions).length > 0
72-
? classTransformer.classToPlain(entity, this.transformOptions)
73-
: value;
79+
? classTransformer.classToPlain(entity, this.transformOptions)
80+
: value;
7481
}
7582

7683
private toValidate(metadata: ArgumentMetadata): boolean {
@@ -82,7 +89,15 @@ export class ValidationPipe implements PipeTransform<any> {
8289
return !types.some(t => metatype === t) && !isNil(metatype);
8390
}
8491

85-
toEmptyIfNil<T = any, R = any>(value: T): R | {} {
92+
private toEmptyIfNil<T = any, R = any>(value: T): R | {} {
8693
return isNil(value) ? {} : value;
8794
}
95+
96+
private stripProtoKeys(value: Record<string, any>) {
97+
delete value.__proto__;
98+
const keys = Object.keys(value);
99+
keys
100+
.filter(key => typeof value[key] === 'object')
101+
.forEach(key => this.stripProtoKeys(value[key]));
102+
}
88103
}

0 commit comments

Comments
 (0)