PortGate

Guides · AI · 3 min

Preventing Indirect Prompt Injection & API Key Leaks in LLM Apps

Treat user text as data. A local proxy can block known instruction overrides and redact secrets before a prompt leaves the machine.

A prompt injection is user-controlled text that tries to override the instructions you sent to a model. The model cannot reliably tell your system prompt from a sentence hidden in a ticket, a web page, or a retrieved document. If the model can call tools, read files, or see production secrets, that sentence is an attack.

Keep instructions and data apart

Put your rules in the system or developer role. Put untrusted text in a user message, or in a clearly delimited document block, and tell the model that the block is data. This does not stop a determined injection. It stops the accidental kind, where an app concatenates a customer message directly onto the system prompt.

Do not give the model a tool that can read environment files, shell out, or send mail unless a person confirms the call. A blocked prompt is useless if the same process can already cat /etc/passwd.

Filter on the way out, not in the model

PortGate's agent runs a reverse proxy on 127.0.0.1:8787. Point the OpenAI-compatible client at it:

export OPENAI_BASE_URL=http://127.0.0.1:8787/v1

The proxy inspects content, text, system, prompt, and input fields. It blocks a request with HTTP 400 when the text matches an instruction-override pattern, including:

  • telling the model to ignore previous instructions
  • asking it to reveal the system prompt
  • claiming a jailbreak or "do anything now" mode
  • asking it to bypass its safety rules

The proxy returns HTTP 400 and does not forward that request. Blocking is a denylist of known phrases. It is not a guarantee that a novel sentence will fail. Keep the model away from privileged tools anyway.

Redact secrets before they are logged or sent

On requests that are not blocked, the same proxy replaces:

  • sk- API keys
  • AWS access key IDs that start with AKIA
  • US social security numbers
  • card numbers that pass the Luhn check

Replacements look like [REDACTED_SECRET]. The original value is not written to the telemetry database. Telemetry sent to PortGate is a count of blocked prompts and redactions, not the prompt text.

You can try the same rules without an account on the prompt injection tester. Paste a sample. The page runs the rules locally in the app and shows the blocked labels and the redacted text.

What to watch

Alert when the blocked-prompt counter jumps, when a tool call targets the filesystem, or when a response contains a secret that should have been redacted. Rotate any key that already appeared in a prompt, a ticket, or a model log. Redaction on the next request does not undo a key that left the machine yesterday.