Skip to content

Chrome DevTools for coding agents

fix: truncate data: URLs in accessibility snapshots (#3292)

mjdch11e7d474953d
diff --git a/src/formatters/NetworkFormatter.ts b/src/formatters/NetworkFormatter.ts
index 326d5988..3feeb63e 100644
--- a/src/formatters/NetworkFormatter.ts
+++ b/src/formatters/NetworkFormatter.ts
@@ -11,10 +11,9 @@ import {
type HTTPRequest,
type HTTPResponse,
} from '../third_party/index.js';
-import {truncateText} from '../utils/text.js';
+import {truncateText, truncateUrl} from '../utils/text.js';
const BODY_CONTEXT_SIZE_LIMIT = 10000;
-const URL_CONTEXT_SIZE_LIMIT = 255;
/**
* How long to wait for a response body. Puppeteer reads a body only after the
* response finished loading, with no timeout of its own, and a response that
@@ -311,7 +310,7 @@ function convertNetworkRequestConciseToString(
): string {
// Long URLs (e.g., data: URLs) bloat the concise list output. The full URL
// remains available via the detailed view and the structured content.
- const url = getSizeLimitedString(data.url, URL_CONTEXT_SIZE_LIMIT);
+ const url = truncateUrl(data.url);
return `reqid=${data.requestId} ${data.method} ${url} [${data.status}]${data.selectedInDevToolsUI ? ` [selected in the DevTools Network panel]` : ''}`;
}
diff --git a/src/formatters/SnapshotFormatter.ts b/src/formatters/SnapshotFormatter.ts
index 4a3bd131..9282cb93 100644
--- a/src/formatters/SnapshotFormatter.ts
+++ b/src/formatters/SnapshotFormatter.ts
@@ -6,6 +6,7 @@
import type {TextSnapshot} from '../TextSnapshot.js';
import type {TextSnapshotNode} from '../types.js';
+import {truncateUrl} from '../utils/text.js';
export class SnapshotFormatter {
#snapshot: TextSnapshot;
@@ -177,6 +178,9 @@ Get a verbose snapshot to include all elements if you are interested in the sele
if (value) {
result[attr] = true;
}
+ } else if (attr === 'url' && typeof value === 'string') {
+ // Keep other URLs whole: the snapshot is their only full copy.
+ result[attr] = value.startsWith('data:') ? truncateUrl(value) : value;
} else if (typeof value === 'string' || typeof value === 'number') {
result[attr] = value;
}
diff --git a/src/utils/text.ts b/src/utils/text.ts
index c3f70b65..19927b89 100644
--- a/src/utils/text.ts
+++ b/src/utils/text.ts
@@ -54,3 +54,12 @@ export function truncateText(
: maxLength;
return StringUtilities.truncateToCodeUnitLength(text, keep) + ellipsis;
}
+
+const URL_SIZE_LIMIT = 255;
+
+/**
+ * Truncates a URL for tool output, such as a `data:` URL with a base64 payload.
+ */
+export function truncateUrl(url: string): string {
+ return truncateText(url, URL_SIZE_LIMIT, {ellipsis: '... <truncated>'});
+}
diff --git a/tests/formatters/snapshotFormatter.test.ts b/tests/formatters/snapshotFormatter.test.ts
index 21fb0316..4c672d78 100644
--- a/tests/formatters/snapshotFormatter.test.ts
+++ b/tests/formatters/snapshotFormatter.test.ts
@@ -175,6 +175,41 @@ describe('snapshotFormatter', () => {
);
});
+ it('keeps long non-data url attributes in full', () => {
+ const url = 'https://example.com/?q=' + 'a'.repeat(500);
+ const node = {...createNode('1_1', 'link', 'Next'), url};
+
+ const formatter = new SnapshotFormatter({root: node} as TextSnapshot);
+ assert.strictEqual(
+ formatter.toString(),
+ `uid=1_1 link "Next" url="${url}"\n`,
+ );
+ assert.deepStrictEqual(formatter.toJSON(), {
+ id: '1_1',
+ role: 'link',
+ name: 'Next',
+ url,
+ });
+ });
+
+ it('truncates long data url attributes in text and JSON output', () => {
+ const url = 'data:image/png;base64,' + 'A'.repeat(5000);
+ const truncated = url.slice(0, 255) + '... <truncated>';
+ const node = {...createNode('1_1', 'image', 'logo'), url};
+
+ const formatter = new SnapshotFormatter({root: node} as TextSnapshot);
+ assert.strictEqual(
+ formatter.toString(),
+ `uid=1_1 image "logo" url="${truncated}"\n`,
+ );
+ assert.deepStrictEqual(formatter.toJSON(), {
+ id: '1_1',
+ role: 'image',
+ name: 'logo',
+ url: truncated,
+ });
+ });
+
it('formats a snapshot with value properties', () => {
const node: TextSnapshotNode = {
id: '1_1',
diff --git a/tests/utils/text.test.ts b/tests/utils/text.test.ts
index 5e7f6072..74203118 100644
--- a/tests/utils/text.test.ts
+++ b/tests/utils/text.test.ts
@@ -7,7 +7,7 @@
import assert from 'node:assert';
import {describe, it} from 'node:test';
-import {truncateText} from '../../src/utils/text.js';
+import {truncateText, truncateUrl} from '../../src/utils/text.js';
const EMOJI = '\u{1F600}';
// Family emoji: four code points joined with zero-width joiners.
@@ -75,3 +75,15 @@ describe('truncateText', () => {
assert.strictEqual(truncateText('abc', 0), '...');
});
});
+
+describe('truncateUrl', () => {
+ it('returns a URL within 255 characters unchanged', () => {
+ const url = 'https://example.com/' + 'a'.repeat(235);
+ assert.strictEqual(truncateUrl(url), url);
+ });
+
+ it('cuts a long data URL at 255 characters', () => {
+ const url = 'data:image/png;base64,' + 'A'.repeat(5000);
+ assert.strictEqual(truncateUrl(url), url.slice(0, 255) + '... <truncated>');
+ });
+});