Secrets Detection (Rust)
Rust-backed secrets detection and redaction for ContextForge and MCP Gateway.
Features
- Detects likely credentials in prompt arguments, tool inputs, tool outputs, and resource content
- Built-in detectors for AWS keys, Google API keys, GitHub tokens, Stripe keys, Slack tokens, and private key blocks
- Optional broad detectors for generic API key assignments, JWT-like strings, long hex strings, and base64-like secrets
- Blocking, redaction, or metadata-only reporting modes
- Recursive scanning for nested dicts, lists, tuples, Pydantic-style objects,
__dict__, and__slots__ - Optional dotted field allowlist and denylist controls for structured payload scanning
- Sanitized outward metadata that reports finding types and counts, not original secret values
Build
make install
Runtime Requirements
This plugin depends on cpex>=0.1.0,<0.2 and imports hook models from cpex.framework. The compiled Rust extension is mandatory; there is no Python fallback implementation.
Usage
The plugin scans these hooks:
prompt_pre_fetch: scanspayload.argstool_pre_invoke: scans tool invocation payloads before executiontool_post_invoke: scanspayload.resultresource_post_fetch: scanspayload.content.text
Typical uses:
- block requests that contain likely credentials before they reach tools or prompts
- redact secrets from returned tool or resource payloads
- surface sanitized findings metadata for observability and tuning
Detection Coverage
Enabled by default:
aws_access_key_idaws_secret_access_keygoogle_api_keygithub_tokenstripe_secret_keyslack_tokenprivate_key_block
Disabled by default because they are broader and more false-positive-prone:
generic_api_key_assignmentjwt_likehex_secret_32base64_24
The detectors are regex-based. They do not verify whether a credential is real, active, or revoked.
Configuration
config:
enabled:
aws_access_key_id: true
aws_secret_access_key: true
google_api_key: true
github_token: true
stripe_secret_key: true
slack_token: true
private_key_block: true
generic_api_key_assignment: false
jwt_like: false
hex_secret_32: false
base64_24: false
redact: false
redaction_text: "***REDACTED***"
block_on_detection: true
min_findings_to_block: 1
field_allowlist: []
field_denylist: []
| Field | Type | Default | Description |
|---|---|---|---|
enabled |
dict | built-in defaults | Per-detector enable flags; unspecified detectors inherit defaults |
redact |
bool | false |
Replace matched secret values in returned payloads |
redaction_text |
string | "***REDACTED***" |
Replacement text used when redact=true |
block_on_detection |
bool | true |
Return a violation when enough findings are present |
min_findings_to_block |
integer | 1 |
Minimum finding count required before blocking |
field_allowlist |
list[string] | [] |
Dotted field paths eligible for scanning; empty means all structured fields are eligible |
field_denylist |
list[string] | [] |
Dotted field paths excluded from scanning; denylist entries take precedence over allowlist entries |
Field Filtering
field_allowlist and field_denylist apply to structured scan targets:
prompt_pre_fetch: paths are relative topayload.argstool_pre_invoke: paths are relative topayload.argstool_post_invoke: paths are relative topayload.result
For example, accounts.credentials.token matches payload.args.accounts.credentials.token in pre-hooks and payload.result.accounts.credentials.token in tool_post_invoke.
Rules:
- An empty
field_allowlistscans all structured fields. - A non-empty
field_allowlistscans only the listed paths and their descendants. - A
field_denylistentry excludes that path and all descendants. - When both lists match,
field_denylistwins. - Parent containers are still traversed to reach nested allowlisted paths.
- Matching is segment-aware:
layer1does not matchlayer10. - Lists and tuples are transparent to field matching; numeric indices are not used.
- Mapping keys themselves are not scanned.
- Direct scalar scan targets, including
payload.content.textinresource_post_fetch, retain current behavior and are not filtered by field paths.
Valid field paths use non-empty dot-separated segments. Empty paths, whitespace-only paths, leading or trailing dots, and empty segments such as layer1..token are rejected during plugin initialization.
Example:
config:
field_allowlist:
- "layer1"
- "accounts.credentials"
field_denylist:
- "layer1.layer2.layer3"
- "accounts.credentials.test_token"
Behavior Notes
- Redaction preserves payload shape where possible instead of flattening everything to plain dicts.
aws_secret_access_keyrecognizes=and:assignments with optional single or double quotes around the value.aws_secret_access_key,generic_api_key_assignment, andbase64_24use capture-group redaction so only the detected secret span is replaced; assignment labels, separators, quotes, and leading non-base64 boundary characters are preserved.- Broad detectors remain opt-in to reduce noisy matches on ordinary identifiers.
- Binary resource bodies are not scanned;
resource_post_fetchonly scans text content exposed aspayload.content.text. - The plugin does not decode archives, compressed data, or arbitrary encoded blobs before scanning.
Returned Metadata
prompt_pre_fetch, tool_pre_invoke, tool_post_invoke, and resource_post_fetch accept an optional extensions parameter carrying OpenTelemetry trace context. When a trace context is present (via extensions.request.trace_id), the plugin emits operational metrics on result.metadata["secrets_detection"] with the following schema:
result.metadata["secrets_detection"] = {
"total_detections": 2, # int — total number of findings in this call
"total_masked": 2, # int — number redacted (masking action taken)
"total_blocked": 0, # int — number that caused a block (blocking action taken)
"secret_types": ["aws_access_key_id", "slack_token"], # list[str] — distinct type names, sorted, deduped
}
total_masked and total_blocked are mutually exclusive per call: exactly one of them carries the finding count (the other is 0), depending on whether the redaction branch or the blocking branch executed. If neither redaction nor blocking is configured, both are 0 and only total_detections/secret_types are non-zero (findings-only reporting mode).
Gating: Metrics are only emitted when a valid trace_id is present in the trace context (extensions.request.trace_id). No trace context means no result.metadata write at all, regardless of any config flag — this keeps the untraced path byte-for-byte identical to before metrics existed.
Security Note (S1): The plugin never includes raw secret values in result.metadata, logs, or any other output. Only counts and type-category names (e.g. "aws_access_key_id") are reported.
tool_pre_invoke is in scope for this metrics contract on the same terms as the other 3 hooks: it accepts extensions and emits result.metadata["secrets_detection"] under the identical gating/schema once a valid trace_id is present.
Blocking responses use the SECRETS_DETECTED violation code.
Migration Note
Version 0.3.8 adds optional field filtering:
field_allowlistandfield_denylistdefault to empty lists, so existing configurations keep scanning the same fields as before.- When configured, field filters affect detection counts, metadata, redaction,
min_findings_to_block, and blocking decisions. field_denylistentries take precedence overfield_allowlistentries.
Version 0.3.7 is a breaking change for any existing consumer reading detection metadata:
- The old flat
result.metadatakeys —secrets_redacted,count(redaction path) andsecrets_findings,count(findings-only path) — have been removed entirely. There is no compatibility shim; code reading those keys will silently stop receiving data. - Detection/redaction/blocking metrics are now emitted on
result.metadata["secrets_detection"]instead, with keystotal_detections,total_masked,total_blocked, andsecret_types(see Returned Metadata above for the full schema). - All 4 hooks —
prompt_pre_fetch,tool_pre_invoke,tool_post_invoke, andresource_post_fetch— now accept a new optionalextensionsparameter carrying OpenTelemetry trace context. Emission toresult.metadata["secrets_detection"]is gated solely onextensions.request.trace_idbeing present and valid — if no trace context is supplied, no metrics are written at all, regardless of any config flag. - Consumers that previously read
result.metadata["secrets_redacted"]/result.metadata["secrets_findings"]unconditionally must migrate to readingresult.metadata["secrets_detection"]and must pass atrace_idviaextensionsto receive metrics. tool_pre_invokepreviously never receivedextensionsand could never emit metrics (a regression introduced earlier on this branch, since fixed) — it now follows the exact same contract as the other 3 hooks.
Security Notes
- Outward-facing findings metadata and violation examples do not include original matched secret values.
- Enable broad detectors only after testing against representative payloads.
- The detector is best-effort pattern matching and should complement, not replace, upstream secret management controls.
Testing
make ci
Metadata
Release files for cpex-secrets-detection 0.3.14
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| cpex_secrets_detection-0.3.14.tar.gz | 59.0 kB | Details |
Built distributions (wheels)
| File | Reset | |||
|---|---|---|---|---|
| cpex_secrets_detection-0.3.14-cp311-abi3-win_amd64.whl | CPython 3.11 | abi3 | Windows x86-64 | Details |
| cpex_secrets_detection-0.3.14-cp311-abi3-manylinux_2_34_x86_64.whl | CPython 3.11 | abi3 | Linux glibc 2.34+ x86-64 | Details |
| cpex_secrets_detection-0.3.14-cp311-abi3-manylinux_2_34_s390x.whl | CPython 3.11 | abi3 | Linux glibc 2.34+ IBM System/390x | Details |
| cpex_secrets_detection-0.3.14-cp311-abi3-manylinux_2_34_ppc64le.whl | CPython 3.11 | abi3 | Linux glibc 2.34+ PowerPC 64-le | Details |
| cpex_secrets_detection-0.3.14-cp311-abi3-manylinux_2_34_aarch64.whl | CPython 3.11 | abi3 | Linux glibc 2.34+ ARM64 | Details |
| cpex_secrets_detection-0.3.14-cp311-abi3-macosx_11_0_arm64.whl | CPython 3.11 | abi3 | macOS 11.0+ ARM64 | Details |
Total release size: 5.1 MB
Release files / cpex_secrets_detection-0.3.14.tar.gz
| Download URL | cpex_secrets_detection-0.3.14.tar.gz |
|---|---|
| Size | 59.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
683affa7307ef9e5fb01cd62f2c2247ca9450510d50c57b930b156f8615c2cfe
|
|
BLAKE2b-256 checksum How to use checksums |
373b3f0c0ea0107e283d1be377f1bd62fc226c449ac5fda628eac898e5a16b71
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 22, 2026.
Transparency logRelease files / cpex_secrets_detection-0.3.14-cp311-abi3-win_amd64.whl
| Download URL | cpex_secrets_detection-0.3.14-cp311-abi3-win_amd64.whl |
|---|---|
| Size | 786.1 kB |
| Tags | CPython 3.11 Windows x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
93582e92a52fd89e331fb07b24c97c7d97d2f0e48e0bb77018df1671f5674fd6
|
|
BLAKE2b-256 checksum How to use checksums |
288a201412f65da0763dc0e59a7a17b02b4720cdd757fb4c3afc302cabd3e64c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 22, 2026.
Transparency logRelease files / cpex_secrets_detection-0.3.14-cp311-abi3-manylinux_2_34_x86_64.whl
| Download URL | cpex_secrets_detection-0.3.14-cp311-abi3-manylinux_2_34_x86_64.whl |
|---|---|
| Size | 863.6 kB |
| Tags | CPython 3.11 Linux glibc 2.34+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
4d48fa8a483664dac52412eb1ac35f563c64362b636062c30d040ac8b78fe083
|
|
BLAKE2b-256 checksum How to use checksums |
8e76fb182a461adc968e44ddf8d91af503389e805d7ce8d15cd5a476b0232e98
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 22, 2026.
Transparency logRelease files / cpex_secrets_detection-0.3.14-cp311-abi3-manylinux_2_34_s390x.whl
| Download URL | cpex_secrets_detection-0.3.14-cp311-abi3-manylinux_2_34_s390x.whl |
|---|---|
| Size | 901.9 kB |
| Tags | CPython 3.11 Linux glibc 2.34+ IBM System/390x abi3 |
|
SHA-256 checksum How to use checksums |
6d5af7faf6ff9e5771891666e29e074b3a3304e6d5a1f8c729edff45cc9a84c6
|
|
BLAKE2b-256 checksum How to use checksums |
72a8bd479b9e9b21c2671285c46de2b6634590f0b959d932e892b3050f29c481
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 22, 2026.
Transparency logRelease files / cpex_secrets_detection-0.3.14-cp311-abi3-manylinux_2_34_ppc64le.whl
| Download URL | cpex_secrets_detection-0.3.14-cp311-abi3-manylinux_2_34_ppc64le.whl |
|---|---|
| Size | 893.2 kB |
| Tags | CPython 3.11 Linux glibc 2.34+ PowerPC 64-le abi3 |
|
SHA-256 checksum How to use checksums |
8af32ae41feec858567320b5abe4d0838b45d3dc8fc5f12222ad92e8f95a6b3f
|
|
BLAKE2b-256 checksum How to use checksums |
ec85c9bb4f4615fe5c7b10b50ac83379c574986775817932fc89241035cb299c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 22, 2026.
Transparency logRelease files / cpex_secrets_detection-0.3.14-cp311-abi3-manylinux_2_34_aarch64.whl
| Download URL | cpex_secrets_detection-0.3.14-cp311-abi3-manylinux_2_34_aarch64.whl |
|---|---|
| Size | 807.7 kB |
| Tags | CPython 3.11 Linux glibc 2.34+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
04a5a39c9aed60ae076fb354dfaafbf5d27248c2c5a717b69d8943549fc19796
|
|
BLAKE2b-256 checksum How to use checksums |
a8b74219662336061adb8baa6f1d315dbbf3bd131c87061dd134f3502dd1466a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 22, 2026.
Transparency logRelease files / cpex_secrets_detection-0.3.14-cp311-abi3-macosx_11_0_arm64.whl
| Download URL | cpex_secrets_detection-0.3.14-cp311-abi3-macosx_11_0_arm64.whl |
|---|---|
| Size | 754.2 kB |
| Tags | CPython 3.11 abi3 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
4ba473c5caf5b9e761b6e11ff735b1e6811720d187ef00379845db1f5b6993f2
|
|
BLAKE2b-256 checksum How to use checksums |
32278442fabfe0fa850010b3e85315715e4fc461b03ea6170eabafc46b22752e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 22, 2026.
Transparency log