M.K*_*afi 5 security node-modules webpack electron
出于安全原因,我需要禁止 webpack 包含在我的包中的 3rd 方模块用于window.postMessage
与我的 Electron 应用程序中的其他进程进行通信。
那可能吗?
一个标准技巧可以在这里提供帮助:只需将方法移动到窗口上的另一个变量,就像这样:
window._postMessage = window.postMessage;
window.postMessage = () => {};
Run Code Online (Sandbox Code Playgroud)
首先在渲染脚本上运行此代码,或者<script>
在 html 和插件上使用标签运行该代码,这些标签和插件将无法再发送事件(事实上,它们不会崩溃,但永远不会得到响应)。
编辑
如果您想确保只有授权用户才能使用它,可以使用以下方法:
function createSecurePostMessage() {
const _postMessage = window.postMessage;
return {
get() {return function(...args) {
if (isAuthorized(...args) {
_postMessage(...args);
}
}
}
}
window.postMessage = createSecurePostMessage().get();
Run Code Online (Sandbox Code Playgroud)
现在,原始的 postMessage 位于函数内部并且无法访问,您可以实现一个方法,例如检查是否可以发送特定消息。如果有人再次调用 create,那么受保护的 postMethod 将再次“受保护”。
归档时间: |
|
查看次数: |
133 次 |
最近记录: |