SecretLeakGuardrail

org.llm4s.agent.guardrails.builtin.SecretLeakGuardrail
See theSecretLeakGuardrail companion object
@Experimental
class SecretLeakGuardrail(val secretTypes: Seq[SecretType], val onFail: GuardrailAction) extends InputGuardrail, OutputGuardrail

Detects and redacts secrets / credentials in LLM input and output.

Prevents two classes of leak:

  • Input leak: a user accidentally pastes an API key into a prompt that is then logged, cached, or forwarded to third-party LLM providers.
  • Output leak: the LLM echoes a secret back in its response (e.g. when asked to summarise a config file that contains credentials).

Detected credential types (defaults):

  • OpenAI API keys (sk-... / sk-proj-...)
  • Anthropic API keys (sk-ant-...)
  • Google API keys (AIza...)
  • Voyage API keys (pa-...)
  • Langfuse keys (pk-lf-... / sk-lf-...)
  • AWS Access Key IDs (AKIA...)
  • JWT tokens (eyJ...eyJ...sig)

Behaviour is controlled by GuardrailAction:

  • Block (default) – reject the text and return a Left error.
  • Fix – replace secrets with typed placeholders and continue (e.g. [REDACTED_OPENAI_KEY]).
  • Warn – allow the text through unchanged; the caller may inspect the Right and decide what to log.

Example usage:

// Block any input that contains a credential
new GuardrailMiddleware(input = Seq(SecretLeakGuardrail()), output = Nil)

// Mask secrets automatically and let the query proceed
new GuardrailMiddleware(input = Seq(SecretLeakGuardrail.masking), output = Nil)

// Also scrub LLM responses
new GuardrailMiddleware(input = Nil, output = Seq(SecretLeakGuardrail.masking))

// each on an agent: Agent.builder("assistant", client).withMiddleware(guardrails).build()

Value parameters

onFail

Action to take when a secret is detected (default: Block)

secretTypes

Secret types to detect (default: all common provider keys)

Attributes

Companion
object
Source
SecretLeakGuardrail.scala
Graph
Supertypes
trait Guardrail[String]
class Object
trait Matchable
class Any
Show all

Members list

Value members

Concrete methods

True if the text contains at least one detectable secret.

True if the text contains at least one detectable secret.

Attributes

Source
SecretLeakGuardrail.scala
def summarize(text: String): Map[String, Int]

Returns a map of secret-type name → count for the given text.

Returns a map of secret-type name → count for the given text.

Attributes

Source
SecretLeakGuardrail.scala
override def transform(input: String): String

Replace every detected secret with its type-specific placeholder.

Replace every detected secret with its type-specific placeholder.

e.g. sk-abc123... → [REDACTED_OPENAI_KEY]

This is called automatically when onFail == Fix. It can also be called directly for unconditional sanitisation regardless of the guardrail mode.

Attributes

Definition Classes
Source
SecretLeakGuardrail.scala
def validate(value: String): Result[String]

Validate the text for secrets.

Validate the text for secrets.

Returns:

  • Right(original) if no secrets found, or onFail == Warn
  • Right(masked) if onFail == Fix
  • Left(error) if onFail == Block and secrets are present

Attributes

Source
SecretLeakGuardrail.scala

Inherited methods

Compose this guardrail with another sequentially.

Compose this guardrail with another sequentially.

The second guardrail runs only if this one passes.

Value parameters

other

The guardrail to run after this one

Attributes

Returns

A composite guardrail that runs both in sequence

Inherited from:
Guardrail
Source
Guardrail.scala
@Experimental
def validateIn(value: String, context: UsageAccounting): Result[String]

Validates value inside an agent, as validate(value) does, with the context of the stage that runs it: an input guardrail's is the turn's admission (AdmissionContext), an output guardrail's the afterAgent hook's ModelContext. A guardrail that calls a model overrides this to call through context.accounted, so the call is counted in the thread's usage and on the run's terminal event even when the task fails (design §4.22); one that combines guardrails passes context on to each. GuardrailMiddleware and GuardrailReviewMiddleware call this form. A guardrail that needs more than accounting matches on the context it was given.

Validates value inside an agent, as validate(value) does, with the context of the stage that runs it: an input guardrail's is the turn's admission (AdmissionContext), an output guardrail's the afterAgent hook's ModelContext. A guardrail that calls a model overrides this to call through context.accounted, so the call is counted in the thread's usage and on the run's terminal event even when the task fails (design §4.22); one that combines guardrails passes context on to each. GuardrailMiddleware and GuardrailReviewMiddleware call this form. A guardrail that needs more than accounting matches on the context it was given.

Named apart from the Stable validate(value) so that MiMa, which skips an @Experimental method with every overload of its name, still checks that one (#2835).

Attributes

Inherited from:
Guardrail
Source
Guardrail.scala

Concrete fields

override val description: Option[String]

Optional description of what this guardrail validates.

Optional description of what this guardrail validates.

Attributes

Source
SecretLeakGuardrail.scala
val name: String

Name of this guardrail for logging and error messages.

Name of this guardrail for logging and error messages.

Attributes

Source
SecretLeakGuardrail.scala