Hostname: page-component-77f85d65b8-jkvpf Total loading time: 0 Render date: 2026-04-18T08:24:39.026Z Has data issue: false hasContentIssue false

Enhancing security in text-to-SQL systems: A novel dataset and agent-based framework

Published online by Cambridge University Press:  15 August 2025

Salmane Chafik*
Affiliation:
College of Computing, Mohammed VI Polytechnic University, Ben Guerir, Morocco
Saad Ezzini
Affiliation:
Information and Computer Science Department and Interdisciplinary Research Center for Intelligent Manufacturing and Robotics, King Fahd University of Petroleum and Minerals, Dhahran, Saudi Arabia
Ismail Berrada
Affiliation:
College of Computing, Mohammed VI Polytechnic University, Ben Guerir, Morocco
*
Corresponding author: Salmane Chafik; Email: chafik.salmane@um6p.ma
Rights & Permissions [Opens in a new window]

Abstract

This paper explores the significant advancements in generating Structured Query Language (SQL) from natural language, primarily driven by Large Language Models (LLMs). These advancements have led to the development of sophisticated text-to-SQL integrated applications, enabling easier database (DB) querying for users unfamiliar with SQL syntax using natural language queries. However, reliance on LLMs exposes these applications to potential attacks through the introduction of malicious prompts or by compromising models with malicious data during the training phase. Such attacks pose severe risks, including unauthorized data access or even complete DB destruction upon success. To address these concerns, we introduce a novel large-scale dataset comprising malicious and safe prompts along with their corresponding SQL queries, enabling model fine-tuning on malicious query detection tasks. Moreover, we propose the implementation of two transformer-based classification solutions to aid in the detection of malicious attacks. Finally, we present a secure agent-based text-to-SQL architecture that incorporates these solutions to enhance overall system security, resulting in a 70% security enhancement overall compared to solely relying on a conventional text-to-SQL model.

Information

Type
Article
Creative Commons
Creative Common License - CCCreative Common License - BYCreative Common License - NCCreative Common License - ND
This is an Open Access article, distributed under the terms of the Creative Commons Attribution-NonCommercial-NoDerivatives licence (https://creativecommons.org/licenses/by-nc-nd/4.0/), which permits non-commercial re-use, distribution, and reproduction in any medium, provided that no alterations are made and the original article is properly cited. The written permission of Cambridge University Press must be obtained prior to any commercial use and/or adaptation of the article.
Copyright
© The Author(s), 2025. Published by Cambridge University Press
Figure 0

Figure 1. Text-to-SQL integrated systems general architecture.

Figure 1

Listing 1. Restricted Prompt.

Figure 2

Table 1. Prompt Injection Variants.

Figure 3

Table 2. Malicious patterns

Figure 4

Algorithm 1. Transform_NLQ_SQL_Pair

Figure 5

Table 3. SQL queries templates

Figure 6

Figure 2. From data construction to models finetuning.

Figure 7

Figure 3. Our secure agent-based text-to-SQL system.

Figure 8

Listing 2. Agent run prompt.

Figure 9

Listing 3. GPT completion.

Figure 10

Listing 4. Code completion models prompt.

Figure 11

Table 4. Malicious test set inference results

Figure 12

Table 5. Our models performance on the validation and test sets of SQLShield

Figure 13

Table 6. The running time for each part of our system on 100 examples