DEV Community

Discussion on: Natural-Language SQL on Atlassian Forge: A Secure Pattern with Rovo (LLM) + Forge SQL (TiDB)

Collapse
 
vzakharchenko profile image
Vasiliy Zakharchenko • Edited

just a quick follow-up. I've slightly improved the "Guard" pattern by adding three independent validation layers to ensure the query truly touches only the intended table:

  • 1. Fast Pre-Check (Static AST Analysis): Before doing anything, the query is parsed via node-sql-parser, ensuring it is a single SQL statement, strictly a SELECT, references only one table (security_notes), and contains no scalar subqueries inside SELECT columns.
  • 2. EXPLAIN-based Table Verification: Before running the query, I check the EXPLAIN plan. I verify that every accessObject is exactly table:security_notes. This blocks any hidden joins, subqueries, or optimizer rewrites that might try to access other tables.
  • 3. Post-Execution Metadata Check: After the query runs, I use the metadata Forge SQL provides. I validate that all returned fields that have an origin must come from orgTable === “security_notes”. This is the final guarantee against any cross-table data leakage.

The best part is that all aggregate functions (like COUNT, SUM, AVG, etc.) still work perfectly, as long as they operate on this single, validated table.