I'm using this code in background.js
in a Chrome extension to copy text to the user's clipboard:
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.command == "copy") {
executeCopy(request.text);
sendResponse({farewell: "copy request received"});
}
}
);
function executeCopy(text){
var copyDiv = document.createElement('div');
copyDiv.contentEditable = true;
document.body.appendChild(copyDiv);
copyDiv.innerHTML = text;
copyDiv.unselectable = "off";
copyDiv.focus();
document.execCommand('SelectAll');
document.execCommand("Copy", false, null);
document.body.removeChild(copyDiv);
}
It copies the text with formatting. How can I copy the text in plain text with no formatting?
Your question's code contains a common security issue known as XSS. Because you take untrusted input and assign it to
.innerHTML
, you're allowing attackers to insert arbitrary HTML in the context of your document.Fortunately, attackers cannot run scripts in the context of your extension because the extension's default Content security policy forbid inline scripts. This CSP is enforced in Chrome extensions exactly because of situations like this, to prevent XSS vulnerabilities.
The correct way to convert HTML to text is via the
DOMParser
API. The following two functions show how to copy text as text, or for your case HTML as text:Note that
.textContent
completely ignores HTML tags. If you want to interpret<br>
s as line breaks, use the non-standard (but supported in Chrome).innerText
property instead of.textContent
.Here are two of the many examples of how XSS could be abused using the
executeCopy
function from your question: