Skip to content

Reject pgoutput's required options without a value - #6708

Closed
Brar wants to merge 2 commits into
npgsql:mainfrom
Brar:repl/pgoutput-option-values
Closed

Brar wants to merge 2 commits into
npgsql:mainfrom
Brar:repl/pgoutput-option-values

Conversation

@Brar

@Brar Brar commented Oct 25, 2026

Copy link
Copy Markdown
Member

Depends on #6707, which isn't merged yet; its commit shows up here until it is.

Npgsql sends a logical replication option whose value is null as a bare key, e.g. START_REPLICATION SLOT s LOGICAL 0/0 ("proto_version"). For pgoutput's proto_version and publication_names, that crashes the walsender: pgoutput reads both values with strVal() without checking for a missing one (pgoutput.c:325 and :351 on REL_18_STABLE; the same on 10 through master), and the postmaster then terminates every other session of the server and runs crash recovery. A role with REPLICATION is enough; it doesn't have to be a superuser.

Starting pgoutput with one of these two options and a null value now throws an ArgumentException ("The pgoutput option '…' requires a value.") before anything is sent, so the connection stays usable. Other options and other plugins are unchanged: pgoutput reads its other options with defGetBoolean() (no value means true) or defGetString(), which raises a clean error, and test_decoding checks for a missing value itself.

This is reported to PostgreSQL as BUG #19795 (https://www.postgresql.org/message-id/flat/19795-6d7d506133b7f062%40postgresql.org). A patch that reads both options with defGetString() has been posted there but isn't committed yet. Servers without that fix keep crashing, which is why Npgsql checks.

How a real server gets there

Through the public low-level StartLogicalReplication(slot, cancellationToken, walLocation, options) in Npgsql.Replication.Internal, with a null value for one of the two options, e.g. from a generic options configuration. PgOutputReplicationOptions never produces this.

On PostgreSQL 18.6, main sent START_REPLICATION SLOT crash_slot LOGICAL 0/0 ("proto_version"), and the server logged "client backend … was terminated by exception 0xC0000005" and "terminating any other active server processes", then ran crash recovery. The crash reproduced on 14.24, 15.19, 16.15, 17.11 and 18.6 with both options (Windows builds), with proto_version also as a non-superuser REPLICATION role. origin without a value, as a control, gives the clean "origin requires a parameter" error.

Tests

  • Live: LogicalReplicationOptionTests.Pgoutput_options_without_required_value_are_rejected (proto_version, publication_names) expects the ArgumentException and then runs IDENTIFY_SYSTEM on the same connection. It names a slot that doesn't exist, so that without the check the server answers 42704 instead of crashing: red without this change (42704 instead of the ArgumentException), green with it, and the test server survives either way.

For the reviewer

  • No public API change.
  • START_REPLICATION doesn't name the plugin, so the check goes by the plugin name of the slot object the caller passes (pgoutput, compared case-insensitively). The option names are compared case-sensitively, as pgoutput does (strcmp, pgoutput.c:313 and :339).

Dependencies and merge order

Fixes #6693

Brar and others added 2 commits October 25, 2026 16:25
Npgsql sent slot and output plugin names unquoted, and wrapped option
values and publication names in quotes without escaping them:
- Valid slot names starting with a digit (e.g. "1abc") failed with a
  syntax error, because they lex as a number.
- A name like "x TEMPORARY" or "x WAIT" changed the command.
- Option values or publication names containing quotes caused syntax
  errors or subscribed to a different list of publications.

Slot and plugin names are now sent as quoted identifiers, after
removing surrounding whitespace and lowercasing A-Z, which is what the
server did with the unquoted names, so existing names keep their
meaning. Names that callers already wrapped in double quotes are passed
through unchanged. Option names are quoted identifiers and option values
quoted literals in which embedded quotes are doubled, the way
PostgreSQL's own clients do it since a75bd485b5e. publication_names
becomes a literal containing a list of quoted identifiers. Callers who
escaped quotes in option values or publication names themselves to work
around this must stop doing so.

Null option names are now rejected with an ArgumentException instead of
being sent as an empty quoted identifier. Physical CreateReplicationSlot
and ReadReplicationSlot throw ArgumentNullException for a null slot
name, which used to cause a server syntax error.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Starting pgoutput with proto_version or publication_names but no value
is now rejected with an ArgumentException: pgoutput dereferences the
missing value, which crashes the walsender and makes the postmaster
terminate all sessions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Recommendation

The narrow client-side guard is justified, but normalize the plugin name consistently with creation before merging. The inline finding is a reproducible bypass, and fixing it can reuse the existing helper rather than adding new plumbing. I reviewed this PR's incremental change separately from #6707.

Problem and expected approach

The server bug is real. Through the public low-level API, a plugin wrapper can supply a missing value:

await using var e = rc.StartLogicalReplication(
    new PgOutputReplicationSlot("existing_pgoutput_slot"),
    CancellationToken.None, options: [new("proto_version", null)])
    .GetAsyncEnumerator();
await e.MoveNextAsync();

On an affected server this crashes the walsender, not merely the command. I would expect a small pre-send check for the two affected pgoutput options, preserving valid bare options for other plugins. The PostgreSQL implementation confirms why those two missing values are different. The high-level PgOutputReplicationOptions API does not produce this input.

Alignment and gap

The check is correctly placed before sending anything, checks option names case-sensitively, and does not introduce a general plugin-validation layer. The tests deliberately use a nonexistent slot, which is sensible for keeping the shared test server safe. However, the plugin comparison does not follow #6707's normalization: pgoutput and "pgoutput" both create actual pgoutput slots, but a custom LogicalReplicationSlot retaining that same plugin argument bypasses this check. Add regression cases for those accepted spellings; no server metadata lookup is necessary.

Manual validation

Using a separate console client and an isolated PostgreSQL 18.4 cluster, I supplied each missing value to a real slot on the old code. Both caused SIGSEGV, termination of the other server processes, and crash recovery, as confirmed in the server log. At this PR's head, ordinary pgoutput was rejected with ArgumentException for both options and IDENTIFY_SYSTEM still worked on the same connection. A test_decoding bare boolean option still streamed normally. I then created slots through CreateLogicalReplicationSlot with pgoutput and "pgoutput", retaining each argument in a custom slot wrapper: the PR still crashed the dedicated server for those spellings. All 21 targeted tests passed, demonstrating that the current coverage misses this case. No shared PostgreSQL instance was used.

Posted by copilot

.Append(walLocation ?? slot.ConsistentPoint);

var opts = new List<KeyValuePair<string, string?>>(options ?? Array.Empty<KeyValuePair<string, string?>>());
var isPgOutput = string.Equals(slot.OutputPlugin, "pgoutput", StringComparison.OrdinalIgnoreCase);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[P2] Normalize the plugin name before identifying pgoutput

This raw comparison disagrees with the name handling introduced in #6707. CreateLogicalReplicationSlot(..., " pgoutput ") and CreateLogicalReplicationSlot(..., "\"pgoutput\"") both create an actual pgoutput slot, but a custom LogicalReplicationSlot initialized with that same plugin argument makes isPgOutput false here. The null-valued option is then sent despite the new guard.

I reproduced both variants through the public low-level API on an isolated PostgreSQL 18.4 server at this commit: creation succeeded, then starting with a null proto_version or publication_names caused SIGSEGV and restarted the server processes. This doesn't require the wrapper to misidentify the underlying plugin; it's the same name spelling accepted during creation.

Please reuse the existing canonicalization, e.g. compare ReplicationCommandBuilder.QuoteName(slot.OutputPlugin) with "\"pgoutput\"", and cover whitespace-padded and prequoted spellings in the guard tests. That keeps the fix small and avoids server metadata lookups.

Posted by copilot

@Brar Brar Nov 1, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed (now in #6707, ad02d776a): the check compares QuoteName(slot.OutputPlugin) with "pgoutput", still case-insensitively, and the test covers pgoutput, pgoutput and "pgoutput" with both options. The padded and the quoted spelling failed it before.

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not sure how #6707 and this are related - merge them to a single PR?

@Brar

Brar commented Nov 1, 2026 •

Copy link
Copy Markdown
Member Author

They were separate only because BUG #19795 wasn't public yet when the PRs went up. I've moved the check into #6707's review commit (ad02d776a), together with the fix for the inline comment, so I'm closing this one.

@Brar Brar closed this Nov 1, 2026
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.

Replication commands don't quote slot names, output plugin names and option values

2 participants