|
| 1 | +"""PyJWT instrumentation for REPLAY mode. |
| 2 | +
|
| 3 | +Patches PyJWT to disable all verification during test replay: |
| 4 | +1. _merge_options - returns all verification options as False |
| 5 | +2. _verify_signature - no-op (defense in depth) |
| 6 | +3. _validate_claims - no-op (defense in depth) |
| 7 | +
|
| 8 | +Only active in REPLAY mode. |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +import logging |
| 14 | +from types import ModuleType |
| 15 | + |
| 16 | +from ...core.types import TuskDriftMode |
| 17 | +from ..base import InstrumentationBase |
| 18 | + |
| 19 | +logger = logging.getLogger(__name__) |
| 20 | + |
| 21 | + |
| 22 | +class PyJWTInstrumentation(InstrumentationBase): |
| 23 | + """Patches PyJWT to disable verification in REPLAY mode.""" |
| 24 | + |
| 25 | + def __init__(self, mode: TuskDriftMode = TuskDriftMode.DISABLED, enabled: bool = True) -> None: |
| 26 | + self.mode = mode |
| 27 | + should_enable = enabled and mode == TuskDriftMode.REPLAY |
| 28 | + |
| 29 | + super().__init__( |
| 30 | + name="PyJWTInstrumentation", |
| 31 | + module_name="jwt", |
| 32 | + supported_versions="*", |
| 33 | + enabled=should_enable, |
| 34 | + ) |
| 35 | + |
| 36 | + def patch(self, module: ModuleType) -> None: |
| 37 | + if self.mode != TuskDriftMode.REPLAY: |
| 38 | + return |
| 39 | + |
| 40 | + self._patch_merge_options() |
| 41 | + self._patch_signature_verification() |
| 42 | + self._patch_claim_validation() |
| 43 | + logger.debug("[PyJWTInstrumentation] All patches applied") |
| 44 | + |
| 45 | + def _patch_signature_verification(self) -> None: |
| 46 | + """No-op signature verification.""" |
| 47 | + try: |
| 48 | + from jwt import api_jws |
| 49 | + |
| 50 | + def patched_verify_signature(self, *args, **kwargs): |
| 51 | + logger.debug("[PyJWTInstrumentation] _verify_signature called - skipping verification") |
| 52 | + return None |
| 53 | + |
| 54 | + api_jws.PyJWS._verify_signature = patched_verify_signature |
| 55 | + logger.debug("[PyJWTInstrumentation] Patched PyJWS._verify_signature") |
| 56 | + except Exception as e: |
| 57 | + logger.warning(f"[PyJWTInstrumentation] Failed to patch _verify_signature: {e}") |
| 58 | + |
| 59 | + def _patch_claim_validation(self) -> None: |
| 60 | + """No-op claim validation.""" |
| 61 | + try: |
| 62 | + from jwt import api_jwt |
| 63 | + |
| 64 | + def patched_validate_claims(self, *args, **kwargs): |
| 65 | + logger.debug("[PyJWTInstrumentation] _validate_claims called - skipping validation") |
| 66 | + return None |
| 67 | + |
| 68 | + api_jwt.PyJWT._validate_claims = patched_validate_claims |
| 69 | + logger.debug("[PyJWTInstrumentation] Patched PyJWT._validate_claims") |
| 70 | + except Exception as e: |
| 71 | + logger.warning(f"[PyJWTInstrumentation] Failed to patch _validate_claims: {e}") |
| 72 | + |
| 73 | + def _patch_merge_options(self) -> None: |
| 74 | + """Patch _merge_options to always return disabled verification options.""" |
| 75 | + try: |
| 76 | + from jwt import api_jwt |
| 77 | + |
| 78 | + disabled_options = { |
| 79 | + "verify_signature": False, |
| 80 | + "verify_exp": False, |
| 81 | + "verify_nbf": False, |
| 82 | + "verify_iat": False, |
| 83 | + "verify_aud": False, |
| 84 | + "verify_iss": False, |
| 85 | + "verify_sub": False, |
| 86 | + "verify_jti": False, |
| 87 | + "require": [], |
| 88 | + "strict_aud": False, |
| 89 | + } |
| 90 | + |
| 91 | + def patched_merge_options(self, options=None): |
| 92 | + logger.debug("[PyJWTInstrumentation] _merge_options called - returning disabled options") |
| 93 | + return disabled_options |
| 94 | + |
| 95 | + api_jwt.PyJWT._merge_options = patched_merge_options |
| 96 | + logger.debug("[PyJWTInstrumentation] Patched PyJWT._merge_options") |
| 97 | + except Exception as e: |
| 98 | + logger.warning(f"[PyJWTInstrumentation] Failed to patch _merge_options: {e}") |
0 commit comments