沙箱 – 安全代码执行 – Sandboxing C#

C# 沙盒 – Sandbox

C# 沙箱是一种受限环境,代码在此环境中运行时权限、资源和特权有限。使用沙箱的主要目的是隔离潜在的不信任或不安全代码,限制其在出现安全漏洞或恶意行为时可能造成的潜在破坏。

沙箱环境有助于降低安全风险,确保代码的安全执行:

本文探讨了 C# 沙箱的概念,并演示了如何使用 Aspose.HTML for .NET 实现安全代码执行。我们将重点研究 Aspose.HTML for .NET 提供的用于检测和应对 C# 应用程序中潜在安全威胁的 Configuration 类的 Security 属性。

沙盒环境

代码安全 – 阻止脚本执行

在沙箱中,您可以将潜在的不信任元素与应用程序的其他部分隔离,从而确保代码的安全性,保护整个应用程序不出现漏洞。沙箱标志*集是由零个或多个标志组成的集合,用于限制潜在不信任资源的能力。沙盒属性允许你对框架中加载的内容设置一系列限制,例如阻止表单和脚本。这可以提高当前文档的代码安全性,尤其是从未经验证的来源加载文档时。

下面的 C# 示例展示了如何将脚本标记为不可信任的资源,并在将 HTML 转换为 PDF 时禁用它们:

  1. 初始化 Configuration 类的实例。
  2. 设置配置实例的 sandbox flag 以包含 Sandbox.Scripts 值。这就将脚本标记为沙盒环境中不可信任的资源。这一步至关重要,因为脚本会带来执行恶意代码的潜在风险。
  3. 使用 HTMLDocument(address, configuration) 构造函数创建 HTMLDocument 类实例,该构造函数接收 HTML 文件路径和配置实例。
  4. 调用 ConvertHTML(document, options, outputPath) 方法将 HTML 转换为 PDF。
 1// How to disable scripts for HTML to PDF conversion using C#
 2
 3// Create an instance of the Configuration class
 4using (Configuration configuration = new Configuration())
 5{
 6    // Mark "scripts" as an untrusted resource
 7    configuration.Security |= Sandbox.Scripts;
 8
 9    // Initialize an HTML document with specified configuration
10    using (HTMLDocument document = new HTMLDocument(Path.Combine(DataDir, "document-with-scripts.html"), configuration))
11    {
12        // Convert HTML to PDF
13        Converter.ConvertHTML(document, new PdfSaveOptions(), Path.Combine(OutputDir, "document-sandbox.pdf"));
14    }
15}

通过设置启用 “Sandbox.Scripts “的安全标记, Aspose.HTML for .NET 库 可确保不执行 HTML 文档中的脚本,从而帮助提高安全性并降低与不受信任的脚本相关的潜在风险。

如何禁用图像加载

举个例子,在执行 HTML 代码时,使用沙箱禁止加载图片:

  1. 编写 HTML 代码并保存到文件中。HTML 代码包含一个具有内联样式的<span>元素,该样式设置了来自 URL 的背景图像。
  2. 沙盒设置。创建一个 Configuration 类的新实例,并在配置实例的 Security 属性中设置 Sandbox.Images 标记。这表明图像资源在沙盒环境中应被视为不可信任的。将图像标记为不信任资源后,对潜在恶意图像源的访问就会受到限制。
  3. 使用指定配置初始化 HTML 文档。
  4. 调用 ConvertHTML(document, options, outputPath) 方法将 HTML 文档转换为 PDF 文件。
 1// Disable loading images in HTML with sandbox configuration using C#
 2
 3// Prepare HTML code and save it to a file
 4string code = "<span style=\"background-image:url('https://docs.aspose.com/html/images/work/lioness.jpg')\">Hello, World!!</span> " +
 5              "<script>document.write('Have a nice day!');</script>";
 6
 7File.WriteAllText(Path.Combine(OutputDir, "sandboxing.html"), code);
 8
 9// Create an instance of Configuration
10using (Configuration configuration = new Configuration())
11{
12    // Mark 'Images' as an untrusted resource
13    configuration.Security |= Sandbox.Images;
14
15    // Initialize an HTML document with specified configuration
16    using (HTMLDocument document = new HTMLDocument(Path.Combine(OutputDir, "sandboxing.html"), configuration))
17    {
18        // Convert HTML to PDF
19        Converter.ConvertHTML(document, new PdfSaveOptions(), Path.Combine(OutputDir, "sandboxing-out.pdf"));
20    }
21}

上面的 C# 示例演示了对 HTML 代码进行沙箱处理的过程,在沙箱环境中将特定资源(在本例中为图像)标记为不可信任的,然后将 HTML 转换为 PDF 格式并禁用图像加载。

沙箱标志 - Sandboxing Flags

在软件开发领域,安全是一个首要问题。沙箱就是创建一个安全的环境,让不受信任的代码在不影响系统完整性的情况下运行。C# 沙箱通过保护应用程序免受潜在漏洞的影响,帮助开发人员降低代码安全风险。Aspose.HTML C# 库提供了多种 sandboxing flags,每种标志都提供了不同的隔离和安全情况。下面是一些沙箱标志:

NameDescription
NoneIf a sandbox flag is not set, then every sandbox function is accepted.
NavigationThe flag prevents content from navigating browsing contexts other than the sandboxed browsing context itself (or browsing contexts further nested inside it), auxiliary browsing contexts, and the top-level browsing context.
PluginsThe flag prevents content from instantiating plugins, whether using the embed element, the object element, the applet element, or through the navigation of a nested browsing context unless those plugins can be secured.
OriginThe flag forces content into a unique origin, thus preventing it from accessing other content from the same origin.
FormsThe flag blocks form submission.
ScriptsThe flag blocks script execution.
ImagesThe flag disables image loading.
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.