Detecting Cross-Scope Data Leaks with Canary Tokens in Python
RagLeap Security 0.4.0 introduces a standalone canary module for detecting when traceable data appears in an unexpected scope.
A canary is a token placed in data that you want to monitor. If the token appears in a scanned location later, the system can report a hit.
Canaries don't prevent copying. They help detect and trace a potential leak.
Install
pip install ragleap-security The canary module uses the Python standard library and can be imported independently:
from ragleap_security.canary import ( CanaryIssuer, MemoryCanaryStore, SQLiteCanaryStore, ) Issue a canary
A canary is scoped to exactly one Scope.
The issuer creates a token with a random identifier and a MAC. The token itself does not contain the scope or the canary's metadata.
Conceptually:
canary = issuer.issue(scope, label="customer-document") The canary record is stored separately. The token is not stored as a raw token value.
Scan content
The scanner checks text for valid canary tokens:
hits = issuer.scan( text, scope, source="document-retrieval", ) A scan can return own-scope hits and foreign-scope hits.
A foreign hit means a valid canary belonging to another scope appeared in the scanned scope's data.
Importantly, a foreign hit exposes only a count. It does not reveal the foreign canary's ID, label, or owner.
Why normalize text?
Tokens may pass through systems that change character representation.
The scanner handles case changes, fullwidth forms, and format characters including zero-width and bidi characters.
It also validates the token MAC before querying the store. Invalid lookalikes are ignored.
Revoke a canary
issuer.revoke(scope, canary.id) Revocation deletes the stored record. Unknown IDs and wrong-scope requests produce the same error behavior.
Storage options
The module provides:
-
MemoryCanaryStorefor in-memory use -
SQLiteCanaryStorefor persistent local storage
The scanner applies bounded limits to input size, normalization, candidate lookups, and stored canaries. Oversized text is refused rather than truncated.
Audit behavior
Canary events include issuance, hits, foreign hits, and revocation.
Audit records do not contain the scanned text or token value.
If audit writing fails, issuance leaves no canary behind, and scanning returns no results with an audit_failed reason.
Security limitations
Canaries are not a prevention system.
A hit proves that the token reached a scanned location — not who caused it or why. A missing hit does not prove that no leak occurred.
Tokens can be removed, paraphrased, split, encoded, or transformed beyond the scanner's supported normalization. The visible rlc1_ prefix can also be filtered by an attacker.
There is no network beacon or agent integration in 0.4.0. Windows and macOS are not tested, and the release has not received an independent security review.
Tests
The release includes 91 canary tests and 70 additional mutation tests, all killed on Python 3.10 and 3.12. Store-dependent tests run against both memory and SQLite implementations.
The published wheel was verified against the source tag and tested for cross-scope detection, token transformations, forged lookalikes, revocation, and audit integrity.
Summary
Canaries are useful when you need an additional signal that traceable data crossed a boundary.
They complement — but do not replace — access controls, tenant isolation, and leak prevention.
RagLeap Security 0.4.0 is open source and self-hosted.
Repository: https://github.com/ragleap/ragleap-core
Release: ragleap-security 0.4.0