Skip to content

Commit

Permalink
fix command injection vulnerability
Browse files Browse the repository at this point in the history
  • Loading branch information
Canvinus committed Mar 18, 2024
1 parent ae6723c commit ab260ae
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 0 deletions.
2 changes: 2 additions & 0 deletions nest/src/dtos/compile.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString } from 'class-validator';
import { IsSafePath } from 'src/validators/safe-path.decorator';

export class CompileRustDto {
@ApiProperty({
Expand All @@ -8,5 +9,6 @@ export class CompileRustDto {
})
@IsNotEmpty()
@IsString()
@IsSafePath()
entryPoint: string;
}
4 changes: 4 additions & 0 deletions nest/src/dtos/github.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString } from 'class-validator';
import { IsSafeString } from 'src/validators/safe-string.decorator';
import { IsSafeUrl } from 'src/validators/safe-url.decorator';

export class GithubDto {
@ApiProperty({
Expand All @@ -8,6 +10,7 @@ export class GithubDto {
})
@IsNotEmpty()
@IsString()
@IsSafeUrl()
repo: string;

@ApiProperty({
Expand All @@ -16,6 +19,7 @@ export class GithubDto {
})
@IsNotEmpty()
@IsString()
@IsSafeString()
sha: string;
}

Expand Down
2 changes: 2 additions & 0 deletions nest/src/dtos/verify.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString } from 'class-validator';
import { IsSafePath } from 'src/validators/safe-path.decorator';

export class VerifyRustDto {
@ApiProperty({
Expand All @@ -8,6 +9,7 @@ export class VerifyRustDto {
})
@IsNotEmpty()
@IsString()
@IsSafePath()
entryPoint: string;

@ApiProperty({
Expand Down
1 change: 1 addition & 0 deletions nest/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ async function bootstrap() {
forbidNonWhitelisted: true, // Throw errors if non-whitelisted values are provided
transform: true, // Automatically transform payloads to be objects typed according to their DTO classes
disableErrorMessages: false, // Optionally set this to true in production mode
validateCustomDecorators: true, // Enable usage of custom decorators
}),
);

Expand Down
33 changes: 33 additions & 0 deletions nest/src/validators/safe-path.decorator.ts
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,
});
};
}
32 changes: 32 additions & 0 deletions nest/src/validators/safe-string.decorator.ts
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,
});
};
}
38 changes: 38 additions & 0 deletions nest/src/validators/safe-url.decorator.ts
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,
});
};
}

0 comments on commit ab260ae

Please sign in to comment.