Sandbox

Description

An iframe that renders untrusted HTML or external URLs in a locked-down context

Sandbox wraps an <iframe> and configures it so that content it renders cannot access the host page, its cookies, or browser storage. By default, the frame is loaded via srcdoc with sandbox="allow-scripts allow-forms"allow-same-origin is intentionally omitted, which means the sandboxed document runs under an opaque origin and has no access to the host origin.

A strict Content-Security-Policy meta tag (default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; img-src data: blob:;) is injected as the first element in the document head so that network exfiltration is blocked even if the sandbox flags are relaxed. Both the CSP and the sandbox attribute string are fully configurable.

A small bootstrap script is injected into the document alongside the CSP tag. It sets up a MessageChannel-based communication channel between the host page and the frame. Uncaught errors, unhandled promise rejections, and CSP violations raised inside the frame are captured by the bootstrap and posted back to the host where they surface through the OnError callback. Arbitrary messages can be exchanged in both directions via OnMessage / PostMessage.

The frame height can optionally track its content: when FitHeightToContent is enabled, the bootstrap script posts height measurements (on load and via ResizeObserver) which the component uses to resize the iframe.

Use UI.Sandbox(html) for inline HTML content and UI.SandboxUrl(url) for external URLs. CSP injection and bootstrap messaging only apply to srcdoc content; cross-origin src documents are not modified.

AllowSameOrigin weakens isolation

Setting .AllowSameOrigin() lets the framed document share the host origin, which allows the host to read contentDocument and contentWindow. Only use it with content you trust, and not in combination with untrusted user-supplied HTML.

API reference

class

Sandbox

public sealed class Sandbox : ComponentBase<Sandbox, HTMLIFrameElement>, ISpecialCaseStyling

A locked-down <iframe> for rendering untrusted HTML / apps. By default the frame is fully sandboxed: the content is loaded through srcdoc with sandbox="allow-scripts allow-forms" (no allow-same-origin, so the document runs in an opaque origin and cannot touch the host page, cookies or storage), and a strict Content-Security-Policy meta tag is injected as the very first thing in the document so the sandboxed code cannot exfiltrate data over the network. A small bootstrap script is injected into the document that wires up a MessageChannel based post-message flow: uncaught errors, unhandled promise rejections and CSP violations are captured inside the frame and posted back to the host where they can be surfaced through OnError. The same channel is used for arbitrary host <-> sandbox messaging via OnMessage / PostMessage.

Namespace
Tesserae
Inheritance
ComponentBase<Sandbox, HTMLIFrameElement> → Sandbox
Implements
ISpecialCaseStyling

Constructors

NameDescription
SandboxCreates a new fully-sandboxed frame, optionally with initial HTML content.
Constructor
Sandbox
public Sandbox(string html = null)

Creates a new fully-sandboxed frame, optionally with initial HTML content.

Properties

NameDescription
StylingContainerThe element that receives sizing styles (the iframe itself).
PropagateToStackItemParentStyling propagates up to the stack item parent.
IFrameElementThe underlying iframe element. Only safe to reach into when AllowSameOrigin is set.
Property
Sandbox.StylingContainer
public HTMLElement StylingContainer

The element that receives sizing styles (the iframe itself).

Property
Sandbox.PropagateToStackItemParent
public bool PropagateToStackItemParent

Styling propagates up to the stack item parent.

Property
Sandbox.IFrameElement
public HTMLIFrameElement IFrameElement

The underlying iframe element. Only safe to reach into when AllowSameOrigin is set.

Methods

NameDescription
FromHtmlSets the HTML document rendered inside the sandbox (loaded via srcdoc).
FromUrlLoads an external URL into the frame (via src). CSP / bootstrap injection do not apply to cross-origin documents.
AllowScriptsAllows the sandboxed content to run scripts (on by default).
AllowFormsAllows the sandboxed content to submit forms (on by default).
AllowPopupsAllows popups (e.g. target="_blank", window.open) to escape the sandbox.
AllowModalsAllows the content to open modal dialogs (alert, confirm, prompt).
AllowDownloadsAllows downloads initiated from within the frame.
AllowSameOriginLets the framed document share the host origin. This weakens the sandbox - it is only needed when the host has to reach into contentDocument / contentWindow (for example to hook events or measure layout). Do not combine with untrusted content.
AllowTokenAdds a raw sandbox token (e.g. "allow-pointer-lock") on top of the configured flags.
SandboxAttributeReplaces the computed sandbox value with an explicit one.
UnsandboxedRemoves the sandbox attribute entirely - the frame is no longer sandboxed. Use with extreme care.
ContentSecurityPolicyOverrides the injected Content-Security-Policy (only applied to srcdoc HTML content).
NoContentSecurityPolicyDisables injecting a Content-Security-Policy meta tag into the document.
FitHeightToContentGrows the frame to match its content height (driven by height messages from the bootstrap script).
ScrollingEnables or disables scrolling within the frame.
OnLoadedInvoked every time the frame finishes loading, with the underlying iframe element. This is the place to hook into the document - but reading contentDocument requires AllowSameOrigin.
OnErrorInvoked when the sandboxed content reports an uncaught error, an unhandled promise rejection or a CSP violation back over the post-message channel.
OnMessageInvoked with the payload of any custom message the content sends via window.tssSandbox.post(...).
PostMessageSends a message into the sandboxed document. Received inside the frame as a tss:message window event.
RenderRenders the component's root iframe element.
Method
Sandbox.FromHtml
public Sandbox FromHtml(string html)

Sets the HTML document rendered inside the sandbox (loaded via srcdoc).

Method
Sandbox.FromUrl
public Sandbox FromUrl(string url)

Loads an external URL into the frame (via src). CSP / bootstrap injection do not apply to cross-origin documents.

Method
Sandbox.AllowScripts
public Sandbox AllowScripts(bool allow = true)

Allows the sandboxed content to run scripts (on by default).

Method
Sandbox.AllowForms
public Sandbox AllowForms(bool allow = true)

Allows the sandboxed content to submit forms (on by default).

Method
Sandbox.AllowPopups
public Sandbox AllowPopups(bool allow = true)

Allows popups (e.g. target="_blank", window.open) to escape the sandbox.

Method
Sandbox.AllowModals
public Sandbox AllowModals(bool allow = true)

Allows the content to open modal dialogs (alert, confirm, prompt).

Method
Sandbox.AllowDownloads
public Sandbox AllowDownloads(bool allow = true)

Allows downloads initiated from within the frame.

Method
Sandbox.AllowSameOrigin
public Sandbox AllowSameOrigin(bool allow = true)

Lets the framed document share the host origin. This weakens the sandbox - it is only needed when the host has to reach into contentDocument / contentWindow (for example to hook events or measure layout). Do not combine with untrusted content.

Method
Sandbox.AllowToken
public Sandbox AllowToken(string token)

Adds a raw sandbox token (e.g. "allow-pointer-lock") on top of the configured flags.

Method
Sandbox.SandboxAttribute
public Sandbox SandboxAttribute(string value)

Replaces the computed sandbox value with an explicit one.

Method
Sandbox.Unsandboxed
public Sandbox Unsandboxed()

Removes the sandbox attribute entirely - the frame is no longer sandboxed. Use with extreme care.

Method
Sandbox.ContentSecurityPolicy
public Sandbox ContentSecurityPolicy(string policy)

Overrides the injected Content-Security-Policy (only applied to srcdoc HTML content).

Method
Sandbox.NoContentSecurityPolicy
public Sandbox NoContentSecurityPolicy()

Disables injecting a Content-Security-Policy meta tag into the document.

Method
Sandbox.FitHeightToContent
public Sandbox FitHeightToContent(bool fit = true)

Grows the frame to match its content height (driven by height messages from the bootstrap script).

Method
Sandbox.Scrolling
public Sandbox Scrolling(bool enabled)

Enables or disables scrolling within the frame.

Method
Sandbox.OnLoaded
public Sandbox OnLoaded(Action<HTMLIFrameElement> onLoaded)

Invoked every time the frame finishes loading, with the underlying iframe element. This is the place to hook into the document - but reading contentDocument requires AllowSameOrigin.

Method
Sandbox.OnError
public Sandbox OnError(Action<SandboxError> onError)

Invoked when the sandboxed content reports an uncaught error, an unhandled promise rejection or a CSP violation back over the post-message channel.

Method
Sandbox.OnMessage
public Sandbox OnMessage(Action<object> onMessage)

Invoked with the payload of any custom message the content sends via window.tssSandbox.post(...).

Method
Sandbox.PostMessage
public Sandbox PostMessage(object message)

Sends a message into the sandboxed document. Received inside the frame as a tss:message window event.

Method
Sandbox.Render
public override HTMLElement Render()

Renders the component's root iframe element.

Fields

NameDescription
DefaultContentSecurityPolicyThe fully-locked default CSP: inline scripts and styles only, images limited to data/blob URIs, no network access.
Field
Sandbox.DefaultContentSecurityPolicy
public const string DefaultContentSecurityPolicy = "default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; img-src data: blob:;"

The fully-locked default CSP: inline scripts and styles only, images limited to data/blob URIs, no network access.

class

SandboxError

public sealed class SandboxError

An error, unhandled rejection or CSP violation reported back from a Sandbox's content.

Namespace
Tesserae

Properties

NameDescription
KindOne of error, unhandledrejection or csp.
MessageHuman-readable error message.
SourceThe source file the error originated from, when available.
LineLine number, when available.
ColumnColumn number, when available.
StackJavaScript stack trace, when available.
IsContentSecurityPolicyViolationTrue for a Content-Security-Policy violation report.
Property
SandboxError.Kind
public string Kind { get; }

One of error, unhandledrejection or csp.

Property
SandboxError.Message
public string Message { get; }

Human-readable error message.

Property
SandboxError.Source
public string Source { get; }

The source file the error originated from, when available.

Property
SandboxError.Line
public int Line { get; }

Line number, when available.

Property
SandboxError.Column
public int Column { get; }

Column number, when available.

Property
SandboxError.Stack
public string Stack { get; }

JavaScript stack trace, when available.

Property
SandboxError.IsContentSecurityPolicyViolation
public bool IsContentSecurityPolicyViolation

True for a Content-Security-Policy violation report.

Methods

NameDescription
ToStringReturns a single-line, human readable description of the error.
Method
SandboxError.ToString
public override string ToString()

Returns a single-line, human readable description of the error.

See also

© 2026 Curiosity. All rights reserved.