Skip to content

Only explain sampled Postgres statements that are a single SQL statement - #24839

Open
azhou-datadog wants to merge 6 commits into
masterfrom
allen.zhou/vuln-92306-clean
Open

Only explain sampled Postgres statements that are a single SQL statement#24839
azhou-datadog wants to merge 6 commits into
masterfrom
allen.zhou/vuln-92306-clean

Conversation

@azhou-datadog

@azhou-datadog azhou-datadog commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

The PREPARE that explains a parameterized query now goes out over the extended query protocol, where the server refuses a multi-command string outright instead of executing every statement in it.

  • A pipeline is the only route to that protocol here. psycopg leaves the simple protocol only for a query carrying parameters, one requesting binary results — which the pool's ClientCursor refuses — or one inside a pipeline.
  • It fails closed. Pipeline mode needs libpq 14+; without it the query goes unexplained rather than falling back to the simple protocol, and the check warns once at init. Agent 7 latest links libpq 18.3, so that branch should never be reached in a shipped Agent.
  • Verified on Postgres 16. A planted SELECT 1; CREATE TEMP TABLE …; --$1 is refused with cannot insert multiple commands into a prepared statement, and the table never appears. That is an integration test; the suite is 773 passed, 4 skipped.

Motivation

Text sampled from pg_stat_activity.query is interpolated into PREPARE dd_<signature> AS <statement> and run as the monitoring user over the simple query protocol, which executes every ;-separated statement. Any user who can query a monitored database can plant one and have the agent run it with the monitoring role's privileges. The feature is on by default.

https://datadoghq.atlassian.net/browse/VULN-92306

Review checklist (to be filled by reviewers)

  • Feature or bugfix MUST have appropriate tests (unit, integration, e2e)
  • Add qa/required if this PR needs QA validation, or qa/skip-qa if it does not. Exactly one of the two is required.
  • If you need to backport this PR to another branch, you can add the backport/<branch-name> label to the PR and it will automatically open a backport PR once this one is merged

Sampled statement text is read out of pg_stat_activity and interpolated into a
PREPARE that the agent executes as the monitoring user, using the simple query
protocol. Anyone able to run a query on a monitored database could therefore
plant statement separators in the text and have the agent run them with the
monitoring role's privileges.

Reject any sampled statement that is not unambiguously one top-level statement,
before either explain path touches the database, and again at the PREPARE sink
itself. Text that cannot be tokenized with certainty (unterminated literals or
comments, a backslash in a plain string whose meaning depends on
standard_conforming_strings) is refused rather than guessed at.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@cit-pr-commenter-54b7da

cit-pr-commenter-54b7da Bot commented Aug 12, 2026

Copy link
Copy Markdown

evalya-impact-summary

evalya impact analysis
Impact analysis: 0 selected, 0 skipped (of 0 test tasks)
Publish tasks:   0 (always emitted)
Diff (4 files):
  postgres/changelog.d/24839.security
  postgres/datadog_checks/postgres/explain_parameterized_queries.py
  postgres/datadog_checks/postgres/util.py
  postgres/tests/test_explain_parameterized_queries.py

Debug a specific task: evalya plan impact --path <path> --task <task>

Learn more about CI impact filtering

@datadog-official

datadog-official Bot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Tests  Code Coverage

🎉 All green!

🧪 All tests passed
❄️ No new flaky tests detected

🚧 1 test that failed was ignored due to quarantine View in Datadog

🎯 Code Coverage (details)
Patch Coverage: 93.88%
Overall Coverage: 94.17% (+5.72%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: c21a78b | Docs | Datadog PR Page | Give us feedback!

azhou-datadog and others added 2 commits August 12, 2026 10:46
psycopg only leaves the simple query protocol, which executes every
;-separated statement in a string, when a query carries parameters, asks for
binary results or runs in a pipeline. The pool's client-side cursors reject
binary results, so a pipeline is the only route: wrap the PREPARE in one, and
the server itself refuses a multi-command string. Guarded by a libpq 14+
capability check, since pipeline mode needs it.

This puts the server rather than the single-statement scan in charge of the
boundary. The scan stays: it reports a clean error code instead of a syntax
error, and it is the only guard where libpq is older than 14.

Also trims the comments and docstrings this branch added, and drops a stale
example from the pre-existing datatype_mismatch comment: a multi-statement
string sent through datadog.explain_statement raises InvalidCursorDefinition,
not a datatype mismatch.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The pipeline makes the server reject sampled text holding more than one
statement, so the hand-written tokenizer was a second, weaker implementation of
the same rule, and the only thing it still covered was a build whose libpq is
too old for pipeline mode.

Fail closed there instead: without pipeline support the query goes unexplained,
with one warning at init, rather than falling back to the simple query protocol.
Explaining a parameterized query is worth less than the guarantee that sampled
text never runs.

Also reverts the test_failed_explain_handling expectation. That statement has no
$1, so it takes the datadog.explain_statement path, which rejects a multi-query
plan on its own and still reports InvalidCursorDefinition.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3719462adc

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread postgres/tests/test_explain_parameterized_queries.py
azhou-datadog and others added 2 commits August 12, 2026 14:06
test_execute_prepare_uses_a_pipeline asserted the pipeline branch while taking
_can_use_pipeline from whatever libpq the machine links, so it would fail rather
than skip on a build older than 14. Set it explicitly, as the fail-closed test
already does.

The integration test had the same dependence. Its injection assertion holds
either way -- fail closed sends nothing -- so only the error-class assertion is
now conditional.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@dd-octo-sts

dd-octo-sts Bot commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Validation Report

All 21 validations passed.

Show details
Validation Description Status
agent-reqs Verify check versions match the Agent requirements file
ci Validate CI configuration and code coverage settings
codeowners Validate every integration has a CODEOWNERS entry
config Validate default configuration files against spec.yaml
dep Verify dependency pins are consistent and Agent-compatible
http Validate integrations use the HTTP wrapper correctly
imports Validate check imports do not use deprecated modules
integration-style Validate check code style conventions
jmx-metrics Validate JMX metrics definition files and config
labeler Validate PR labeler config matches integration directories
legacy-signature Validate no integration uses the legacy Agent check signature
license-headers Validate Python files have proper license headers
licenses Validate third-party license attribution list
metadata Validate metadata.csv metric definitions
models Validate configuration data models match spec.yaml
openmetrics Validate OpenMetrics integrations disable the metric limit
package Validate Python package metadata and naming
qa-label Validate the pull request declares whether it needs QA for the next Agent release
readmes Validate README files have required sections
saved-views Validate saved view JSON file structure and fields
version Validate version consistency between package and changelog

View full run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants