Cover illustration safety and operations
HipTrip trips start with a stylized SVG illustration (see Viewing Itineraries). Most come from the curated collection in public/illustrations/. When nothing in the collection fits, or when an owner asks from the Change cover picker, a model writes a new SVG. Model output is untrusted code, so it goes through a fixed pipeline before anything is stored or shown.
Pipeline
- Brief. At trip creation the fast model answers
{ kind: "pick", illustrationId }or{ kind: "generate", brief }. From the picker the brief is the owner's one-line prompt (max 200 characters, control characters stripped) or a default derived from the trip. The brief is only ever a scene description; the system prompt tells the model to ignore instructions inside it. - Generate.
lib/cover-illustration-generator.tscalls the model configured byMODEL_ILLUSTRATIONinlib/models.tswith the house-style prompt inlib/cover-illustration-style.ts(viewBox 1600x1000, flat layered shapes, HipTrip palette, no text, no people, focal point in the central band, two curated exemplars). - Sanitize.
lib/svg-sanitize.ts(below). - Render check.
lib/svg-render-check.tsrasterizes the sanitized SVG at 400px wide withsharpand rejects it if it fails to render, is mostly transparent, or more than 90% of pixels are one flat color. - Store. The sanitizer's re-serialized string (never the model's raw text) is uploaded to Vercel Blob at
illustrations/generated/<uuid>.svg, public,image/svg+xml, one-year cache. The URL is saved inItinerary.coverImage; there is no schema change. - Serve. Covers render through an
<img>element only (next/image, unoptimized for illustrations). Script inside an SVG loaded through<img>never runs, andtests/unit/lib/no-inline-generated-svg.test.tsguards againstdangerouslySetInnerHTMLor<object>/<embed>/<iframe>ever being used for covers.
isIllustrationUrl() recognizes both /illustrations/<id>.svg and Blob URLs under illustrations/generated/, so generated art is treated as artwork everywhere: it is never given seasonal or attribution labels and never used as a stop photo.
Sanitizer guarantees
sanitizeSvg() is an allowlist sanitizer with its own tokenizer (no new parsing dependency). It either returns a re-serialized SVG or rejects the whole input with a reason; it never "repairs" dangerous markup.
Accepted output contains only:
- Elements:
svg,g,defs,linearGradient,radialGradient,stop,path,circle,ellipse,rect,polygon,polyline,line,clipPath,use,symbol. - Attributes, each value-validated: geometry (
d,points,cx,cy,r,rx,ry,fx,fy,x,y,x1,y1,x2,y2,width,height,offset), paint (fill,stroke,stop-color, opacities, stroke properties,fill-rule,clip-rule),transform/gradientTransform(onlytranslate,scale,rotate,matrix,skewX,skewYwith numbers),gradientUnits,spreadMethod,id,preserveAspectRatio,clip-path="url(#id)",fill="url(#id)", and<use href="#id">. - References only to ids defined in the same document. A
<use>may not point at content that itself contains<use>(no cycles, no use-bombs). - The root is rebuilt:
viewBox="0 0 1600 1000", fixed width and height, canonicalxmlns.
Rejected outright (not stripped): script, foreignObject, image, style elements and attributes, a, animation (animate*, set), text, any on* attribute, javascript:, data:, http(s):, file: and any other URL scheme or non-fragment reference, xlink:href to anything but a local fragment, DOCTYPE, entities, CDATA, processing instructions (including the <?xml?> declaration), non-ASCII or control characters, text nodes, nested <svg>, a missing or wrong viewBox, duplicate ids or attributes, malformed or unclosed markup, input over 40 KB, more than 1,500 elements, or nesting deeper than 20. (The generator strips markdown fences and chatter around the <svg> element before sanitizing.)
Unknown but harmless attributes (for example class) are dropped, not emitted.
The curated collection passes the sanitizer, which tests/unit/lib/svg-sanitize.test.ts asserts alongside the adversarial corpus.
Limits and failure behavior
| Where | Budget | On failure |
|---|---|---|
| Trip creation (selection + generation) | about 25 s total; selection 8 s | falls back to the closest curated illustration; the trip is never blocked or failed |
| Owner picker | 55 s for the model, route maxDuration 90 s | "Couldn't draw that one — try again"; cover unchanged |
| Rate limit (picker) | 3 per itinerary per hour, 10 per user per hour (Postgres rate_limits) | friendly hourly-limit message |
Generation from the picker is owner-only (auth and ownership are checked before any model spend). A fully authored SVG typically takes 30 to 50 seconds, so at trip creation the 25 s budget means generated covers will often fall back to the collection; the picker has the longer budget.
Set COVER_ILLUSTRATION_GENERATION=off to disable generation everywhere (selection keeps working; "generate" answers fall back to the collection).
Structured logs (scope: "cover-illustration"; events requested, accepted, rejected with a reason, fallback, selection_pick, selection_generate, rate_limited) carry only the itinerary id, a coarse reason and sizes. They never include the SVG or the brief. Model calls are also traced through the AI SDK telemetry (cover-illustration-selection, cover-illustration-generation).
Replaced generated blobs are not deleted yet (follow-up: a cleanup job for illustrations/generated/ blobs no longer referenced by any itinerary).
Promoting a generated illustration into the collection
Generated art is private to its trip and is never published into the shared collection automatically. To promote one after a human review:
pnpm dlx tsx scripts/promote-illustration.ts <blob-url> --id onsen-town --title "Onsen town" --tags "onsen,hot spring,japan"
# or: --itinerary <itineraryId> (needs DATABASE_URL)
The script re-sanitizes the SVG, writes public/illustrations/<id>.svg, and prints the manifest entry to paste into COVER_ILLUSTRATIONS in lib/cover-illustrations.ts. The first tag is the scene's signature tag and must be unique across the manifest (the script checks). Review the SVG visually, run pnpm test, and commit both files.