Skip to content

API Reference parrot.security.query_validator

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.security.query_validator

View source

Query safety validator — shared across ai-parrot and parrot-tools.

Provides QueryLanguage (the supported query dialects) and QueryValidator (a static, dependency-free safety check for SQL, Flux, and Elasticsearch JSON DSL).

This module lives in parrot.security so both the DatabaseQueryTool (in parrot-tools) and the DatabaseToolkit (in ai-parrot) can reuse it without creating a circular dependency between packages.

Class QueryLanguage

Supported query languages.

Class QueryValidator

Validates queries based on query language.

Method QueryValidator.validate_sql_query

Validate SQL query for safety.

validate_sql_query(query: str) -> Dict[str, Any]

Parameter Type Default Description
query str

Returns: Dict[str, Any]

Method QueryValidator.validate_flux_query

Validate InfluxDB Flux query for safety.

validate_flux_query(query: str) -> Dict[str, Any]

Parameter Type Default Description
query str

Returns: Dict[str, Any]

Method QueryValidator.validate_elasticsearch_query

Validate Elasticsearch query (JSON DSL format).

validate_elasticsearch_query(query: str) -> Dict[str, Any]

Parameter Type Default Description
query str

Returns: Dict[str, Any]

Method QueryValidator.validate_query

Validate query based on its language.

validate_query(query: str, query_language: QueryLanguage) -> Dict[str, Any]

Parameter Type Default Description
query str
query_language QueryLanguage

Returns: Dict[str, Any]

Method QueryValidator.validate_sql_ast

sqlglot-backed SQL safety validator.

Parses query into an AST and enforces the configured policy:

  • Rejects unparseable SQL and multi-statement queries.
  • DDL (Create/Drop/Alter/TruncateTable/Grant/ Revoke) is always rejected.
  • read_only=True — allows only Select/Union/Intersect/ Except/With/Describe/Pragma and the read-only Command keywords EXPLAIN/SHOW/DESCRIBE/DESC.
  • read_only=False — DML is permitted, but Update/Delete must carry a WHERE clause.
  • When require_pk_in_where=True, Update/Delete WHERE clauses must reference at least one column from primary_keys. This prevents accidental full-table updates/deletes. When require_pk_in_where=True but primary_keys is empty or None, the check is treated as a misconfiguration and the query is rejected.

Falls back to the regex-based :meth:validate_sql_query when sqlglot is not installed.

validate_sql_ast(query: str, dialect: Optional[str] = None, read_only: bool = True, require_pk_in_where: bool = False, primary_keys: Optional[List[str]] = None) -> Dict[str, Any]

Parameter Type Default Description
query str Raw SQL string.
dialect Optional[str] None Optional sqlglot dialect name (e.g. "postgres", "bigquery", "mysql"). None uses sqlglot default.
read_only bool True Whether to reject any write operation.
require_pk_in_where bool False When True, UPDATE/DELETE WHERE clauses must reference at least one column from primary_keys. Defaults to False for backward compatibility.
primary_keys Optional[List[str]] None List of primary key column names used with require_pk_in_where=True. Must be non-empty when require_pk_in_where is True.

Returns: Dict[str, Any] — Dict with is_safe (bool), message (str) and suggestions (list[str]) keys — same shape as the regex validator for drop-in interchangeability.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally