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

fix(api): Improve block range validation in POI endpoint #2645

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions packages/node-core/src/admin/admin.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,6 @@ export class AdminController {
@Get('poi/')
async getPoisByRange(@Query(ValidationPipe) blockRange: BlockRangeDto): Promise<ProofOfIndexHuman[]> {
const {endBlock, startBlock} = blockRange;
// TODO, class validator seems not work properly, need to complete in future
if (endBlock && Number(startBlock) > Number(endBlock)) {
throw new HttpException(
{
status: HttpStatus.BAD_REQUEST,
error: 'startBlock must be greater than endBlock',
},
HttpStatus.BAD_REQUEST
);
}
logger.info(`[POI] Getting poi history for blocks from ${startBlock} to ${endBlock}`);
return handleServiceCall(async () => {
const pois = await this.poiService.plainPoiRepo.getPoiBlocksByRange(
Expand Down
26 changes: 25 additions & 1 deletion packages/node-core/src/admin/blockRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,30 @@
// SPDX-License-Identifier: GPL-3.0

import {Type} from 'class-transformer';
import {IsInt, IsPositive, Min, IsOptional} from 'class-validator';
import {IsInt, IsPositive, Min, IsOptional, ValidateIf, registerDecorator, ValidationOptions, ValidationArguments} from 'class-validator';

export function IsGreaterThan(property: string, validationOptions?: ValidationOptions) {
return function (object: any, propertyName: string) {
registerDecorator({
name: 'isGreaterThan',
target: object.constructor,
propertyName: propertyName,
constraints: [property],
options: validationOptions,
validator: {
validate(value: any, args: ValidationArguments) {
const [relatedPropertyName] = args.constraints;
const relatedValue = (args.object as any)[relatedPropertyName];
return value === undefined || value > relatedValue;
},
defaultMessage(args: ValidationArguments) {
const [relatedPropertyName] = args.constraints;
return `${args.property} must be greater than ${relatedPropertyName}`;
},
},
});
};
}

export interface BlockRangeDtoInterface {
startBlock: number;
Expand All @@ -26,5 +49,6 @@ export class BlockRangeDto implements BlockRangeDtoInterface {
@IsPositive()
@Min(0)
@IsOptional()
@IsGreaterThan('startBlock')
endBlock: number | undefined;
}