mirror of
https://github.com/fosrl/pangolin.git
synced 2026-07-27 15:50:09 +02:00
Fix #3462
This commit is contained in:
@@ -9,16 +9,14 @@ import createHttpError from "http-errors";
|
|||||||
import logger from "@server/logger";
|
import logger from "@server/logger";
|
||||||
import { fromError } from "zod-validation-error";
|
import { fromError } from "zod-validation-error";
|
||||||
import {
|
import {
|
||||||
isValidCIDR,
|
RESOURCE_RULE_MATCH_TYPES,
|
||||||
isValidIP,
|
getResourceRuleValueValidationError
|
||||||
isValidUrlGlobPattern
|
|
||||||
} from "@server/lib/validators";
|
} from "@server/lib/validators";
|
||||||
import { OpenAPITags, registry } from "@server/openApi";
|
import { OpenAPITags, registry } from "@server/openApi";
|
||||||
import { isValidRegionId } from "@server/db/regions";
|
|
||||||
|
|
||||||
const createResourceRuleSchema = z.strictObject({
|
const createResourceRuleSchema = z.strictObject({
|
||||||
action: z.enum(["ACCEPT", "DROP", "PASS"]),
|
action: z.enum(["ACCEPT", "DROP", "PASS"]),
|
||||||
match: z.enum(["CIDR", "IP", "PATH", "COUNTRY", "ASN", "REGION"]),
|
match: z.enum(RESOURCE_RULE_MATCH_TYPES),
|
||||||
value: z.string().min(1),
|
value: z.string().min(1),
|
||||||
priority: z.int(),
|
priority: z.int(),
|
||||||
enabled: z.boolean().optional()
|
enabled: z.boolean().optional()
|
||||||
@@ -151,39 +149,14 @@ export async function createResourceRule(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (match === "CIDR") {
|
const valueValidationError = getResourceRuleValueValidationError(
|
||||||
if (!isValidCIDR(value)) {
|
match,
|
||||||
return next(
|
value
|
||||||
createHttpError(
|
);
|
||||||
HttpCode.BAD_REQUEST,
|
if (valueValidationError) {
|
||||||
"Invalid CIDR provided"
|
return next(
|
||||||
)
|
createHttpError(HttpCode.BAD_REQUEST, valueValidationError)
|
||||||
);
|
);
|
||||||
}
|
|
||||||
} else if (match === "IP") {
|
|
||||||
if (!isValidIP(value)) {
|
|
||||||
return next(
|
|
||||||
createHttpError(HttpCode.BAD_REQUEST, "Invalid IP provided")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else if (match === "PATH") {
|
|
||||||
if (!isValidUrlGlobPattern(value)) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.BAD_REQUEST,
|
|
||||||
"Invalid URL glob pattern provided"
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else if (match === "REGION") {
|
|
||||||
if (!isValidRegionId(value)) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.BAD_REQUEST,
|
|
||||||
"Invalid region ID provided"
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the new resource rule
|
// Create the new resource rule
|
||||||
|
|||||||
@@ -9,12 +9,11 @@ import createHttpError from "http-errors";
|
|||||||
import logger from "@server/logger";
|
import logger from "@server/logger";
|
||||||
import { fromError } from "zod-validation-error";
|
import { fromError } from "zod-validation-error";
|
||||||
import {
|
import {
|
||||||
isValidCIDR,
|
RESOURCE_RULE_MATCH_TYPES,
|
||||||
isValidIP,
|
getResourceRuleValueValidationError,
|
||||||
isValidUrlGlobPattern
|
ResourceRuleMatchType
|
||||||
} from "@server/lib/validators";
|
} from "@server/lib/validators";
|
||||||
import { OpenAPITags, registry } from "@server/openApi";
|
import { OpenAPITags, registry } from "@server/openApi";
|
||||||
import { isValidRegionId } from "@server/db/regions";
|
|
||||||
|
|
||||||
// Define Zod schema for request parameters validation
|
// Define Zod schema for request parameters validation
|
||||||
const updateResourceRuleParamsSchema = z.strictObject({
|
const updateResourceRuleParamsSchema = z.strictObject({
|
||||||
@@ -22,15 +21,7 @@ const updateResourceRuleParamsSchema = z.strictObject({
|
|||||||
resourceId: z.coerce.number().int().positive()
|
resourceId: z.coerce.number().int().positive()
|
||||||
});
|
});
|
||||||
|
|
||||||
const resourceRuleMatchSchema = z.enum([
|
const resourceRuleMatchSchema = z.enum(RESOURCE_RULE_MATCH_TYPES);
|
||||||
"CIDR",
|
|
||||||
"IP",
|
|
||||||
"PATH",
|
|
||||||
"COUNTRY",
|
|
||||||
"COUNTRY_IS_NOT",
|
|
||||||
"ASN",
|
|
||||||
"REGION"
|
|
||||||
]);
|
|
||||||
|
|
||||||
// Define Zod schema for request body validation
|
// Define Zod schema for request body validation
|
||||||
const updateResourceRuleSchema = z
|
const updateResourceRuleSchema = z
|
||||||
@@ -173,14 +164,7 @@ export async function updateResourceRule(
|
|||||||
resource.resourcePolicyId === null &&
|
resource.resourcePolicyId === null &&
|
||||||
resource.defaultResourcePolicyId !== null;
|
resource.defaultResourcePolicyId !== null;
|
||||||
|
|
||||||
let existingMatch:
|
let existingMatch: ResourceRuleMatchType;
|
||||||
| "CIDR"
|
|
||||||
| "IP"
|
|
||||||
| "PATH"
|
|
||||||
| "COUNTRY"
|
|
||||||
| "COUNTRY_IS_NOT"
|
|
||||||
| "ASN"
|
|
||||||
| "REGION";
|
|
||||||
|
|
||||||
if (isInlinePolicy) {
|
if (isInlinePolicy) {
|
||||||
const policyId = resource.defaultResourcePolicyId!;
|
const policyId = resource.defaultResourcePolicyId!;
|
||||||
@@ -264,42 +248,14 @@ export async function updateResourceRule(
|
|||||||
const { value } = updateData;
|
const { value } = updateData;
|
||||||
|
|
||||||
if (value !== undefined) {
|
if (value !== undefined) {
|
||||||
if (match === "CIDR") {
|
const valueValidationError = getResourceRuleValueValidationError(
|
||||||
if (!isValidCIDR(value)) {
|
match,
|
||||||
return next(
|
value
|
||||||
createHttpError(
|
);
|
||||||
HttpCode.BAD_REQUEST,
|
if (valueValidationError) {
|
||||||
"Invalid CIDR provided"
|
return next(
|
||||||
)
|
createHttpError(HttpCode.BAD_REQUEST, valueValidationError)
|
||||||
);
|
);
|
||||||
}
|
|
||||||
} else if (match === "IP") {
|
|
||||||
if (!isValidIP(value)) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.BAD_REQUEST,
|
|
||||||
"Invalid IP provided"
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else if (match === "PATH") {
|
|
||||||
if (!isValidUrlGlobPattern(value)) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.BAD_REQUEST,
|
|
||||||
"Invalid URL glob pattern provided"
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else if (match === "REGION") {
|
|
||||||
if (!isValidRegionId(value)) {
|
|
||||||
return next(
|
|
||||||
createHttpError(
|
|
||||||
HttpCode.BAD_REQUEST,
|
|
||||||
"Invalid region ID provided"
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user