ExMCP.Server.Tools.ASTValidator (ex_mcp v0.9.2)

View Source

Validates AST nodes to ensure they are safe for evaluation.

This module provides security validation for AST nodes that will be evaluated using Code.eval_quoted, preventing code injection attacks.

Summary

Functions

Validates that an AST node contains only safe constructs.

Functions

validate_schema_ast(ast)

@spec validate_schema_ast(any()) :: {:ok, any()} | {:error, String.t()}

Validates that an AST node contains only safe constructs.

Returns {:ok, ast} if the AST is safe, or {:error, reason} if unsafe constructs are detected.

Examples

iex> ASTValidator.validate_schema_ast({:%{}, [], [type: "string"]})
{:ok, {:%{}, [], [type: "string"]}}

iex> ASTValidator.validate_schema_ast({:eval, [], ["dangerous code"]})
{:error, "Unsafe AST node: eval"}