使用 Chrome 扩展中的 prompt() 函数来询问密码是否安全?

Is it safe to use the prompt() function from a Chrome Extension to ask for a password?

我正在开发一个小的扩展,为了方便我的工作,我想使用

prompt("enter password");

以便用户可以输入 his/her 密码。我知道提示会将密码显示为纯文本,但还有哪些其他安全问题?其他网站是否可以访问此提示的内容(在这种情况下这将是一件大事)?

如果您不担心输入的文本在没有 *** 的情况下显示,那么它看起来不错。它将与几乎任何其他 JS 密码处理一样安全。

Can other websites have access to the content of this prompt

如果是内容脚本,他们可以截取 window.prompt 并用自己的实现覆盖它,例如:

// page script:
const originalPrompt = window.prompt;
window.prompt = (text) => {
  const result = originalPrompt(text);
  // HERE, the page can do something malicious with the result
  // such as sending it to their server
  console.log('Page detected:', result);
  return result;
};


// Your script:

prompt('Password?');

但是这种方法会在前端对 任何 密码处理妥协 - 这不是 prompt 的问题,而是前端的问题一般。

要在内容脚本中解决这个问题,请确保您调用的 promptnative 代码版本,这只能在在页面上的任何其他代码运行之前通过 运行 您的代码的万无一失的方式。在 document_start.

上保存对原始提示的引用

此外,请确保在 后端 而不是前端进行验证。如果你的 JS 像

const userInput = prompt("enter password");
if (userInput !== 'theTruePassword') {
  // wrong
  return;
}
// do stuff

那么对于任何有权访问该网站的人来说,绕过密码检查并通过检查源代码进入 do stuff 将是微不足道的。相反,请确保将密码发送到 服务器 并让服务器验证密码是否正确,然后 然后 让服务器将敏感信息发送回客户。

我不建议使用提示,所以这里有一些代码,您可以修改它以在设置正确的密码后显示您的内容

<!DOCTYPE html>
<html>
   <body>
      <label>enter your pin :</label>
            <input type="text" id="pin" name="pin"><br>
    <br>
            <button id="mybutton" onclick="myfunction()">enter</button>
            <p id="demo"></p>
            <script type="text/javascript">
              function myfunction() 
              {
                if ((document.getElementById("pin").value)=="4567") 
                    document.getElementById("demo").innerHTML = "you have the correct password"
                else
                    alert("incorrect password")
               }
            </script>
   </body>
</html>

你也可以把我设置的密码改成任何你想要的