detection

package
v1.10.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package detection holds the deterministic prompt-injection detectors that instincts run: a pattern corpus grouped into families, heuristics for hidden and encoded text, and canary tokens.

Every detector takes a folded Text, matches its Folded form and reports matches as original byte offsets widened to whole lines, so a caller can redact or quote exactly the lines that carried the attack. A result's score is the noisy-or of its distinct families, each at its highest weight.

Index

Constants

View Source
const DefaultCustomWeight = 0.8

DefaultCustomWeight is the weight of a custom pattern when the caller gives none.

Variables

This section is empty.

Functions

func DestructiveCommands

func DestructiveCommands() []string

DestructiveCommands returns the regexp sources of the destructive shell commands the detectors know, written for folded text (see Fold). The values safety principle counts them.

Types

type Family

type Family string

Family names a group of related detections.

const (
	// FamilyOverride is an instruction to ignore earlier instructions.
	FamilyOverride Family = "override"
	// FamilyRole is an attempt to reassign the model's role.
	FamilyRole Family = "role"
	// FamilyImpersonation is text posing as a system or assistant turn.
	FamilyImpersonation Family = "impersonation"
	// FamilyAgentDirective is text addressed to an AI agent, or telling one
	// to approve, merge, deploy or call a tool.
	FamilyAgentDirective Family = "agent_directive"
	// FamilyExfiltration asks for prompts, secrets or a data upload.
	FamilyExfiltration Family = "exfiltration"
	// FamilyJailbreak is a known jailbreak framing.
	FamilyJailbreak Family = "jailbreak"
	// FamilyManipulation claims approval or pushes past a safety step.
	FamilyManipulation Family = "manipulation"
	// FamilyHiddenText is a line carrying zero-width or bidi control runes.
	FamilyHiddenText Family = "hidden_text"
	// FamilyEncoded is a base64 run that decodes to a pattern match.
	FamilyEncoded Family = "encoded"
	// FamilyCanary is a canary token found in the text.
	FamilyCanary Family = "canary"
	// FamilyCustom is a match of a caller-supplied pattern.
	FamilyCustom Family = "custom"
)

Detection families.

func Families

func Families(cat instinct.Category) []Family

Families returns the pattern families a category runs, or nil for an unknown category.

type Match

type Match struct {
	Family Family  `json:"family"`
	Weight float64 `json:"weight"`
	Start  int     `json:"start"`
	End    int     `json:"end"`
}

Match is one detection. Start and End are original byte offsets covering the whole lines the match touched, without their trailing newlines.

type Result

type Result struct {
	Score   float64 `json:"score"`
	Matches []Match `json:"matches,omitempty"`
}

Result is the outcome of one detector. Score is in [0, 1]; zero means no match.

func Canary

func Canary(t Text, token string) Result

Canary reports every line of t that holds token, compared in folded form, so case changes and hidden runes do not hide it. An empty token never matches.

func Heuristics

func Heuristics(t Text) Result

Heuristics runs the checks that are not phrase patterns:

  • hidden_text: each line holding an invisible rune that Fold removed and counts as hiding something (see Text.Hidden).
  • encoded: each line holding a base64 run of 40 or more characters that decodes to mostly printable text matching any corpus family.

func Patterns

func Patterns(t Text, cat instinct.Category, extra []*regexp.Regexp, extraWeight float64) Result

Patterns runs the corpus families of cat over t, plus every extra regex as family custom at extraWeight (DefaultCustomWeight when zero or negative, capped at 1). Extra regexes run on Folded, which is lower case.

type Text

type Text = textnorm.Text

Text is folded text with a map back to the original. Build it with Fold.

func Fold

func Fold(s string) Text

Fold builds the Text the detectors take: NFKC, lower case, zero-width and bidi controls removed, whitespace runs collapsed to one space.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL