Repository navigation
fix: pin search_path when extracting schemas and applying plans - #318
Draft
jtayal-stripe wants to merge 1 commit into
Draft
jtayal-stripe wants to merge 1 commit into
jtayal-stripe wants to merge 1 commit into
Conversation
Postgres deparse functions such as pg_get_indexdef omit the schema of any object visible on the session's search_path, and apply ran plans with the default search_path. A role able to create objects in a schema on that path (typically public) could plant a better-matching overload, such as public.array_length(text[], integer), between planning and apply, so a planned built-in call resolved to attacker code executed by the migrating role. Schema extraction now runs with search_path = pg_catalog so every non-built-in reference is schema-qualified, and apply and plan validation run with the same pinned path, so unqualified names can only resolve to built-ins. Function bodies are stored verbatim rather than deparsed, so, like pg_dump, apply also disables check_function_bodies. GetSchema now fetches on a single connection because search_path is a per-session setting. Co-authored-by: Cursor <cursoragent@cursor.com> Committed-By-Agent: cursor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Pins
search_pathtopg_catalogin two places so that a migration plan cannot resolve to objects created after it was generated:internal/schema/schema.go,GetSchema). Postgres deparse functions (pg_get_indexdef,pg_get_expr,pg_get_constraintdef,pg_get_triggerdef, ...) omit the schema of any object that is visible on the session'ssearch_path. Fetching withsearch_path = pg_catalogmakes every non-built-in reference schema-qualified (for examplepublic.normalize_email(email)). The caller'ssearch_pathis restored afterwards.cmd/pg-schema-diff/apply_cmd.gorunPlan, and plan validation inpkg/diff/plan_generator.go). Statements run withSET SESSION search_path = pg_catalog, so the remaining unqualified names can only resolve to built-ins.Both halves are needed. Pinning only at apply breaks any schema that references objects in
public(user functions, extension operator classes such aspg_trgm'sgin_trgm_ops). Pinning only at extraction leaves unqualified built-ins shadowable.Two supporting changes:
check_function_bodies = falseduring apply and validation. Function bodies are stored verbatim rather than deparsed, so aLANGUAGE sqlbody such asINSERT INTO users ...would fail itsCREATE-time check under the pinnedsearch_path.pg_dumpuses the same setting for the same reason. Run-time behavior of functions is unchanged, and the target schema's bodies were already checked when its DDL was loaded into the temporary database.GetSchemareads on a single connection.search_pathis a per-session setting, so a*sql.DBis no longer queried with up to 50 concurrent goroutines. Results are identical; extraction of very large schemas may be slower.The README "Applying plan" example now shows both
SETstatements for library users who apply plans themselves.Motivation
The CLI
applydisplays a plan, waits for confirmation, and then executes it on a new connection with the defaultsearch_path. A role that can create objects in a schema on that path (typicallypublic) can, during that window, create an exact-type overload of a built-in referenced by the plan, for examplepublic.array_length(text[], integer)for an index onarray_length(tags, 1). Postgres prefers the best type match across all visible schemas (pg_catalogbeing implicitly first only breaks ties), so the planned statement binds to the planted function and runs it with the privileges of the migrating role.This follows the same approach as
pg_dump(CVE-2018-1058) and extends #298, which qualified the tool-authoredpg_catalog.to_timestampcall.Testing
All tests run in the repository's
build/Dockerfile.testimage with-race.New tests
TestCmdTestSuite/TestRunPlanIgnoresFunctionsPlantedOnSearchPathgenerates a plan that adds an index onarray_length(tags, 1), then plantspublic.array_length(text[], integer)(which writes to a log table), then callsrunPlan. It asserts that the index has no dependency on the planted function and that the function never ran.TestSearchPathTestCasesadds two acceptance cases: an expression index on a user function inpublic, and a GIN index usinggin_trgm_opsfrompg_trgminstalled inpublic.Without the fix (Postgres 14), the race test fails on both assertions: the index is bound to the planted function and the function executed during the index build.
With only the apply-side pin (extraction unchanged), both acceptance cases fail, showing they guard the extraction half:
With the fix
Full suite passes on Postgres 14 and 17 (
go test -race ./...). No acceptance-testexpectedPlanDDLchanged. The only updated expectations are ininternal/schema/schema_test.go, where extracted definitions gain apublic.qualifier (for examplenextval('public.foo_id_seq'::regclass),EXECUTE FUNCTION public.increment_version()) and two stored schema hashes change accordingly.