Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Use
Configuration.Security with
Sandbox flags when an HTML document should be processed with restricted capabilities. For example, set Sandbox.Scripts to prevent script execution or Sandbox.Images to disable image loading before creating an HTMLDocument with that configuration.
Aspose.HTML for .NET lets you control document processing behavior through the
Configuration class. The
Environment Configuration article gives the broader picture: user agent settings, fonts, runtime timeouts, and network services. This article focuses only on sandboxing – using Configuration.Security to mark specific document capabilities or resource types as restricted during HTML parsing, rendering, or conversion.
Sandboxing is useful when your application loads HTML from a source you do not fully control, or when a conversion workflow should deliberately ignore scripts, images, forms, plugins, or navigation behavior. It is not an operating-system or container security boundary; use it as an Aspose.HTML document-processing control, together with your normal application security, file-system, and network restrictions.
A sandboxing flag set is a set of one or more Sandbox values that restrict selected document capabilities. In Aspose.HTML for .NET, you apply these flags through the Security property of a Configuration instance, then pass that configuration to the HTMLDocument constructor.
The key workflow is the same for each sandbox rule:
configuration.Security.Use bitwise flag composition when more than one restriction is needed, for example configuration.Security |= Sandbox.Scripts | Sandbox.Images.
Scripts are one of the most common capabilities to disable when processing untrusted HTML. Setting Sandbox.Scripts marks scripts as restricted so they are not executed during document processing.
The following C# example disables scripts before converting HTML to PDF. The input HTML file contains scripts, but the document is loaded with a sandboxed configuration.
To disable scripts during HTML to PDF conversion:
Configuration instance.Sandbox.Scripts to configuration.Security.HTMLDocument with the source file path and configuration.PdfSaveOptions for PDF output. 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}By applying Sandbox.Scripts before the document is created, the conversion workflow processes the document without executing scripts from the HTML source. If you also need to limit how long scripts may run instead of disabling them completely, see
Runtime Service in Environment Configuration.
Images can be disabled when a conversion should ignore remote or local image resources. This is useful for reducing external resource access, testing fallback output, or processing HTML where image URLs are not trusted.
The following C# example creates HTML with a CSS background image and a script, then sets Sandbox.Images before loading the document and converting it to PDF. The image resource is treated as restricted during processing.
To disable image loading in C#:
Configuration instance.Sandbox.Images to configuration.Security.HTMLDocument with the configured environment.Converter.ConvertHTML(). 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}In this workflow, image loading is disabled through the sandbox configuration, while the rest of the document can still be parsed and converted. If you need more detailed control over remote requests, logging, caching, or blocking specific URLs, use Network Service or message handlers instead of sandbox flags alone.
The Sandbox enumeration contains flags for common document restrictions. Use the smallest set of flags that matches the workflow you want to control.
| Sandbox flag | Use it to restrict |
|---|---|
None | No sandbox restriction is applied. |
Navigation | Navigation from the sandboxed browsing context to other browsing contexts. |
Plugins | Plugin instantiation through elements such as embed, object, or applet. |
Origin | Access to content from the same origin by forcing a unique origin. |
Forms | Form submission. |
Scripts | Script execution. |
Images | Image loading. |
| Problem | Cause | Solution |
|---|---|---|
| Scripts still affect output | The document was created before Sandbox.Scripts was applied, or the wrong Configuration instance was used. | Set configuration.Security before creating HTMLDocument, and pass that same configuration to the constructor. |
| Images still appear in the result | Sandbox.Images was not applied before loading the document, or the visual result comes from CSS, text, or cached/local output rather than a loaded image resource. | Apply Sandbox.Images before document creation and verify which resource creates the visual output. |
| Too many resources are blocked | Multiple sandbox flags were combined without checking whether each restriction is required. | Start with the narrowest flag, test the result, then add more flags only when the workflow needs them. |
| Remote URLs need selective blocking | Sandbox flags restrict broad capability categories and do not replace URL-level network policy. | Use Network Service or custom message handlers for request-level control. |
| Sandboxing is treated as full application isolation | Aspose.HTML sandbox flags control document processing behavior; they do not replace OS, container, process, file-system, or network security. | Combine sandbox flags with normal application security controls when processing untrusted input. |
Create a Configuration, add Sandbox.Scripts to configuration.Security, and pass that configuration to the HTMLDocument constructor before converting or rendering the document.
Add Sandbox.Images to configuration.Security before creating the HTMLDocument. Then process or convert the document with that configured environment.
Yes. Sandbox values are flags, so you can combine them, for example Sandbox.Scripts | Sandbox.Images, when the same workflow must restrict more than one capability.
No. Aspose.HTML sandboxing controls selected HTML document capabilities during processing. It does not replace operating-system, container, process, file-system, or network isolation.
Use sandboxing for broad capability restrictions such as scripts or images. Use Network Service or message handlers when you need request-level control, logging, caching, timeouts, or URL-specific blocking.
The complete C# examples and data files are available in the Aspose.HTML for .NET GitHub repository.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.