Skip to content

HTML source preservation

Historical design or acceptance record. This page reproduces source documentation at the 25 November 2026 release snapshot. Some statements describe pre-release work; refer to the current release and installation guide for current instructions.

HTML source preservation and safe signature rendering

Section titled “HTML source preservation and safe signature rendering”
  • Status: Implemented; automated regression coverage is included. Current branch-specific CI evidence is maintained in implementation progress.
  • Scope: Personal overrides, domain templates, administrator overrides, versioned signatures
  • Related: signature_sanitizer.py, signature_renderer.py, signature_catalog.py, signature_storage.py

Implemented on the working branch:

  • Added nullable domains.template_html_source and user_overrides.html_content_source columns through migration 0012_html_source; existing sanitized values are backfilled without claiming that previously removed markup can be recovered.
  • Domain, personal override and administrator override write paths retain submitted source separately while continuing to store sanitized snapshots in the legacy delivery columns.
  • Editor-facing reads return source with a safe legacy fallback; legacy administrator content is sanitized when no source snapshot exists.
  • Reusable signature version files retain original source bytes and their SHA-256 integrity hashes cover those exact bytes.
  • The signature renderer sanitizes source before preview and mail output; raw source is not the delivery representation.
  • All three raw-HTML editors display an Outlook-markup warning explaining that conditional comments and VML are preserved in source but removed from sanitized output.
  • Automated tests cover source round-trips, sanitizer behavior, API delivery paths and migration compatibility.

CI includes source-preservation regression coverage, PostgreSQL/SQLite migrations, Docker/browser smoke and encrypted backup/fresh restore. Current branch-specific CI evidence is maintained in implementation progress.

Automated Chromium/Playwright coverage now exercises the self-hosted CKEditor integration across personal, domain-template and administrator editing paths, plus signature catalog create/update flows. Real SOGo, Outlook and Thunderbird rendering remains separate acceptance work. Outlook conditional comments and VML are preserved in stored source but removed from sanitized preview/delivery output, so this implementation does not make Outlook VML available in delivered signatures.

OpenSigWeave currently sanitizes signature HTML before persistence in all four storage paths:

Content type Current write path Current storage
Domain template PUT /domains/{domain_id} domains.template_html
Personal override POST /api/my-signature user_overrides.html_content
Administrator override POST /api/admin/overrides user_overrides.html_content
Reusable signature/version create_signature / add_signature_version signatur/{name}/.versions/{id}.html

The sanitizer intentionally removes comments and unsupported elements, including Outlook conditional comments and VML. Because sanitization happens before persistence, the original source cannot be recovered. A client-side warning alone does not solve this data-loss problem.

Separate editable source HTML from renderable HTML.

  1. Persist the exact submitted source HTML (subject to input type and size validation) as the editor’s source of truth.
  2. Continue to derive renderable HTML with the existing allowlist sanitizer. Never broaden the allowlist as a side effect of source preservation.
  3. Sanitize immediately before template rendering, including all nested/group templates and preview paths.
  4. Only sanitized, rendered HTML may leave the signature rendering boundary for mail injection.
  5. Raw source may be returned only by authenticated editor/catalog APIs that already authorize the corresponding user or administrator. It must never be included in the Rspamd response or an unauthenticated endpoint.
  6. Keep the warning in the editor, but change its meaning after rollout: Outlook conditional markup is retained in source mode but is excluded from sanitized output. Do not claim that preserving source makes Outlook VML render in delivered messages.

This deliberately separates fidelity of stored source from the security policy of delivered HTML. It does not promise that unsupported markup will appear in outgoing email.

Add nullable source columns:

  • domains.template_html_source
  • user_overrides.html_content_source

Keep the existing template_html and html_content columns as sanitized renderable snapshots for compatibility with existing delivery queries and integrations.

On every write, in one database transaction:

  • validate the original input and enforce the existing 64 KiB limit;
  • store the exact input in the corresponding *_source column;
  • store sanitize_signature_html(source) in the existing renderable column.

Editor GET/list APIs return the source column (falling back to the renderable column for pre-migration or partially migrated rows). Delivery continues to read only the renderable column and sanitizes it again at the rendering boundary as defense in depth.

The migration backfills each source column from its existing sanitized column. This cannot restore historical Outlook markup that has already been discarded; it only establishes a consistent source field for future edits.

Version files become immutable source snapshots. signature_versions.sha256 and size_bytes describe the exact source bytes, not sanitized output. The current signature.html convenience file mirrors the source of the latest version.

Remove pre-persistence sanitization from signature_catalog.py. Keep storage integrity checks over the original bytes. Sanitize after reading a version and immediately before preview or mail rendering. Existing versions are already sanitized; they remain valid source snapshots and need no destructive rewrite.

No second sanitized asset is required: sanitized HTML is deterministic derived data and should be generated at the trust boundary. If performance later justifies caching it, the cache must be disposable and keyed by the source SHA-256 plus sanitizer-policy version.

The intended data flow is:

Editor input
|
v
Validate type and size
|
+----> Persist original source (database source column / immutable version file)
|
v
Allowlist sanitizer
|
v
Persist sanitized snapshot (legacy database columns only)
|
v
Read sanitized content for delivery
|
v
Sanitize again -> sandboxed template rendering -> sanitized group expansion
|
v
Rspamd response / mail injection

Security invariants:

  • The raw source is never passed directly to Markup, srcdoc, the Rspamd response, or a mail client.
  • Dynamic identity values remain HTML-escaped by the sandboxed renderer.
  • Group template content is sanitized before it is wrapped as Markup.
  • Nested group expansion remains one level deep.
  • Sanitization remains allowlist-based; no blanket VML, comment, style, event-handler, or arbitrary-tag allowance is introduced.
  • File size, database request limits, file permissions, path checks and SHA-256 integrity validation remain in force.
  • Preview and delivery use the same sanitizer/rendering policy. A preview must not be more permissive than actual mail output.
Surface Read for editor Write Delivery source
Personal override html_content_source with legacy fallback source + sanitized snapshot html_content
Domain template template_html_source with legacy fallback source + sanitized snapshot template_html
Administrator override html_content_source with legacy fallback source + sanitized snapshot html_content
Reusable signature version immutable version source file immutable source version source file, sanitized after read

All editor-facing routes must preserve their existing authorization checks. The internal mail-rendering endpoint must return only the sanitized rendered result.

  1. Add the two nullable source columns and backfill from current sanitized values.
  2. Update personal, domain and administrator write/read endpoints. Add tests proving source round-trip and sanitized delivery behavior.
  3. Change reusable signature version writes to store source bytes; retain SHA-256 verification against those bytes. Add tests for exact source round-trip and sanitizer-at-render behavior.
  4. Audit every preview, nested group, legacy fallback and mail-rendering call site. Ensure no path skips sanitization and no editor API is used by the mail injection client.
  5. Update the editor warning text and add browser-level tests for all three raw HTML editors.
  6. Only then consider any narrowly scoped Outlook output support as a separate security review and compatibility decision.

The migration must not attempt to reconstruct source markup from old sanitized records. A downgrade must not silently discard newly saved source HTML; if a downgrade path cannot preserve it, it must be explicitly blocked or preceded by an export/backup.

  • Source HTML round-trips byte-for-byte (UTF-8 text) through save and editor reload for all four content types.
  • Sanitized HTML continues to remove scripts, event handlers, unsafe URL schemes, comments and disallowed VML.
  • The mail-rendering endpoint and group preview return sanitized, rendered HTML only.
  • Dynamic template values remain escaped.
  • Group selection, version history, file integrity checks and backup/restore remain intact.
  • Existing records migrate without data loss; historical content already removed by the old sanitizer is not falsely represented as recoverable.
  • All source-returning routes retain their existing authorization boundaries.

Decision: Do not emit Outlook conditional comments or VML in delivered signatures yet. Preserve them only in the authenticated editor source, as implemented above. The existing allowlist sanitizer remains authoritative for preview and delivery.

This is an intentional compatibility limitation, not an accidental sanitizer regression. Outlook conditional comments are HTML comments to most clients but are interpreted by supported Outlook/Word rendering paths. Their payload can contain VML elements, namespace declarations, URL-bearing attributes, CSS and nested fallback markup. Preserving the comment wrapper while passing its payload through the current HTML sanitizer would either remove the VML and produce a non-functional Outlook branch, or require a separate parser and policy for content the current sanitizer does not understand.

A blanket exception for <!--[if mso]> blocks is not acceptable. It would create a second HTML trust boundary and could reintroduce active markup, unsafe URLs, remote-resource abuse, CSS constructs or parser-differential behavior that the current allowlist intentionally removes. Allowing arbitrary v: elements or attributes globally is also out of scope.

  1. Define a deliberately small supported VML subset from real signature examples; do not aim for general VML compatibility.
  2. Parse conditional-comment syntax structurally and reject malformed, nested, duplicated or unsupported conditions. Do not extract blocks with a permissive regular expression.
  3. Sanitize the normal HTML fallback and the Outlook-only payload independently. The Outlook path needs its own explicit element, attribute, namespace, URL-scheme and CSS-property allowlists.
  4. Reject script-capable, form, active, embedded, external-document and unknown namespace content. Remote image behavior must follow the same explicit policy as ordinary signature images.
  5. Keep the source snapshot unchanged, but derive both output branches at the rendering trust boundary. Never return the untrusted source block directly to preview or mail injection.
  6. Add adversarial tests for malformed comments, namespace spoofing, event attributes, unsafe URL schemes, CSS URL/expression constructs, nested fallback content and parser differentials.
  7. Verify output in supported Outlook desktop versions and at least one non-Outlook client, including the actual delivered MIME and final DKIM signature.
  • Enabling strip_comments=False on the shared nh3.Cleaner: this changes comment handling globally and does not make VML safe.
  • Adding VML names to the shared _ALLOWED_TAGS: this broadens the normal HTML trust boundary and does not validate namespace-specific attributes.
  • Passing conditional blocks through unchanged: source preservation is not output sanitization.
  • Treating a successful sanitizer unit test as proof of Outlook rendering: client rendering and delivered MIME need separate acceptance.

Until the requirements above can be met and reviewed, the supported behavior remains: preserve the exact source for editing, remove conditional comments/VML from preview and delivery, and display the existing editor warning.