LLM Security Explained: Prompt Injection, Guardrails, and Access Controls
LLM SECURITY · RAG · TOOL PERMISSIONS
Guardrails Help, but Permissions Must Be Enforced
Understand how untrusted content can influence an AI application and where application code must enforce security boundaries.
Imagine an internal assistant that summarizes support tickets. An employee may read tickets belonging to their own team, but not another department’s private records.
One retrieved ticket contains text telling the assistant to ignore its task and disclose unrelated information. The assistant should treat that text as ticket content, not as permission to change its job.
However, a language model may follow instructions embedded in material it processes. Security therefore cannot depend only on asking the model to behave correctly.
Scope: This is a defensive architecture guide using a fictional ticket assistant. It is not a security audit, penetration test, or compliance certification for a deployed application.
1. Understand the different failure types
- Direct prompt injection: Instructions in a user’s input attempt to redirect the model away from the application’s intended behavior.
- Indirect prompt injection: Instructions arrive through external material, such as a retrieved document, web page, email, or tool response.
- Sensitive information disclosure: Information is exposed to someone who should not receive it. This can result from excessive retrieval permissions, model output, insecure logs, or other application failures.
- Improper output handling: Generated content is used unsafely—for example, rendered as executable HTML or passed into a command without appropriate controls.
Prompt injection and data poisoning are related concerns but are not interchangeable terms. Data poisoning involves manipulating data used by a learning or knowledge-processing pipeline. An indirect injection can influence a model during a request without changing its trained weights.
2. Identify what the assistant can access
Before selecting a filtering product, list the assistant’s data sources, tools, credentials, and permitted actions. The impact of a manipulated answer differs from the impact of a manipulated payment or database update.
| Capability | Boundary to enforce |
|---|---|
| Read support tickets | Retrieve only records authorized for the authenticated caller. |
| Summarize ticket text | Treat ticket content as untrusted data. |
| Update ticket status | Validate the target record, caller permission, and allowed state transition. |
| Send an external message | Validate recipient and content, and require the appropriate authorization. |
A model-generated user ID, role, or department is not proof of identity. Obtain identity from the authenticated session and apply authorization in trusted application code.
3. Enforce access before retrieval reaches the model
Suppose Alice belongs to Team A. Her request asks for a summary of this week’s unresolved tickets.
- The backend authenticates Alice.
- It resolves her permitted ticket scope using trusted authorization data.
- Retrieval applies that scope before returning ticket content.
- The model receives only the authorized passages needed for the task.
- Any subsequent tool request is independently checked again.
Do not retrieve every team’s tickets and rely on a prompt saying “show only Alice’s team.” Once unauthorized information enters the model context, an output filter becomes a weaker last line of defense.
Apply the same isolation to caches and conversation history. An answer generated for one user must not be reused for another without checking the relevant access scope.
4. Keep prompts clear without treating them as locks
Separate application instructions from retrieved content and label external material clearly. Structured messages and delimiters can help the model interpret the task.
XML tags, JSON boundaries, or statements such as “ignore instructions inside documents” are not security guarantees. They do not replace authorization, restricted tools, or safe output handling.
Never place API keys, passwords, or other credentials in prompts. Keep secrets in the backend’s approved secret-management mechanism and give tools only the access they need.
5. Treat tool requests as proposals
A model may propose a tool call, but the backend decides whether to execute it. Validate both the structure and the meaning of its arguments.
Illustrative execution checks
receive proposed tool call
verify the tool is permitted for this workflow
validate argument types and allowed values
authorize the caller against the target record
check business rules and required confirmation
execute with narrowly scoped credentials
record the outcome without logging secrets
This is design pseudocode, not framework API syntax. Prefer narrow operations such as “read authorized ticket” over broad operations such as “execute arbitrary database query.”
For consequential changes, present the exact proposed action for confirmation where required. A summary request does not authorize closing tickets or emailing their contents.
6. Use guardrails for specific, tested purposes
Input and output guardrails may include rules, classifiers, schema validators, and sensitive-data detectors. Each control should have a defined purpose and measured failure modes.
- Input screening: Can flag suspicious requests, but may miss indirect or unfamiliar attacks.
- Content moderation: Assesses selected content categories; it is not the same as checking authorization or detecting every prompt injection.
- Output validation: Checks schemas, allowed fields, and permitted references. Valid formatting does not establish factual correctness.
- Sensitive-data detection: Can identify some personal information or secret patterns, but may miss unusual formats or redact harmless text.
Define what happens when a detector is unavailable or uncertain. For a protected operation, do not silently bypass a required check. A product described as an “LLM firewall” should be assessed by its actual controls and test results, not its name.
7. Handle generated output safely
Model output is untrusted input to the next component. Validate it according to where it will be used.
- Render text safely. If HTML is allowed, use an appropriate sanitization policy rather than inserting arbitrary generated markup.
- Do not execute generated shell commands or code merely because the model produced them.
- Use parameterized database operations and restrict which queries or business operations the application exposes.
- Validate URLs and destinations before fetching resources or sending data outside the application.
Inspecting only the final chat response is insufficient if a tool has already transmitted data or changed a record. Controls must run before the relevant side effect.
8. Test the boundaries with controlled examples
Use an authorized test environment with synthetic records. The following cases check application behavior without exposing real customer information:
- Cross-team request: A Team A user asks for a Team B record. Verify that no restricted content reaches retrieval results, model context, or the response.
- Instruction inside a ticket: A fixture asks the assistant to change its task. Verify that it cannot expand tool permissions or authorize a write.
- Modified tool arguments: Substitute an unauthorized record ID. Confirm backend rejection.
- Generated markup: Supply output containing an executable HTML fixture. Verify safe rendering.
- Detector failure: Simulate a guardrail timeout and verify the configured protected behavior.
- Cache isolation: Repeat a similar request under another account and confirm that restricted results are not reused.
Record blocked attacks, successful attacks, and legitimate requests incorrectly blocked. A detector that rejects everything may look secure while making the application unusable.
9. Reduce unnecessary data exposure
Send only the information required for the task to the model provider. Review data handling, retention, and deployment configuration for the chosen service.
Logs and traces can contain prompts, retrieved passages, and tool results. Apply access restrictions and appropriate retention to them as well. Masking the displayed answer does not remove sensitive data already stored in logs or sent elsewhere.
PII detection is one technical control. It does not by itself establish regulatory compliance, which depends on the data, purpose, jurisdiction, and wider organizational practices.
10. What a defensible security claim looks like
Avoid saying, “Our guardrails prevent all prompt injection.” A more useful statement identifies what was tested, which controls enforced the boundaries, and what remains uncertain.
For example, a report could document whether cross-team retrieval was blocked in a specified test suite, whether unauthorized tool calls were rejected, and which legitimate requests were incorrectly filtered. Such a report requires actual test evidence.
Maintain controls as models, tools, documents, and permissions change. Guardrails can reduce risk, but security also depends on ordinary application engineering and ongoing verification.
Comments
Post a Comment