Concepts / Prompt Hacks and Defenses

Prompt Hacks and Defenses

Input guardrails protect against two risks: leaking private information to external APIs and executing bad prompts that compromise the system.

  • Programming

A Prompt on Its Way Out

Imagine an employee pastes a customer record into a chat box that is connected to an external model API. The record includes a phone number and a bank account number. Once the request is sent, whatever the prompt contains is transmitted to that outside service. The employee did not intend to share those values with anyone outside the organization, but the request has already left the building.

Input guardrails are the mechanisms designed to stop this kind of outcome. They protect an application against the risks of using external model APIs. They sit on the way into the model, examining what is about to be sent and deciding what to do with it. This article follows that path: the two risks guardrails address, how sensitive data is found in a query, how it is masked and later restored, and where the limits of these protections lie.

What do you think happens?

If a query containing a phone number is masked with a placeholder before it is sent, does the external API ever receive the phone number?

  • Yes, the phone number is sent but encrypted

  • No, the placeholder is sent in place of the phone number

Reveal answer
Answer:

No, the placeholder is sent in place of the phone number

Masking replaces the sensitive value with a placeholder before the query leaves the organization. The external service only sees the placeholder. The original value is restored later, inside the organization, using the reverse dictionary.

Two Risks Input Guardrails Target

Input guardrails protect against two distinct risks. The first is leaking private information to external APIs. The second is executing bad prompts that compromise the system. Both risks start with the prompt, but they point in different directions. In the first, the danger is what the prompt reveals to the outside world. In the second, the danger is what the prompt makes the application do.

The first risk concerns data leaving your organization. When you send a prompt to an external model API, whatever the prompt contains is transmitted to that outside service. The second risk concerns the instructions themselves. A prompt can be crafted to manipulate the application into behaving in ways its designers did not intend. The source notes that attackers can exploit applications through prompt hacks, and that defending against those attacks is covered in earlier material.

Private data leavesSent to an external modelAPIApplicationmisbehavesPrompt crafted tomanipulate behaviorWhere it startsPasted text, system prompt,database toolWhere it startsAttackers exploiting prompthacksDetection and maskingGuardrail at the outboundboundaryPrompt defensesCovered in earlier materialtraced toaddressed bytraced toaddressed by
How does the risk of private data leaving the organization differ from the risk of a bad prompt executing, and which part of the guardrail layer addresses each one?

This article focuses on the first risk. The source develops the input guardrail techniques around private information, so the masking and detection steps that follow all address leakage to external services.

Three Ways Private Data Gets Out

Leaking private information to an external API is specific to using external model APIs, because it happens when your data has to be sent outside your organization. The source gives three common ways this occurs.

  • An employee pastes private data into a prompt
  • Internal policies are placed in a system prompt
  • A tool adds private database content to the context

Notice that the leak does not always come from a malicious actor. An ordinary employee can paste the wrong text, a developer can write internal policies into a system prompt for convenience, and a retrieval tool can pull in records that were never meant to leave the building. Because each of these paths ends in the same place, a request to an external service, a guardrail placed at that boundary can cover all three.

Finding Sensitive Data Before It Leaves

The source states that there is no airtight way to eliminate potential leaks when using third-party APIs. What you can do is mitigate them. One approach is to use one of the many available tools that automatically detect sensitive data. Sensitive data detection tools, often AI-based, find values such as ID numbers, phone numbers, bank accounts, and company intellectual property terms that you define.

The detection rules are not fixed by the tool. You decide what counts as sensitive for your application. A tool that finds phone numbers by default is still missing anything your organization cares about unless you add it to the list. For example, a product code name that your company treats as confidential would only be caught if you define it as a term to watch for.

User queryPrompt about to be sentSend unchangedNo flagged value in thequeryQuery not sentEntire query blockedDetection toolChecks against your definedtermsBlock or remove?Two options described inthe sourceMasked query sentValue replaced by aplaceholderSensitive valuefound?ID number, phone, bankaccount, IP termscannedresultsnoyesblockmask
Which parts of a query are flagged as sensitive, and which decision determines whether the query is blocked, masked, or sent unchanged?

The source describes two options once sensitive information is found in a query. You can block the entire query, or you can remove the sensitive information from it. The excerpt does not give a fixed rule for choosing between the two, so the choice is a design decision for the application. It is important not to assume the detection step decides this automatically.

Blocking Versus Masking a Flagged Query

Blocking is the simpler choice. The request is not sent at all, so nothing private reaches the external service. Its drawback is that the user gets no answer from the model for that request. Removing the information keeps the query useful, since the rest of the question still reaches the model. Masking is the usual way to do this removal.

Tracing a Value Through Masking and Unmasking

Masking alone solves the outbound problem, but the application often still needs the real value when the answer comes back. The source handles this with a PII reverse dictionary. The dictionary maps each placeholder to the original information. If the generated response contains a placeholder, the application uses the dictionary to replace it with the original value. That replacement is called unmasking.

User promptContains the original valueExternal model APIReceives only theplaceholderResponse withplaceholderReturned from the externalserviceMasking stepOriginal value swapped fora placeholderUnmasking stepPlaceholder replaced usingthe mapRestored answerOriginal value back in theanswerPII reversedictionaryPlaceholder mapped tooriginal valuedetected valuerecords pairmasked queryanswerplaceholder foundlookupreplaced
How does a private value move from the user's prompt to the external API as a placeholder, and then get restored in the response without the original value leaving the organization?
Worked example

Masking and unmasking one phone number

A user asks the application to draft a reminder text for a customer whose phone number is 555-0100. The application has defined phone numbers as sensitive. Trace what the external API receives and what the user finally sees.

  1. 1
    Detect

    The detection tool finds the phone number in the query because phone numbers are a value type the application has marked as sensitive.

  2. 2
    Mask

    The application replaces the phone number with a placeholder such as PHONE_1, and stores the pair PHONE_1 maps to 555-0100 in the PII reverse dictionary.

  3. 3
    Send

    The external API receives the reminder request with PHONE_1 in place of the number. The real number never leaves the organization.

  4. 4
    Receive

    The model returns a draft that says: Please call PHONE_1 to confirm your appointment.

  5. 5
    Unmask

    The application finds PHONE_1 in the response, looks it up in the reverse dictionary, and replaces it with 555-0100 before showing the draft to the user.

Result

The external API only ever saw PHONE_1. The user sees the full reminder with 555-0100 restored, and the original number stayed inside the organization.

python
Output (expected)
Remind PHONE_1 about the appointment
Please call 555-0100 to confirm.
Notes on the code
  • mask() records each sensitive value in the reverse dictionary before the query is sent
  • Only the masked string is passed to the external API call
  • unmask() looks up each placeholder in the same dictionary to restore the original value

Mistakes With the Reverse Dictionary

  • Common mistake

    Assuming masking alone keeps the value out of the whole pipeline

    What it looks like

    The placeholder is sent, but the reverse dictionary is written to a log that is shipped to a third party.

    Why it fails

    The source's protection depends on the original values staying inside the organization. The dictionary holds those values, so it must be protected too.

    Do this instead

    Treat the reverse dictionary as sensitive data and keep it inside the organization's boundary.

  • Common mistake

    Expecting the detection tool to know what your company considers confidential

    What it looks like

    An internal project name is not flagged because it was never added to the list of terms to watch for.

    Why it fails

    The detection rules are not fixed by the tool. You decide what counts as sensitive for your application.

    Do this instead

    Define the sensitive value types and company terms explicitly before relying on detection.

  • Common mistake

    Treating blocking and masking as interchangeable

    What it looks like

    A query is masked when the user actually needed the complete request answered, but the placeholder makes the answer unusable.

    Why it fails

    Blocking sends nothing, while masking keeps the question useful. Each has a different outcome for the user.

    Do this instead

    Decide per application which outcome is acceptable, and make that choice deliberately.

  • Common mistake

    Forgetting to unmask the response before showing it to the user

Why Guardrails Reduce Risk But Cannot Eliminate It

The source states that there is no airtight way to eliminate potential leaks when using third-party APIs. Input guardrails lower the chance of leakage by catching sensitive values at the outbound boundary, but several residual risks remain. Detection depends on the rules you defined, so a value you did not anticipate may pass through. The detection tools are often AI-based, and the source describes them as finding values, which means their results are not guaranteed to be complete.

The guardrail layer also works only on the path into the model. It examines what is about to be sent. It does not by itself stop a harmful prompt that has been crafted to manipulate the application, since the source places defense against prompt hacks in earlier material. The layers of defense therefore complement one another: input guardrails handle outbound data, and separate defenses handle the instructions themselves. No single layer covers both risks completely.

Practice Tracing a Masked Query

MEDIUM

A query contains an employee ID number and a company project term, and your application has defined both as sensitive. Describe, step by step, what the external API receives, what the reverse dictionary contains, and how the final response is produced. Then explain what would happen to the project term if it had not been added to the detection list.

Hints
  • Start with the detection step and list which values are flagged
  • Ask which string the external API is able to see after masking
  • Think about whether an unflagged term would be masked at all

Key Takeaways

  1. 1
    Input guardrails address two risks: leaking private information to external APIs and executing bad prompts that compromise the system.
  2. 2
    Sensitive data detection finds values such as ID numbers, phone numbers, bank accounts, and company terms that you define.
  3. 3
    A flagged query can be blocked entirely or masked, and masking replaces the sensitive value with a placeholder.
  4. 4
    The PII reverse dictionary maps placeholders back to original values, so the response can be unmasked inside the organization.
  5. 5
    Guardrails reduce leakage risk but cannot eliminate it, so they work best as one layer among several defenses.

At a glance

  • Input guardrails address two risks: leaking private information to external APIs and executing bad prompts that compromise the system.
  • Sensitive data detection finds values such as ID numbers, phone numbers, bank accounts, and company terms that you define.
  • A flagged query can be blocked entirely or masked, and masking replaces the sensitive value with a placeholder.
  • The PII reverse dictionary maps placeholders back to original values so the response can be unmasked inside the organization.
  • Guardrails reduce leakage risk but cannot eliminate it, so they work best as one layer among several defenses.

More concepts starting with P