safetape 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. safetape-0.1.0/.gitattributes +1 -0
  2. safetape-0.1.0/.github/dependabot.yml +17 -0
  3. safetape-0.1.0/.github/workflows/ci.yml +93 -0
  4. safetape-0.1.0/.github/workflows/codeql.yml +38 -0
  5. safetape-0.1.0/.github/workflows/release.yml +60 -0
  6. safetape-0.1.0/.github/workflows/scorecard.yml +37 -0
  7. safetape-0.1.0/.github/workflows/secret-scan.yml +28 -0
  8. safetape-0.1.0/.gitignore +19 -0
  9. safetape-0.1.0/CHANGELOG.md +17 -0
  10. safetape-0.1.0/LICENSE +21 -0
  11. safetape-0.1.0/PKG-INFO +164 -0
  12. safetape-0.1.0/README.md +136 -0
  13. safetape-0.1.0/SECURITY.md +19 -0
  14. safetape-0.1.0/docs/releasing.md +32 -0
  15. safetape-0.1.0/docs/threat-model.md +52 -0
  16. safetape-0.1.0/pyproject.toml +98 -0
  17. safetape-0.1.0/src/safetape/__init__.py +73 -0
  18. safetape-0.1.0/src/safetape/_bodies.py +160 -0
  19. safetape-0.1.0/src/safetape/_credentials.py +118 -0
  20. safetape-0.1.0/src/safetape/_errors.py +52 -0
  21. safetape-0.1.0/src/safetape/_install.py +67 -0
  22. safetape-0.1.0/src/safetape/_matchers.py +161 -0
  23. safetape-0.1.0/src/safetape/_persister.py +247 -0
  24. safetape-0.1.0/src/safetape/_presets.py +135 -0
  25. safetape-0.1.0/src/safetape/_redact.py +27 -0
  26. safetape-0.1.0/src/safetape/_rules.py +274 -0
  27. safetape-0.1.0/src/safetape/_scan.py +160 -0
  28. safetape-0.1.0/src/safetape/_scrub.py +161 -0
  29. safetape-0.1.0/src/safetape/_settings.py +120 -0
  30. safetape-0.1.0/src/safetape/_vcrpy_fixes.py +95 -0
  31. safetape-0.1.0/src/safetape/py.typed +0 -0
  32. safetape-0.1.0/src/safetape/pytest_plugin.py +165 -0
  33. safetape-0.1.0/src/safetape/rules.json +194 -0
  34. safetape-0.1.0/src/safetape/serializer.py +261 -0
  35. safetape-0.1.0/tests/__init__.py +0 -0
  36. safetape-0.1.0/tests/conftest.py +75 -0
  37. safetape-0.1.0/tests/fakes.py +108 -0
  38. safetape-0.1.0/tests/test_credentials.py +113 -0
  39. safetape-0.1.0/tests/test_integration.py +312 -0
  40. safetape-0.1.0/tests/test_matchers.py +177 -0
  41. safetape-0.1.0/tests/test_package.py +17 -0
  42. safetape-0.1.0/tests/test_persister.py +374 -0
  43. safetape-0.1.0/tests/test_pytest_plugin.py +297 -0
  44. safetape-0.1.0/tests/test_rules.py +241 -0
  45. safetape-0.1.0/tests/test_scan.py +211 -0
  46. safetape-0.1.0/tests/test_scrub.py +164 -0
  47. safetape-0.1.0/tests/test_sdks.py +294 -0
  48. safetape-0.1.0/tests/test_serializer.py +338 -0
  49. safetape-0.1.0/tests/test_settings.py +71 -0
  50. safetape-0.1.0/uv.lock +1838 -0
@@ -0,0 +1 @@
1
+ * text=auto eol=lf
@@ -0,0 +1,17 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: "uv"
4
+ directory: "/"
5
+ schedule:
6
+ interval: "weekly"
7
+ groups:
8
+ python-deps:
9
+ patterns: ["*"]
10
+
11
+ - package-ecosystem: "github-actions"
12
+ directory: "/"
13
+ schedule:
14
+ interval: "weekly"
15
+ groups:
16
+ actions:
17
+ patterns: ["*"]
@@ -0,0 +1,93 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+
8
+ permissions:
9
+ contents: read
10
+
11
+ concurrency:
12
+ group: ${{ github.workflow }}-${{ github.ref }}
13
+ cancel-in-progress: true
14
+
15
+ jobs:
16
+ lint:
17
+ runs-on: ubuntu-latest
18
+ steps:
19
+ - uses: step-security/harden-runner@aebdcf6156ab6691adf63218240e476564a1ed4c # v2.23.0
20
+ with:
21
+ egress-policy: audit
22
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
23
+ with:
24
+ persist-credentials: false
25
+ - uses: astral-sh/setup-uv@1db35dd8eee11c5894872e0b383ae6c406cf738d # v10.4.0
26
+ with:
27
+ version: "0.13.10"
28
+ python-version: "3.13"
29
+ - run: uv sync --locked
30
+ - run: uv run ruff check
31
+ - run: uv run ruff format --check
32
+ - run: uv run mypy
33
+
34
+ test:
35
+ runs-on: ${{ matrix.os }}
36
+ strategy:
37
+ fail-fast: false
38
+ matrix:
39
+ os: [ubuntu-latest]
40
+ python: ["3.11", "3.12", "3.13", "3.14"]
41
+ include:
42
+ - os: windows-latest
43
+ python: "3.13"
44
+ - os: macos-latest
45
+ python: "3.13"
46
+ steps:
47
+ - uses: step-security/harden-runner@aebdcf6156ab6691adf63218240e476564a1ed4c # v2.23.0
48
+ with:
49
+ egress-policy: audit
50
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
51
+ with:
52
+ persist-credentials: false
53
+ - uses: astral-sh/setup-uv@1db35dd8eee11c5894872e0b383ae6c406cf738d # v10.4.0
54
+ with:
55
+ version: "0.13.10"
56
+ python-version: ${{ matrix.python }}
57
+ - run: uv sync --locked
58
+ # pytest loads safetape's plugin before pytest-cov starts, so measure from outside.
59
+ - run: uv run coverage run -m pytest
60
+ - run: uv run coverage report
61
+
62
+ oldest:
63
+ # The oldest versions pyproject.toml allows, vcrpy 8.2.1 among them.
64
+ runs-on: ubuntu-latest
65
+ steps:
66
+ - uses: step-security/harden-runner@aebdcf6156ab6691adf63218240e476564a1ed4c # v2.23.0
67
+ with:
68
+ egress-policy: audit
69
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
70
+ with:
71
+ persist-credentials: false
72
+ - uses: astral-sh/setup-uv@1db35dd8eee11c5894872e0b383ae6c406cf738d # v10.4.0
73
+ with:
74
+ version: "0.13.10"
75
+ python-version: "3.11"
76
+ - run: uv run --isolated --resolution lowest-direct pytest
77
+
78
+ audit:
79
+ runs-on: ubuntu-latest
80
+ steps:
81
+ - uses: step-security/harden-runner@aebdcf6156ab6691adf63218240e476564a1ed4c # v2.23.0
82
+ with:
83
+ egress-policy: audit
84
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
85
+ with:
86
+ persist-credentials: false
87
+ - uses: astral-sh/setup-uv@1db35dd8eee11c5894872e0b383ae6c406cf738d # v10.4.0
88
+ with:
89
+ version: "0.13.10"
90
+ python-version: "3.13"
91
+ - run: uv sync --locked
92
+ - name: Check dependencies for known vulnerabilities
93
+ run: uv run pip-audit --skip-editable
@@ -0,0 +1,38 @@
1
+ name: CodeQL
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+ schedule:
8
+ - cron: "27 3 * * 1"
9
+
10
+ permissions:
11
+ contents: read
12
+
13
+ jobs:
14
+ analyze:
15
+ runs-on: ubuntu-latest
16
+ permissions:
17
+ contents: read
18
+ actions: read
19
+ security-events: write
20
+ strategy:
21
+ fail-fast: false
22
+ matrix:
23
+ language: [python, actions]
24
+ steps:
25
+ - uses: step-security/harden-runner@aebdcf6156ab6691adf63218240e476564a1ed4c # v2.23.0
26
+ with:
27
+ egress-policy: audit
28
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
29
+ with:
30
+ persist-credentials: false
31
+ - uses: github/codeql-action/init@915412ff23e8b7659517e607487dbeae018f2934 # v4.39.5
32
+ with:
33
+ languages: ${{ matrix.language }}
34
+ build-mode: none
35
+ queries: security-extended
36
+ - uses: github/codeql-action/analyze@915412ff23e8b7659517e607487dbeae018f2934 # v4.39.5
37
+ with:
38
+ category: "/language:${{ matrix.language }}"
@@ -0,0 +1,60 @@
1
+ name: Release
2
+
3
+ # Pushing a tag like v0.1.0 builds the package and publishes it to PyPI with
4
+ # trusted publishing, so no PyPI token is stored anywhere.
5
+
6
+ on:
7
+ push:
8
+ tags: ["v*"]
9
+
10
+ permissions:
11
+ contents: read
12
+
13
+ jobs:
14
+ build:
15
+ runs-on: ubuntu-latest
16
+ steps:
17
+ - uses: step-security/harden-runner@aebdcf6156ab6691adf63218240e476564a1ed4c # v2.23.0
18
+ with:
19
+ egress-policy: audit
20
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
21
+ with:
22
+ persist-credentials: false
23
+ - uses: astral-sh/setup-uv@1db35dd8eee11c5894872e0b383ae6c406cf738d # v10.4.0
24
+ with:
25
+ version: "0.13.10"
26
+ python-version: "3.13"
27
+ - run: uv sync --locked
28
+ - run: uv run pytest
29
+ - run: uv build
30
+ - name: Check the tag matches the package version
31
+ run: |
32
+ version="${GITHUB_REF_NAME#v}"
33
+ if [ ! -f "dist/safetape-${version}-py3-none-any.whl" ]; then
34
+ echo "Tag ${GITHUB_REF_NAME} doesn't match the version in src/safetape/__init__.py:"
35
+ ls dist
36
+ exit 1
37
+ fi
38
+ - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
39
+ with:
40
+ name: dist
41
+ path: dist/
42
+ if-no-files-found: error
43
+
44
+ publish:
45
+ needs: build
46
+ runs-on: ubuntu-latest
47
+ environment:
48
+ name: pypi
49
+ url: https://pypi.org/p/safetape
50
+ permissions:
51
+ id-token: write
52
+ steps:
53
+ - uses: step-security/harden-runner@aebdcf6156ab6691adf63218240e476564a1ed4c # v2.23.0
54
+ with:
55
+ egress-policy: audit
56
+ - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
57
+ with:
58
+ name: dist
59
+ path: dist/
60
+ - uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # v1.14.3
@@ -0,0 +1,37 @@
1
+ name: Scorecard
2
+
3
+ on:
4
+ branch_protection_rule:
5
+ schedule:
6
+ - cron: "41 5 * * 2"
7
+ push:
8
+ branches: [main]
9
+
10
+ permissions: read-all
11
+
12
+ jobs:
13
+ analysis:
14
+ runs-on: ubuntu-latest
15
+ permissions:
16
+ security-events: write
17
+ id-token: write
18
+ steps:
19
+ - uses: step-security/harden-runner@aebdcf6156ab6691adf63218240e476564a1ed4c # v2.23.0
20
+ with:
21
+ egress-policy: audit
22
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
23
+ with:
24
+ persist-credentials: false
25
+ - uses: ossf/scorecard-action@23f22cd43608bae7a1f1fa2603d7314dee9538a5 # v2.6.0
26
+ with:
27
+ results_file: results.sarif
28
+ results_format: sarif
29
+ publish_results: true
30
+ - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
31
+ with:
32
+ name: scorecard-results
33
+ path: results.sarif
34
+ retention-days: 5
35
+ - uses: github/codeql-action/upload-sarif@915412ff23e8b7659517e607487dbeae018f2934 # v4.39.5
36
+ with:
37
+ sarif_file: results.sarif
@@ -0,0 +1,28 @@
1
+ name: Secret scan
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+
8
+ permissions:
9
+ contents: read
10
+
11
+ jobs:
12
+ gitleaks:
13
+ runs-on: ubuntu-latest
14
+ permissions:
15
+ contents: read
16
+ pull-requests: read
17
+ steps:
18
+ - uses: step-security/harden-runner@aebdcf6156ab6691adf63218240e476564a1ed4c # v2.23.0
19
+ with:
20
+ egress-policy: audit
21
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
22
+ with:
23
+ fetch-depth: 0
24
+ persist-credentials: false
25
+ - uses: gitleaks/gitleaks-action@e0c47f4f8be36e29cdc102c57e68cb5cbf0e8d1e # v3.0.0
26
+ env:
27
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
28
+ GITLEAKS_ENABLE_COMMENTS: "false"
@@ -0,0 +1,19 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ *.egg-info/
4
+ build/
5
+ dist/
6
+ .venv/
7
+ .pytest_cache/
8
+ .mypy_cache/
9
+ .ruff_cache/
10
+ .coverage
11
+ .coverage.*
12
+ coverage.xml
13
+ htmlcov/
14
+ .hypothesis/
15
+ .env
16
+ .env.*
17
+ *.sarif
18
+ .idea/
19
+ .vscode/
@@ -0,0 +1,17 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0
4
+
5
+ The first release.
6
+
7
+ - Provider presets that strip credential headers, query parameters and OAuth body fields
8
+ - Secret rules in a JSON file, with a scanner that checks every part of every interaction, compressed bodies included
9
+ - Credentials safetape strips, and environment variables named like secrets, are looked for by exact value anywhere in a cassette, which catches keys with no recognisable shape
10
+ - `SafePersister`, which refuses to write a cassette that still looks like it has a secret in it, and writes atomically as UTF-8, with a few retries when Windows has the file locked
11
+ - A readable JSON serializer that stores JSON bodies as JSON and streamed responses as events
12
+ - Matchers that ignore stripped credentials, compare JSON by value and show a masked diff when nothing matches
13
+ - vcrpy's "can't play back" error and pytest's failure reports are masked, since both can show the live request
14
+ - Lockdown mode, on by default in CI, that never records
15
+ - A report of cassettes older than 90 days
16
+ - A pytest plugin that sets all of this up for pytest-recording
17
+ - Tested with the OpenAI and Anthropic Python SDKs, sync, async and streaming
safetape-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Parth
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,164 @@
1
+ Metadata-Version: 2.5
2
+ Name: safetape
3
+ Version: 0.1.0
4
+ Summary: Keep API keys out of your recorded LLM test cassettes. Works with vcrpy and pytest-recording.
5
+ Project-URL: Homepage, https://github.com/Ghost0308/safetape
6
+ Project-URL: Issues, https://github.com/Ghost0308/safetape/issues
7
+ Project-URL: Changelog, https://github.com/Ghost0308/safetape/blob/main/CHANGELOG.md
8
+ Author: Parth
9
+ License-Expression: MIT
10
+ License-File: LICENSE
11
+ Keywords: anthropic,cassettes,llm,openai,pytest,secrets,testing,vcrpy
12
+ Classifier: Development Status :: 3 - Alpha
13
+ Classifier: Framework :: Pytest
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: Operating System :: OS Independent
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3 :: Only
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Programming Language :: Python :: 3.13
21
+ Classifier: Programming Language :: Python :: 3.14
22
+ Classifier: Topic :: Security
23
+ Classifier: Topic :: Software Development :: Testing
24
+ Classifier: Typing :: Typed
25
+ Requires-Python: >=3.11
26
+ Requires-Dist: vcrpy>=8.2.1
27
+ Description-Content-Type: text/markdown
28
+
29
+ # safetape
30
+
31
+ Record your LLM API calls once, replay them in tests, and stop worrying about committing an API key by accident.
32
+
33
+ safetape sits on top of [vcrpy](https://github.com/kevin1024/vcrpy) and [pytest-recording](https://github.com/kiwicom/pytest-recording). It knows what OpenAI, Anthropic, Gemini and other provider traffic looks like, so it strips credentials before a cassette is written and refuses to save one that still has a secret in it.
34
+
35
+ **Status: early.** This is 0.1, so expect things to change. It needs Python 3.11 or newer and vcrpy 8.2.1 or newer.
36
+
37
+ ```
38
+ pip install safetape
39
+ ```
40
+
41
+ It's tested with the OpenAI and Anthropic Python SDKs (sync, async and streaming), which send their requests through httpx2. vcrpy patches that as well as httpx, requests, aiohttp and the other usual clients.
42
+
43
+ ## Why I'm building this
44
+
45
+ If you test code that calls an LLM API you've probably been here. Live calls in tests are slow, cost money and give a different answer every run, so you record them with vcrpy. Then you find out vcrpy has no idea which headers or body fields are secret, so you write your own filters. Then a provider SDK adds a header you didn't think of and a key ends up in git.
46
+
47
+ A lot of projects have written their own version of this glue code. I wanted one place where it's done once, with safe defaults instead of opt-in settings.
48
+
49
+ ## With pytest-recording
50
+
51
+ Install safetape next to pytest-recording and you're done. Its pytest plugin switches on by itself for every `@pytest.mark.vcr` test.
52
+
53
+ ```python
54
+ import pytest
55
+ from openai import OpenAI
56
+
57
+
58
+ @pytest.mark.vcr
59
+ def test_capital() -> None:
60
+ reply = OpenAI().responses.create(model="gpt-5-mini", input="Capital of France?")
61
+ assert "Paris" in reply.output_text
62
+ ```
63
+
64
+ Record once with `pytest --record-mode=once`, then commit the cassettes. I'd also switch pytest-recording to JSON so the cassettes are easy to read (more on that below):
65
+
66
+ ```python
67
+ # conftest.py
68
+ @pytest.fixture(scope="module")
69
+ def vcr_config():
70
+ return {"serializer": "json"}
71
+ ```
72
+
73
+ ## With plain vcrpy
74
+
75
+ ```python
76
+ import vcr
77
+ import safetape
78
+
79
+ my_vcr = safetape.install(vcr.VCR(cassette_library_dir="tests/cassettes", serializer="json"))
80
+
81
+ with my_vcr.use_cassette("capital.json"):
82
+ ...
83
+ ```
84
+
85
+ ## What happens before a cassette is saved
86
+
87
+ safetape drops credential headers like `authorization`, `x-api-key`, `x-goog-api-key`, `api-key`, `openai-organization`, `cookie` and `set-cookie`, and credential query parameters like Gemini's `?key=` and the signature on AWS presigned URLs. In JSON and form bodies it replaces fields like `access_token`, `refresh_token` and `client_secret` with `REDACTED`, which covers OAuth token exchanges such as the ones Vertex AI does behind your back.
88
+
89
+ Then it scans everything that's left, compressed bodies included, for anything that looks like a key or token. Some providers (Mistral, Cohere, Azure OpenAI) use keys with no recognisable shape, so it also looks for exact values it already knows are secret: the credentials it just stripped, and the values of environment variables with names like `MISTRAL_API_KEY`. That's how a key pasted into a prompt or echoed back in an error still gets caught, whatever it looks like.
90
+
91
+ If it finds something, nothing is written and the test fails with the location. The error shows the rule and a masked preview, never the value:
92
+
93
+ ```
94
+ safetape didn't save tests/cassettes/test_capital.json because it still has something that looks like a secret:
95
+
96
+ interaction 1, response body: OpenAI project, service account or admin key [openai-key] sk-p... (164 chars)
97
+ ```
98
+
99
+ All of this happens whichever provider a cassette talks to. The per-provider lists are in [_presets.py](src/safetape/_presets.py) and the secret rules are in [rules.json](src/safetape/rules.json). The rules file is plain JSON with regexes that Python and JavaScript read the same way, so other tools can use it too.
100
+
101
+ ## Readable cassettes
102
+
103
+ vcrpy stores every body as one long string. With the `json` serializer, safetape stores JSON bodies as JSON, streamed responses as a list of events, and binary bodies as base64. A re-recorded cassette then gives you a diff you can actually review:
104
+
105
+ ```json
106
+ "body": {
107
+ "sse": [
108
+ {"event": "content_block_delta", "data": {"type": "content_block_delta", "delta": {"text": "Hi"}}},
109
+ {"event": "message_stop", "data": {"type": "message_stop"}}
110
+ ]
111
+ }
112
+ ```
113
+
114
+ It still reads cassettes written by vcrpy's own JSON serializer. On replay the JSON is encoded again, compactly, so the bytes can differ from what the server sent even though the data is the same. Content-Length is updated to match. If a test compares raw response bytes, stay on YAML.
115
+
116
+ ## Matching requests
117
+
118
+ Almost every LLM call is a POST to the same path, so vcrpy's default matching can't tell two prompts apart and just replays answers in the order they were recorded. safetape adds `body` to `match_on`, unless you've set `match_on` yourself.
119
+
120
+ It also replaces vcrpy's `query`, `uri` and `body` matchers. They ignore the credentials it strips, so a cassette recorded with one key still replays with another. The body matcher compares JSON by value, so key order and whitespace don't matter, ignores the random boundary in multipart uploads, and can skip fields that change on every run, like `metadata.user_id` or `messages.*.id`.
121
+
122
+ When nothing matches, vcrpy's error includes a diff of the bodies with any secrets masked.
123
+
124
+ ## Lockdown in CI
125
+
126
+ When the `CI` environment variable is set, safetape never records. Whatever record mode a test asks for, requests are only replayed, and a request with no recording fails the test without touching the network. Set `SAFETAPE_LOCKDOWN=0` or `safetape_lockdown = off` if you really do want to record in CI.
127
+
128
+ ## Old cassettes
129
+
130
+ At the end of a run, safetape lists cassettes whose responses are more than 90 days old, going by their `Date` headers. Providers change their APIs often enough that an old cassette can keep a test green long after the real thing broke. With plain vcrpy you get a `StaleCassetteWarning` when an old cassette loads.
131
+
132
+ ## Settings
133
+
134
+ Everything adds to the defaults. There's no way to switch a default off, so a typo can't quietly stop a header from being stripped.
135
+
136
+ ```toml
137
+ [tool.pytest.ini_options]
138
+ safetape_redact_headers = ["x-corp-auth"]
139
+ safetape_redact_query_params = ["sig2"]
140
+ safetape_redact_body_fields = ["session_secret"]
141
+ safetape_allow = ["^sk-test-"] # regexes for values that look secret but aren't
142
+ safetape_rules = ["tests/more-rules.json"]
143
+ safetape_ignore_body_fields = ["metadata.user_id", "messages.*.id"]
144
+ safetape_stale_days = 90 # 0 turns the report off
145
+ safetape_lockdown = "auto" # or "on" or "off"
146
+ ```
147
+
148
+ With plain vcrpy, pass the same things to `safetape.install(my_vcr, safetape.Settings(...))`.
149
+
150
+ To turn the plugin off for a run, use `pytest -p no:safetape`.
151
+
152
+ ## What it can't promise
153
+
154
+ Detection is pattern based, plus exact matches for the credentials it knows about. A key in an unusual format that isn't in a header or an environment variable can still slip through, and so can a known key that was encoded or split up on its way into a body. Use it as a safety net on top of secret scanning in your repo, not instead of it, and rotate anything that leaks.
155
+
156
+ When a test fails, the error and the frames pytest prints can describe the live request before it was scrubbed. The plugin masks secrets in pytest's failure reports and captured output, and vcrpy's "can't play back this request" error is masked wherever it's shown. Other tools that print local variables, like rich tracebacks or Sentry, are outside what safetape can see.
157
+
158
+ ## Security
159
+
160
+ See [SECURITY.md](SECURITY.md) for how to report a problem, and [docs/threat-model.md](docs/threat-model.md) for what this project tries to protect against.
161
+
162
+ ## License
163
+
164
+ MIT
@@ -0,0 +1,136 @@
1
+ # safetape
2
+
3
+ Record your LLM API calls once, replay them in tests, and stop worrying about committing an API key by accident.
4
+
5
+ safetape sits on top of [vcrpy](https://github.com/kevin1024/vcrpy) and [pytest-recording](https://github.com/kiwicom/pytest-recording). It knows what OpenAI, Anthropic, Gemini and other provider traffic looks like, so it strips credentials before a cassette is written and refuses to save one that still has a secret in it.
6
+
7
+ **Status: early.** This is 0.1, so expect things to change. It needs Python 3.11 or newer and vcrpy 8.2.1 or newer.
8
+
9
+ ```
10
+ pip install safetape
11
+ ```
12
+
13
+ It's tested with the OpenAI and Anthropic Python SDKs (sync, async and streaming), which send their requests through httpx2. vcrpy patches that as well as httpx, requests, aiohttp and the other usual clients.
14
+
15
+ ## Why I'm building this
16
+
17
+ If you test code that calls an LLM API you've probably been here. Live calls in tests are slow, cost money and give a different answer every run, so you record them with vcrpy. Then you find out vcrpy has no idea which headers or body fields are secret, so you write your own filters. Then a provider SDK adds a header you didn't think of and a key ends up in git.
18
+
19
+ A lot of projects have written their own version of this glue code. I wanted one place where it's done once, with safe defaults instead of opt-in settings.
20
+
21
+ ## With pytest-recording
22
+
23
+ Install safetape next to pytest-recording and you're done. Its pytest plugin switches on by itself for every `@pytest.mark.vcr` test.
24
+
25
+ ```python
26
+ import pytest
27
+ from openai import OpenAI
28
+
29
+
30
+ @pytest.mark.vcr
31
+ def test_capital() -> None:
32
+ reply = OpenAI().responses.create(model="gpt-5-mini", input="Capital of France?")
33
+ assert "Paris" in reply.output_text
34
+ ```
35
+
36
+ Record once with `pytest --record-mode=once`, then commit the cassettes. I'd also switch pytest-recording to JSON so the cassettes are easy to read (more on that below):
37
+
38
+ ```python
39
+ # conftest.py
40
+ @pytest.fixture(scope="module")
41
+ def vcr_config():
42
+ return {"serializer": "json"}
43
+ ```
44
+
45
+ ## With plain vcrpy
46
+
47
+ ```python
48
+ import vcr
49
+ import safetape
50
+
51
+ my_vcr = safetape.install(vcr.VCR(cassette_library_dir="tests/cassettes", serializer="json"))
52
+
53
+ with my_vcr.use_cassette("capital.json"):
54
+ ...
55
+ ```
56
+
57
+ ## What happens before a cassette is saved
58
+
59
+ safetape drops credential headers like `authorization`, `x-api-key`, `x-goog-api-key`, `api-key`, `openai-organization`, `cookie` and `set-cookie`, and credential query parameters like Gemini's `?key=` and the signature on AWS presigned URLs. In JSON and form bodies it replaces fields like `access_token`, `refresh_token` and `client_secret` with `REDACTED`, which covers OAuth token exchanges such as the ones Vertex AI does behind your back.
60
+
61
+ Then it scans everything that's left, compressed bodies included, for anything that looks like a key or token. Some providers (Mistral, Cohere, Azure OpenAI) use keys with no recognisable shape, so it also looks for exact values it already knows are secret: the credentials it just stripped, and the values of environment variables with names like `MISTRAL_API_KEY`. That's how a key pasted into a prompt or echoed back in an error still gets caught, whatever it looks like.
62
+
63
+ If it finds something, nothing is written and the test fails with the location. The error shows the rule and a masked preview, never the value:
64
+
65
+ ```
66
+ safetape didn't save tests/cassettes/test_capital.json because it still has something that looks like a secret:
67
+
68
+ interaction 1, response body: OpenAI project, service account or admin key [openai-key] sk-p... (164 chars)
69
+ ```
70
+
71
+ All of this happens whichever provider a cassette talks to. The per-provider lists are in [_presets.py](src/safetape/_presets.py) and the secret rules are in [rules.json](src/safetape/rules.json). The rules file is plain JSON with regexes that Python and JavaScript read the same way, so other tools can use it too.
72
+
73
+ ## Readable cassettes
74
+
75
+ vcrpy stores every body as one long string. With the `json` serializer, safetape stores JSON bodies as JSON, streamed responses as a list of events, and binary bodies as base64. A re-recorded cassette then gives you a diff you can actually review:
76
+
77
+ ```json
78
+ "body": {
79
+ "sse": [
80
+ {"event": "content_block_delta", "data": {"type": "content_block_delta", "delta": {"text": "Hi"}}},
81
+ {"event": "message_stop", "data": {"type": "message_stop"}}
82
+ ]
83
+ }
84
+ ```
85
+
86
+ It still reads cassettes written by vcrpy's own JSON serializer. On replay the JSON is encoded again, compactly, so the bytes can differ from what the server sent even though the data is the same. Content-Length is updated to match. If a test compares raw response bytes, stay on YAML.
87
+
88
+ ## Matching requests
89
+
90
+ Almost every LLM call is a POST to the same path, so vcrpy's default matching can't tell two prompts apart and just replays answers in the order they were recorded. safetape adds `body` to `match_on`, unless you've set `match_on` yourself.
91
+
92
+ It also replaces vcrpy's `query`, `uri` and `body` matchers. They ignore the credentials it strips, so a cassette recorded with one key still replays with another. The body matcher compares JSON by value, so key order and whitespace don't matter, ignores the random boundary in multipart uploads, and can skip fields that change on every run, like `metadata.user_id` or `messages.*.id`.
93
+
94
+ When nothing matches, vcrpy's error includes a diff of the bodies with any secrets masked.
95
+
96
+ ## Lockdown in CI
97
+
98
+ When the `CI` environment variable is set, safetape never records. Whatever record mode a test asks for, requests are only replayed, and a request with no recording fails the test without touching the network. Set `SAFETAPE_LOCKDOWN=0` or `safetape_lockdown = off` if you really do want to record in CI.
99
+
100
+ ## Old cassettes
101
+
102
+ At the end of a run, safetape lists cassettes whose responses are more than 90 days old, going by their `Date` headers. Providers change their APIs often enough that an old cassette can keep a test green long after the real thing broke. With plain vcrpy you get a `StaleCassetteWarning` when an old cassette loads.
103
+
104
+ ## Settings
105
+
106
+ Everything adds to the defaults. There's no way to switch a default off, so a typo can't quietly stop a header from being stripped.
107
+
108
+ ```toml
109
+ [tool.pytest.ini_options]
110
+ safetape_redact_headers = ["x-corp-auth"]
111
+ safetape_redact_query_params = ["sig2"]
112
+ safetape_redact_body_fields = ["session_secret"]
113
+ safetape_allow = ["^sk-test-"] # regexes for values that look secret but aren't
114
+ safetape_rules = ["tests/more-rules.json"]
115
+ safetape_ignore_body_fields = ["metadata.user_id", "messages.*.id"]
116
+ safetape_stale_days = 90 # 0 turns the report off
117
+ safetape_lockdown = "auto" # or "on" or "off"
118
+ ```
119
+
120
+ With plain vcrpy, pass the same things to `safetape.install(my_vcr, safetape.Settings(...))`.
121
+
122
+ To turn the plugin off for a run, use `pytest -p no:safetape`.
123
+
124
+ ## What it can't promise
125
+
126
+ Detection is pattern based, plus exact matches for the credentials it knows about. A key in an unusual format that isn't in a header or an environment variable can still slip through, and so can a known key that was encoded or split up on its way into a body. Use it as a safety net on top of secret scanning in your repo, not instead of it, and rotate anything that leaks.
127
+
128
+ When a test fails, the error and the frames pytest prints can describe the live request before it was scrubbed. The plugin masks secrets in pytest's failure reports and captured output, and vcrpy's "can't play back this request" error is masked wherever it's shown. Other tools that print local variables, like rich tracebacks or Sentry, are outside what safetape can see.
129
+
130
+ ## Security
131
+
132
+ See [SECURITY.md](SECURITY.md) for how to report a problem, and [docs/threat-model.md](docs/threat-model.md) for what this project tries to protect against.
133
+
134
+ ## License
135
+
136
+ MIT
@@ -0,0 +1,19 @@
1
+ # Security policy
2
+
3
+ ## Reporting a problem
4
+
5
+ Please don't open a public issue for a security problem. Use GitHub's private reporting instead: open the **Security** tab of this repo and click **Report a vulnerability**.
6
+
7
+ I'll reply within 7 days. If the report checks out I'll work on a fix, publish an advisory once a fixed version is out, and credit you in the release notes if you'd like that.
8
+
9
+ ## Supported versions
10
+
11
+ safetape is still pre 1.0, so only the latest release gets security fixes.
12
+
13
+ ## What I treat as a vulnerability
14
+
15
+ - A secret ends up in a cassette file when safetape should have stopped it, in a case the docs say is covered
16
+ - Loading a cassette through safetape can run code or read files it shouldn't
17
+ - Something in the build or release process that could let someone ship a tampered package
18
+
19
+ A secret format that safetape doesn't recognise yet is useful to know about too, but that can go in a normal issue. Just don't paste a real key, a made-up one in the same format is enough.