Skip to content

fix: qualify pg_catalog.to_timestamp in bigint-to-timestamp migrations - #298

Merged
rajsuvariya-stripe merged 1 commit into
mainfrom
fix/pin-search-path-sat-28189
Jul 28, 2026
Merged

rajsuvariya-stripe merged 1 commit into
mainfrom
fix/pin-search-path-sat-28189

Conversation

@rajsuvariya-stripe

@rajsuvariya-stripe rajsuvariya-stripe commented Jul 20, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

When migrating a bigint column to timestamp without time zone, pg-schema-diff emits a to_timestamp() call in the ALTER COLUMN ... USING clause. An unqualified call resolves via search_path, so a user with CREATE on a schema can plant shadow functions that run during apply instead of the built-ins (CVE-2018-1058). On a default Postgres install, search_path is "$user", public, pg_catalog — public is already searched before pg_catalog.

Fix: emit a fully qualified expression:

pg_catalog.to_timestamp(
  col::pg_catalog.float8 OPERATOR(pg_catalog./) 1000.0::pg_catalog.float8
)

The shadow acceptance case plants public.to_timestamp(double precision) because the fixed emission passes float8 to to_timestamp (not bigint). That matches the overload that would win if pg_catalog. qualification were removed while keeping the float8 casts. The original pre-fix emission to_timestamp(col / 1000) uses bigint division and is covered by the "without fix" evidence below.

Evidence of vulnerability without fix

Reverted generateTypeTransformationStatement to pre-fix emission (to_timestamp(col / 1000)) and ran the shadow acceptance test locally (PostgreSQL 14.15 via pgengine, default search_path).

ERROR:  pwnd
STATEMENT:  ... using to_timestamp("some_time_col" / 1000);

    acceptance_test.go:182: ERROR: pwnd (SQLSTATE P0001)
--- FAIL: TestColumnTestCases/Change_BIGINT_to_TIMESTAMP_(ignores_shadowed_to_timestamp_in_USING)

Unqualified to_timestamp() in the emitted USING clause invokes the planted shadow during apply.

The validate case asserts qualified DDL in expectedPlanDDL; without the fix the generator emits using 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_dump 14.23 with --restrict-key, default search_path):

export PATH="/opt/homebrew/opt/postgresql@14/bin:$PATH"
go test ./internal/migration_acceptance_tests/... \
  -run 'TestColumnTestCases/Change_BIGINT_to_TIMESTAMP' -count=1 -v
=== RUN   TestColumnTestCases/Change_BIGINT_to_TIMESTAMP_(validate_conversion_and_ANALYZE)
=== RUN   TestColumnTestCases/Change_BIGINT_to_TIMESTAMP_(ignores_shadowed_to_timestamp_in_USING)
--- PASS: TestColumnTestCases (0.00s)
    --- PASS: TestColumnTestCases/Change_BIGINT_to_TIMESTAMP_(validate_conversion_and_ANALYZE) (1.92s)
    --- PASS: TestColumnTestCases/Change_BIGINT_to_TIMESTAMP_(ignores_shadowed_to_timestamp_in_USING) (1.92s)
PASS
ok  	github.com/stripe/pg-schema-diff/internal/migration_acceptance_tests	4.943s

Both cases pass the full acceptance harness: plan → apply → pg_dump schema compare. Apply succeeds with no ERROR: pwnd in Postgres logs (the shadow is not invoked).

CI run_tests (PG 14, 15, 16, 17) passed on 490295f and 794276a.

Tests

  • Change BIGINT to TIMESTAMP (validate conversion and ANALYZE) — expectedPlanDDL for qualified emission + ANALYZE
  • Change BIGINT to TIMESTAMP (ignores shadowed to_timestamp in USING) — planted public.to_timestamp(double precision) + row data; apply succeeds only with the fix
go test ./internal/migration_acceptance_tests/... \
  -run 'TestColumnTestCases/Change_BIGINT_to_TIMESTAMP' -count=1

Test plan

  • Standard bigint→timestamp case asserts plan DDL + ANALYZE
  • Shadow case verifies apply succeeds against planted public.to_timestamp(double precision) on default search_path
  • Verified shadow case fails with pwnd when fix is reverted to to_timestamp(col / 1000)
  • Local acceptance tests pass end-to-end on PG 14.23 (plan/apply/pgdump)
  • CI green on PG 14–17

@rajsuvariya-stripe
rajsuvariya-stripe marked this pull request as ready for review July 20, 2026 14:10

@alexaub-stripe alexaub-stripe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixing pg_catalog.to_timestamp makes sense, but I don't think the pinSearchPathBeforeExec feature should be implemented this way.

Comment thread cmd/pg-schema-diff/apply_cmd.go Resolved
Comment thread pkg/diff/sql_generator.go Resolved
@rajsuvariya-stripe rajsuvariya-stripe changed the title fix: pin search_path before executing generated DDL (SAT-28189) fix: qualify built-in calls and cast targets in type transformation DDL Jul 21, 2026
Comment thread internal/schema/cast_type.go Resolved
Comment thread internal/schema/cast_type.go Resolved
Copilot AI review requested due to automatic review settings July 21, 2026 08:15

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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::type with CAST(col AS <qualified-type>), using a new schema.QualifyTypeForCast helper to map format_type names to pg_catalog typnames.
  • Add contract/unit tests and new acceptance tests that plant shadow objects in public to verify emitted DDL is not search_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.

Comment thread internal/schema/cast_type.go Resolved
Comment thread internal/schema/cast_type_test.go Resolved
Copilot AI review requested due to automatic review settings July 21, 2026 08:42

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.

jtayal-stripe
jtayal-stripe previously approved these changes Jul 21, 2026

@jtayal-stripe jtayal-stripe left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM !!

@alexaub-stripe alexaub-stripe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Comment thread internal/schema/cast_type.go Resolved
Copilot AI review requested due to automatic review settings July 22, 2026 08:48
@rajsuvariya-stripe rajsuvariya-stripe changed the title fix: qualify built-in calls and cast targets in type transformation DDL fix: qualify pg_catalog.to_timestamp in bigint-to-timestamp migrations (SAT-28189) Jul 22, 2026
@rajsuvariya-stripe

Copy link
Copy Markdown
Collaborator Author

@alexaub-stripe Per your review — reverted the cast qualification changes in 07b3322. Scope is now only the pg_catalog.to_timestamp fix for the bigint→timestamp migration.

  • Removed QualifyTypeForCast / CAST(... AS pg_catalog.*); generic transforms back to col::type
  • Removed TestGenericCastMigrationUsesQualifiedTargetType
  • Unqualified SET DATA TYPE integer left as-is (typename shadowing != function shadowing under CVE-2018-1058)

Ready for another look when you have a moment.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

Comment thread pkg/diff/sql_generator.go Resolved

@alexaub-stripe alexaub-stripe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! But one more ask on testing

Copilot AI review requested due to automatic review settings July 23, 2026 15:16

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Copilot AI review requested due to automatic review settings July 23, 2026 15:22

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) in pkg/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),
			),

Copilot AI review requested due to automatic review settings July 23, 2026 15:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 the double precision overload so this acceptance case actually proves the pg_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 $$;

Copilot AI review requested due to automatic review settings July 23, 2026 15:46

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

@rajsuvariya-stripe rajsuvariya-stripe changed the title fix: qualify pg_catalog.to_timestamp in bigint-to-timestamp migrations (SAT-28189) fix: qualify pg_catalog.to_timestamp in bigint-to-timestamp migrations Jul 23, 2026
Copilot AI review requested due to automatic review settings July 23, 2026 16:09

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Copilot AI review requested due to automatic review settings July 23, 2026 16:13

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment thread pkg/diff/sql_generator.go Resolved
Copilot AI review requested due to automatic review settings July 24, 2026 07:04

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  • roles is 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 that roles is intentionally non-empty to isolate the ALTER ROLE postgres SET search_path ... change (or refactor the harness to support explicit per-test engine isolation).
		roles: []string{"shadow_test_role"},

Copilot AI review requested due to automatic review settings July 28, 2026 05:49

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@alexaub-stripe alexaub-stripe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@rajsuvariya-stripe
rajsuvariya-stripe force-pushed the fix/pin-search-path-sat-28189 branch from 794276a to b6305b1 Compare July 28, 2026 14:55
@rajsuvariya-stripe
rajsuvariya-stripe merged commit 0e16b30 into main Jul 28, 2026
12 checks passed
@rajsuvariya-stripe
rajsuvariya-stripe deleted the fix/pin-search-path-sat-28189 branch July 28, 2026 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants