Skip to content

Automate browser based workflows with AI

[Oncall] [SKY-8859] Log malformed auth token shape without changing auth behavior (#5458)

Marc Kelechavaa6dadc4290dc
diff --git a/skyvern/forge/sdk/services/org_auth_service.py b/skyvern/forge/sdk/services/org_auth_service.py
index 8412f3724..e9e3a030c 100644
--- a/skyvern/forge/sdk/services/org_auth_service.py
+++ b/skyvern/forge/sdk/services/org_auth_service.py
@@ -23,6 +23,14 @@ LOG = structlog.get_logger()
AUTHENTICATION_TTL = 60 * 60 # one hour
CACHE_SIZE = 128
ALGORITHM = "HS256"
+_SAFE_JWT_ERROR_REASONS = {
+ "Not enough segments",
+ "Invalid payload padding",
+ "Invalid header padding",
+ "Invalid crypto padding",
+ "Signature verification failed",
+ "Invalid header string: must be a json object",
+}
@dataclass
@@ -32,6 +40,110 @@ class ApiKeyValidationResult:
token: OrganizationAuthToken
+def _normalize_api_key_with_flags(raw_api_key: str) -> tuple[str, dict[str, bool]]:
+ normalized = raw_api_key
+ flags = {
+ "api_key_had_whitespace_padding": False,
+ "api_key_had_bearer_prefix": False,
+ "api_key_had_outer_quotes": False,
+ }
+
+ def _strip_and_track(value: str) -> str:
+ stripped = value.strip()
+ if stripped != value:
+ flags["api_key_had_whitespace_padding"] = True
+ return stripped
+
+ # At most three wrapper layers matter here: whitespace, Bearer prefix, outer quotes.
+ for _ in range(3):
+ updated = normalized
+
+ updated = _strip_and_track(updated)
+
+ if updated[:7].lower() == "bearer ":
+ flags["api_key_had_bearer_prefix"] = True
+ updated = _strip_and_track(updated[7:])
+
+ if len(updated) >= 2 and updated[0] == updated[-1] and updated[0] in {"'", '"'}:
+ flags["api_key_had_outer_quotes"] = True
+ updated = _strip_and_track(updated[1:-1])
+
+ if updated == normalized:
+ break
+ normalized = updated
+
+ return normalized, flags
+
+
+def _get_api_key_debug_fields(
+ raw_api_key: str | None, normalized_api_key: str | None, flags: dict[str, bool] | None
+) -> dict[str, bool | int | str | None]:
+ if raw_api_key is None or normalized_api_key is None or flags is None:
+ return {
+ "api_key_original_length": None,
+ "api_key_normalized_length": None,
+ "api_key_raw_segment_count": None,
+ "api_key_normalized_segment_count": None,
+ "api_key_had_whitespace_padding": None,
+ "api_key_had_bearer_prefix": None,
+ "api_key_had_outer_quotes": None,
+ "api_key_was_normalized": None,
+ "normalized_api_key_decodes": None,
+ "normalized_api_key_would_be_expired": None,
+ "normalized_api_key_error_type": None,
+ "normalized_api_key_error_reason": None,
+ }
+
+ debug_fields: dict[str, bool | int | str | None] = {
+ "api_key_original_length": len(raw_api_key),
+ "api_key_normalized_length": len(normalized_api_key),
+ "api_key_raw_segment_count": raw_api_key.count(".") + 1 if raw_api_key else 0,
+ "api_key_normalized_segment_count": normalized_api_key.count(".") + 1 if normalized_api_key else 0,
+ "api_key_had_whitespace_padding": flags["api_key_had_whitespace_padding"],
+ "api_key_had_bearer_prefix": flags["api_key_had_bearer_prefix"],
+ "api_key_had_outer_quotes": flags["api_key_had_outer_quotes"],
+ "api_key_was_normalized": normalized_api_key != raw_api_key,
+ "normalized_api_key_decodes": None,
+ "normalized_api_key_would_be_expired": None,
+ "normalized_api_key_error_type": None,
+ "normalized_api_key_error_reason": None,
+ }
+ if not normalized_api_key or normalized_api_key == raw_api_key:
+ return debug_fields
+
+ try:
+ payload = jwt.decode(
+ normalized_api_key,
+ settings.SECRET_KEY,
+ algorithms=[ALGORITHM],
+ # Diagnostic only: determine whether the token shape is valid regardless of expiry.
+ options={"verify_exp": False},
+ )
+ api_key_data = TokenPayload(**payload)
+ debug_fields["normalized_api_key_decodes"] = True
+ debug_fields["normalized_api_key_would_be_expired"] = api_key_data.exp < time.time()
+ # Diagnostic code should never change the main 403 path.
+ except Exception as exc:
+ debug_fields["normalized_api_key_decodes"] = False
+ debug_fields["normalized_api_key_error_type"] = type(exc).__name__
+ debug_fields["normalized_api_key_error_reason"] = _get_safe_auth_error_reason(exc)
+
+ return debug_fields
+
+
+def _get_safe_auth_error_reason(exc: Exception) -> str:
+ if isinstance(exc, ValidationError):
+ locations = [tuple(error["loc"]) for error in exc.errors()]
+ return f"{exc.error_count()} validation error(s): {locations}"
+
+ if isinstance(exc, PyJWTError):
+ message = str(exc)
+ if message in _SAFE_JWT_ERROR_REASONS:
+ return message
+
+ return type(exc).__name__
+
+
async def get_current_org(
x_api_key: Annotated[
str | None,
@@ -199,8 +311,25 @@ async def resolve_org_from_api_key(
options={"verify_exp": False},
)
api_key_data = TokenPayload(**payload)
- except (PyJWTError, ValidationError):
- LOG.error("Error decoding JWT", exc_info=True)
+ except (PyJWTError, ValidationError) as exc:
+ try:
+ if x_api_key is None:
+ normalized_api_key = None
+ normalization_flags = None
+ else:
+ normalized_api_key, normalization_flags = _normalize_api_key_with_flags(x_api_key)
+ api_key_debug_fields = _get_api_key_debug_fields(x_api_key, normalized_api_key, normalization_flags)
+ LOG.error(
+ "Error decoding JWT",
+ error_type=type(exc).__name__,
+ error_reason=_get_safe_auth_error_reason(exc),
+ **api_key_debug_fields,
+ )
+ except Exception as diagnostic_exc:
+ LOG.warning(
+ "Diagnostic helper failed during JWT error logging",
+ diagnostic_error_type=type(diagnostic_exc).__name__,
+ )
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Could not validate credentials",
diff --git a/tests/unit/test_org_auth_service.py b/tests/unit/test_org_auth_service.py
new file mode 100644
index 000000000..82997c872
--- /dev/null
+++ b/tests/unit/test_org_auth_service.py
@@ -0,0 +1,177 @@
+from types import SimpleNamespace
+
+import jwt
+import pytest
+from fastapi import HTTPException
+
+from skyvern.config import settings
+from skyvern.forge.sdk.core.security import create_access_token
+from skyvern.forge.sdk.services import org_auth_service
+from skyvern.forge.sdk.services.org_auth_service import (
+ _get_api_key_debug_fields,
+ _normalize_api_key_with_flags,
+)
+
+
+def test_normalize_api_key_strips_whitespace() -> None:
+ raw_api_key = " token.value.parts \n"
+ normalized, flags = _normalize_api_key_with_flags(raw_api_key)
+ debug_fields = _get_api_key_debug_fields(raw_api_key, normalized, flags)
+
+ assert normalized == "token.value.parts"
+ assert debug_fields["api_key_had_whitespace_padding"] is True
+ assert debug_fields["api_key_was_normalized"] is True
+
+
+def test_normalize_api_key_strips_outer_quotes() -> None:
+ raw_api_key = '"token.value.parts"'
+ normalized, flags = _normalize_api_key_with_flags(raw_api_key)
+ debug_fields = _get_api_key_debug_fields(raw_api_key, normalized, flags)
+
+ assert normalized == "token.value.parts"
+ assert debug_fields["api_key_had_outer_quotes"] is True
+ assert debug_fields["api_key_was_normalized"] is True
+
+
+def test_normalize_api_key_strips_bearer_prefix() -> None:
+ raw_api_key = "Bearer token.value.parts"
+ normalized, flags = _normalize_api_key_with_flags(raw_api_key)
+ debug_fields = _get_api_key_debug_fields(raw_api_key, normalized, flags)
+
+ assert normalized == "token.value.parts"
+ assert debug_fields["api_key_had_bearer_prefix"] is True
+ assert debug_fields["api_key_normalized_segment_count"] == 3
+
+
+def test_normalize_api_key_handles_quoted_bearer_value() -> None:
+ raw_api_key = '"Bearer token.value.parts"'
+ normalized, flags = _normalize_api_key_with_flags(raw_api_key)
+ debug_fields = _get_api_key_debug_fields(raw_api_key, normalized, flags)
+
+ assert normalized == "token.value.parts"
+ assert debug_fields["api_key_had_bearer_prefix"] is True
+ assert debug_fields["api_key_had_outer_quotes"] is True
+
+
+def test_normalize_api_key_tracks_whitespace_removed_after_wrapper_stripping() -> None:
+ raw_api_key = 'Bearer " token.value.parts "'
+ normalized, flags = _normalize_api_key_with_flags(raw_api_key)
+ debug_fields = _get_api_key_debug_fields(raw_api_key, normalized, flags)
+
+ assert normalized == "token.value.parts"
+ assert debug_fields["api_key_had_whitespace_padding"] is True
+ assert debug_fields["api_key_had_bearer_prefix"] is True
+ assert debug_fields["api_key_had_outer_quotes"] is True
+
+
+def test_debug_fields_report_no_shadow_decode_for_unchanged_value() -> None:
+ raw_api_key = "token.value.parts"
+ normalized, flags = _normalize_api_key_with_flags(raw_api_key)
+ debug_fields = _get_api_key_debug_fields(raw_api_key, normalized, flags)
+
+ assert normalized == "token.value.parts"
+ assert debug_fields["api_key_was_normalized"] is False
+ assert debug_fields["normalized_api_key_decodes"] is None
+ assert debug_fields["normalized_api_key_would_be_expired"] is None
+ assert debug_fields["normalized_api_key_error_type"] is None
+
+
+def test_debug_fields_show_when_normalized_token_would_decode(monkeypatch) -> None:
+ token = create_access_token("o_test")
+ monkeypatch.setattr(org_auth_service.time, "time", lambda: 0)
+ raw_api_key = f"Bearer {token}"
+ normalized, flags = _normalize_api_key_with_flags(raw_api_key)
+ debug_fields = _get_api_key_debug_fields(raw_api_key, normalized, flags)
+
+ assert debug_fields["api_key_had_bearer_prefix"] is True
+ assert debug_fields["normalized_api_key_decodes"] is True
+ assert debug_fields["normalized_api_key_would_be_expired"] is False
+ assert debug_fields["normalized_api_key_error_type"] is None
+
+
+def test_debug_fields_show_when_normalized_token_still_fails() -> None:
+ raw_api_key = '"Bearer definitely-not-a-jwt"'
+ normalized, flags = _normalize_api_key_with_flags(raw_api_key)
+ debug_fields = _get_api_key_debug_fields(raw_api_key, normalized, flags)
+
+ assert normalized == "definitely-not-a-jwt"
+ assert debug_fields["normalized_api_key_decodes"] is False
+ assert debug_fields["normalized_api_key_error_type"] == "DecodeError"
+ assert debug_fields["normalized_api_key_error_reason"] == "Not enough segments"
+
+
+def test_normalize_api_key_handles_empty_string() -> None:
+ raw_api_key = ""
+ normalized, flags = _normalize_api_key_with_flags(raw_api_key)
+ debug_fields = _get_api_key_debug_fields(raw_api_key, normalized, flags)
+
+ assert normalized == ""
+ assert debug_fields["api_key_raw_segment_count"] == 0
+ assert debug_fields["normalized_api_key_decodes"] is None
+
+
+def test_normalize_api_key_handles_single_character() -> None:
+ raw_api_key = '"'
+ normalized, flags = _normalize_api_key_with_flags(raw_api_key)
+ debug_fields = _get_api_key_debug_fields(raw_api_key, normalized, flags)
+
+ assert normalized == '"'
+ assert debug_fields["api_key_had_outer_quotes"] is False
+ assert debug_fields["normalized_api_key_decodes"] is None
+
+
+def test_debug_fields_reports_validation_error_for_missing_claims() -> None:
+ raw_api_key = f"Bearer {jwt.encode({}, settings.SECRET_KEY, algorithm='HS256')}"
+ normalized, flags = _normalize_api_key_with_flags(raw_api_key)
+ debug_fields = _get_api_key_debug_fields(raw_api_key, normalized, flags)
+
+ assert debug_fields["normalized_api_key_decodes"] is False
+ assert debug_fields["normalized_api_key_error_type"] == "ValidationError"
+ assert debug_fields["normalized_api_key_error_reason"] == "2 validation error(s): [('sub',), ('exp',)]"
+
+
+def test_debug_fields_handles_none_inputs() -> None:
+ debug_fields = _get_api_key_debug_fields(None, None, None)
+
+ assert debug_fields["api_key_original_length"] is None
+ assert debug_fields["normalized_api_key_decodes"] is None
+ assert debug_fields["normalized_api_key_error_type"] is None
+ assert debug_fields["normalized_api_key_error_reason"] is None
+
+
+async def test_resolve_org_from_api_key_logs_decode_error_reason(monkeypatch) -> None:
+ logged: dict[str, object] = {}
+
+ def fake_error(_message: str, **kwargs: object) -> None:
+ logged.update(kwargs)
+
+ monkeypatch.setattr(org_auth_service.LOG, "error", fake_error)
+
+ with pytest.raises(HTTPException) as exc_info:
+ await org_auth_service.resolve_org_from_api_key("definitely-not-a-jwt", SimpleNamespace(), ())
+
+ assert exc_info.value.status_code == 403
+ assert logged["error_type"] == "DecodeError"
+ assert logged["error_reason"] == "Not enough segments"
+
+
+async def test_resolve_org_from_api_key_returns_403_when_diagnostic_helper_fails(monkeypatch) -> None:
+ warnings: dict[str, object] = {}
+
+ def fake_warning(_message: str, **kwargs: object) -> None:
+ warnings.update(kwargs)
+
+ monkeypatch.setattr(org_auth_service.LOG, "warning", fake_warning)
+
+ def fail_helper(*_args: object, **_kwargs: object) -> object:
+ raise RuntimeError("boom")
+
+ monkeypatch.setattr(org_auth_service, "_get_api_key_debug_fields", fail_helper)
+
+ with pytest.raises(HTTPException) as exc_info:
+ await org_auth_service.resolve_org_from_api_key("definitely-not-a-jwt", SimpleNamespace(), ())
+
+ assert exc_info.value.status_code == 403
+ assert warnings["diagnostic_error_type"] == "RuntimeError"