简体   繁体   English

在注入的脚本和Google Chrome扩展程序代码/内容脚本之间传递消息的最安全方法是什么?

[英]What is the most secure way of passing messages between an injected script and Google Chrome extension code/content script?

Definitions: Please note from the outset that by 'injected script', 'extension code' and 'content script' I will be using the definitions provided in the excellent first answer to this question . 定义:请从一开始就注意,通过“注入脚本”,“扩展代码”和“内容脚本”,我将使用该问题的出色第一答案中提供的定义。

Assumption: Handling confidential information is less secure if I do it directly within my injected script (in the web zone) than if I do it within the chrome:// zone of content scripts and extension code. 假设:如果直接在注入的脚本(在Web区域中)中处理机密信息,则比在内容脚本和扩展代码的chrome://区域中处理机密信息的安全性低。 I therefore should use message passing to send confidential information from the web zone to the chrome:// zone for it to be handled. 因此,我应该使用消息传递将机密信息从Web区域发送到chrome://区域以进行处理。

Question: I'm building a Google Chrome extension where I need to run some operations on sensitive user data derived from my injected script. 问题:我正在构建一个Google Chrome扩展程序,我需要在该程序中对从注入的脚本派生的敏感用户数据进行一些操作。 The data in question is confidential and I must do all I can to ensure that it can't be seen by anyone but the user of the extension until I've operated on it. 有问题的数据是机密的,我必须尽一切努力确保只有扩展用户才能看到该数据,除非我对其进行了操作。 Of the 3 techniques (defined below) that can be used to pass messages between an injected script and extension code/content script which would be best for this purpose? 可以用于在注入的脚本和扩展代码/内容脚本之间传递消息的3种技术(如下定义)中最适合此目的?

My understanding of the 3 different techniques that can be used for passing data between an injected script and extension code/content script: 我对可用于在注入的脚本和扩展代码/内容脚本之间传递数据的3种不同技术的理解:

  1. For messaging passing between an injected script and extension code (eg a background page), one can use the chrome.runtime API . 为了在注入的脚本和扩展代码 (例如,后台页面)之间传递消息,可以使用chrome.runtime API

  2. For messaging passing between an injected script and a content script one can use window.postMessage . 为了在注入的脚本和内容脚本之间传递消息,可以使用window.postMessage

  3. Another way of passing messages between an injected script and a content script is via document.dispatchEvent(CustomEvent) . 在注入的脚本和内容脚本之间传递消息的另一种方法是通过document.dispatchEvent(CustomEvent)

My understanding is that method 1. cannot be used for message passing between an injected script and a content script while methods 2. and 3. cannot be used for message passing between an injected script and extension code (unless the message is forwarded by the content script to, for example, a background page). 我的理解是,方法1.不能用于在注入的脚本和内容脚本之间传递消息,而方法2.和3.不能用于在注入的脚本和扩展代码之间传递消息(除非消息由内容转发)脚本,例如到后台页面)。

While code running in your background page / content script is pretty well isolated, as soon as you inject a script into the page context - you're in the Wild West. 尽管在后台页面/内容脚本中运行的代码被很好地隔离了,但一旦将脚本注入页面上下文中,您就处于狂野西部。 Any extension, and the page itself, has access to that context and can influence how your code executes. 任何扩展名以及页面本身都可以访问该上下文,并且可以影响代码的执行方式。

For example, some extension can override chrome.runtime.sendMessage to send the message AND log it. 例如,某些扩展名可以覆盖chrome.runtime.sendMessage以发送消息并进行记录。 This needs to be seriously taken into account - probably, you already lost. 需要认真考虑这一点-可能您已经迷路了。

That said, method 1 is harder to break into than 2/3 - as explained, the attacker extension would need to directly alter the page context to interfere, while in case of DOM events it can just listen to them from the safety of its content script - events are broadcast to all content script contexts. 就是说,方法1比2/3更加难以破解-如所解释的,攻击者扩展将需要直接更改页面上下文以进行干预,而在发生DOM事件的情况下,它可以从内容安全的角度来监听它们脚本-事件会广播到所有内容脚本上下文。

Hypothetically, you could employ some sort of asymmetric cryptography for the channel as well - provide the injected script with the encryption key and keep the decryption key in the privileged zone. 假设地,您也可以对通道采用某种非对称加密-为注入的脚本提供加密密钥,并将解密密钥保留在特权区域中。 That safeguards the communication, if that's the only thing intercepted, but at some point the plaintext data exists in the global context - that may be enough for the attacker script to extract (that, you have to assume, executed before your injected script). 如果这是唯一被拦截的东西,则可以保护通信,但是在某些时候,纯文本数据存在于全局上下文中-可能足以使攻击者脚本提取(必须假定在注入的脚本之前执行)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 chrome 扩展中的后台和内容脚本之间传递消息有困难 - Difficulty passing messages between background and content script in chrome extension Chrome扩展程序,这是将消息从注入的脚本发送到后台的最佳方法 - Chrome extension, best way to send messages from injected script to background Chrome扩展程序–尝试在后台和第二个内容脚本之间发送消息(?) - Chrome extension – Trying to send messages between the background and a second content script(?) Google Chrome 扩展:通过 chrome.tabs.executeScript 将来自注入脚本的变量暴露给以相同方式注入的另一个脚本 - Google Chrome Extension: Expose variable from injected script via chrome.tabs.executeScript to another script injected the same way Chrome 扩展:将消息从注入的脚本发送到内容脚本 - Chrome Extension: Sending message from Injected Script to Content Script Chrome 扩展消息在后台和内容脚本之间传递 - Chrome Extension Message Passing between background and content script 扩展脚本不接收来自内容脚本的消息 - chrome扩展 - Extension script not receiving messages from content script - chrome extension 删除chrome扩展中的注入脚本 - Remove injected script in chrome extension Chrome 扩展:检查内容脚本是否已被注入 - Chrome extension: Checking if content script has been injected or not Chrome扩展程序的内容脚本未注入特定网站 - Chrome extension's content script not getting injected on specific websites
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM