ExMCP.Content.Sanitizer (ex_mcp v1.0.0-rc.8)

Copy Markdown View Source

Content sanitization utilities for MCP content.

Experimental

Not part of the core stable Handler/DSL surface. Best-effort text sanitization only — not a full security sandbox.

Implemented

Deprecated (not MCP/ACP requirements)

  • :remove_metadata / remove_metadata/1 — EXIF stripping was never implemented; not required by MCP. Prefer app-level media pipelines if needed.
  • :compress_media — no-op stub; planned for removal in 2.0.0

Summary

Types

Sanitization operation

Functions

Escapes HTML entities in text content.

Normalizes Unicode text to NFC form.

Removes potentially dangerous metadata from content.

Sanitizes content by applying a list of sanitization operations.

Sanitizes file paths to prevent directory traversal.

Sanitizes text content specifically.

Removes script tags and JavaScript from HTML content.

Removes SQL injection attempts from text.

Types

sanitization_op()

@type sanitization_op() ::
  :html_escape
  | :strip_scripts
  | :normalize_unicode
  | :limit_size
  | :remove_metadata
  | :compress_media
  | {:custom, function()}
  | atom()

Sanitization operation

Functions

html_escape(text)

@spec html_escape(String.t()) :: String.t()

Escapes HTML entities in text content.

normalize_unicode(text)

@spec normalize_unicode(String.t()) :: String.t()

Normalizes Unicode text to NFC form.

Uses :unicode.characters_to_nfc_binary/1. This reduces some homograph confusion but is not a complete security control by itself.

remove_metadata(content)

This function is deprecated. Not implemented for EXIF; not an MCP requirement. Planned for removal in 2.0.0..

Removes potentially dangerous metadata from content.

Deprecated

Never implemented for real EXIF stripping. Not required by MCP/ACP. Returns content unchanged (or clears a :metadata key when present). Planned for removal in 2.0.0.

sanitize(content, operations)

Sanitizes content by applying a list of sanitization operations.

Examples

safe_content = Sanitizer.sanitize(content, [
  :html_escape,
  :strip_scripts,
  {:limit_size, 1_000_000}
])

sanitize_path(path)

@spec sanitize_path(String.t()) :: String.t()

Sanitizes file paths to prevent directory traversal.

sanitize_text(text, operations)

@spec sanitize_text(String.t(), [sanitization_op()]) :: String.t()

Sanitizes text content specifically.

strip_scripts(text)

@spec strip_scripts(String.t()) :: String.t()

Removes script tags and JavaScript from HTML content.

strip_sql_injection(text)

@spec strip_sql_injection(String.t()) :: String.t()

Removes SQL injection attempts from text.