feat(api): implement per-user edit token for settings mutations

This commit is contained in:
shay
2026-04-19 09:34:26 -04:00
parent 3ecb6ad67b
commit 016583c9f3
16 changed files with 774 additions and 116 deletions
+12 -2
View File
@@ -5,6 +5,7 @@ import { WorkshopScreen } from './screens/WorkshopScreen'
import { ResultScreen } from './screens/ResultScreen'
import type { WidgetData } from './lib/renderWidgets'
import { DEMO_WIDGET_DATA } from './lib/demoData'
import { authHeaders, saveEditToken } from './lib/editToken'
export type Achievement = {
title: string
@@ -86,7 +87,13 @@ export default function App() {
try {
const r = await fetch(`/api/${encodeURIComponent(user)}/data`)
if (aborted.current) return
const body = await r.json().catch(() => ({} as { status?: string; data?: WidgetData }))
const body = await r.json().catch(
() => ({} as { status?: string; data?: WidgetData; edit_token?: string })
)
// The 202 auto-enroll response surfaces an edit_token exactly once
// per fresh username. Persist it so PATCH /settings and POST
// /refresh can authenticate this browser as the profile owner.
if (body.edit_token) saveEditToken(user, body.edit_token)
const status = body.status
if (r.status === 200 && status === 'ready' && body.data) {
@@ -149,7 +156,10 @@ export default function App() {
}
const patchRes = await fetch(`/api/${encodeURIComponent(username)}/settings`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
headers: {
'Content-Type': 'application/json',
...authHeaders(username),
},
body: JSON.stringify(backendSettings),
})
if (!patchRes.ok && patchRes.status !== 404) {
@@ -1,7 +1,28 @@
import { useState, useMemo, type ReactNode } from 'react'
import DOMPurify from 'dompurify'
import type { WidgetSettings, Achievement, PerWidgetSettings } from '../App'
import { renderAllWidgets, type WidgetData } from '../lib/renderWidgets'
// Defense-in-depth: the client-rendered preview assembles SVG from
// GitHub-sourced strings (collaborator logins, repo names) and user-typed
// achievement text. renderWidgets escapes known fields, but one missed
// interpolation anywhere upstream would turn into HTML in the preview.
// DOMPurify neutralises that without affecting normal SVG shapes/styles.
const SVG_SANITIZE_CONFIG = {
USE_PROFILES: { svg: true, svgFilters: true },
ADD_TAGS: [
'image', 'use', 'clipPath', 'defs', 'style', 'animate', 'animateTransform',
'feDropShadow', 'filter', 'linearGradient', 'stop',
],
ADD_ATTR: [
'xlink:href', 'href', 'clip-path', 'viewBox', 'preserveAspectRatio',
'xmlns', 'xmlns:xlink', 'rx', 'ry', 'attributeName', 'from', 'to', 'dur',
'fill', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-linecap',
'stroke-linejoin', 'dominant-baseline', 'letter-spacing', 'opacity',
'filter', 'flood-color', 'flood-opacity', 'stdDeviation',
],
}
const THEMES = [
{ id: 'midnight', label: 'Midnight', color: '#121820' },
{ id: 'onyx', label: 'Onyx', color: '#09090b' },
@@ -254,7 +275,7 @@ export function WorkshopScreen({
// Render SVG client-side whenever settings change (instant, no API call)
const svgContent = useMemo(() => {
if (!widgetData) return ''
return renderAllWidgets({
const raw = renderAllWidgets({
data: widgetData,
theme: settings.theme,
widgets: settings.widgets,
@@ -264,6 +285,7 @@ export function WorkshopScreen({
hiddenLanguages: settings.hiddenLanguages,
username,
})
return DOMPurify.sanitize(raw, SVG_SANITIZE_CONFIG)
}, [widgetData, settings, username])
return (