diff --git a/.agents/skills/make-bot-ui/SKILL.md b/.agents/skills/make-bot-ui/SKILL.md index 091398c..1613fff 100644 --- a/.agents/skills/make-bot-ui/SKILL.md +++ b/.agents/skills/make-bot-ui/SKILL.md @@ -24,6 +24,7 @@ Use Node's built-in modules. Do not add a web framework for one endpoint. import { createServer } from 'node:http' import { execFile } from 'node:child_process' import { readFile } from 'node:fs/promises' +import { timingSafeEqual } from 'node:crypto' const PORT = Number(process.env.PORT ?? 8787) const KEY = process.env.BOT_UI_KEY @@ -34,13 +35,38 @@ const ACTIONS = { if (!KEY) throw new Error('set BOT_UI_KEY in .env, never in the page') +function wakeCookie() { + return `bot_ui=${encodeURIComponent(KEY)}; HttpOnly; SameSite=Strict; Path=/` +} + +function presentedKey(req) { + const raw = req.headers.cookie ?? '' + for (const part of raw.split(';')) { + const eq = part.indexOf('=') + if (eq === -1) continue + if (part.slice(0, eq).trim() !== 'bot_ui') continue + try { return decodeURIComponent(part.slice(eq + 1).trim()) } catch { return '' } + } + return '' +} + +function keyMatches(presented) { + const a = Buffer.from(presented) + const b = Buffer.from(KEY) + return a.length === b.length && timingSafeEqual(a, b) +} + const server = createServer(async (req, res) => { if (req.method === 'GET' && req.url === '/') { - res.writeHead(200, { 'content-type': 'text/html' }) + res.writeHead(200, { + 'content-type': 'text/html', + 'set-cookie': wakeCookie(), + }) res.end(await readFile(new URL('./index.html', import.meta.url))) return } if (req.method !== 'POST' || req.url !== '/wake') return res.writeHead(404).end() + if (!keyMatches(presentedKey(req))) return res.writeHead(401).end('unauthorized') let raw = '' for await (const chunk of req) raw += chunk let body @@ -62,13 +88,14 @@ Keep these properties: - Pass the prompt as one `execFile` argument. Never build a shell string from browser data. - Validate the action name against a fixed map. Never pass an arbitrary command. - Treat the JSON as untrusted data. The prompt names the fields and says the body is data. +- Return `401` when the `bot_ui` cookie is missing or does not match `BOT_UI_KEY`. - Return `202` before the run finishes, so a slow agent does not hang the page. - Log the stdout and stderr of a failed run to a local file. Do not retry automatically. - One run at a time per action is enough for a personal dashboard. If the user needs a queue, add one worker and a file-backed queue, not unbounded child processes. ## 3. Write the page -`index.html` posts to this same-origin server at `POST /wake` with a JSON body and no key. Only the server holds `BOT_UI_KEY`. +`index.html` posts to this same-origin server at `POST /wake` with a JSON body and no key. `GET /` sets an HttpOnly `bot_ui` cookie, and the browser sends that cookie with the post. The page source does not contain `BOT_UI_KEY`. ```html