Skip to main content

Pytest plugin to hide sensitive data in test reports

Project description

pytest-mask-secrets

pytest-mask-secrets is a plugin for pytest that removes sensitive data from test reports.

Based on the configuration, it searches for specified secrets, passwords, and tokens in the records and replaces them with asterisks.

While this feature is usually provided by CI tools, it can be insufficient in many situations as it only strips secrets from the captured output. A common case of leaking secrets is through generated JUnit files that are not curated by CI tools. Therefore, it is necessary to have such functionality at the pytest level.

Installation

pip install pytest-mask-secrets

Usage

pytest-mask-secrets needs to know which values to mask. These values are read from environment variables. The list of these variables is passed in the MASK_SECRETS environment variable, which contains a comma-separated list of all environment variables containing secrets. Here is an example:

export PYPI_API_TOKEN=mytoken
export SOME_PASSWORD=mypassword
export MASK_SECRETS=PYPI_API_TOKEN,SOME_PASSWORD

pytest

With pytest-mask-secrets installed, all occurrences of "mytoken" and "mypassword" will be eliminated from the report.

Define Secret Values in the Code

Tests can use config.stash to define secret values to be masked. There is a mask_secrets_key available that provides access to a set() where additional secret values can be added. Here is an example:

from pytest_mask_secrets.plugin import mask_secrets_key

def test(pytestconfig):
    pytestconfig.stash[mask_secrets_key].add("true-secret")
    ...

All occurrences of "true-secret" will be removed from the report.

Automagic Identification of Variables with Secrets

If MASK_SECRETS_AUTO is set to anything other than zero ("0"), all environment variables containing the words "TOKEN", "SECRET," "PASSWORD," or "PASSWD" in their names are considered sensitive, and their values are removed from the report.

This discovery mode should be used with caution. CI workflows, in particular, should rely on an explicit list of secret variables. Under certain circumstances, this method can lead to the leakage of other sensitive data (if, by accident, a secret from an environment variable matches text commonly present in the test report). Nevertheless, this method can still be useful for example for local execution to prevent accidental leaks through copy-and-paste.

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

pytest_mask_secrets-1.6.0.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

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

pytest_mask_secrets-1.6.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file pytest_mask_secrets-1.6.0.tar.gz.

File metadata

  • Download URL: pytest_mask_secrets-1.6.0.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.26.2 CPython/3.12.3 Linux/6.11.0-1018-azure

File hashes

Hashes for pytest_mask_secrets-1.6.0.tar.gz
Algorithm Hash digest
SHA256 d6e7ccb5940fc4493c2ffbe63031f32d412d8c78e2f1462e7df4ea51e5d7e43a
MD5 1fc25a1311ef28570e636612009c77f3
BLAKE2b-256 8b78f5c0859cecedd38af2a5d2b45a867d2ddaf2043655a7e55e60e59e1e7f65

See more details on using hashes here.

File details

Details for the file pytest_mask_secrets-1.6.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_mask_secrets-1.6.0-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.26.2 CPython/3.12.3 Linux/6.11.0-1018-azure

File hashes

Hashes for pytest_mask_secrets-1.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c67e67d33a2670db65820db0ddbb8e28e27c9542c0f2604c1f6fa1619f5e54ea
MD5 9660ee56b7ba0d00a4069127f0a01d2d
BLAKE2b-256 9e29965b59b36a40bb0e1c67f0d671dffa2086980677da5c05e1d62fef2bdd89

See more details on using hashes here.

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