Skip to main content

ragleap-security

Small, auditable security primitives for the ragleap libraries. The core is standard library only; encryption is an optional extra. Alpha. Not independently audited: see docs/threat-model.md for exactly what is and is not defended.

What is in 0.4.0

From 0.1.0:

  • Scope: an immutable tenant scope (tenant, subject, agent, session) with an unambiguous key, covers, narrow and require_scope, so stores can refuse to work without one.
  • AuditLog: an append-only JSON Lines log with a SHA-256 hash chain and verify(). Tamper-evident, not tamper-proof (see the threat model).
  • SecretStr, from_env, redact: secrets that do not print themselves, read from the environment by name, and a heuristic redactor for logs.
  • check_size, check_json_shape, bounded_read, Deadline: hard limits for untrusted input.

New in 0.2.0:

  • Key, KeyRing: a 32-byte key with an id that never prints its value, and a ring of keys for rotation.
  • ragleap_security.crypto (needs pip install ragleap-security[crypto]): encrypt, decrypt, encrypt_with_passphrase, decrypt_with_passphrase, reencrypt, key_id. AES-256-GCM through the cryptography package, a versioned envelope, and a context (for example a Scope) that must match on decrypt.
  • RateLimiter, SQLiteRateLimiter: a token bucket per key, in memory or shared between processes on one machine. They refuse when they cannot decide.
  • build_manifest, verify_manifest, Manifest, file_digest: detect that files changed, with an optional HMAC so the manifest itself cannot be edited unnoticed.

New in 0.3.0 and 0.4.0 (standard library only, each imported on its own):

  • ragleap_security.net: SafeFetcher, FetchPolicy. Outbound HTTP (GET and HEAD) that an attacker cannot point at your own network: a per-tenant host allowlist (deny by default), strict URL parsing, every resolved address checked, the connection pinned to the checked address, redirects re-checked on every hop, one deadline, a body cap, TLS verification always on, no proxy from the environment. Every refusal is a fixed reason code; the audit record never holds a path, query, header or body.

    fetcher = SafeFetcher(lambda scope: FetchPolicy(allow_hosts=("api.example.com",)), audit=log) res = fetcher.get(Scope("acme"), "https://api.example.com/v1/items") # NetError(reason) on any refusal

  • ragleap_security.canary (0.4.0): CanaryIssuer, MemoryCanaryStore, SQLiteCanaryStore. Tripwire tokens that detect and trace a leak; they never prevent copying. You plant a token (rlc1_ plus 26 characters: a random id and a MAC) in a system prompt, a memory entry, a tool description or a document, and scan model answers, outbound tool arguments, logs or exports for it. A token belongs to one Scope. A hit is own (a canary of this scope or a narrower one) or foreign (another scope's valid canary in this scope's data: a cross-tenant leak signal that reveals neither the owner nor the id to the caller). Lookalikes are rejected by the MAC before any store lookup; matching survives a changed case, fullwidth forms and hidden characters. The token is never stored. Audit events hold ids, labels and source names, never text or tokens, and a failed audit write fails the call.

    from ragleap_security.canary import CanaryIssuer, MemoryCanaryStore

    issuer = CanaryIssuer(key, MemoryCanaryStore(), audit=log) canary = issuer.issue(Scope("acme"), label="system-prompt") # plant canary.token somewhere hits = issuer.scan(model_answer, Scope("acme"), source="model-output") # [CanaryHit(kind="own", ...)]

What a hit proves: the string reached the place you scanned. Not who put it there. A missing hit proves nothing: a model may paraphrase or drop a token, and an attacker may transform or filter it. There is no network beacon.

Nothing in 0.1.0, 0.2.0 or 0.3.0 changed. The new modules are additive.

Examples

Encrypt data for one tenant and rotate keys:

from ragleap_security import Key, KeyRing, Scope
from ragleap_security.crypto import decrypt, encrypt, reencrypt

scope = Scope("acme", subject="alice")
old = Key.generate("2026-10")
box = encrypt(old, b"private notes", scope)           # the scope is authenticated too
assert decrypt(old, box, scope) == b"private notes"   # a different scope would fail

new = Key.generate("2026-11")
ring = KeyRing([new, old])                             # the first key is active
box = reencrypt(ring, box, scope)                      # now encrypted with "2026-11"

Keys come from the environment by variable name (base64), never from code:

key = Key.from_env("MY_APP_KEY", "2026-11")

Rate limit by scope:

from ragleap_security import RateLimiter, Scope

limiter = RateLimiter(rate=5, burst=10)               # 5 per second, bursts of 10
limiter.require(Scope("acme", subject="alice"))       # raises RateLimitError when refused

Detect changed files:

from ragleap_security import Key, build_manifest, verify_manifest

key = Key.from_env("MY_APP_KEY", "manifest")
manifest = build_manifest("app/", key=key, exclude=["__pycache__"])
result = verify_manifest("app/", manifest, key=key, exclude=["__pycache__"])
assert result.ok, (result.changed, result.missing, result.extra)

What this does not do

Encryption does not protect data while the program is running, and does not help against someone who has the key. Rate limits do not stop attacks spread over many machines. An integrity check detects changes; it does not prevent anyone from copying your code, models or data. The threat model lists the rest.

Verifying it yourself

pip install -e ".[test,crypto]"
pytest
python scripts/mutation_check.py   # breaks one protection at a time; every mutation must be killed

Without the cryptography package the crypto tests are skipped and everything else still runs.

Not in 0.4.0

Watermarks and network beacons for canaries (deliberately not built: weak evidence, and no infrastructure for beacons), pin, POST and other methods in net, public-key signatures, streaming encryption for very large files, a policy engine, output detectors and a red-team harness are planned (issue #899) and not available yet.

Metadata

Release files for ragleap-security 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ragleap-security 0.4.0
File Size Uploaded Provenance Actions
ragleap_security-0.4.0.tar.gz 77.6 kB With provenance Details for ragleap_security-0.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ragleap-security 0.4.0
File Interpreter ABI Platform Actions
ragleap_security-0.4.0-py3-none-any.whl Python 3 none any Details for ragleap_security-0.4.0-py3-none-any.whl

Total release size: 114.1 kB

Release files / ragleap_security-0.4.0.tar.gz

Download URL ragleap_security-0.4.0.tar.gz
Size 77.6 kB
Tags Source
SHA-256 checksum
How to use checksums
d9f7025bc6ec1ef52982197fbbe6042d45abce701b7ec4295fdcf7976d1dd545
BLAKE2b-256 checksum
How to use checksums
078272f253f608bdc80253adeb1698d125b80f018454c49d8801384dcce3244d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 27, 2026.

Transparency log

Release files / ragleap_security-0.4.0-py3-none-any.whl

Download URL ragleap_security-0.4.0-py3-none-any.whl
Size 36.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5996dbae160203dbb3e5318b3b609346fc6889d6d9621b8a260db69dcdaa6b6d
BLAKE2b-256 checksum
How to use checksums
35fd1c28b89342af27dec902b3aff9392fc7fc5f821f43adc931ad591a91cc27
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 27, 2026.

Transparency log

Security for ragleap-security 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for ragleap_security-0.4.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 27, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page