-
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.
- Loading branch information
Showing
7 changed files
with
112 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { | ||
ValidationArguments, | ||
ValidationOptions, | ||
ValidatorConstraint, | ||
ValidatorConstraintInterface, | ||
registerDecorator, | ||
} from 'class-validator'; | ||
|
||
@ValidatorConstraint({ async: false }) | ||
class IsSafePathConstraint implements ValidatorConstraintInterface { | ||
validate(path: string, args: ValidationArguments) { | ||
if (typeof path !== 'string') return false; | ||
|
||
const isSuspicious = /[;&|`$<>]/.test(path); | ||
return !isSuspicious; | ||
} | ||
|
||
defaultMessage(args: ValidationArguments) { | ||
return `The path "${args.value}" contains invalid characters. Only safe paths are allowed.`; | ||
} | ||
} | ||
|
||
export function IsSafePath(validationOptions?: ValidationOptions) { | ||
return function (object: any, propertyName: string) { | ||
registerDecorator({ | ||
target: object.constructor, | ||
propertyName: propertyName, | ||
options: validationOptions, | ||
constraints: [], | ||
validator: IsSafePathConstraint, | ||
}); | ||
}; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { | ||
registerDecorator, | ||
ValidationOptions, | ||
ValidatorConstraint, | ||
ValidatorConstraintInterface, | ||
} from 'class-validator'; | ||
|
||
@ValidatorConstraint({ async: false }) | ||
export class IsSafeStringConstraint implements ValidatorConstraintInterface { | ||
validate(text: string) { | ||
if (typeof text !== 'string') return false; | ||
|
||
const sanitizedText = text.replace(/[^a-zA-Z0-9_.-]/g, ''); | ||
return sanitizedText === text; | ||
} | ||
|
||
defaultMessage() { | ||
return 'Input contains invalid characters'; | ||
} | ||
} | ||
|
||
export function IsSafeString(validationOptions?: ValidationOptions) { | ||
return function (object: any, propertyName: string) { | ||
registerDecorator({ | ||
target: object.constructor, | ||
propertyName: propertyName, | ||
options: validationOptions, | ||
constraints: [], | ||
validator: IsSafeStringConstraint, | ||
}); | ||
}; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { | ||
ValidationArguments, | ||
ValidationOptions, | ||
ValidatorConstraint, | ||
ValidatorConstraintInterface, | ||
registerDecorator, | ||
} from 'class-validator'; | ||
|
||
@ValidatorConstraint({ async: false }) | ||
export class IsSafeUrlConstraint implements ValidatorConstraintInterface { | ||
validate(url: string, args: ValidationArguments) { | ||
if (typeof url !== 'string') return false; | ||
|
||
// Define the pattern for a safe URL here. This is a simplistic approach; | ||
// consider using more sophisticated validation depending on your requirements. | ||
const unsafePatterns = /(;|&|\||`|\$)/; | ||
|
||
// URL is considered safe if it doesn't match unsafe patterns | ||
return !unsafePatterns.test(url); | ||
} | ||
|
||
defaultMessage(args: ValidationArguments) { | ||
return 'The URL contains unsafe characters that could lead to command line injection.'; | ||
} | ||
} | ||
|
||
export function IsSafeUrl(validationOptions?: ValidationOptions) { | ||
return function (object: any, propertyName: string) { | ||
registerDecorator({ | ||
name: 'isSafeUrl', | ||
target: object.constructor, | ||
propertyName: propertyName, | ||
options: validationOptions, | ||
constraints: [], | ||
validator: IsSafeUrlConstraint, | ||
}); | ||
}; | ||
} |