Skip to main content

Rust-backed secrets detection plugin for MCP Gateway

Project description

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__
  • 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: scans payload.args
  • tool_pre_invoke: scans tool invocation payloads before execution
  • tool_post_invoke: scans payload.result
  • resource_post_fetch: scans payload.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_id
  • aws_secret_access_key
  • google_api_key
  • github_token
  • stripe_secret_key
  • slack_token
  • private_key_block

Disabled by default because they are broader and more false-positive-prone:

  • generic_api_key_assignment
  • jwt_like
  • hex_secret_32
  • base64_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 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

Behavior Notes

  • Redaction preserves payload shape where possible instead of flattening everything to plain dicts.
  • base64_24 uses capture-group redaction so 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_fetch only scans text content exposed as payload.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.7 is a breaking change for any existing consumer reading detection metadata:

  • The old flat result.metadata keys — secrets_redacted, count (redaction path) and secrets_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 keys total_detections, total_masked, total_blocked, and secret_types (see Returned Metadata above for the full schema).
  • All 4 hooks — prompt_pre_fetch, tool_pre_invoke, tool_post_invoke, and resource_post_fetch — now accept a new optional extensions parameter carrying OpenTelemetry trace context. Emission to result.metadata["secrets_detection"] is gated solely on extensions.request.trace_id being 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 reading result.metadata["secrets_detection"] and must pass a trace_id via extensions to receive metrics.
  • tool_pre_invoke previously never received extensions and 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cpex_secrets_detection-0.3.7.tar.gz (52.3 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

cpex_secrets_detection-0.3.7-cp311-abi3-win_amd64.whl (767.5 kB view details)

Uploaded CPython 3.11+Windows x86-64

cpex_secrets_detection-0.3.7-cp311-abi3-manylinux_2_34_x86_64.whl (839.5 kB view details)

Uploaded CPython 3.11+manylinux: glibc 2.34+ x86-64

cpex_secrets_detection-0.3.7-cp311-abi3-manylinux_2_34_s390x.whl (873.8 kB view details)

Uploaded CPython 3.11+manylinux: glibc 2.34+ s390x

cpex_secrets_detection-0.3.7-cp311-abi3-manylinux_2_34_ppc64le.whl (865.9 kB view details)

Uploaded CPython 3.11+manylinux: glibc 2.34+ ppc64le

cpex_secrets_detection-0.3.7-cp311-abi3-manylinux_2_34_aarch64.whl (783.2 kB view details)

Uploaded CPython 3.11+manylinux: glibc 2.34+ ARM64

cpex_secrets_detection-0.3.7-cp311-abi3-macosx_11_0_arm64.whl (741.9 kB view details)

Uploaded CPython 3.11+macOS 11.0+ ARM64

File details

Details for the file cpex_secrets_detection-0.3.7.tar.gz.

File metadata

  • Download URL: cpex_secrets_detection-0.3.7.tar.gz
  • Upload date:
  • Size: 52.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for cpex_secrets_detection-0.3.7.tar.gz
Algorithm Hash digest
SHA256 fad0c691e46fda5fe797ff948cde94d32bfa95e6b5c121ede0167bb9b00f5a86
MD5 3febee34c1cf0f8aa039b1c3bca9cf07
BLAKE2b-256 9aaacead63b217271081fd60117218091ba899dc361fed33d94053c80616fbc9

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpex_secrets_detection-0.3.7.tar.gz:

Publisher: release-rust-python-package.yaml on IBM/cpex-plugins

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cpex_secrets_detection-0.3.7-cp311-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for cpex_secrets_detection-0.3.7-cp311-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 e6b55b312f14b19e81f30f3b99b617d11006bfd7fe328d1d35a8c10a873bd781
MD5 0717d53781324ee0577a814c7e1b4190
BLAKE2b-256 84892caea51417a4f7e8ceb8b604001bfa754abd5cfd324340d4296ec2fdb2fe

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpex_secrets_detection-0.3.7-cp311-abi3-win_amd64.whl:

Publisher: release-rust-python-package.yaml on IBM/cpex-plugins

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cpex_secrets_detection-0.3.7-cp311-abi3-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for cpex_secrets_detection-0.3.7-cp311-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 a13e0d77fccde5201d0cb0599bd201fd783de8ce4af3391a281bcc9384c01ef8
MD5 42aa4b96cb4d66f319d5af0562d3c070
BLAKE2b-256 d37995df182829bb6a2909e6b719cad18473435e344f73d370fd9da9efc732b5

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpex_secrets_detection-0.3.7-cp311-abi3-manylinux_2_34_x86_64.whl:

Publisher: release-rust-python-package.yaml on IBM/cpex-plugins

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cpex_secrets_detection-0.3.7-cp311-abi3-manylinux_2_34_s390x.whl.

File metadata

File hashes

Hashes for cpex_secrets_detection-0.3.7-cp311-abi3-manylinux_2_34_s390x.whl
Algorithm Hash digest
SHA256 966447ae8f5961ae23a42da810bf82d0575988b7bb1d1123a8086c4bdb2aa947
MD5 f53ab4428ef99279ba41837cb29a0977
BLAKE2b-256 cff8485ca6f1361ae1cb57766b042641b49c47008382b6dbd04acc056253c7db

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpex_secrets_detection-0.3.7-cp311-abi3-manylinux_2_34_s390x.whl:

Publisher: release-rust-python-package.yaml on IBM/cpex-plugins

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cpex_secrets_detection-0.3.7-cp311-abi3-manylinux_2_34_ppc64le.whl.

File metadata

File hashes

Hashes for cpex_secrets_detection-0.3.7-cp311-abi3-manylinux_2_34_ppc64le.whl
Algorithm Hash digest
SHA256 49991b2bb3c3447d515bb175e977b13be3e592f4043b79e529141ef2ce8a73dd
MD5 008777660eeb69c84643a553cd8875d2
BLAKE2b-256 756ddb0328d0fade11b366b26a74bdd47935de3a821794ea6dc152e3d02697a1

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpex_secrets_detection-0.3.7-cp311-abi3-manylinux_2_34_ppc64le.whl:

Publisher: release-rust-python-package.yaml on IBM/cpex-plugins

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cpex_secrets_detection-0.3.7-cp311-abi3-manylinux_2_34_aarch64.whl.

File metadata

File hashes

Hashes for cpex_secrets_detection-0.3.7-cp311-abi3-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 5be8874ac0d34a2078fed2c4dce9c359a8ccfcafc394ab83d8a44bd73666d05b
MD5 0ff1591c4174a208e6673299f46ce8c0
BLAKE2b-256 fe870662ab524bbbf20c90ac5a46f6313c1cf7b185db64541961c7b502aa3bba

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpex_secrets_detection-0.3.7-cp311-abi3-manylinux_2_34_aarch64.whl:

Publisher: release-rust-python-package.yaml on IBM/cpex-plugins

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cpex_secrets_detection-0.3.7-cp311-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for cpex_secrets_detection-0.3.7-cp311-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 50cccbb6876b433080eb7de738af95bb2a3ed4f33fa42b40ca168d9342fc244a
MD5 7fc8cda15e9ca7c78b2ecae6529f690d
BLAKE2b-256 cceac892b4ab3a9cf0a22da7c972c1b3b866bedf24b6678b163d0ead308b0b61

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpex_secrets_detection-0.3.7-cp311-abi3-macosx_11_0_arm64.whl:

Publisher: release-rust-python-package.yaml on IBM/cpex-plugins

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page