fix(places): pin the image and website URL fields on the way in

image_url and website were bare strings in the contract, and the create/update
bodies are open records, so the Zod pipe never looked at either. Both leave the
database for something that treats them as a URL: the thumbnail is interpolated
into hand-built marker HTML, and the homepage is handed to window.open, where a
javascript: value would run in this origin rather than opening a page.

Both are allow-listed on the way in now, next to hexColorSchema and checked in
the handler the way validateRouteColor already is, on all three write surfaces
so REST, the plugin RPC and MCP agree on what they accept. The entity schemas
stay open: rows written before this still have to round-trip.

safeHttpUrl covers the read side for exactly those rows, the same split
safeHexColor uses, and the window.open calls it guards pick up
noopener,noreferrer while they are being touched.
This commit is contained in:
Maurice
2026-08-16 21:37:52 +02:00
committed by Maurice
parent 0f0bf50cd3
commit c8607ba234
15 changed files with 205 additions and 19 deletions
+35
View File
@@ -0,0 +1,35 @@
import { describe, it, expect } from 'vitest'
import { safeHttpUrl } from './safeUrl'
describe('safeHttpUrl', () => {
it('FE-UTIL-SAFEURL-001: passes an ordinary https homepage through', () => {
expect(safeHttpUrl('https://louvre.fr/en/visit')).toBe('https://louvre.fr/en/visit')
})
it('FE-UTIL-SAFEURL-002: passes plain http through — plenty of small venues still run it', () => {
expect(safeHttpUrl('http://pension-alpenblick.at')).toBe('http://pension-alpenblick.at')
})
it('FE-UTIL-SAFEURL-003: rejects javascript:, which window.open would run in this origin', () => {
expect(safeHttpUrl('javascript:fetch("/api/trips")')).toBeNull()
expect(safeHttpUrl('JavaScript:alert(1)')).toBeNull()
expect(safeHttpUrl(' javascript:alert(1)')).toBeNull()
})
it('FE-UTIL-SAFEURL-004: rejects the other schemes that are not a web page', () => {
expect(safeHttpUrl('data:text/html,<script>alert(1)</script>')).toBeNull()
expect(safeHttpUrl('vbscript:msgbox(1)')).toBeNull()
expect(safeHttpUrl('file:///etc/passwd')).toBeNull()
})
it('FE-UTIL-SAFEURL-005: rejects a bare host, since window.open would treat it as a relative path', () => {
expect(safeHttpUrl('louvre.fr')).toBeNull()
expect(safeHttpUrl('//evil.example')).toBeNull()
})
it('FE-UTIL-SAFEURL-006: treats absent and empty as nothing to open', () => {
expect(safeHttpUrl(null)).toBeNull()
expect(safeHttpUrl(undefined)).toBeNull()
expect(safeHttpUrl('')).toBeNull()
})
})
+18
View File
@@ -0,0 +1,18 @@
import { placeWebsiteSchema } from '@trek/shared'
/**
* Allow-list a URL before handing it to window.open.
*
* A place's website is a plain string in the older contracts, and it reaches
* window.open from the inspector, both sidebar context menus and the mobile
* place sheet. `window.open('javascript:…')` does not open a page — it evaluates
* the script in a fresh document that inherits this origin, so a co-traveller
* with place_edit could plant one and wait for someone to click Website.
*
* Server-side validation closes the write path. This closes the read path, which
* is what covers rows written before that validation existed and instances that
* have not updated yet — the same split as safeHexColor.
*/
export function safeHttpUrl(value: string | null | undefined): string | null {
return typeof value === 'string' && placeWebsiteSchema.safeParse(value).success ? value : null
}