fix: clamp page margin values to [0, 100] to prevent crash on paste

When a user pastes a large value (e.g. '1224') into the margin
input field, the value bypasses the HTML <input type='number' max='100'>
and gets stored directly into resume metadata. The PDF renderer then
cannot handle an absurdly large margin, crashing the builder entirely.

This fix adds two layers of defense:
1. Zod schema: add .max(100) to marginX and marginY in pageSchema,
   so any value > 100 is rejected at the validation level.
2. Input handler: clamp the onChange value to [0, 100] so the input
   never even temporarily stores an out-of-range value.

The same pattern can apply to any other numeric inputs that need
a reasonable upper bound.

Fixes: #3263
This commit is contained in:
Emran-Y
2026-07-29 15:57:32 +00:00
committed by Cursor Agent
parent b303b89758
commit 3e2c991344
2 changed files with 14 additions and 8 deletions
@@ -53,11 +53,16 @@ function PageSectionForm() {
persist({ ...form.state.values, [name]: value });
};
const pageNumberFields = [
{ name: "marginX" as const, label: <Trans>Margin (Horizontal)</Trans>, min: 0, max: 100 as number | undefined },
{ name: "marginY" as const, label: <Trans>Margin (Vertical)</Trans>, min: 0, max: 100 as number | undefined },
{ name: "gapX" as const, label: <Trans>Spacing (Horizontal)</Trans>, min: 0, max: undefined },
{ name: "gapY" as const, label: <Trans>Spacing (Vertical)</Trans>, min: 0, max: undefined },
const CLAMP_MIN = 0;
const CLAMP_MAX = 100;
const clamp = (value: number, min: number, max: number) => Math.min(Math.max(value, min), max);
const pageNumberFields = [
{ name: "marginX" as const, label: <Trans>Margin (Horizontal)</Trans>, min: CLAMP_MIN, max: CLAMP_MAX },
{ name: "marginY" as const, label: <Trans>Margin (Vertical)</Trans>, min: CLAMP_MIN, max: CLAMP_MAX },
{ name: "gapX" as const, label: <Trans>Spacing (Horizontal)</Trans>, min: 0, max: undefined },
{ name: "gapY" as const, label: <Trans>Spacing (Vertical)</Trans>, min: 0, max: undefined },
];
const pageSwitchFields = [
@@ -151,7 +156,8 @@ function PageSectionForm() {
onBlur={field.handleBlur}
onChange={(e) => {
const v = e.target.value;
const num = v === "" ? ("" as unknown as number) : Number(v);
const raw = v === "" ? ("" as unknown as number) : Number(v);
const num = max !== undefined && typeof raw === "number" ? clamp(raw, min ?? 0, max) : raw;
field.handleChange(num);
handleAutoSave(name, num);
}}
+2 -2
View File
@@ -444,8 +444,8 @@ export const layoutSchema = z.object({
export const pageSchema = z.object({
gapX: z.number().min(0).describe("The horizontal gap between the sections of the page, defined in points (pt)."),
gapY: z.number().min(0).describe("The vertical gap between the sections of the page, defined in points (pt)."),
marginX: z.number().min(0).describe("The horizontal margin of the page, defined in points (pt)."),
marginY: z.number().min(0).describe("The vertical margin of the page, defined in points (pt)."),
marginX: z.number().min(0).max(100).describe("The horizontal margin of the page, defined in points (pt)."),
marginY: z.number().min(0).max(100).describe("The vertical margin of the page, defined in points (pt)."),
format: z
.enum(["a4", "letter", "free-form"])
.describe("The format of the page. Can be 'a4', 'letter', or 'free-form'.")