Skip to content

Add the template editor: layers, inspector and live preview - #2

Open
mgilardi wants to merge 8 commits into
mainfrom
co/labels-inspector
Open

mgilardi wants to merge 8 commits into
mainfrom
co/labels-inspector

Conversation

@mgilardi

Copy link
Copy Markdown
Contributor

Captive Origins task: K6. Stacks on #1 (base is co/labels-core; retarget to main after #1 merges). Follows: the design's builder section and D13.

What's in it

  • /client editor on the template's elements field: layers list (arrow keys, move up/down, hide, delete, add, announced results), inspector (position, size, rotation, text and tokens, font size, fit rule, barcode type and value), live SVG preview from layout() with an editable sample record, and a warnings list by layer.
  • New peer deps: react, @payloadcms/ui (MIT). No canvas (K7).

Tested: 54 vitest tests (10 new, jsdom); typecheck, build and leak check pass.

Not tested: a real browser, a screen reader, the Payload admin, color contrast, the field wrapper that reads the form.

No version bump.

🤖 Generated with Claude Code

mgilardi and others added 6 commits November 16, 2026 00:14
Starts the build of the first checkpoint: package files copied from the sibling plugin's shape, a Payload-free layout engine (tokens, fitting, sheet positions, the 4 x 6 in preset), the provider contract types, and a plugin that registers the three collections with writes closed by default.

Not tested: any database, PDF or SVG output, the print endpoint, the admin UI, site filtering for scopeField.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reads, creates, updates and deletes now stay inside the person's own scope when scopeField is set, with scopeOf resolving the scope and failing closed. Shared (no scope) templates are readable by all and changed only by a platform admin. Covered on an in-memory MongoDB with two made-up sites.

Not tested: the print endpoint's scope check (not built), a real site, a browser.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Both outputs draw the same marks, with barcodes, QR and Data Matrix as vectors from bwip-js (MIT) and the PDF written with pdfkit (MIT). The PDF writer is its own export so the plugin config does not load pdfkit.

Not tested: PDF in a viewer or printer, SVG in a browser, scanning a printed code, sheet pages, images.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
POST /api/labels/print renders a PDF for given documents and a template. It refuses signed-out requests, people with no scope, another scope's template, any document the person cannot read and more than 2,000 labels, and writes one log entry of ids and counts.

Not tested: over real HTTP, render time at 2,000 labels, sets, media choice, sheet pages.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The plugin now throws at config time unless access.manage or access.print is set, and no longer lets any signed-in person read templates or print. access.print decides printing, otherwise access.manage does. Template lookup tries the key, then the id as given, so numeric ids work on SQL databases.

Not tested: numeric ids on a real SQL database; the cost of reading documents at depth 3.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
An admin field component for a template's elements. A layers list with keyboard selection and announced reorder, hide and delete; an inspector for the selected layer; and an SVG preview from the same layout as printing, with a sample record and a warnings list. Styled with Payload theme variables.

Not tested: a real browser, a screen reader, the Payload admin, color contrast.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Main took the core branch by rebase, so the same changes sit under new
commit ids. Main's tree matches the core branch exactly, so this keeps the
branch tree. Nothing is rewritten. Tests: see pull request.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mgilardi
mgilardi changed the base branch from co/labels-core to main November 16, 2026 16:43
@mgilardi

Copy link
Copy Markdown
Contributor Author

Security and privacy review (EXTANT APE, Head of Research for Simon)

Verdict: changes. The live preview has a stored XSS.

Checked: CI green. Local: 54 of 54 tests pass. No new endpoints, and template access is unchanged. The new dependencies are all MIT. The README's keyboard and announcement claims match the code (listbox with aria-activedescendant, Home and End, the status line, role="alert" for bad JSON).

Fix before merge

  1. Stored XSS in the preview. src/client/TemplateEditor.tsx:137 puts renderSvg output into the page with dangerouslySetInnerHTML. src/core/svg.ts writes stroke-width="${m.thickness}" (lines 29 and 31) and rotate(${m.rotation} ...) (line 40) without n(). layout.ts passes el.thickness and el.rotation through unchecked (lines 20 and 56). The elements json field has no validation, so a manager can save thickness: '0"/><img src=x onerror=...>' through REST, and the script runs for whoever opens the editor. That includes platform admins, who can read every site's templates, so one site's manager can act with an admin's session.
    • Fix: validate elements on the server (finite numbers, element types and enums from a fixed list, strings where strings belong), and pass every number in svg.ts through n(). Add a test that saves a malicious thickness and rotation and expects a refusal.
  2. The same validation stops one bad element from breaking the editor. A non-string content makes el.content.slice throw (TemplateEditor.tsx:29), so nobody can open that template.

Also

🤖 Generated with Claude Code

The review found a stored XSS: thickness and rotation reached the live
preview unescaped, and the elements field had no checks. Elements are now
validated on the server and every SVG number is coerced to a finite number.

Tested: 4 new tests (58 pass). Not tested: the editor in a browser.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant