: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(data-imports): fail source config parsing with a clear, non-leaking error (#81397)
Tom Owersd0cc04b94eac
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.pyindex fa0cac1725d4..29f864bac3fc 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@@ -136,6 +136,15 @@ class UndecryptedConfigError(ValueError):"""+class ConfigValueError(ValueError):+ """Raised when a config field's value can't be converted to its declared type.++ A converter (e.g. ``int``) failing on user-supplied input would otherwise crash with an+ opaque, value-leaking builtin error such as ``invalid literal for int() with base 10: 'lakjsd'``.+ We name the field but never its value, so nothing a user typed reaches logs or error tracking.+ """++def _convert_value(convert: typing.Callable[[typing.Any], typing.Any], value: typing.Any, field_name: str) -> typing.Any:@@ -143,7 +152,10 @@ def _convert_value(raise UndecryptedConfigError(f"Config field '{field_name}' is still encrypted; the stored credentials could not be decrypted")- return convert(value)+ try:+ return convert(value)+ except (ValueError, TypeError) as e:+ raise ConfigValueError(f"Config field '{field_name}' has a value that could not be parsed") from e@dataclasses.dataclass@@ -210,6 +222,21 @@ def validate_config(if not is_valid:errors.extend(nested_errors)+ elif field_meta and field_meta.converter != _noop_convert:+ # Scalar field with a converter: presence is checked above, but a value that can't be+ # converted to the declared type (e.g. `port: "lakjsd"`) would otherwise slip through and+ # crash later in `to_config`. Run the converter now so it surfaces as a validation error.+ if field_flat_key in d:+ field_key = field_flat_key+ elif field_nested_key in d:+ field_key = field_nested_key+ else:+ field_key = field.name+ try:+ _convert_value(field_meta.converter, d[field_key], field.name)+ except (ValueError, TypeError):+ errors.append(f"Field '{field.name}' has an invalid value")+return len(errors) == 0, errorsdiff --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.pyindex 7d569a3ebfca..bd2bd3dcacc6 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@@ -151,6 +151,38 @@ def test_from_dict_raises_clear_error_on_undecrypted_secret(config_dict):assert _UNDECRYPTED_TOKEN not in str(exc_info.value)+def test_from_dict_raises_clear_error_on_unconvertible_value():+ """A converter failing on bad user input must fail with a clear error that names the field but+ not the value, instead of the opaque, value-leaking `invalid literal for int()` crash."""++ @config.config+ class TestConfig(config.Config):+ host: str+ port: int = config.value(converter=int)++ with pytest.raises(config.ConfigValueError) as exc_info:+ TestConfig.from_dict({"host": "db.example.com", "port": "not-a-number"})++ assert "port" in str(exc_info.value)+ assert "not-a-number" not in str(exc_info.value)+++def test_validate_dict_rejects_unconvertible_value():+ """`validate_dict` must flag a value that can't convert to the declared type rather than pass and+ defer the crash to `to_config`. The reported error came from a value that cleared validation."""++ @config.config+ class TestConfig(config.Config):+ host: str+ port: int = config.value(converter=int)++ is_valid, errors = TestConfig.validate_dict({"host": "db.example.com", "port": "not-a-number"})++ assert is_valid is False+ assert len(errors) == 1+ assert "not-a-number" not in errors[0]++def test_nested_to_config_with_flat_dict():"""Test `config.to_config` with a nested set of classes.