Skip to content

Commit e4aca6a

Browse files
authored
fix(products): input validation and better date parsing (#326)
1 parent a7ed13d commit e4aca6a

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

charts/brokencrystals/Chart.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: |
44
Benchmark application that uses modern technologies and implements a set of
55
common security vulnerabilities
66
type: application
7-
version: 0.0.60
7+
version: 0.0.61
88
keywords:
99
- brokencrystals
1010
- brkn

src/products/products.controller.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ export class ProductsController {
3737

3838
constructor(private readonly productsService: ProductsService) {}
3939

40+
private parseDate(dateString: string): Date {
41+
const dateParts = dateString.split('-');
42+
const year = parseInt(dateParts[2], 10);
43+
const month = parseInt(dateParts[1], 10) - 1;
44+
const day = parseInt(dateParts[0], 10);
45+
46+
return new Date(year, month, day);
47+
}
48+
4049
@Get()
4150
@UseGuards(AuthGuard)
4251
@JwtType(JwtProcessorType.RSA)
@@ -67,10 +76,14 @@ export class ProductsController {
6776
let df = new Date(new Date().setFullYear(new Date().getFullYear() - 1));
6877
let dt = new Date();
6978
if (dateFrom) {
70-
df = new Date(`${dateFrom} 00:00:00.000Z`);
79+
df = this.parseDate(dateFrom);
7180
}
7281
if (dateTo) {
73-
dt = new Date(`${dateTo} 00:00:00.000Z`);
82+
dt = this.parseDate(dateTo);
83+
}
84+
85+
if (isNaN(df.getTime()) || isNaN(dt.getTime())) {
86+
throw new BadRequestException('Invalid date format');
7487
}
7588

7689
const allProducts = await this.productsService.findAll(df, dt);

0 commit comments

Comments
 (0)