-
-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Automatic sanitization of sensitive data in the output (#1842)
* feat: Automatic output sanitization to obscure sensitive data by default Ref: #1794 * test: more stable test * test: CLI test * test: reuse setup code * test: more tests * chore: support pytest * chore: mask config * chore: mask config * chore: do not use hooks * docs: update * chore: naming
- Loading branch information
1 parent
34f2bcd
commit bde2ec7
Showing
23 changed files
with
926 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -217,6 +217,7 @@ User's Guide | |
contrib | ||
stateful | ||
how | ||
sanitizing | ||
compatibility | ||
examples | ||
graphql | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
.. _sanitizing-output: | ||
|
||
Sanitizing Output | ||
================= | ||
|
||
Schemathesis automatically sanitizes sensitive data in both the generated test case and the received response to prevent accidental exposure of sensitive information. | ||
This feature replaces certain headers, cookies, and other fields that could contain sensitive data with the string ``[Filtered]``. | ||
|
||
.. note:: | ||
Schemathesis does not sanitize sensitive data in response bodies due to the challenge of preserving the original formatting of the payload. | ||
|
||
You can control this feature through the ``--sanitize-output`` CLI option: | ||
|
||
.. code-block:: bash | ||
schemathesis run --sanitize-output=false ... | ||
Or in Python tests: | ||
|
||
.. code-block:: python | ||
schema = schemathesis.from_dict({...}, sanitize_output=False) | ||
Disabling this option will turn off the automatic sanitization of sensitive data in the output. | ||
|
||
For more advanced customization of the sanitization process, you can define your own sanitization configuration and pass it to the ``configure`` function. | ||
Here's how you could do it: | ||
|
||
.. code-block:: python | ||
import schemathesis | ||
# Create a custom config | ||
custom_config = ( | ||
schemathesis.sanitization.Config(replacement="[Custom]") | ||
.with_keys_to_sanitize("X-Customer-ID") | ||
.with_sensitive_markers("address") | ||
) | ||
# Configure Schemathesis to use your custom sanitization configuration | ||
schemathesis.sanitization.configure(custom_config) | ||
This will sanitize the ``X-Customer-ID`` headers (case-insensitive), and any fields containing the substring "address" (case-insensitive) in their names, with the string "[Custom]" in the generated test case and the received response. | ||
|
||
This will sanitize the ``X-Customer-ID`` headers, and any fields containing the substring "address" in their names, with the string "[Custom]" in the generated test case and the received response. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from dataclasses import dataclass | ||
|
||
from ..runner import events | ||
from ..sanitization import sanitize_serialized_check, sanitize_serialized_interaction | ||
from .handlers import EventHandler, ExecutionContext | ||
|
||
|
||
@dataclass | ||
class SanitizationHandler(EventHandler): | ||
def handle_event(self, context: ExecutionContext, event: events.ExecutionEvent) -> None: | ||
if isinstance(event, events.AfterExecution): | ||
for check in event.result.checks: | ||
sanitize_serialized_check(check) | ||
for interaction in event.result.interactions: | ||
sanitize_serialized_interaction(interaction) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.