use strict zod objects and hide proto on targets

This commit is contained in:
Milo Schwartz 2024-11-14 00:00:17 -05:00
parent 44b932937f
commit ba3505a385
No known key found for this signature in database
14 changed files with 154 additions and 162 deletions

View file

@ -10,6 +10,7 @@ import {
import { and, eq, or } from "drizzle-orm";
import createHttpError from "http-errors";
import HttpCode from "@server/types/HttpCode";
import logger from "@server/logger";
export async function verifySiteAccess(
req: Request,
@ -28,6 +29,7 @@ export async function verifySiteAccess(
}
if (isNaN(siteId)) {
logger.debug(JSON.stringify(req.body));
return next(createHttpError(HttpCode.BAD_REQUEST, "Invalid site ID"));
}

View file

@ -150,7 +150,6 @@ authenticated.get(
authenticated.post(
"/resource/:resourceId",
verifyResourceAccess,
verifySiteAccess,
verifyUserHasAction(ActionsEnum.updateResource),
resource.updateResource
);

View file

@ -12,11 +12,13 @@ import config from "@server/config";
import { fromError } from "zod-validation-error";
import { defaultRoleAllowedActions } from "../role";
const createOrgSchema = z.object({
orgId: z.string(),
name: z.string().min(1).max(255),
// domain: z.string().min(1).max(255).optional(),
});
const createOrgSchema = z
.object({
orgId: z.string(),
name: z.string().min(1).max(255),
// domain: z.string().min(1).max(255).optional(),
})
.strict();
const MAX_ORGS = 5;

View file

@ -18,6 +18,7 @@ const updateOrgBodySchema = z
name: z.string().min(1).max(255).optional(),
domain: z.string().min(1).max(255).optional(),
})
.strict()
.refine((data) => Object.keys(data).length > 0, {
message: "At least one field must be provided for update",
});

View file

@ -25,10 +25,12 @@ const createResourceParamsSchema = z.object({
orgId: z.string(),
});
const createResourceSchema = z.object({
name: z.string().min(1).max(255),
subdomain: z.string().min(1).max(255).optional(),
});
const createResourceSchema = z
.object({
name: z.string().min(1).max(255),
subdomain: z.string().min(1).max(255).optional(),
})
.strict();
export type CreateResourceResponse = Resource;

View file

@ -18,8 +18,9 @@ const updateResourceBodySchema = z
name: z.string().min(1).max(255).optional(),
subdomain: z.string().min(1).max(255).optional(),
ssl: z.boolean().optional(),
siteId: z.number(),
// siteId: z.number(),
})
.strict()
.refine((data) => Object.keys(data).length > 0, {
message: "At least one field must be provided for update",
});

View file

@ -14,10 +14,12 @@ const createRoleParamsSchema = z.object({
orgId: z.string(),
});
const createRoleSchema = z.object({
name: z.string().min(1).max(255),
description: z.string().optional(),
});
const createRoleSchema = z
.object({
name: z.string().min(1).max(255),
description: z.string().optional(),
})
.strict();
export const defaultRoleAllowedActions: ActionsEnum[] = [
ActionsEnum.getOrg,

View file

@ -18,6 +18,7 @@ const updateRoleBodySchema = z
name: z.string().min(1).max(255).optional(),
description: z.string().optional(),
})
.strict()
.refine((data) => Object.keys(data).length > 0, {
message: "At least one field must be provided for update",
});

View file

@ -15,13 +15,15 @@ const createSiteParamsSchema = z.object({
orgId: z.string(),
});
const createSiteSchema = z.object({
name: z.string().min(1).max(255),
exitNodeId: z.number().int().positive(),
subdomain: z.string().min(1).max(255).optional(),
pubKey: z.string(),
subnet: z.string(),
});
const createSiteSchema = z
.object({
name: z.string().min(1).max(255),
exitNodeId: z.number().int().positive(),
subdomain: z.string().min(1).max(255).optional(),
pubKey: z.string(),
subnet: z.string(),
})
.strict();
export type CreateSiteResponse = {
name: string;

View file

@ -23,6 +23,7 @@ const updateSiteBodySchema = z
megabytesIn: z.number().int().nonnegative().optional(),
megabytesOut: z.number().int().nonnegative().optional(),
})
.strict()
.refine((data) => Object.keys(data).length > 0, {
message: "At least one field must be provided for update",
});

View file

@ -15,13 +15,15 @@ const createTargetParamsSchema = z.object({
resourceId: z.string().transform(Number).pipe(z.number().int().positive()),
});
const createTargetSchema = z.object({
ip: z.string().ip(),
method: z.string().min(1).max(10),
port: z.number().int().min(1).max(65535),
protocol: z.string().optional(),
enabled: z.boolean().default(true),
});
const createTargetSchema = z
.object({
ip: z.string().ip(),
method: z.string().min(1).max(10),
port: z.number().int().min(1).max(65535),
protocol: z.string().optional(),
enabled: z.boolean().default(true),
})
.strict();
export type CreateTargetResponse = Target;
@ -104,6 +106,7 @@ export async function createTarget(
.insert(targets)
.values({
resourceId,
protocol: "tcp", // hard code for now
...targetData,
})
.returning();

View file

@ -15,12 +15,12 @@ const updateTargetParamsSchema = z.object({
const updateTargetBodySchema = z
.object({
// ip: z.string().ip().optional(), // for now we cant update the ip; you will have to delete
ip: z.string().ip().optional(), // for now we cant update the ip; you will have to delete
method: z.string().min(1).max(10).optional(),
port: z.number().int().min(1).max(65535).optional(),
protocol: z.string().optional(),
enabled: z.boolean().optional(),
})
.strict()
.refine((data) => Object.keys(data).length > 0, {
message: "At least one field must be provided for update",
});