Repository navigation
fix: qualify pg_catalog.to_timestamp in bigint-to-timestamp migrations - #298
Conversation
alexaub-stripe
left a comment
There was a problem hiding this comment.
Fixing pg_catalog.to_timestamp makes sense, but I don't think the pinSearchPathBeforeExec feature should be implemented this way.
There was a problem hiding this comment.
Pull request overview
This PR hardens generated ALTER COLUMN ... USING expressions against search_path shadowing by schema-qualifying the to_timestamp builtin and by ensuring cast targets resolve to pg_catalog types rather than user-planted lookalikes.
Changes:
- Qualify the bigint→timestamp conversion to
pg_catalog.to_timestamp(...)and force the builtin overload via::pg_catalog.float8. - Replace
col::typewithCAST(col AS <qualified-type>), using a newschema.QualifyTypeForCasthelper to mapformat_typenames topg_catalogtypnames. - Add contract/unit tests and new acceptance tests that plant shadow objects in
publicto verify emitted DDL is notsearch_path-sensitive.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| pkg/diff/sql_generator.go | Updates type-transformation DDL generation to use pg_catalog.to_timestamp and CAST(... AS ...) with qualified cast targets. |
| pkg/diff/type_transformation_sql_test.go | Adds contract tests asserting emitted DDL uses qualified to_timestamp and avoids ::type casts in USING. |
| internal/schema/cast_type.go | Introduces QualifyTypeForCast mapping format_type output to schema-qualified pg_catalog typnames for safe CAST targets. |
| internal/schema/cast_type_test.go | Adds unit tests for QualifyTypeForCast mappings. |
| internal/migration_acceptance_tests/search_path_shadow_cases_test.go | Adds acceptance coverage proving migrations ignore shadowed public.to_timestamp(bigint) and public.integer domain traps. |
| internal/migration_acceptance_tests/column_cases_test.go | Updates expected plan DDL strings to reflect the new qualified to_timestamp/CAST output. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
alexaub-stripe
left a comment
There was a problem hiding this comment.
The to_timestamp change looks good: we're injecting an intentional pg_catalog conversion call in the bigint->timestamp case and we definitely want to use the system function.
The type scoping changes are less clear to me:
- Users should be able to use custom types in their schemas. I think the cast_type behavior breaks that as-is (see comment).
- The behavior in cast_type seems pretty coupled to postgres internals, and seems like it could break in surprising ways as postgres types evolve. If we wanted to do that properly, we should probably get a fully qualified type name at read time (pg-schema-diff/internal/queries/queries.sql#L66) and propagate that through the column diffs instead of assuming it's a pg_catalog type at alter time.
- I don't think typename's suffer the same issue as functions described in CVE-2018-1058: for functions a better matching public function will shadow the pg_catalog entry with the default search path. But for typenames that's not the case since there's no better match behavior: unless the user intentionally puts pg_catalog in their search path after public, postgres will check pg_catalog first and take that type over anything in public.
Based on that last point, is there any reason to scope typenames in these alter statements at all? Maybe we can just make the to_timestamp change and leave the USING behavior as-is?
|
@alexaub-stripe Per your review — reverted the cast qualification changes in 07b3322. Scope is now only the
Ready for another look when you have a moment. |
alexaub-stripe
left a comment
There was a problem hiding this comment.
Looks good! But one more ask on testing
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (1)
pkg/diff/sql_generator.go:1406
- PR description lists a contract test (e.g.
TestTypeTransformationQualifiesToTimestampBuiltin) inpkg/diff/..., but there doesn’t appear to be a unit/contract test covering this DDL generation path; only the acceptance case asserts the emitted DDL. Either add the described unit test to lock the generator behavior in place, or update the PR description/tests section to match what’s actually in the change.
if strings.EqualFold(oldType, "bigint") &&
strings.EqualFold(newType, "timestamp without time zone") {
return Statement{
DDL: fmt.Sprintf("%s SET DATA TYPE %s using pg_catalog.to_timestamp(%s::pg_catalog.float8 / 1000.0::pg_catalog.float8)",
csg.alterColumnPrefix(col),
newType,
schema.EscapeIdentifier(col.Name),
),
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (1)
internal/migration_acceptance_tests/column_cases_test.go:334
- Same issue in the new-schema DDL: the shadowed function is declared as
to_timestamp(bigint), but the migration's USING expression passes a float8/double-precision argument. Shadow thedouble precisionoverload so this acceptance case actually proves thepg_catalog.to_timestamp(...)qualification is necessary.
CREATE FUNCTION public.to_timestamp(bigint)
RETURNS timestamp without time zone LANGUAGE plpgsql AS $$
BEGIN
RAISE EXCEPTION 'pwnd';
END $$;
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
internal/migration_acceptance_tests/column_cases_test.go:340
rolesis only used to trigger a dedicated pgengine instance (and the role itself is never referenced in the DDL). That makes the test’s intent non-obvious and adds unnecessary global role creation work. Consider adding an inline comment explaining thatrolesis intentionally non-empty to isolate theALTER ROLE postgres SET search_path ...change (or refactor the harness to support explicit per-test engine isolation).
roles: []string{"shadow_test_role"},
alexaub-stripe
left a comment
There was a problem hiding this comment.
Looks great! Thank you for all the cycling
Qualify to_timestamp and float8 division in ALTER COLUMN USING to prevent search_path shadowing (CVE-2018-1058). Add acceptance tests for conversion and shadow-function resistance.
794276a to
b6305b1
Compare
Summary
When migrating a
bigintcolumn totimestamp without time zone, pg-schema-diff emits ato_timestamp()call in theALTER COLUMN ... USINGclause. An unqualified call resolves viasearch_path, so a user withCREATEon a schema can plant shadow functions that run during apply instead of the built-ins (CVE-2018-1058). On a default Postgres install,search_pathis"$user", public, pg_catalog—publicis already searched beforepg_catalog.Fix: emit a fully qualified expression:
The shadow acceptance case plants
public.to_timestamp(double precision)because the fixed emission passes float8 toto_timestamp(not bigint). That matches the overload that would win ifpg_catalog.qualification were removed while keeping the float8 casts. The original pre-fix emissionto_timestamp(col / 1000)uses bigint division and is covered by the "without fix" evidence below.Evidence of vulnerability without fix
Reverted
generateTypeTransformationStatementto pre-fix emission (to_timestamp(col / 1000)) and ran the shadow acceptance test locally (PostgreSQL 14.15 via pgengine, defaultsearch_path).Unqualified
to_timestamp()in the emittedUSINGclause invokes the planted shadow during apply.The validate case asserts qualified DDL in
expectedPlanDDL; without the fix the generator emitsusing to_timestamp("some_time_col" / 1000).Evidence of tests passing with fix
Restored the fix and re-ran both acceptance tests locally (PostgreSQL 14.23 via pgengine,
pg_dump14.23 with--restrict-key, defaultsearch_path):Both cases pass the full acceptance harness: plan → apply → pg_dump schema compare. Apply succeeds with no
ERROR: pwndin Postgres logs (the shadow is not invoked).CI
run_tests(PG 14, 15, 16, 17) passed on490295fand794276a.Tests
expectedPlanDDLfor qualified emission + ANALYZEpublic.to_timestamp(double precision)+ row data; apply succeeds only with the fixTest plan
public.to_timestamp(double precision)on default search_pathpwndwhen fix is reverted toto_timestamp(col / 1000)