-
Notifications
You must be signed in to change notification settings - Fork 22
Sanitize card HTML at render and add a Content-Security-Policy #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| import Link from "next/link"; | ||
| import { useCallback, useEffect, useMemo, useRef, useState } from "react"; | ||
| import { cardDraftKey, keepSelectedCard, nextCardAfterRemoval } from "../lib/card-focus"; | ||
| import { sanitizeCardHtml } from "../lib/card-html"; | ||
| import { cardShortcut } from "../lib/card-shortcut"; | ||
| import { clusterForCard, type Topic } from "../lib/card-cluster"; | ||
| import { compareByImpact, impactPoints } from "../lib/rise"; | ||
|
|
@@ -177,7 +178,10 @@ function AgentCard({ idea, actionable, onAction, onInteraction }: { idea: Idea; | |
| const detailsState = renderedCardIdRef.current === idea.id | ||
| ? new Map(Array.from(root.querySelectorAll("details"), (detail) => [detail.querySelector("summary")?.textContent, detail.open])) | ||
| : new Map(); | ||
| root.innerHTML = `<style>:host{display:block;font-family:inherit}*{box-sizing:border-box}[data-radar-action]{min-height:44px;cursor:pointer}[data-radar-action="open"]{display:inline-flex!important;align-items:center;gap:.38em}[data-radar-action="open"]::after{content:"↗";font-size:.8em;line-height:1;opacity:.68;transform:translateY(-.08em)}</style>${idea.cardHtml}`; | ||
| const hostStyle = document.createElement("style"); | ||
| hostStyle.textContent = `:host{display:block;font-family:inherit}*{box-sizing:border-box}[data-radar-action]{min-height:44px;cursor:pointer}[data-radar-action="open"]{display:inline-flex!important;align-items:center;gap:.38em}[data-radar-action="open"]::after{content:"↗";font-size:.8em;line-height:1;opacity:.68;transform:translateY(-.08em)}`; | ||
| // Agent-written HTML may carry injected content from the sources it read. | ||
| root.replaceChildren(hostStyle, sanitizeCardHtml(idea.cardHtml)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: When a hostile card is opened from Done, Prompt for AI agents |
||
| root.querySelectorAll('[data-radar-action="change"], [data-radar-action="no"]').forEach((button) => button.remove()); | ||
| root.querySelectorAll("details").forEach((detail) => { | ||
| const open = detailsState.get(detail.querySelector("summary")?.textContent); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,9 +36,11 @@ function canIngest(request: Request) { | |
| return Boolean(expected) && request.headers.get("x-radar-agent-key") === expected; | ||
| } | ||
|
|
||
| // Early feedback for agents, not the security boundary: the host sanitizes card HTML when it | ||
| // renders (lib/card-html.ts) and the page's Content-Security-Policy blocks remote loads. | ||
| function unsafeHtml(html: string) { | ||
| return /<\s*(script|iframe|object|embed|form|meta|base|link|svg|math|a)\b/i.test(html) | ||
| || /\son[a-z]+\s*=/i.test(html) | ||
| || /[\s"'/]on[a-z]+\s*=/i.test(html) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Cards containing ordinary text like Prompt for AI agents |
||
| || /javascript\s*:/i.test(html) | ||
| || /@import\b/i.test(html) | ||
| || /url\s*\(\s*["']?(?:https?:)?\/\//i.test(html) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,29 @@ | ||
| import type { NextConfig } from "next"; | ||
|
|
||
| // Agent-written cards render inside this page. If card HTML ever gets past the sanitizer | ||
| // (lib/card-html.ts), the browser still only loads images, media, fonts and requests from | ||
| // this origin, so a card cannot beacon or send data elsewhere. | ||
| const contentSecurityPolicy = [ | ||
| "default-src 'self'", | ||
| "script-src 'self' 'unsafe-inline'", | ||
| "style-src 'self' 'unsafe-inline'", | ||
| "img-src 'self' data: blob:", | ||
| "media-src 'self' data: blob:", | ||
| "font-src 'self' data:", | ||
| "connect-src 'self'", | ||
| "frame-src 'none'", | ||
| "object-src 'none'", | ||
| "base-uri 'none'", | ||
| "form-action 'self'", | ||
| "frame-ancestors 'none'", | ||
| ].join("; "); | ||
|
|
||
| const nextConfig: NextConfig = { | ||
| /* config options here */ | ||
| async headers() { | ||
| const headers = [{ key: "Content-Security-Policy", value: contentSecurityPolicy }]; | ||
| // vinext's `/:path*` does not match the bare root, where the card feed renders. | ||
| return [{ source: "/", headers }, { source: "/:path*", headers }]; | ||
| }, | ||
| }; | ||
|
|
||
| export default nextConfig; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P3: The CSP description omits its explicit
data:/blob:exceptions, making “to the app itself” inaccurate for images, media, and fonts. Mention those allowed inline URL schemes so card authors understand which non-network assets remain supported.Prompt for AI agents