Skip to content

Commit

Permalink
fix: add validation for share id and zip compression config variables
Browse files Browse the repository at this point in the history
  • Loading branch information
stonith404 committed Nov 25, 2024
1 parent da54ce6 commit 3160f90
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
24 changes: 23 additions & 1 deletion backend/src/config/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
NotFoundException,
} from "@nestjs/common";
import { Config } from "@prisma/client";
import { PrismaService } from "src/prisma/prisma.service";
import { EventEmitter } from "events";
import { PrismaService } from "src/prisma/prisma.service";

/**
* ConfigService extends EventEmitter to allow listening for config updates,
Expand Down Expand Up @@ -100,6 +100,8 @@ export class ConfigService extends EventEmitter {
);
}

this.validateConfigVariable(key, value);

const updatedVariable = await this.prisma.config.update({
where: {
name_category: {
Expand All @@ -116,4 +118,24 @@ export class ConfigService extends EventEmitter {

return updatedVariable;
}

validateConfigVariable(key: string, value: string | number | boolean) {
const validations = [
{
key: "share.shareIdLength",
condition: (value: number) => value >= 2 && value <= 50,
message: "Share ID length must be between 2 and 50",
},
{
key: "share.zipCompressionLevel",
condition: (value: number) => value >= 0 && value <= 9,
message: "Zip compression level must be between 0 and 9",
},
];

const validation = validations.find((validation) => validation.key == key);
if (validation && !validation.condition(value as any)) {
throw new BadRequestException(validation.message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ const CreateUploadModalBody = ({
</Group>

<Text
truncate
italic
size="xs"
sx={(theme) => ({
Expand Down

0 comments on commit 3160f90

Please sign in to comment.