Skip to content

Open-source AI browser agent for Chrome and Firefox (monorepo) 🧠

Fail CodeMirror verification when the view shows only part of the document

Barack Sokulluc161ef5b5467
diff --git a/src/chrome/src/content/content.js b/src/chrome/src/content/content.js
index 2b48a0528..5c7faca01 100644
--- a/src/chrome/src/content/content.js
+++ b/src/chrome/src/content/content.js
@@ -2493,7 +2493,7 @@
|| _sameNativeChatCaretContext(nativeContextBefore, nativeContextAfter));
const unknown = () => failure('The editor handled a cancelled beforeinput and changed. Its complete value could not be verified in the same scope. No additional text was dispatched.',
{ cancelled: true, recoveryRequired: 'verify_or_restore_field', retryable: false });
- if (!sameScope || (draftJsEditor && readDraftJsText(el) === null)) return unknown();
+ if (!sameScope || (draftJsEditor && readDraftJsText(el) === null) || (el.isContentEditable && el.classList?.contains('cm-content') && readCodeMirrorText(el) === null)) return unknown();
try {
if (verifyNativeObservation) {
if (!verifyNativeObservation()) throw Object.assign(new Error(`The native text observation lost its original operation or received browser intervention (${verifyNativeObservation.reason || 'observation_invalid'})${verifyNativeObservation.diagnostic ? ' [' + verifyNativeObservation.diagnostic + ']' : ''}.`), { code: 'page_feedback_pending' });
@@ -2592,6 +2592,10 @@
return failure('The DraftJS editor structure changed during typing and cannot establish its exact document text.',
{ recoveryRequired: 'verify_or_restore_field', retryable: false });
}
+ if ((el.isContentEditable && el.classList?.contains('cm-content') && readCodeMirrorText(el) === null)) {
+ return failure('The CodeMirror editor shows only part of its document (a viewport gap or folded text), so its exact text cannot be read.',
+ { recoveryRequired: 'verify_or_restore_field', retryable: false });
+ }
const value = readValue();
const expected = clear ? text : previous + text;
const normalize = value => String(value).replace(/\r\n?/g, '\n');
@@ -9942,6 +9946,10 @@
'The DraftJS editor structure changed during typing and cannot establish its exact document text.',
{ ref_id, verified: false, fieldMeta, recoveryRequired: 'verify_or_restore_field', failureScope: `field-value:${ref_id}`, retryable: false },
);
+ if ((el.isContentEditable && el.classList?.contains('cm-content') && readCodeMirrorText(el) === null)) return failure(
+ 'The CodeMirror editor shows only part of its document (a viewport gap or folded text), so its exact text cannot be read.',
+ { ref_id, verified: false, fieldMeta, recoveryRequired: 'verify_or_restore_field', failureScope: `field-value:${ref_id}`, retryable: false },
+ );
const actual = el.isContentEditable ? _editableTextValue(el) : (el.value || '');
const verified = selectExpected !== null
? actual === selectExpected
@@ -10150,6 +10158,10 @@
'The DraftJS editor structure changed during typing and cannot establish its exact document text.',
{ ref_id, verified: false, fieldMeta, recoveryRequired: 'verify_or_restore_field', failureScope: `field-value:${ref_id}`, retryable: false },
);
+ if ((el.isContentEditable && el.classList?.contains('cm-content') && readCodeMirrorText(el) === null)) return failure(
+ 'The CodeMirror editor shows only part of its document (a viewport gap or folded text), so its exact text cannot be read.',
+ { ref_id, verified: false, fieldMeta, recoveryRequired: 'verify_or_restore_field', failureScope: `field-value:${ref_id}`, retryable: false },
+ );
const actual = el.isContentEditable ? _editableTextValue(el) : (el.value || '');
const verified = _setFieldValueMatches(actual, prevValue, text, clear, el.isContentEditable, (readProseMirrorText(el) !== null || readCodeMirrorText(el) !== null || readDraftJsText(el) !== null || readEmptyCaretText(el) !== null));
const fallbackAttempted = false;
@@ -10458,6 +10470,8 @@
if (!el || !el.isConnected) return { success: false, verified: false, error: `ref_id ${ref_id} is stale` };
if (isDraftJsEditor(el) && readDraftJsText(el) === null)
return { success: false, verified: false, error: 'The DraftJS editor structure cannot establish its exact document text.' };
+ if ((el.isContentEditable && el.classList?.contains('cm-content') && readCodeMirrorText(el) === null))
+ return { success: false, verified: false, error: 'The CodeMirror editor shows only part of its document (a viewport gap or folded text), so its exact text cannot be read.' };
const actual = el.isContentEditable ? _editableTextValue(el) : (el.value || '');
const verifiesAppend = el.isContentEditable && typeof appendText === 'string';
const expectedPrefix = verifiesAppend
@@ -10529,6 +10543,8 @@
}
if (isDraftJsEditor(el) && readDraftJsText(el) === null)
return { success: false, verified: false, error: 'The DraftJS editor structure cannot establish its exact document text.' };
+ if ((el.isContentEditable && el.classList?.contains('cm-content') && readCodeMirrorText(el) === null))
+ return { success: false, verified: false, error: 'The CodeMirror editor shows only part of its document (a viewport gap or folded text), so its exact text cannot be read.' };
const value = String(el.isContentEditable ? _editableTextValue(el) : (el.value || ''));
if (!globalThis.crypto?.subtle) return { success: false, error: 'SHA-256 is unavailable' };
const digest = await globalThis.crypto.subtle.digest('SHA-256', new TextEncoder().encode(value));
diff --git a/src/firefox/src/content/content.js b/src/firefox/src/content/content.js
index d0f744f4c..f64a164dd 100644
--- a/src/firefox/src/content/content.js
+++ b/src/firefox/src/content/content.js
@@ -3093,7 +3093,7 @@
|| _sameNativeChatCaretContext(nativeContextBefore, nativeContextAfter));
const unknown = () => failure('The editor handled a cancelled beforeinput and changed. Its complete value could not be verified in the same scope. No additional text was dispatched.',
{ cancelled: true, recoveryRequired: 'verify_or_restore_field', retryable: false });
- if (!sameScope || (draftJsEditor && readDraftJsText(el) === null)) return unknown();
+ if (!sameScope || (draftJsEditor && readDraftJsText(el) === null) || (el.isContentEditable && el.classList?.contains('cm-content') && readCodeMirrorText(el) === null)) return unknown();
try {
if (verifyNativeObservation) {
if (!verifyNativeObservation()) throw Object.assign(new Error(`The native text observation lost its original operation or received browser intervention (${verifyNativeObservation.reason || 'observation_invalid'})${verifyNativeObservation.diagnostic ? ' [' + verifyNativeObservation.diagnostic + ']' : ''}.`), { code: 'page_feedback_pending' });
@@ -3192,6 +3192,10 @@
return failure('The DraftJS editor structure changed during typing and cannot establish its exact document text.',
{ recoveryRequired: 'verify_or_restore_field', retryable: false });
}
+ if ((el.isContentEditable && el.classList?.contains('cm-content') && readCodeMirrorText(el) === null)) {
+ return failure('The CodeMirror editor shows only part of its document (a viewport gap or folded text), so its exact text cannot be read.',
+ { recoveryRequired: 'verify_or_restore_field', retryable: false });
+ }
const value = readValue();
const expected = clear ? text : previous + text;
const normalize = value => String(value).replace(/\r\n?/g, '\n');
@@ -9001,6 +9005,10 @@
'The DraftJS editor structure changed during typing and cannot establish its exact document text.',
{ ref_id, verified: false, fieldMeta, recoveryRequired: 'verify_or_restore_field', failureScope: `field-value:${ref_id}`, retryable: false },
);
+ if ((el.isContentEditable && el.classList?.contains('cm-content') && readCodeMirrorText(el) === null)) return failure(
+ 'The CodeMirror editor shows only part of its document (a viewport gap or folded text), so its exact text cannot be read.',
+ { ref_id, verified: false, fieldMeta, recoveryRequired: 'verify_or_restore_field', failureScope: `field-value:${ref_id}`, retryable: false },
+ );
const actual = el.isContentEditable ? _editableTextValue(el) : (el.value || '');
const verified = selectExpected !== null
? actual === selectExpected
@@ -9191,6 +9199,10 @@
'The DraftJS editor structure changed during typing and cannot establish its exact document text.',
{ ref_id, verified: false, fieldMeta, recoveryRequired: 'verify_or_restore_field', failureScope: `field-value:${ref_id}`, retryable: false },
);
+ if ((el.isContentEditable && el.classList?.contains('cm-content') && readCodeMirrorText(el) === null)) return failure(
+ 'The CodeMirror editor shows only part of its document (a viewport gap or folded text), so its exact text cannot be read.',
+ { ref_id, verified: false, fieldMeta, recoveryRequired: 'verify_or_restore_field', failureScope: `field-value:${ref_id}`, retryable: false },
+ );
const actual = el.isContentEditable ? _editableTextValue(el) : (el.value || '');
const verified = _setFieldValueMatches(actual, prevValue, text, clear, el.isContentEditable, (readProseMirrorText(el) !== null || readCodeMirrorText(el) !== null || readDraftJsText(el) !== null || readEmptyCaretText(el) !== null));
const fallbackAttempted = false;
@@ -9408,6 +9420,8 @@
if (!el || !el.isConnected) return { success: false, verified: false, error: `ref_id ${ref_id} is stale` };
if (isDraftJsEditor(el) && readDraftJsText(el) === null)
return { success: false, verified: false, error: 'The DraftJS editor structure cannot establish its exact document text.' };
+ if ((el.isContentEditable && el.classList?.contains('cm-content') && readCodeMirrorText(el) === null))
+ return { success: false, verified: false, error: 'The CodeMirror editor shows only part of its document (a viewport gap or folded text), so its exact text cannot be read.' };
const actual = el.isContentEditable ? _editableTextValue(el) : (el.value || '');
const verifiesAppend = el.isContentEditable && typeof appendText === 'string';
const expectedPrefix = verifiesAppend
@@ -9479,6 +9493,8 @@
}
if (isDraftJsEditor(el) && readDraftJsText(el) === null)
return { success: false, verified: false, error: 'The DraftJS editor structure cannot establish its exact document text.' };
+ if ((el.isContentEditable && el.classList?.contains('cm-content') && readCodeMirrorText(el) === null))
+ return { success: false, verified: false, error: 'The CodeMirror editor shows only part of its document (a viewport gap or folded text), so its exact text cannot be read.' };
const value = String(el.isContentEditable ? _editableTextValue(el) : (el.value || ''));
if (!globalThis.crypto?.subtle) return { success: false, error: 'SHA-256 is unavailable' };
const digest = await globalThis.crypto.subtle.digest('SHA-256', new TextEncoder().encode(value));
diff --git a/test/codemirror-text.mjs b/test/codemirror-text.mjs
index a9e6d72aa..b6e2c534e 100644
--- a/test/codemirror-text.mjs
+++ b/test/codemirror-text.mjs
@@ -79,6 +79,16 @@ for (const [kind, engine] of [['chrome', chromium], ['firefox', firefox]]) {
// and a fold hides text behind a widget: neither establishes the document.
await open(editorHtml(rendered, '<div class="cm-gap" style="height: 2000px"></div>'));
assert.equal((await verify(post)).verified, false, 'A partly rendered document is never certified');
+ // Even when the rendered lines alone are exactly what was asked for, the
+ // gap stands for more of the document: rendered text must not certify
+ // or fingerprint it.
+ const head = 'line one\nline two\nline three';
+ await open(editorHtml(head.split('\n').map(line).join(''), '<div class="cm-gap" style="height: 2000px"></div>'));
+ const partial = await verify(head);
+ assert.equal(partial.verified, false, JSON.stringify(partial));
+ const partialDigest = await call(page, 'field_value_digest', { ref_id: 'editor', expected: head });
+ assert.equal(partialDigest.success, false, JSON.stringify(partialDigest));
+ assert.equal(partialDigest.valueSha256, undefined, 'An incomplete view has no document fingerprint');
await open(editorHtml(post.split('\n').map(line).join('').replace('<div class="cm-line"><span class="tok-meta">Las',
'<div class="cm-line"><span class="cm-foldPlaceholder" contenteditable="false">…</span><span class="tok-meta">Las')));
assert.equal((await verify(post)).verified, false, 'Folded text is not readable from the DOM');