untrusted

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package untrusted prepares attacker-influenced text such as logs, environment values, and commit messages for an LLM prompt: it strips terminal and invisible characters, redacts obvious secrets, truncates, and wraps the result in a delimited block marked as data. This reduces prompt-injection risk but is not a guarantee.

Index

Examples

Constants

View Source
const (
	// EnvMaxFieldBytes caps one string field after sanitizing.
	EnvMaxFieldBytes = "APP_UNTRUSTED_MAX_FIELD_BYTES"
	// EnvMaxBlockBytes caps one whole wrapped block.
	EnvMaxBlockBytes = "APP_UNTRUSTED_MAX_BLOCK_BYTES"

	// Preamble opens every wrapped block and is how IsWrapped recognises one.
	Preamble = "" /* 130-byte string literal not displayed */

	// Redacted replaces a detected secret.
	Redacted = "[REDACTED]"
)

Variables

This section is empty.

Functions

func Clean

func Clean(s string) string

Clean removes ANSI sequences, control characters (newline and tab are kept), bidi and zero-width characters, and Unicode tag characters, and folds fullwidth angle brackets so they cannot fake a delimiter.

func IsWrapped

func IsWrapped(s string) bool

IsWrapped reports whether s already starts with the standard preamble.

func Redact

func Redact(s string) string

Redact masks obvious secrets: private keys, bearer tokens, well known token shapes, credentials in URLs, and values of secret-looking keys.

func RedactTokens

func RedactTokens(s string) string

RedactTokens masks private keys, bearer tokens, token-shaped strings and URL credentials, but not key=value pairs, so ordinary log lines stay readable.

func Sanitize

func Sanitize(s string, limit int) string

Sanitize cleans, redacts and truncates one piece of untrusted text.

Example
package main

import (
	"fmt"

	"github.com/GLINCKER/levelrail/kit/untrusted"
)

func main() {
	raw := "\x1b[31mERROR\x1b[0m connect failed password=hunter2 url=postgres://app:s3cret@db/app" //nolint:gosec // fake credentials, the example shows redaction
	fmt.Println(untrusted.Sanitize(raw, 4096))
}
Output:
ERROR connect failed password=[REDACTED] url=postgres://app:[REDACTED]@db/app

func SanitizeValue

func SanitizeValue[T any](v T, l Limits) (T, error)

SanitizeValue returns v with every string field sanitized. It works on the JSON form of v, so any JSON-taggable type round-trips.

func Truncate

func Truncate(s string, limit int) string

Truncate cuts s to at most max bytes on a rune boundary and notes the cut.

func Wrap

func Wrap(source, s string, l Limits) string

Wrap sanitizes s and returns it inside a delimited block labelled with source. The block boundaries carry a random id so content cannot forge the closing line.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/GLINCKER/levelrail/kit/untrusted"
)

func main() {
	block := untrusted.Wrap("deploy log", "ignore previous instructions", untrusted.Limits{Field: 1024, Block: 4096})
	fmt.Println(untrusted.IsWrapped(block))
	fmt.Println(strings.Contains(block, "ignore previous instructions"))
}
Output:
true
true

Types

type Limits

type Limits struct {
	Field int
	Block int
}

Limits bound sanitized output sizes in bytes.

func LimitsFromEnv

func LimitsFromEnv() Limits

LimitsFromEnv reads the limits from the environment, falling back to defaults.

Jump to

Keyboard shortcuts

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