CodeQL 文档

跨窗口通信,目标来源未受限制

ID: js/cross-window-information-leak
Kind: path-problem
Security severity: 4.3
Severity: error
Precision: high
Tags:
   - security
   - external/cwe/cwe-201
   - external/cwe/cwe-359
Query suites:
   - javascript-code-scanning.qls
   - javascript-security-extended.qls
   - javascript-security-and-quality.qls

点击查看 CodeQL 存储库中的查询

window.postMessage 方法允许不同的窗口或 iframe 直接通信,即使它们是从不同的来源加载的,也绕过了通常的同源策略。

消息发送者可以通过指定目标来源来限制接收者的来源。如果接收者窗口不是来自该来源,则不会发送消息。

或者,发送者可以指定 '*' 作为目标来源,这意味着任何来源都是可以接受的,并且消息始终会被发送。

如果要发送的消息包含敏感数据(例如用户凭据),则不应使用此功能:接收者窗口可能已从恶意网站加载,然后数据将变得可用。

建议

如果可能,在使用 window.postMessage 时指定目标来源。或者,在发送敏感数据之前对其进行加密,以防止未经授权的接收者访问数据。

示例

以下示例代码将用户凭据(在本例中为用户名)发送到 window.parent,而无需检查其来源。如果恶意网站将包含此代码的页面加载到 iframe 中,它将能够访问用户名。

window.parent.postMessage(userName, '*');

为防止这种情况发生,应限制目标窗口的来源,如以下示例所示

window.parent.postMessage(userName, 'https://github.com');

参考

  • ©GitHub, Inc.
  • 条款
  • 隐私