SecretPatterns

org.llm4s.agent.guardrails.patterns.SecretPatterns
@Stable

Library of credential-detection patterns used by SecretLeakGuardrail.

All matching is performed via SecretType instances. Each type knows its own pattern, human-readable name, and the placeholder text to use when redacting. The built-in types share their patterns with the redaction llm4s applies to its own logs and error messages, and each takes time linear in the text.

Attributes

Source
SecretPatterns.scala
Graph
Supertypes
class Object
trait Matchable
class Any
Self type

Members list

Type members

Classlikes

final case class SecretMatch

A single match of a secret inside a larger string.

A single match of a secret inside a larger string.

Value parameters

endIndex

Exclusive end position in the original string.

secretType

The SecretType that produced this match.

startIndex

Inclusive start position in the original string.

value

The matched secret text (un-redacted).

Attributes

Companion
object
Source
SecretPatterns.scala
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
object SecretMatch

Attributes

Companion
class
Source
SecretPatterns.scala
Supertypes
trait Product
trait Mirror
class Object
trait Matchable
class Any
Self type
class SecretType(val name: String, val pattern: Regex, val placeholder: String)

A single category of secret credential.

A single category of secret credential.

The built-in types are in the companion. The class is open: an application adds its own credential format with new SecretType("Acme token", """acme_[A-Za-z0-9]{32}""".r, "[REDACTED_ACME_TOKEN]") and passes it to SecretLeakGuardrail beside the built-in ones. A pattern runs on text a model or a user supplies, so write one that takes linear time: no nested quantifiers, and no scan to the end of a run from every position in it.

Value parameters

name

human-readable label used in error messages

pattern

the regex that recognises this credential. When it has a capturing group, group 1 is the secret and the rest of the match is context that is kept; otherwise the whole match is

placeholder

what to write in place of the secret when masking

Attributes

Companion
object
Source
SecretPatterns.scala
Supertypes
class Object
trait Matchable
class Any
Known subtypes
object AnthropicKey
object AwsAccessKey
object GoogleApiKey
object JwtToken
object LangfuseKey
object OpenAIKey
object PrivateKeyPem
object VoyageKey
Show all
object SecretType

Attributes

Companion
class
Source
SecretPatterns.scala
Supertypes
class Object
trait Matchable
class Any
Self type
SecretType.type

Value members

Concrete methods

def containsSecret(text: String, types: Seq[SecretType]): Boolean

Returns true if text contains at least one secret from types.

Returns true if text contains at least one secret from types.

Attributes

Source
SecretPatterns.scala
def detect(text: String, types: Seq[SecretType]): Seq[SecretMatch]

Find every secret in text using the supplied types (defaults to SecretType.default).

Find every secret in text using the supplied types (defaults to SecretType.default).

Attributes

Source
SecretPatterns.scala
def redactAll(text: String, types: Seq[SecretType]): String

Replace each matched secret with its type-specific placeholder (e.g. sk-abc... → [REDACTED_OPENAI_KEY]).

Replace each matched secret with its type-specific placeholder (e.g. sk-abc... → [REDACTED_OPENAI_KEY]).

The types are applied left to right, each to the text the ones before it left, so where two types would match the same text the earlier one in types redacts it.

Attributes

Source
SecretPatterns.scala
def summarize(text: String, types: Seq[SecretType]): Map[String, Int]

Count occurrences of each credential type in text.

Count occurrences of each credential type in text.

Attributes

Returns

A map from SecretType.name to the number of matches found. Types with zero matches are omitted.

Source
SecretPatterns.scala