Secure LLM Data Access Architecture: Enabling AI to Understand Data Without Touching It
This article presents a security architecture for enterprise AI data querying that separates natural language understanding from data execution. It explains why connecting large language models directly to databases creates structural risks such as prompt injection, privilege escalation, sensitive field leakage, and non-deterministic audit evidence. The core approach, Positive Data Control, moves access control from post-hoc auditing to pre-execution interception through six layers: access, intent, control, execution, post-processing, and audit. The article details mandatory policy checks, engineering defenses against injection and overreach, observability and audit loops, a phased rollout path, and common anti-patterns to avoid. Readers will learn how to keep probabilistic models out of permission decisions while preserving usability and governance.
1. The Starting Problem: A Semantic Gap, Not a Generation Gap
Enterprise data access needs have existed for a long time, but the way those needs are expressed and the way they are governed sit on different layers. On the governance side, everything is defined in terms of databases, tables, columns, row-level conditions, masking rules, and approval workflows. On the user side, requests sound like “show me the customer orders,” “analyze the salary distribution for a department,” or “export all customer information.” A semantic gap sits between natural language requests and structured permission models.
Historically, this gap was filled by people (data administrators) and processes (ticket-based approvals). After introducing large language models, many teams’ first instinct is to let the model connect directly to the database and fetch data, treating “model capability” as part of the permission system. This is the most common and most dangerous architectural mistake in enterprise AI data querying projects today.
The risk does not mainly come from “whether the SQL is written correctly.” It comes from four structural problems:
- Prompt injection: user input, document content, and conversation history can all carry instructions that steer the model away from the intended policy.
- Privilege escalation and semantic drift: a vague request gets “helpfully completed” into a broader query scope, and permissions are amplified during translation.
- Sensitive field leakage: once result sets enter the model context, they can be restated, summarized, or carried across sessions, creating secondary disclosure.
- Non-determinism: model output is probabilistic and cannot serve as “deterministic evidence” in a compliance audit.
The conclusion is direct: a probabilistic component must not carry permission responsibilities. Models are suited to understanding requirements, drafting statements, and explaining policies. Permission decisions, statement validation, execution control, and result handling must be performed by deterministic systems.
2. The Core Paradigm: Positive Data Control
The key point of this paradigm is to move data access from “post-hoc auditing” forward to “pre-execution interception.” An analogy helps: in the traditional model, users operate data systems directly, and governance relies on correct configuration and human discipline. Positive Data Control is more like fly-by-wire: the user only issues intent, and a control layer in between is responsible for interpretation, validation, limiting, and recording. Only after passing through it is an action allowed to reach the data layer.
This yields four non-negotiable design constraints:
- Users do not connect directly to the database;
- Models do not connect directly to the database;
- All requests must pass through the control layer;
- Policy decisions happen before execution, and result handling and auditing happen after the return.
These four constraints shift “trust” from a model attribute to a system attribute, which is the prerequisite for the entire architecture to hold.
3. A Layered Reference Architecture
A practical implementation typically contains six layers with strictly separated responsibilities:
- Access layer: identity authentication, session and tenant context binding, injecting attributes such as user ID, department, role, and data domain that cannot be tampered with by the frontend.
- Intent layer (model): normalizes natural language into a structured query intent, outputting a draft statement and a list of required fields without touching real data.
- Control layer (deterministic): policy engine, statement parsing and rewriting, injection and privilege-escalation detection, rate limiting and quotas, approval orchestration.
- Execution layer: runs with a restricted account on a read-only replica, enforcing row-level/column-level filtering and timeout circuit breaking.
- Post-processing layer: result masking, aggregation-floor protection, watermarking, and export control.
- Audit layer: end-to-end traceability, with requests, policy hits, statements, and result fingerprints unified into logging services and monitoring alerts.
Illustrative pseudocode for a policy decision:
def authorize(ctx, draft):# ctx comes from the server-side session and does not accept frontend overridesif not policy.allow_domain(ctx.user, draft.domain):return deny("DOMAIN_FORBIDDEN")cols = parse_columns(draft.sql)if any(policy.is_sensitive(c) for c in cols) \and not policy.has_clearance(ctx.user, cols):return deny("COLUMN_FORBIDDEN")sql = inject_row_filter(draft.sql, ctx.user) # enforce row-level permissionssql = enforce_aggregate_floor(sql, min_rows=10)if cost_estimate(sql) > ctx.quota:return deny("COST_EXCEEDED")return allow(sql, mask_plan=policy.mask_plan(cols))
The key point is that the model output is only a “draft pending review,” and if any step fails, the process terminates. There is no “best-effort execution.”
4. The Policy Checklist the Control Layer Must Cover
- Identity and attributes: department, role, data domain, classification level, and validity period, all taken from an authoritative identity source.
- Column-level visibility: sensitive columns are invisible by default and require explicit authorization bound to a purpose.
- Row-level filtering: filter conditions are automatically assembled by organization, region, or owner, and must not be generated by the model.
- Statement whitelist: only controlled query forms are allowed; data definition and write operations are prohibited; subqueries and function usage are restricted.
- Cost guardrails: quotas on scan volume, returned rows, concurrency, and frequency to prevent “fetch-the-whole-table” questions.
- Aggregation protection: small-sample suppression and coarse differential-risk screening to avoid inferring individuals through repeated questions.
- Output control: masking, truncation, export prohibition, watermarking, and restrictions on secondary distribution.
5. Engineering Defenses Against Prompt Injection and Privilege Escalation
The injection surface is not limited to user input; it also includes retrieved documents, table comments, and conversation history. Engineering recommendations:
- Separate content from instructions: retrieved fragments are always marked as data, using explicit delimiters and structured encapsulation, and are forbidden from carrying executable semantics.
- Whitelist intents: the model can only choose from a predefined set of intents rather than freely generating arbitrary statements.
- Dual-channel validation: parse-tree validation plus rule-engine validation; both must pass before release.
- Least-privilege execution account: read-only, limited to a defined set of databases and tables, with no export or table-creation permissions.
- Fail closed: validation exceptions, timeouts, and field-parsing failures are all treated as denials to avoid degraded pass-through.
6. Observability and the Audit Loop
Auditing must be able to answer four questions: who asked, under which policy, what was actually executed, and what was returned. It is recommended to use a unified request ID across the entire chain, record policy hits and denial reasons, store only fingerprints and scale rather than raw text for result sets, and feed abnormal patterns (frequent rewriting, repeated probing of sensitive columns, sudden cost spikes) into monitoring alerts and automatic rate limiting. Periodically regress policy effectiveness with red-team cases, and treat “whether the policy was bypassed” as a continuous testing item.
7. Rollout Path and Common Anti-Patterns
A recommended four-step progression: first build read-only AI data querying with column-level authorization; then add row-level filtering and aggregation protection; then integrate approval orchestration and export control; finally improve audit analysis and policy regression.
Anti-patterns to watch for include: treating the system prompt as a permission boundary; letting the model decide on its own whether something is sensitive; loosening the statement whitelist in pursuit of accuracy; placing masking at the presentation layer instead of the execution layer; and auditing without intercepting.
8. Conclusion
The value of large language models lies in understanding vague requirements and lowering the barrier to use, not in carrying trust decisions. By separating understanding from execution and letting deterministic systems hold the authority over “what is allowed,” enterprises can achieve both efficiency and controllability in knowledge base Q&A, AI data querying, data-fetching agents, and data governance scenarios. If building such a system on Baidu AI Cloud, the same recommendation applies: assemble it from general-purpose capabilities, using identity and permission services to provide authoritative attributes, logging services and monitoring alerts to carry the audit loop, and a container platform to host the isolated deployment of the control and execution layers, so that the security boundary becomes an inherent property of the architecture rather than an after-the-fact patch.
