Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

fix(native): reject bare filename lookalikes (#1098)

yxlyx716b2c118ed4
diff --git a/apps/native/electron/markdown-links.test.cjs b/apps/native/electron/markdown-links.test.cjs
index 162abf4b..5618b24d 100644
--- a/apps/native/electron/markdown-links.test.cjs
+++ b/apps/native/electron/markdown-links.test.cjs
@@ -68,7 +68,7 @@ test('real assistant and user messages route their emitted anchors through Elect
following: false,
}));
const user = renderToStaticMarkup(React.createElement(UserBubble, {
- text: 'Try www.example.com, not file:///tmp/example.com.',
+ text: 'Try www.example.com, not file:///tmp/example.com or image.png.',
}));
const destinations = [...hrefs(assistant), ...hrefs(user)];
assert.deepEqual(destinations, [REPORTED_URL, 'https://www.example.com/']);
@@ -76,6 +76,7 @@ test('real assistant and user messages route their emitted anchors through Elect
assert.match(user, /target="_blank"/);
assert.doesNotMatch(assistant, /href="javascript:/);
assert.doesNotMatch(user, /href="file:/);
+ assert.doesNotMatch(user, /href="https:\/\/image\.png/);
const contents = new EventEmitter();
const opened = [];
diff --git a/apps/native/lib/browser-links.test.ts b/apps/native/lib/browser-links.test.ts
index 707dd393..c88df7bf 100644
--- a/apps/native/lib/browser-links.test.ts
+++ b/apps/native/lib/browser-links.test.ts
@@ -20,10 +20,20 @@ describe("normalizeBrowserTarget", () => {
"README.md",
"component.tsx",
+ "image.png",
+ "photo.final.jpeg",
+ "archive.tar.gz",
+ "slides.pptx",
+ "clip.mp4",
"src/example.com",
"999.1.1.1",
]) assert.equal(normalizeBrowserTarget(value), null, value);
});
+
+ it("allows explicit or www-prefixed destinations even when the host resembles a filename", () => {
+ assert.equal(normalizeBrowserTarget("https://image.png/docs"), "https://image.png/docs");
+ assert.equal(normalizeBrowserTarget("www.image.png/docs"), "https://www.image.png/docs");
+ });
});
describe("browserLinkSegments", () => {
@@ -49,7 +59,7 @@ describe("browserLinkSegments", () => {
it("does not link email, unsafe-scheme, or file-path lookalikes", () => {
for (const text of [
- "[email protected] src/example.com README.md",
+ "[email protected] src/example.com README.md image.png photo.final.jpeg archive.tar.gz",
"javascript:example.com mailto:example.com custom:example.com",
]) assert.deepEqual(browserLinkSegments(text), [{ kind: "text", value: text }]);
});
diff --git a/apps/native/lib/browser-links.ts b/apps/native/lib/browser-links.ts
index d614bffb..b627b8bf 100644
--- a/apps/native/lib/browser-links.ts
+++ b/apps/native/lib/browser-links.ts
@@ -5,9 +5,13 @@ export type BrowserLinkSegment =
const EXPLICIT_HTTP = /^https?:\/\//i;
const LOCAL_HOST = /^(?:localhost|127\.0\.0\.1|0\.0\.0\.0|\[::1\]|\d{1,3}(?:\.\d{1,3}){3})(?::\d{1,5})?(?:[/?#]|$)/i;
const FILELIKE_SUFFIXES = new Set([
- "c", "cc", "cpp", "css", "csv", "go", "h", "hpp", "html", "java", "js", "json",
- "jsx", "lock", "md", "mjs", "py", "rb", "rs", "sh", "sql", "svg", "toml", "ts",
- "tsx", "txt", "xml", "yaml", "yml", "zig",
+ "7z", "avif", "avi", "bak", "bmp", "bz2", "c", "cc", "cfg", "conf", "cpp", "css",
+ "csv", "dmg", "doc", "docx", "eot", "exe", "flac", "gif", "go", "gz", "h", "hpp",
+ "html", "ico", "ini", "java", "jpeg", "jpg", "js", "json", "jsx", "lock", "log", "m4a",
+ "map", "md", "mjs", "mkv", "mov", "mp3", "mp4", "msi", "ogg", "otf", "pdf", "pkg",
+ "png", "ppt", "pptx", "py", "rar", "rb", "rs", "sh", "sql", "svg", "swp", "tar",
+ "tgz", "toml", "ts", "tsx", "ttf", "txt", "wasm", "wav", "webm", "webmanifest", "webp",
+ "woff", "woff2", "xls", "xlsx", "xml", "xz", "yaml", "yml", "zig", "zip",
]);
const CANDIDATE = /https?:\/\/[^\s<>"'`]+|www\.(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9-]{2,63}(?::\d{1,5})?(?:[/?#][^\s<>"'`]*)?|(?:localhost|127\.0\.0\.1|0\.0\.0\.0|\[::1\]|\d{1,3}(?:\.\d{1,3}){3})(?::\d{1,5})?(?:[/?#][^\s<>"'`]*)?|(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+(?:[a-z]{2,63}|xn--[a-z0-9-]{2,59})(?::\d{1,5})?(?:[/?#][^\s<>"'`]*)?/giu;
const TRAILING_PUNCTUATION = /[.,;:!?]+$/u;
@@ -50,10 +54,10 @@ function hasValidIpv4(hostname: string): boolean {
return hostname.split(".").every(part => Number(part) <= 255);
}
-function looksFilelikeHostname(hostname: string, raw: string): boolean {
- if (raw.toLowerCase().startsWith("www.")) return false;
+function looksFilelikeHostname(hostname: string, raw: string, explicit: boolean): boolean {
+ if (explicit || raw.toLowerCase().startsWith("www.")) return false;
const labels = hostname.toLowerCase().split(".");
- return labels.length === 2 && FILELIKE_SUFFIXES.has(labels[1] ?? "");
+ return FILELIKE_SUFFIXES.has(labels.at(-1) ?? "");
}
/** Turn text that clearly names a browser destination into a safe HTTP(S) href.
@@ -68,7 +72,7 @@ export function normalizeBrowserTarget(raw: string): string | null {
try {
const url = new URL(explicit ? value : `${local ? "http" : "https"}://${value}`);
if (!['http:', 'https:'].includes(url.protocol) || url.username || url.password || !url.hostname) return null;
- if (!hasValidIpv4(url.hostname) || looksFilelikeHostname(url.hostname, value)) return null;
+ if (!hasValidIpv4(url.hostname) || looksFilelikeHostname(url.hostname, value, explicit)) return null;
return url.href;
} catch {
return null;