Skip to content

Turn any idea, plan, or codebase into a beautiful interactive diagram. An agent skill for Claude Code, Codex, and more.

fix(brand-capture): read a bounded head instead of capping the whole page

korjavind15440816187
diff --git a/archify/renderers/shared/brand-marks.mjs b/archify/renderers/shared/brand-marks.mjs
index 1e18e0b5..4f0a8f1c 100644
--- a/archify/renderers/shared/brand-marks.mjs
+++ b/archify/renderers/shared/brand-marks.mjs
@@ -269,6 +269,34 @@ async function readLimited(response, maximum) {
return Buffer.concat(chunks, total);
}
+// Icon <link> tags live in <head>, but a page's total byte size is driven by
+// its body. Capping the whole page at MAX_HTML_BYTES rejected pages with a
+// tiny head and a large body before a single icon candidate was even looked
+// at. Read incrementally and stop as soon as </head> appears instead of
+// reading (and counting) the rest of the page; only fail closed if the byte
+// budget runs out before a head close is seen.
+async function readHtmlHead(response, maximum) {
+ if (!response.body || typeof response.body[Symbol.asyncIterator] !== 'function') {
+ return (await readLimited(response, maximum)).toString('utf8');
+ }
+ const chunks = [];
+ let total = 0;
+ for await (const value of response.body) {
+ chunks.push(Buffer.from(value));
+ total += value.byteLength;
+ const soFar = Buffer.concat(chunks, total);
+ if (soFar.toString('latin1').toLocaleLowerCase('en-US').includes('</head')) {
+ response.body.destroy?.();
+ return soFar.toString('utf8');
+ }
+ if (total > maximum) {
+ response.body.destroy?.();
+ throw new Error('brand asset is too large');
+ }
+ }
+ return Buffer.concat(chunks, total).toString('utf8');
+}
+
function attribute(tag, name) {
const match = tag.match(new RegExp(`\\b${name}\\s*=\\s*(?:"([^"]*)"|'([^']*)'|([^\\s>]+))`, 'i'));
return match ? (match[1] ?? match[2] ?? match[3] ?? '') : '';
@@ -378,7 +406,7 @@ async function captureRemoteBrand(value, deadline = Date.now() + captureTimeoutM
page.response.body?.destroy?.();
return fallback('linked page is not HTML');
}
- const html = (await readLimited(page.response, MAX_HTML_BYTES)).toString('utf8');
+ const html = await readHtmlHead(page.response, MAX_HTML_BYTES);
const iconErrors = [];
for (const candidate of iconCandidates(html, page.finalUrl)) {
try {
diff --git a/archify/test/brand-marks.test.mjs b/archify/test/brand-marks.test.mjs
index aeeda2ab..6649c45e 100644
--- a/archify/test/brand-marks.test.mjs
+++ b/archify/test/brand-marks.test.mjs
@@ -274,6 +274,90 @@ test('capture command returns a digest-pinned brand object that renders reproduc
}
});
+test('capture reads a small head even when the body is far larger than the byte cap', async () => {
+ const icon = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=', 'base64');
+ const server = http.createServer((request, response) => {
+ if (request.url === '/mark.png') {
+ response.writeHead(200, { 'content-type': 'image/png' });
+ response.end(icon);
+ return;
+ }
+ response.writeHead(200, { 'content-type': 'text/html; charset=utf-8' });
+ response.write('<!doctype html><head><title>Big Body</title><link rel="icon" type="image/png" href="/mark.png"></head><body>');
+ // Past the head close, so it must never be counted against the byte cap.
+ response.end('x'.repeat(300 * 1024));
+ });
+ await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve));
+ try {
+ const address = server.address();
+ const url = `http://127.0.0.1:${address.port}/big`;
+ const capture = await runCliAsync(['brands', 'capture', url, '--json'], { ARCHIFY_BRAND_ALLOW_PRIVATE: '1' });
+ assert.equal(capture.status, 0, capture.stderr || capture.stdout);
+ const receipt = JSON.parse(capture.stdout);
+ assert.equal(receipt.ok, true);
+ assert.deepEqual(receipt.brand, {
+ url,
+ sha256: createHash('sha256').update(icon).digest('hex'),
+ });
+ } finally {
+ await new Promise((resolve) => server.close(resolve));
+ }
+});
+
+test('capture finds a head close split across separate response chunks', async () => {
+ const icon = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=', 'base64');
+ const server = http.createServer(async (request, response) => {
+ if (request.url === '/mark.png') {
+ response.writeHead(200, { 'content-type': 'image/png' });
+ response.end(icon);
+ return;
+ }
+ response.writeHead(200, { 'content-type': 'text/html; charset=utf-8' });
+ response.write('<!doctype html><head><title>Split</title><link rel="icon" type="image/png" href="/mark.png"></he');
+ await new Promise((resolve) => setTimeout(resolve, 20));
+ response.end('ad><body>hi</body>');
+ });
+ await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve));
+ try {
+ const address = server.address();
+ const url = `http://127.0.0.1:${address.port}/split`;
+ const capture = await runCliAsync(['brands', 'capture', url, '--json'], { ARCHIFY_BRAND_ALLOW_PRIVATE: '1' });
+ assert.equal(capture.status, 0, capture.stderr || capture.stdout);
+ const receipt = JSON.parse(capture.stdout);
+ assert.equal(receipt.ok, true);
+ assert.deepEqual(receipt.brand, {
+ url,
+ sha256: createHash('sha256').update(icon).digest('hex'),
+ });
+ } finally {
+ await new Promise((resolve) => server.close(resolve));
+ }
+});
+
+test('capture still fails closed when the head itself exceeds the byte cap', async () => {
+ const server = http.createServer(async (request, response) => {
+ response.writeHead(200, { 'content-type': 'text/html; charset=utf-8' });
+ response.write('<!doctype html><head><title>Huge head</title>');
+ // Still inside <head>, and past the 256 KiB cap on its own. Flushed and
+ // awaited before </head> so the client reads it as a chunk that already
+ // exceeds the cap with no head close in sight yet, instead of Node
+ // coalescing every write into one chunk that contains </head> already.
+ response.write(`<!-- ${'x'.repeat(300 * 1024)} -->`);
+ await new Promise((resolve) => setTimeout(resolve, 20));
+ response.end('</head><body>never reached</body>');
+ });
+ await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve));
+ try {
+ const address = server.address();
+ const url = `http://127.0.0.1:${address.port}/huge-head`;
+ const capture = await runCliAsync(['brands', 'capture', url, '--json'], { ARCHIFY_BRAND_ALLOW_PRIVATE: '1' });
+ assert.equal(capture.status, 2, capture.stdout);
+ assert.match(capture.stderr, /brand asset is too large/);
+ } finally {
+ await new Promise((resolve) => server.close(resolve));
+ }
+});
+
test('a pinned brand fails closed when the remote icon digest changes', async () => {
const firstIcon = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=', 'base64');
const changedIcon = Buffer.from(firstIcon);