fix: 2026-06-09 full-codebase audit hardening
Validation: shared NaN-guarded Zod coercion helpers in schemas/common.ts replace the raw number|string transform idiom across every schema (the root-cause NaN bug class); emailOrEmpty + lenient isoDateString/timeString. Security: roles privilege-escalation closed; refresh-token family revocation on reuse; TOTP uses config params; read endpoints permission-guarded; received-invoices gross VAT on all paths; orders-pdf custom-items authz. Concurrency: $queryRaw SELECT...FOR UPDATE locks in ascending-id order (warehouse confirm/cancel, attendance lockUserRow); uniqueness checks moved into create transactions (TOCTOU -> 409); deterministic id tiebreak on second-precision timestamp ordering (plan resolveCell/resolveGrid, warehouse FIFO). Frontend: Rules-of-Hooks fixed across ~14 pages + PlanCellModal; UTC-date persisted fields; dashboard invalidation gaps; stale-closure confirm bugs. Tooling/tests: ESLint flat config (react-hooks/rules-of-hooks = error) + Prettier; tsconfig.test.json so tsc -b type-checks the tests; removed 3 dead deps; npm audit fix (8 -> 3). Suite 195 -> 247 (happy-path auth, FIFO oldest-first, flakiness fixes), isolated on app_test via .env.test with a hard-throw setup guard. Gates: tsc 0 | build 0 | vitest 247/247 | eslint 0 errors. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,104 +1,68 @@
|
||||
import { z } from "zod";
|
||||
import {
|
||||
numberFromForm,
|
||||
numberInRange,
|
||||
nonNegativeNumberFromForm,
|
||||
positiveNumberFromForm,
|
||||
nullableIntIdFromForm,
|
||||
booleanFromForm,
|
||||
} from "./common";
|
||||
|
||||
const InvoiceItemSchema = z.object({
|
||||
description: z.string().nullish(),
|
||||
item_description: z.string().nullish(),
|
||||
quantity: z
|
||||
.union([z.number(), z.string()])
|
||||
.transform((v) => {
|
||||
const n = Number(v);
|
||||
if (isNaN(n) || n <= 0) throw new Error("Invalid quantity");
|
||||
return n;
|
||||
})
|
||||
.optional()
|
||||
.default(1),
|
||||
unit: z.string().nullish(),
|
||||
unit_price: z
|
||||
.union([z.number(), z.string()])
|
||||
.transform((v) => {
|
||||
const n = Number(v);
|
||||
if (isNaN(n)) throw new Error("Invalid unit_price");
|
||||
return n;
|
||||
})
|
||||
.optional()
|
||||
.default(0),
|
||||
vat_rate: z
|
||||
.union([z.number(), z.string()])
|
||||
.transform((v) => Number(v))
|
||||
.optional()
|
||||
.default(21.0),
|
||||
position: z
|
||||
.union([z.number(), z.string()])
|
||||
.transform((v) => Number(v))
|
||||
.optional(),
|
||||
description: z.string().max(8000).nullish(),
|
||||
item_description: z.string().max(8000).nullish(),
|
||||
quantity: positiveNumberFromForm.default(1),
|
||||
unit: z.string().max(255).nullish(),
|
||||
unit_price: numberFromForm.default(0),
|
||||
vat_rate: numberInRange(0, 100).optional().default(21.0),
|
||||
position: nonNegativeNumberFromForm.optional(),
|
||||
});
|
||||
|
||||
export const CreateInvoiceSchema = z.object({
|
||||
invoice_number: z.string().nullish(),
|
||||
order_id: z
|
||||
.union([z.number(), z.string()])
|
||||
.transform((v) => Number(v))
|
||||
.nullish(),
|
||||
customer_id: z
|
||||
.union([z.number(), z.string()])
|
||||
.transform((v) => Number(v))
|
||||
.nullish(),
|
||||
status: z.string().optional().default("issued"),
|
||||
currency: z.string().optional().default("CZK"),
|
||||
vat_rate: z
|
||||
.union([z.number(), z.string()])
|
||||
.transform((v) => Number(v))
|
||||
.optional()
|
||||
.default(21.0),
|
||||
apply_vat: z
|
||||
.preprocess((v) => v === true || v === 1 || v === "1", z.boolean())
|
||||
.optional()
|
||||
.default(true),
|
||||
payment_method: z.string().nullish(),
|
||||
constant_symbol: z.string().nullish(),
|
||||
bank_name: z.string().nullish(),
|
||||
bank_swift: z.string().nullish(),
|
||||
bank_iban: z.string().nullish(),
|
||||
bank_account: z.string().nullish(),
|
||||
issue_date: z.string().nullish(),
|
||||
due_date: z.string().nullish(),
|
||||
tax_date: z.string().nullish(),
|
||||
issued_by: z.string().nullish(),
|
||||
billing_text: z.string().nullish(),
|
||||
language: z.string().optional().default("cs"),
|
||||
notes: z.string().nullish(),
|
||||
internal_notes: z.string().nullish(),
|
||||
invoice_number: z.string().max(255).nullish(),
|
||||
order_id: nullableIntIdFromForm.nullish(),
|
||||
customer_id: nullableIntIdFromForm.nullish(),
|
||||
status: z.string().max(20).optional().default("issued"),
|
||||
currency: z.string().max(20).optional().default("CZK"),
|
||||
vat_rate: numberInRange(0, 100).optional().default(21.0),
|
||||
apply_vat: booleanFromForm.optional().default(true),
|
||||
payment_method: z.string().max(255).nullish(),
|
||||
constant_symbol: z.string().max(255).nullish(),
|
||||
bank_name: z.string().max(255).nullish(),
|
||||
bank_swift: z.string().max(255).nullish(),
|
||||
bank_iban: z.string().max(255).nullish(),
|
||||
bank_account: z.string().max(255).nullish(),
|
||||
issue_date: z.string().max(255).nullish(),
|
||||
due_date: z.string().max(255).nullish(),
|
||||
tax_date: z.string().max(255).nullish(),
|
||||
issued_by: z.string().max(255).nullish(),
|
||||
billing_text: z.string().max(8000).nullish(),
|
||||
language: z.string().max(20).optional().default("cs"),
|
||||
notes: z.string().max(8000).nullish(),
|
||||
internal_notes: z.string().max(8000).nullish(),
|
||||
items: z.array(InvoiceItemSchema).optional(),
|
||||
});
|
||||
|
||||
export const UpdateInvoiceSchema = z.object({
|
||||
status: z.string().optional(),
|
||||
currency: z.string().optional(),
|
||||
language: z.string().optional(),
|
||||
payment_method: z.string().nullish(),
|
||||
constant_symbol: z.string().nullish(),
|
||||
bank_name: z.string().nullish(),
|
||||
bank_swift: z.string().nullish(),
|
||||
bank_iban: z.string().nullish(),
|
||||
bank_account: z.string().nullish(),
|
||||
issued_by: z.string().nullish(),
|
||||
customer_id: z
|
||||
.union([z.number(), z.string(), z.null()])
|
||||
.transform((v) => (v === null ? null : Number(v)))
|
||||
.optional(),
|
||||
vat_rate: z
|
||||
.union([z.number(), z.string()])
|
||||
.transform((v) => Number(v))
|
||||
.optional(),
|
||||
apply_vat: z
|
||||
.preprocess((v) => v === true || v === 1 || v === "1", z.boolean())
|
||||
.optional(),
|
||||
issue_date: z.union([z.string(), z.null()]).optional(),
|
||||
due_date: z.union([z.string(), z.null()]).optional(),
|
||||
tax_date: z.union([z.string(), z.null()]).optional(),
|
||||
notes: z.string().nullish(),
|
||||
internal_notes: z.string().nullish(),
|
||||
paid_date: z.union([z.string(), z.null()]).optional(),
|
||||
status: z.string().max(20).optional(),
|
||||
currency: z.string().max(20).optional(),
|
||||
language: z.string().max(20).optional(),
|
||||
payment_method: z.string().max(255).nullish(),
|
||||
constant_symbol: z.string().max(255).nullish(),
|
||||
bank_name: z.string().max(255).nullish(),
|
||||
bank_swift: z.string().max(255).nullish(),
|
||||
bank_iban: z.string().max(255).nullish(),
|
||||
bank_account: z.string().max(255).nullish(),
|
||||
issued_by: z.string().max(255).nullish(),
|
||||
customer_id: nullableIntIdFromForm.optional(),
|
||||
vat_rate: numberInRange(0, 100).optional(),
|
||||
apply_vat: booleanFromForm.optional(),
|
||||
issue_date: z.union([z.string().max(255), z.null()]).optional(),
|
||||
due_date: z.union([z.string().max(255), z.null()]).optional(),
|
||||
tax_date: z.union([z.string().max(255), z.null()]).optional(),
|
||||
notes: z.string().max(8000).nullish(),
|
||||
internal_notes: z.string().max(8000).nullish(),
|
||||
paid_date: z.union([z.string().max(255), z.null()]).optional(),
|
||||
items: z.array(InvoiceItemSchema).optional(),
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user