Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(data-imports): raise a clear error when source config is still encrypted (#68829)

Tom Owersb66275d13fba
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/common/config.py b/products/warehouse_sources/backend/temporal/data_imports/sources/common/config.py
index ecc24006f594..8bf77384390e 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/common/config.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/common/config.py
@@ -121,6 +121,30 @@ def _noop_convert(x: typing.Any) -> typing.Any:
return x
+# Fernet tokens always start with this marker (version byte 0x80 + timestamp, base64-encoded).
+# A stored config value that still carries it never got decrypted upstream.
+_ENCRYPTED_SECRET_PREFIX = "gAAAAA"
+
+
+class UndecryptedConfigError(ValueError):
+ """Raised when a config value reaches conversion still Fernet-encrypted.
+
+ Upstream decryption of the stored job inputs failed (e.g. the key that wrote them is no
+ longer in the keychain), so the raw token would otherwise crash the field converter with an
+ opaque, secret-leaking error such as ``invalid literal for int() with base 10: 'gAAAAA...'``.
+ """
+
+
+def _convert_value(
+ convert: typing.Callable[[typing.Any], typing.Any], value: typing.Any, field_name: str
+) -> typing.Any:
+ if isinstance(value, str) and value.startswith(_ENCRYPTED_SECRET_PREFIX):
+ raise UndecryptedConfigError(
+ f"Config field '{field_name}' is still encrypted; the stored credentials could not be decrypted"
+ )
+ return convert(value)
+
+
@dataclasses.dataclass
class MetaConfig:
"""Class used to store metadata used for config."""
@@ -258,7 +282,7 @@ def to_config(
except KeyError:
continue
else:
- inputs[field.name] = convert(value)
+ inputs[field.name] = _convert_value(convert, value, field.name)
break
field_type_meta: MetaConfig | None = _try_get_meta(config_type)
@@ -302,7 +326,7 @@ def to_config(
except KeyError:
continue
else:
- inputs[field.name] = convert(value)
+ inputs[field.name] = _convert_value(convert, value, field.name)
return config_cls(**inputs)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/common/test/test_config.py b/products/warehouse_sources/backend/temporal/data_imports/sources/common/test/test_config.py
index eb00fd2c3818..bffa26fd51c6 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/common/test/test_config.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/common/test/test_config.py
@@ -97,6 +97,37 @@ def test_optional_int_converter_handles_numeric_via_from_dict(value, expected):
assert cfg.port == expected
+# A representative Fernet token, as it appears when `job_inputs` decryption fails upstream and the
+# raw ciphertext flows into config parsing. Not a real credential.
+_UNDECRYPTED_TOKEN = (
+ "gAAAAABqTJ0OKLTREfglaXr0DZrJ4eYT7GVcKeA0avLvEzY7k3ll0JuD8VpL--DGJ9Dbt9y3KwyAolvAXit1ceqyuOzwyjTqlg=="
+)
+
+
+ "config_dict",
+ [
+ # The reported crash: an int-converted `port` that never got decrypted would raise the
+ # opaque `invalid literal for int() with base 10: 'gAAAAA...'`.
+ {"host": "db.example.com", "port": _UNDECRYPTED_TOKEN},
+ # A plain string field left encrypted must also fail here, not pass ciphertext downstream.
+ {"host": _UNDECRYPTED_TOKEN, "port": "5432"},
+ ],
+)
+def test_from_dict_raises_clear_error_on_undecrypted_secret(config_dict):
+ """Still-encrypted job inputs must fail with a clear error that does not echo the token."""
+
+ @config.config
+ class TestConfig(config.Config):
+ host: str
+ port: int = config.value(converter=int)
+
+ with pytest.raises(config.UndecryptedConfigError) as exc_info:
+ TestConfig.from_dict(config_dict)
+
+ assert _UNDECRYPTED_TOKEN not in str(exc_info.value)
+
+
def test_nested_to_config_with_flat_dict():
"""Test `config.to_config` with a nested set of classes.