如何在 Python 中安全加载和转换不可信的 HTML – 沙箱

为什么沙箱很重要

在处理外部 HTML 内容时,总会存在脚本或媒体导致不可预测行为的风险。Aspose.HTML for Python via .NET 提供了沙箱机制,允许您在处理过程中控制哪些文档元素可以执行或加载,从而确保 HTML 渲染和转换的安全、可预测和高效。

大多数 HTML 处理场景(如将 HTML 转换为 PDF 或图像)并不需要执行 JavaScript 或加载远程内容。允许访问这些资源会降低处理速度,甚至可能导致不期望的行为。通过使用沙箱标志,您可以显式禁用或限制脚本、图像、插件、表单等资源类型,从而在渲染或转换期间对文档的行为进行细粒度控制。

阻止 JavaScript 执行

下面的示例展示了在将 HTML 转换为 PDF 时如何禁用 JavaScript。这确保在渲染期间不会执行任何脚本——适用于安全的 PDF(或其他格式)转换。

  1. 初始化 Configuration 类的实例。
  2. 为配置实例设置 sandbox 标志,将 Sandbox.SCRIPTS 包含进去。这会将脚本标记为不可信资源。此步骤至关重要,因为脚本可能执行恶意代码。
  3. 使用构造函数 HTMLDocument(address, configuration) 创建 HTMLDocument 实例,传入 HTML 文件路径和配置实例。
  4. 调用 Converter.convert_html() 方法将 HTML 转换为 PDF。
 1# How to disable scripts for HTML to PDF conversion using Python
 2
 3import os
 4import aspose.html as ah
 5import aspose.html.converters as conv
 6import aspose.html.saving as sav
 7
 8# Define input and output directories
 9data_dir = "data"
10output_dir = "output"
11os.makedirs(output_dir, exist_ok=True)
12
13# Create an instance of the Configuration class
14with ah.Configuration() as config:
15    # Mark "scripts" as an untrusted resource
16    config.security |= ah.Sandbox.SCRIPTS
17
18    # Initialize an HTML document with the specified configuration
19    html_path = os.path.join(data_dir, "document-with-scripts.html")
20    with ah.HTMLDocument(html_path, config) as doc:
21        # Convert HTML to PDF
22        output_pdf = os.path.join(output_dir, "document-sandbox.pdf")
23        conv.Converter.convert_html(doc, sav.PdfSaveOptions(), output_pdf)

结果:HTML 文档中的任何 JavaScript 都会被忽略,生成的输出是静态且安全的。

阻止不可信的图像

以下步骤演示如何在安全模式下禁用图像加载并将 HTML 转换为 PDF:

  1. 准备 HTML 代码并保存为文件。该 HTML 包含一个 <span> 元素,使用内联样式从 URL 设置背景图像。
  2. 创建配置实例。为此,请实例化 Configuration 类,以便为您的 HTML 文档定义自定义安全设置。
  3. 在配置中设置 Sandbox.IMAGES 标志,以将所有图像标记为不可信资源。
  4. 初始化 HTML 文档。使用自定义配置加载已保存的 HTML 文件,沙箱限制将在加载期间自动生效。
  5. 使用 Converter.convert_html() 方法渲染文档并保存为 PDF 文件。
 1# Disable loading images in HTML with sandbox configuration using Python
 2
 3import os
 4import aspose.html as ah
 5import aspose.html.converters as conv
 6import aspose.html.saving as sav
 7
 8# Prepare HTML code and save it to a file
 9code = "<span style=\"background-image:url('https://docs.aspose.com/html/images/work/lioness.jpg')\">Hello, World!!</span> " \
10       "<script>document.write('Have a nice day!');</script>"
11
12output_dir = "output"
13os.makedirs(output_dir, exist_ok=True)
14html_path = os.path.join(output_dir, "sandboxing.html")
15output_pdf = os.path.join(output_dir, "sandboxing-out.pdf")
16
17with open(html_path, "w", encoding="utf-8") as file:
18    file.write(code)
19
20# Create an instance of Configuration
21with ah.Configuration() as configuration:
22    # Mark 'IMAGES' as an untrusted resource
23    configuration.security |= ah.Sandbox.IMAGES
24
25    # Initialize an HTML document with the specified configuration
26    with ah.HTMLDocument(html_path, configuration) as document:
27        # Convert HTML to PDF
28        conv.Converter.convert_html(document, sav.PdfSaveOptions(), output_pdf)

结果:来自外部 URL 的图像将不会被加载,输出的 PDF 只包含静态文本内容。

常见问题

1. 哪些沙箱选项可用?

Aspose.HTML 提供多种 sandbox 标志,您可以组合使用,例如:

2. 沙箱如何提升性能?

通过跳过脚本执行并阻止对外部资源的网络请求,沙箱显著降低了处理时间和资源消耗,从而提升转换性能。

3. 如何同时启用多个沙箱限制?

您可以一次性启用多个限制,例如:

configuration.security |= ah.Sandbox.SCRIPTS | ah.Sandbox.IMAGES

此行代码同时禁用 JavaScript 执行和图像加载。

4. 沙箱是否支持其他语言?

是的。相同的功能在 Aspose.HTML for .NETAspose.HTML for Java 中也可用,API 类似,可用于管理沙箱限制。

5. 沙箱的常见使用场景有哪些?

您可以从 GitHub 下载完整的示例和数据文件。

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.