日本語

Fixing Lighthouse’s “Prevents users from pasting into input fields” Warning

​​Lighthouse​HTML,JavaScript,Playwright

Lighthouse's "Prevents users from pasting into input fields" audit goes away when you remove the code that calls preventDefault() in a paste event handler. This applies when a signup or login form blocks pasting into a field such as "email (confirm)" or a password field, so users cannot paste a password generated by a password manager.

The audit also flags code that uses the paste event to clean up pasted text, such as removing hyphens from a phone number. This article shows how to rewrite that code. It also shows a way to block paste that passes Lighthouse but still doesn't work. I checked all of this in a test environment.

Note

The test environment was Ubuntu 24.04 and Node.js 22. I ran Lighthouse 13.6.0 from the command line with the default settings (mobile), limited to the Best Practices category. Chrome was Chrome for Testing 153, bundled with Playwright 1.63.0. For the actual paste tests, I put text on the clipboard with Playwright (chrome-headless-shell 157) and pressed Ctrl+V. The server was a small one I wrote with Node.js's http module, running on localhost on the same machine. I did not test the Lighthouse panel in DevTools, PageSpeed Insights, a real password manager, a physical smartphone, contenteditable elements, or Lighthouse 12 and earlier.

Lighthouse sends a paste event to each input field to check it

This audit is in the User Experience group under Best Practices. In the Lighthouse 13.6.0 configuration, its weight is 3, so it counts toward the score. On the test page, when this was the only failing audit, the Best Practices score was 88.

Looking at the Lighthouse 13.6.0 code, it goes through the textarea, input, and select elements on the page and, for each one that isn't read-only (readOnly), dispatches this synthetic event:

js
preventsPaste = !inputEl.dispatchEvent(new ClipboardEvent('paste', {cancelable: true}));

dispatchEvent() returns false if someone calls preventDefault(). So if even one field cancels the paste event, the audit fails. Lighthouse does not check whether text is actually inserted. The official explanation also says that blocking paste in ways other than a paste event listener is not detected.

Testing a signup form that blocks paste, and seeing the warning

For testing, I built a signup form with four fields: email, email (confirm), password, and phone number. The confirmation field has onpaste="return false".

html
<input id="email2" name="email2" type="email" autocomplete="email" onpaste="return false">

The password and phone number fields have listeners added in JavaScript (broken.js). The phone number listener doesn't block paste. It removes hyphens from the pasted text before inserting it.

js
// Block pasting into the password field
document.querySelector('#password').addEventListener('paste', (e) => {
  e.preventDefault();
});

// Telephone number: strip hyphens from the pasted text before inserting it
const tel = document.querySelector('#tel');
tel.addEventListener('paste', (e) => {
  e.preventDefault();
  const text = e.clipboardData.getData('text').replace(/[-‐-ー]/g, '');
  tel.setRangeText(text, tel.selectionStart, tel.selectionEnd, 'end');
});

I ran Lighthouse with CHROME_PATH pointing to Chrome 153, then used a small script (summarize.mjs) to pull the audit out of the JSON result.

console
$ npx lighthouse http://localhost:8400/broken --only-categories=best-practices --output=json --output-path=./out/broken.json --chrome-flags="--headless=new --no-sandbox" --quiet
exit: 0
$ node summarize.mjs out/broken.json
lighthouseVersion: 13.6.0  formFactor: mobile  locale: en-US
url: http://localhost:8400/broken
best-practices score: 88
paste-preventing-inputs: score=0 title="Prevents users from pasting into input fields"
  in best-practices: weight=3 group=best-practices-ux
  failing: <input id="email2" name="email2" type="email" autocomplete="email" onpaste="return false">  (body > form#signup > p > input#email2)
  failing: <input id="password" name="password" type="password" autocomplete="new-password">  (body > form#signup > p > input#password)
  failing: <input id="tel" name="tel" type="tel" autocomplete="tel">  (body > form#signup > p > input#tel)

All three fields appear as failing elements. The phone number field is included because its listener calls preventDefault(), even though it only changes the pasted text. In the report generated with the Japanese locale (--locale=ja), the item appears under a translated title that means "Users cannot paste into input fields."

Japanese-language Lighthouse report. The "Users cannot paste into input fields" item has a red warning triangle, and three input fields are listed as failing elements: the confirmation email, password, and phone number fields. (The labels in this image are in Japanese.)
Report for the test form that blocks paste (Lighthouse 13.6.0, Chrome 153)

When I pasted with Ctrl+V from the clipboard, the confirmation and password fields stayed empty. The phone number field got the value with hyphens removed, but hyphens typed by hand were kept.

console
$ node paste.mjs http://localhost:8400/broken
browser: 157.0.8103.78  url: http://localhost:8400/broken
#email     貼り付けた文字: taro@example.com   欄の値: "taro@example.com"
#email2    貼り付けた文字: taro@example.com   欄の値: ""
#password  貼り付けた文字: Xk9#vR2!qLm7       欄の値: ""
#tel       貼り付けた文字: 090-1234-5678      欄の値: "09012345678"
#tel       手で打った文字: 080-9876-5432      欄の値: "080-9876-5432"

Remove the paste-blocking code, and clean up text with the input event

I removed onpaste="return false" from the confirmation field and removed the whole listener from the password field. The official explanation says that in most cases the cause is a preventDefault() call inside a paste listener, and that if a listener exists only to block paste, you should remove the listener entirely. If you don't know which code is doing the blocking, the explanation says you can check the Clipboard paste box under Event Listener Breakpoints in DevTools, paste, and execution will pause on the first line of the listener that blocks it.

diff
-<p><label for="email2">メールアドレス(確認)</label><br><input id="email2" name="email2" type="email" autocomplete="email" onpaste="return false"></p>
+<p><label for="email2">メールアドレス(確認)</label><br><input id="email2" name="email2" type="email" autocomplete="email"></p>

For the phone number field, I stopped using the paste event. Instead, I used the input event, which fires whenever the value changes from user action, and removed the hyphens there (fixed.js).

js
// Telephone number: strip hyphens from the entered value (pasted and typed text are handled the same way)
const tel = document.querySelector('#tel');
tel.addEventListener('input', () => {
  tel.value = tel.value.replace(/[-‐-ー]/g, '');
});

On the fixed page, this audit passed, and the Best Practices score was 100.

console
$ node summarize.mjs out/fixed.json
lighthouseVersion: 13.6.0  formFactor: mobile  locale: en-US
url: http://localhost:8400/fixed
best-practices score: 100
paste-preventing-inputs: score=1 title="Allows users to paste into input fields"
  in best-practices: weight=3 group=best-practices-ux

When I pasted into all four fields, each one got its value. The phone number had hyphens removed both when pasted and when typed by hand. The paste version only worked for pasted text, but the input version also works for typed text.

console
$ node paste.mjs http://localhost:8400/fixed
browser: 157.0.8103.78  url: http://localhost:8400/fixed
#email     貼り付けた文字: taro@example.com   欄の値: "taro@example.com"
#email2    貼り付けた文字: taro@example.com   欄の値: "taro@example.com"
#password  貼り付けた文字: Xk9#vR2!qLm7       欄の値: "Xk9#vR2!qLm7"
#tel       貼り付けた文字: 090-1234-5678      欄の値: "09012345678"
#tel       手で打った文字: 080-9876-5432      欄の値: "08098765432"

If you block paste on document, it passes Lighthouse but still doesn't paste

I also tested blocking paste for the whole page with one listener on document, instead of on each field.

js
// Block pasting into the confirmation email and password fields across the whole page
document.addEventListener('paste', (e) => {
  if (e.target.matches('#email2, #password')) e.preventDefault();
});

On this page, the audit passed, and the Best Practices score was 100.

console
$ node summarize.mjs out/doc-bubble.json
lighthouseVersion: 13.6.0  formFactor: mobile  locale: en-US
url: http://localhost:8400/doc-bubble
best-practices score: 100
paste-preventing-inputs: score=1 title="Allows users to paste into input fields"
  in best-practices: weight=3 group=best-practices-ux

However, when I actually pasted, the confirmation and password fields stayed empty. (The output below is an excerpt showing only those two fields.)

console
$ node paste.mjs http://localhost:8400/doc-bubble
browser: 157.0.8103.78  url: http://localhost:8400/doc-bubble
#email2    貼り付けた文字: taro@example.com   欄の値: ""
#password  貼り付けた文字: Xk9#vR2!qLm7       欄の値: ""

The reason is the synthetic event Lighthouse sends. As described in the MDN explanation of the paste event, a paste the browser generates from the clipboard bubbles up to document. However, new ClipboardEvent('paste', {cancelable: true}) does not specify bubbles. As described in the MDN explanation of the Event() constructor, bubbles defaults to false, so this event does not bubble. When I sent the same event to the password field, bubbles was false, and the listener on document never received it.

console
$ node synthetic.mjs
doc-bubble: #password bubbles=false preventsPaste=false
doc-capture: #password bubbles=false preventsPaste=true

The second line is the result for a page where the same listener was added with { capture: true }. On that page, the synthetic event reached the capture listener, and Lighthouse flagged the confirmation and password fields.

Point

Passing Lighthouse does not prove that no code blocks paste through a document listener added without capture. After a fix, paste into every field in the form, not only the fields that were flagged, and confirm the value actually appears.

If you block paste on document, the fix is the same. Remove the preventDefault() call.

​Posted by 86