Skip to content

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

Merge dev and enforce bounded brand-head streaming

tt-a1ib0de439c4b95
diff --cc archify.zip
index 82fb97c6,ede3ba35..8b775ab0
Binary files differ
diff --cc archify/renderers/shared/brand-marks.mjs
index 4f0a8f1c,1e18e0b5..9b0cf868
--- a/archify/renderers/shared/brand-marks.mjs
+++ b/archify/renderers/shared/brand-marks.mjs
@@@ -269,34 -269,6 +269,42 @@@ async function readLimited(response, ma
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.
++// Read only the bounded head, independent of network chunk boundaries. Scan
++// bytes once so many tiny chunks cannot cause repeated concatenation/rescanning.
+async function readHtmlHead(response, maximum) {
- if (!response.body || typeof response.body[Symbol.asyncIterator] !== 'function') {
- return (await readLimited(response, maximum)).toString('utf8');
- }
- const chunks = [];
++ const chunks = response.body && typeof response.body[Symbol.asyncIterator] === 'function'
++ ? response.body : [await readLimited(response, maximum)];
++ const buffer = Buffer.alloc(maximum);
++ const closing = Buffer.from('</head');
+ 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');
++ let matched = 0;
++ for await (const value of chunks) {
++ const chunk = Buffer.from(value);
++ const length = Math.min(chunk.length, maximum - total);
++ chunk.copy(buffer, total, 0, length);
++ for (let offset = 0; offset < length; offset++) {
++ const byte = chunk[offset];
++ if (matched === closing.length) {
++ if (byte === 0x3e) {
++ response.body?.destroy?.();
++ return buffer.toString('utf8', 0, total + offset + 1);
++ }
++ if (byte === 9 || byte === 10 || byte === 12 || byte === 13 || byte === 32) continue;
++ matched = byte === 0x3c ? 1 : 0;
++ } else {
++ const lower = byte >= 65 && byte <= 90 ? byte + 32 : byte;
++ matched = lower === closing[matched] ? matched + 1 : (byte === 0x3c ? 1 : 0);
++ }
+ }
- if (total > maximum) {
- response.body.destroy?.();
++ total += length;
++ if (chunk.length > length) {
++ response.body?.destroy?.();
+ throw new Error('brand asset is too large');
+ }
+ }
- return Buffer.concat(chunks, total).toString('utf8');
++ return buffer.toString('utf8', 0, total);
+}
+
function attribute(tag, name) {
const match = tag.match(new RegExp(`\\b${name}\\s*=\\s*(?:"([^"]*)"|'([^']*)'|([^\\s>]+))`, 'i'));
return match ? (match[1] ?? match[2] ?? match[3] ?? '') : '';
diff --cc archify/test/brand-marks.test.mjs
index 6649c45e,4aba30bd..f47e0e4d
--- a/archify/test/brand-marks.test.mjs
+++ b/archify/test/brand-marks.test.mjs
@@@ -274,90 -298,6 +298,127 @@@ test('capture command returns a digest-
}
});
+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));
+ }
+});
+
++for (const scenario of ['over-limit-close', 'exact-limit-close', 'body-only-icon']) {
++ test(`capture bounds the head bytes independently of response chunks: ${scenario}`, async () => {
++ const maximum = 256 * 1024;
++ const icon = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=', 'base64');
++ const prefix = '<head><link rel="icon" type="image/png" href="/mark.png">';
++ const server = http.createServer(async (request, response) => {
++ if (request.url === '/mark.png') {
++ response.writeHead(200, { 'content-type': 'image/png' });
++ response.end(icon);
++ return;
++ }
++ if (request.url === '/favicon.ico') {
++ response.writeHead(404); response.end(); return;
++ }
++ response.writeHead(200, { 'content-type': 'text/html; charset=utf-8' });
++ if (scenario === 'over-limit-close') {
++ response.write(prefix + ' '.repeat(maximum - 1 - Buffer.byteLength(prefix)));
++ await new Promise((resolve) => setTimeout(resolve, 20));
++ response.end('x</head><body>');
++ } else if (scenario === 'exact-limit-close') {
++ response.end(prefix + ' '.repeat(maximum - 7 - Buffer.byteLength(prefix)) + '</HEAD><body>' + 'x'.repeat(maximum));
++ } else {
++ response.end('<head><title>No icon</title></head><body><link rel="icon" href="/mark.png"></body>');
++ }
++ });
++ await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve));
++ try {
++ const capture = await runCliAsync(['brands', 'capture', `http://127.0.0.1:${server.address().port}/page`, '--json'], { ARCHIFY_BRAND_ALLOW_PRIVATE: '1' });
++ assert.equal(capture.status, scenario === 'exact-limit-close' ? 0 : 2, capture.stderr || capture.stdout);
++ if (scenario === 'over-limit-close') assert.match(capture.stderr, /brand asset is too large/);
++ if (scenario === 'exact-limit-close') assert.equal(JSON.parse(capture.stdout).brand.sha256, createHash('sha256').update(icon).digest('hex'));
++ } 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);