Browse Source

improve zod errors, consistent server errors

pull/1244/head
Bernd Storath 11 months ago
parent
commit
344b6bd2c9
  1. 2
      package.json
  2. 2
      src/package.json
  3. 18
      src/server/utils/WireGuard.ts
  4. 55
      src/server/utils/types.ts

2
package.json

@ -7,5 +7,5 @@
"sudostart": "sudo docker run --env WG_HOST=0.0.0.0 --name wg-easy --cap-add=NET_ADMIN --cap-add=SYS_MODULE --sysctl=\"net.ipv4.conf.all.src_valid_mark=1\" --mount type=bind,source=\"$(pwd)\"/config,target=/etc/wireguard -p 51820:51820/udp -p 51821:51821/tcp wg-easy",
"start": "docker run --env WG_HOST=0.0.0.0 --name wg-easy --cap-add=NET_ADMIN --cap-add=SYS_MODULE --sysctl=\"net.ipv4.conf.all.src_valid_mark=1\" --mount type=bind,source=\"$(pwd)\"/config,target=/etc/wireguard -p 51820:51820/udp -p 51821:51821/tcp wg-easy"
},
"packageManager": "pnpm@9.7.0"
"packageManager": "pnpm@9.7.1"
}

2
src/package.json

@ -47,5 +47,5 @@
"typescript": "^5.5.4",
"vue-tsc": "^2.0.29"
},
"packageManager": "pnpm@9.7.0"
"packageManager": "pnpm@9.7.1"
}

18
src/server/utils/WireGuard.ts

@ -6,14 +6,6 @@ import QRCode from 'qrcode';
const debug = debug_logger('WireGuard');
class ServerError extends Error {
statusCode: number;
constructor(message: string, statusCode = 500) {
super(message);
this.statusCode = statusCode;
}
}
type Server = {
privateKey: string;
publicKey: string;
@ -216,7 +208,10 @@ ${
const config = await this.getConfig();
const client = config.clients[clientId];
if (!client) {
throw new ServerError(`Client Not Found: ${clientId}`, 404);
throw createError({
statusCode: 404,
statusMessage: `Client Not Found: ${clientId}`,
});
}
return client;
@ -356,7 +351,10 @@ Endpoint = ${WG_HOST}:${WG_CONFIG_PORT}`;
const client = await this.getClient({ clientId });
if (!isValidIPv4(address)) {
throw new ServerError(`Invalid Address: ${address}`, 400);
throw createError({
statusCode: 400,
statusMessage: `Invalid Address: ${address}`,
});
}
client.address = address;

55
src/server/utils/types.ts

@ -11,44 +11,59 @@ const safeStringRefine = z
const id = z
.string()
.uuid('Client ID must be a valid UUID')
.and(safeStringRefine);
.pipe(safeStringRefine);
const address = z
.string({ message: 'Address must be a valid string' })
.and(safeStringRefine);
.pipe(safeStringRefine);
const name = z
.string({ message: 'Name must be a valid string' })
.min(1, 'Name must be at least 1 Character')
.and(safeStringRefine);
.pipe(safeStringRefine);
const file = z
.string({ message: 'File must be a valid string' })
.and(safeStringRefine);
.pipe(safeStringRefine);
const password = z
.string({ message: 'Password must be a valid string' })
.and(safeStringRefine);
.pipe(safeStringRefine);
export const clientIdType = z.object({
clientId: id,
});
export const clientIdType = z.object(
{
clientId: id,
},
{ message: "This shouldn't happen" }
);
export const addressType = z.object({
address: address,
});
export const addressType = z.object(
{
address: address,
},
{ message: 'Body must be a valid object' }
);
export const nameType = z.object({
name: name,
});
export const nameType = z.object(
{
name: name,
},
{ message: 'Body must be a valid object' }
);
export const fileType = z.object({
file: file,
});
export const fileType = z.object(
{
file: file,
},
{ message: 'Body must be a valid object' }
);
export const passwordType = z.object({
password: password,
});
export const passwordType = z.object(
{
password: password,
},
{ message: 'Body must be a valid object' }
);
export function validateZod<T>(schema: ZodSchema<T>) {
return async (data: unknown) => {

Loading…
Cancel
Save