Files
Triple-C/app/src/components/layout/HelpDialog.test.tsx
T
shadow-testandClaude Opus 5 092972fe92 security: close capability, CSP and auth-bridge holes
capabilities/default.json
- Drop every `store:*` grant. `@tauri-apps/plugin-store` has no caller in
  `app/src`, and the plugin's `resolve_store_path` is a `PathBuf::push` against
  AppData — `push` discards the base for an absolute path, so the grant was an
  arbitrary host read/write from the webview.
- Replace `opener:default` with a scoped `opener:allow-open-url` (http/https
  only). That drops `reveal_item_in_dir`, which the plugin does not scope-check
  and nothing here calls, and the unused mailto:/tel: scope.
- Record the unscopable `drag:allow-start-drag` residual risk in `description`.

tauri.conf.json
- Add `form-action 'none'`, `base-uri 'none'`, `object-src 'none'`.
  `form-action` has no `default-src` fallback, so an injected auto-submitting
  form was unblocked even though `script-src 'self'` blocks XSS.
- Remove the dead `asset:` / `https://asset.localhost` img-src and `data:`
  font-src grants; `blob:` stays (the file viewer uses it).

auth_bridge
- The reserved-port set covered only this project's mappings and the two
  browser-view ranges. It now also covers the gateway, STT and web-terminal
  host ports (configured value and shipped default, read off the settings
  models) and every other project's published host port. A container binding
  container-loopback 4000 / 9876 / 7681 while those services were stopped had
  that port mirrored onto the host, unauthenticated, within one poll.
- Gate the host listener on fetch metadata: refuse a request that is a
  cross-site sub-resource, allow navigations (the OAuth redirect) and anything
  without `Sec-Fetch-*`. Non-HTTP connections are classified from their first
  line and forwarded verbatim. Residual risk is spelled out in the module docs.
- Bound the forwards: max concurrent connections per port, a first-byte
  deadline enforced before any `docker exec` is created, and an idle timeout.

browser_view/mod.rs
- `pick_viewer_port` reads procfs with `/usr/bin/cat`, not a bare `cat` the
  container can shim via its writable PATH entry.
- Treat port choice as check-then-bind: walk to the next free candidate when
  the viewer does not come up, instead of failing the start.

BrowserTab.tsx
- Sandbox the viewer iframe. Container-controlled content could `top.location`
  the app's webview away. `allow-top-navigation*` and
  `allow-popups-to-escape-sandbox` are deliberately absent.

HelpDialog.tsx
- Escape the quote characters in the entity pass and escape captured attribute
  values. `href="$2"` with `$2` = `[^)]+` let remote GitHub markdown close the
  attribute and open another, in a document rendered with
  `dangerouslySetInnerHTML`.

web_terminal/terminal.html
- SRI hashes plus `crossorigin` on the three jsdelivr bundles and the
  stylesheet, and a CSP for the page — it is served 0.0.0.0 behind a permissive
  CORS layer and nothing else gives it one.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GBq2rGum6GX7xXgsas1fDc
2026-08-23 11:13:15 -07:00

101 lines
4.2 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { renderMarkdown } from "./HelpDialog";
/**
* `renderMarkdown` builds HTML by regex substitution and the result is handed
* to `dangerouslySetInnerHTML`. Its input is the help document, which is
* fetched from GitHub at runtime — remote, versioned by someone else, and not
* something the app gets to trust. These tests pin the escaping.
*/
/**
* Parse rendered HTML and return its first anchor, asserting that *no* element
* anywhere in the output grew an attribute outside the allowed set. A broken
* attribute value is only interesting if it becomes an attribute, so the check
* has to run through a real parser rather than over the string.
*/
const ALLOWED_ATTRS = new Set(["class", "href", "target", "rel", "id"]);
function onlyAnchor(html: string): HTMLAnchorElement {
const doc = new DOMParser().parseFromString(html, "text/html");
for (const el of Array.from(doc.body.querySelectorAll("*"))) {
for (const name of attrNames(el)) {
expect(ALLOWED_ATTRS.has(name), `unexpected attribute ${name}`).toBe(true);
}
}
const anchors = doc.querySelectorAll("a");
expect(anchors.length).toBeGreaterThan(0);
return anchors[0] as HTMLAnchorElement;
}
/** Attribute names the parser actually saw on an element. */
function attrNames(el: Element): string[] {
return Array.from(el.attributes).map((a) => a.name);
}
describe("renderMarkdown escaping", () => {
it("escapes the quote characters an attribute value is delimited by", () => {
const html = renderMarkdown('He said "hi" and it\'s fine.');
expect(html).not.toMatch(/said "hi"/);
expect(html).toContain("&quot;hi&quot;");
expect(html).toContain("it&#39;s");
});
it("does not let a link target break out of href=\"…\"", () => {
// The sink: the URL capture is `[^)]+`, which includes `"` and spaces, and
// the value lands directly inside `href="…"`. Asserted through the DOM,
// not by string matching — the payload text legitimately survives *inside*
// the attribute value; what must not happen is it becoming an attribute.
const a = onlyAnchor(
renderMarkdown(
'[click](https://example.com/" onmouseover="steal() formaction="https://evil.example)',
),
);
expect(attrNames(a)).toEqual(["class", "href", "target", "rel"]);
expect(a.getAttribute("href")).toContain('" onmouseover="');
});
it("does not let an in-document anchor break out of href=\"#…\"", () => {
const a = onlyAnchor(
renderMarkdown('[jump](#top" onfocus="steal() autofocus="x)'),
);
expect(attrNames(a)).toEqual(["class", "href"]);
});
it("does not let a bare URL break out of href=\"…\"", () => {
const a = onlyAnchor(
renderMarkdown('See https://example.com/a"onmouseover="steal()\n'),
);
expect(attrNames(a)).toEqual(["class", "href", "target", "rel"]);
});
it("still renders ordinary links intact", () => {
const html = renderMarkdown("[docs](https://example.com/a?x=1&y=2)");
// `&` was entity-escaped by the first pass and must not be escaped twice.
expect(html).toContain('href="https://example.com/a?x=1&amp;y=2"');
expect(html).not.toContain("&amp;amp;");
expect(html).toContain('target="_blank"');
expect(html).toContain('rel="noopener noreferrer"');
expect(html).toContain(">docs</a>");
});
it("still renders an in-document anchor link intact", () => {
const html = renderMarkdown("[jump](#getting-started)");
expect(html).toContain('href="#getting-started"');
});
it("keeps header slugs stable across the new quote escaping", () => {
// The regression this guards: quotes now become entities *before*
// `slugify` sees them, and an entity's letters would otherwise survive
// into the id ("claude39s-setup"), silently breaking every
// `[…](#claudes-setup)` in the document.
expect(renderMarkdown("## Claude's setup")).toContain('id="claudes-setup"');
expect(renderMarkdown('## The "safe" mode')).toContain('id="the-safe-mode"');
});
it("still refuses to emit raw tags from the source document", () => {
const html = renderMarkdown("<img src=x onerror=alert(1)>");
expect(html).not.toContain("<img");
expect(html).toContain("&lt;img");
});
});