-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from SkinSightYnov/dev
correction guard sanitizer / ajout helmet + ajout rate limiter
- Loading branch information
Showing
5 changed files
with
31 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,27 @@ | ||
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common'; | ||
import { Observable } from 'rxjs'; | ||
import xss from 'xss'; | ||
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common'; | ||
import * as xss from 'xss'; | ||
|
||
@Injectable() | ||
export class SanitizerGuard implements CanActivate { | ||
canActivate( | ||
context: ExecutionContext, | ||
): boolean | Promise<boolean> | Observable<boolean> { | ||
canActivate(context: ExecutionContext): boolean { | ||
const request = context.switchToHttp().getRequest(); | ||
|
||
if (request.body) { | ||
request.body = this.cleanData(request.body); | ||
} | ||
|
||
if (request.query) { | ||
request.query = this.cleanData(request.query); | ||
} | ||
|
||
if (request.params) { | ||
request.params = this.cleanData(request.params); | ||
this.sanitizeData(request.body); | ||
} | ||
|
||
return true; | ||
} | ||
private cleanData(data: Record<string, any>): Record<string, any> { | ||
|
||
private sanitizeData(data: Record<string, any>): void { | ||
for (const key in data) { | ||
if (data.hasOwnProperty(key) && typeof data[key] === 'string') { | ||
data[key] = xss(data[key]); | ||
if (data.hasOwnProperty(key)) { | ||
if (typeof data[key] === 'string') { | ||
data[key] = xss.filterXSS(data[key]); | ||
} else if (typeof data[key] === 'object') { | ||
this.sanitizeData(data[key]); | ||
} | ||
} | ||
} | ||
|
||
return data; | ||
} | ||
} |