fix(cli): capture saves an inline SVG as a file only when it is a plain image (#8699)

An inline SVG that carries a script, an event handler, embedded HTML, a link or a
reference to another file is no longer written under assets/svgs/. It is counted as
unavailable and stays in extracted/page.html. Downloaded SVG files already had this rule.

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
James Russo
2026-11-24 11:29:43 +00:00
committed by GitHub
co-authored by Claude Fable 5.2
parent fb81a9d3d3
commit 37e4d4b75a
4 changed files with 37 additions and 2 deletions
+1 -1
View File
@@ -428,7 +428,7 @@ way to tell them apart is to open the page yourself.
| `size-floor` | Fetched, then judged too small to be a real asset rather than a spacer or tracking pixel. |
| `budget-exhausted` | `--capture-budget` ran out before this one was reached. Raise it, or pass `--skip-vision` to buy time. |
| `cap-reached` | A per-run or per-family limit was already met: 30 inline SVGs, 30 fonts, 6 faces per family. |
| `unavailable` | The request or the write failed: network error, timeout, refused address, bad status, disk. |
| `unavailable` | The request or the write failed: network error, timeout, refused address, bad status, disk. Also an inline SVG that is more than a plain image (it carries a script, an event handler, embedded HTML, a link, or a reference to another file): it stays in `extracted/page.html` but is not saved as a file under `assets/svgs/`. |
Three of those are decisions the capture made and one is a failure it hit, so a
run that is thin with an all-zero `dropped` is a thin page, and a run that is
@@ -469,6 +469,33 @@ describe("drop counts — why a referenced asset is not in the capture", () => {
expect(drops["size-ceiling"]).toBe(1);
});
});
it("writes only inline SVGs that are plain images as files", async () => {
await withTempDir(async (dir) => {
const plain =
'<svg viewBox="0 0 24 24" class="icon" fill="currentColor"><path d="M0 0h24v24H0z"/></svg>';
const shape = '<rect width="24" height="24" fill="#abc"/>';
const notPlain = [
`<svg viewBox="0 0 24 24">${shape}<script>fetch("/api/projects")</script></svg>`,
`<svg viewBox="0 0 24 24" onload="fetch('/api/projects')">${shape}</svg>`,
`<svg viewBox="0 0 24 24">${shape}<foreignObject><iframe src="/"></iframe></foreignObject></svg>`,
`<svg viewBox="0 0 24 24">${shape}<a href="javascript:fetch('/')"><rect width="9" height="9"/></a></svg>`,
`<svg viewBox="0 0 24 24">${shape}<use href="https://elsewhere.example/sprite.svg#mark"/></svg>`,
];
const { assets, drops } = await downloadAssets(
{
svgs: [plain, ...notPlain].map((outerHTML) => ({ outerHTML, isLogo: false })),
sections: [],
ogImage: "",
} as unknown as DesignTokens,
dir,
);
expect(readdirSync(join(dir, "assets", "svgs"))).toHaveLength(1);
expect(assets).toHaveLength(1);
expect(readFileSync(join(dir, assets[0]!.localPath), "utf-8")).toBe(toStandaloneSvg(plain));
expect(drops.unavailable).toBe(notPlain.length);
});
});
});
describe("asset fetches present the same identity as the page navigation", () => {
+5 -1
View File
@@ -22,7 +22,7 @@ import {
type DownloadByteBudget,
} from "./readBoundedResponse.js";
import { captureFontExtension, captureFontFilename } from "./captureFontValidation.js";
import { captureImageExtension } from "./captureImageValidation.js";
import { captureImageExtension, passiveInlineSvg } from "./captureImageValidation.js";
import { loadSharp } from "../utils/optionalPackages.js";
interface DownloadBudgetOptions {
@@ -352,6 +352,10 @@ export async function downloadAssets(
drops["size-ceiling"]++;
continue;
}
if (!passiveInlineSvg(svgFile)) {
drops.unavailable++;
continue;
}
const slug = svgContentHashSlug(svgFile, !!svg.isLogo);
let finalSlug = slug;
let suffix = 2;
@@ -75,6 +75,10 @@ function passiveSvg(bytes: Buffer, embedded: Set<string>): boolean {
});
}
export function passiveInlineSvg(markup: string): boolean {
return passiveSvg(Buffer.from(markup, "utf8"), new Set());
}
function passiveSvgAttribute(name: string, value: string, embedded: Set<string>): boolean {
if (name.startsWith("on") || name === "xml:base") return false;
if (["href", "xlink:href", "src"].includes(name)) return localImageReference(value, embedded);