refactor: better form decoding

This commit is contained in:
axel 2025-04-19 16:56:41 +02:00
parent 1e32591387
commit 464905749b
7 changed files with 27 additions and 27 deletions

7
package-lock.json generated
View File

@ -13,6 +13,7 @@
"@types/wake_on_lan": "^0.0.33", "@types/wake_on_lan": "^0.0.33",
"bcryptjs": "^3.0.2", "bcryptjs": "^3.0.2",
"bits-ui": "^1.3.19", "bits-ui": "^1.3.19",
"decode-formdata": "^0.9.0",
"drizzle-orm": "^0.41.0", "drizzle-orm": "^0.41.0",
"humanize-duration": "^3.32.1", "humanize-duration": "^3.32.1",
"lowdb": "^7.0.1", "lowdb": "^7.0.1",
@ -1700,6 +1701,12 @@
} }
} }
}, },
"node_modules/decode-formdata": {
"version": "0.9.0",
"resolved": "https://registry.npmjs.org/decode-formdata/-/decode-formdata-0.9.0.tgz",
"integrity": "sha512-q5uwOjR3Um5YD+ZWPOF/1sGHVW9A5rCrRwITQChRXlmPkxDFBqCm4jNTIVdGHNH9OnR+V9MoZVgRhsFb+ARbUw==",
"license": "MIT"
},
"node_modules/deepmerge": { "node_modules/deepmerge": {
"version": "4.3.1", "version": "4.3.1",
"resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-4.3.1.tgz", "resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-4.3.1.tgz",

View File

@ -35,6 +35,7 @@
"@types/wake_on_lan": "^0.0.33", "@types/wake_on_lan": "^0.0.33",
"bcryptjs": "^3.0.2", "bcryptjs": "^3.0.2",
"bits-ui": "^1.3.19", "bits-ui": "^1.3.19",
"decode-formdata": "^0.9.0",
"drizzle-orm": "^0.41.0", "drizzle-orm": "^0.41.0",
"humanize-duration": "^3.32.1", "humanize-duration": "^3.32.1",
"lowdb": "^7.0.1", "lowdb": "^7.0.1",

View File

@ -4,6 +4,7 @@ import type { Updated } from '$lib/server/db/types';
import { toPublicUser, type User } from '$lib/server/db/types/user.js'; import { toPublicUser, type User } from '$lib/server/db/types/user.js';
import { type Actions, type ServerLoad } from '@sveltejs/kit'; import { type Actions, type ServerLoad } from '@sveltejs/kit';
import bcrypt from 'bcryptjs'; import bcrypt from 'bcryptjs';
import { decode } from 'decode-formdata';
import { z } from 'zod'; import { z } from 'zod';
export const load: ServerLoad = async ({ locals: { guard }, params }) => { export const load: ServerLoad = async ({ locals: { guard }, params }) => {
@ -31,7 +32,7 @@ export const actions = {
}), }),
}); });
const parsed = schema.safeParse(Object.fromEntries(await request.formData())); const parsed = schema.safeParse(decode(await request.formData()));
if (!parsed.success) return PARSE_ERROR(parsed.error); if (!parsed.success) return PARSE_ERROR(parsed.error);
let updatedUser: Updated<User> = { id: user.id, ...parsed.data }; let updatedUser: Updated<User> = { id: user.id, ...parsed.data };

View File

@ -1,6 +1,7 @@
import { FORBIDDEN, PARSE_ERROR, SUCCESS } from '$lib/server/commonResponses'; import { FORBIDDEN, PARSE_ERROR, SUCCESS } from '$lib/server/commonResponses';
import { devices, users } from '$lib/server/db/index.js'; import { devices, users } from '$lib/server/db/index.js';
import { fail, redirect, type Actions, type ServerLoad } from '@sveltejs/kit'; import { fail, redirect, type Actions, type ServerLoad } from '@sveltejs/kit';
import { decode } from 'decode-formdata';
import validator from 'validator'; import validator from 'validator';
import { wake } from 'wake_on_lan'; import { wake } from 'wake_on_lan';
import { z } from 'zod'; import { z } from 'zod';
@ -23,8 +24,6 @@ export const actions = {
update: async ({ request, params, locals: { guard } }) => { update: async ({ request, params, locals: { guard } }) => {
if (guard.requiresAdmin().isFailed()) return FORBIDDEN; if (guard.requiresAdmin().isFailed()) return FORBIDDEN;
const form = await request.formData();
const schema = z.object({ const schema = z.object({
name: z name: z
.string({ message: 'Name is required.' }) .string({ message: 'Name is required.' })
@ -46,13 +45,7 @@ export const actions = {
.max(50, { message: 'Packets quantity must be at most 50.' }), .max(50, { message: 'Packets quantity must be at most 50.' }),
}); });
const parsed = schema.safeParse({ const parsed = schema.safeParse(decode(await request.formData()));
name: form.get('name'),
mac: form.get('mac'),
broadcast: form.get('broadcast'),
port: form.get('port'),
packets: form.get('packets'),
});
if (!parsed.success) { if (!parsed.success) {
return PARSE_ERROR(parsed.error); return PARSE_ERROR(parsed.error);

View File

@ -1,6 +1,7 @@
import { FORBIDDEN, PARSE_ERROR, SUCCESS } from '$lib/server/commonResponses'; import { FORBIDDEN, PARSE_ERROR, SUCCESS } from '$lib/server/commonResponses';
import { devices, groups } from '$lib/server/db'; import { devices, groups } from '$lib/server/db';
import { redirect, type Actions, type ServerLoad } from '@sveltejs/kit'; import { redirect, type Actions, type ServerLoad } from '@sveltejs/kit';
import { decode } from 'decode-formdata';
import { z } from 'zod'; import { z } from 'zod';
export const load: ServerLoad = async ({ locals: { guard }, params }) => { export const load: ServerLoad = async ({ locals: { guard }, params }) => {
@ -22,8 +23,6 @@ export const actions = {
update: async ({ request, locals: { guard }, params }) => { update: async ({ request, locals: { guard }, params }) => {
if (guard.requiresAdmin().isFailed()) return FORBIDDEN; if (guard.requiresAdmin().isFailed()) return FORBIDDEN;
const form = await request.formData();
const schema = z.object({ const schema = z.object({
name: z name: z
.string({ message: 'Name is required.' }) .string({ message: 'Name is required.' })
@ -32,10 +31,11 @@ export const actions = {
devices: z.array(z.string()), devices: z.array(z.string()),
}); });
const parsed = schema.safeParse({ const parsed = schema.safeParse(
name: form.get('name'), decode(await request.formData(), {
devices: form.getAll('devices'), arrays: ['devices'],
}); }),
);
if (!parsed.success) { if (!parsed.success) {
return PARSE_ERROR(parsed.error); return PARSE_ERROR(parsed.error);

View File

@ -4,6 +4,7 @@ import type { Updated } from '$lib/server/db/types';
import { toPublicUser, type User } from '$lib/server/db/types/user.js'; import { toPublicUser, type User } from '$lib/server/db/types/user.js';
import { redirect, type Actions, type ServerLoad } from '@sveltejs/kit'; import { redirect, type Actions, type ServerLoad } from '@sveltejs/kit';
import bcrypt from 'bcryptjs'; import bcrypt from 'bcryptjs';
import { decode } from 'decode-formdata';
import { z } from 'zod'; import { z } from 'zod';
export const load: ServerLoad = async ({ locals: { guard }, params }) => { export const load: ServerLoad = async ({ locals: { guard }, params }) => {
@ -26,8 +27,6 @@ export const actions = {
update: async ({ request, locals: { guard }, params }) => { update: async ({ request, locals: { guard }, params }) => {
if (guard.requiresAdmin().isFailed()) return FORBIDDEN; if (guard.requiresAdmin().isFailed()) return FORBIDDEN;
const form = await request.formData();
const schema = z.object({ const schema = z.object({
name: z name: z
.string({ message: 'Name is required.' }) .string({ message: 'Name is required.' })
@ -48,13 +47,12 @@ export const actions = {
devices: z.array(z.string()), devices: z.array(z.string()),
}); });
const parsed = schema.safeParse({ const parsed = schema.safeParse(
name: form.get('name'), decode(await request.formData(), {
admin: form.get('admin') === 'on', arrays: ['groups', 'devices'],
password: form.get('password'), booleans: ['admin'],
groups: form.getAll('groups'), }),
devices: form.getAll('devices'), );
});
if (!parsed.success) { if (!parsed.success) {
return PARSE_ERROR(parsed.error); return PARSE_ERROR(parsed.error);

View File

@ -4,6 +4,7 @@ import { users } from '$lib/server/db';
import { createSession } from '$lib/server/sessions'; import { createSession } from '$lib/server/sessions';
import { fail, redirect } from '@sveltejs/kit'; import { fail, redirect } from '@sveltejs/kit';
import bcrypt from 'bcryptjs'; import bcrypt from 'bcryptjs';
import { decode } from 'decode-formdata';
import humanizeDuration from 'humanize-duration'; import humanizeDuration from 'humanize-duration';
import { z } from 'zod'; import { z } from 'zod';
import type { Actions } from './$types'; import type { Actions } from './$types';
@ -60,8 +61,7 @@ export const actions = {
password: z.string({ message: 'Password is required.' }), password: z.string({ message: 'Password is required.' }),
}); });
const data = await request.formData(); const parsed = schema.safeParse(decode(await request.formData()));
const parsed = schema.safeParse(Object.fromEntries(data.entries()));
if (!parsed.success) { if (!parsed.success) {
return PARSE_ERROR(parsed.error); return PARSE_ERROR(parsed.error);