Repository navigation
Description
Several call sites in src/ hand text that is not a literal, such as libpq's error message or a column name, to cpp11::stop() or cpp11::warning() as the format string. RPostgres does not define CPP11_USE_FMT, so cpp11 passes that string to Rf_errorcall() or Rf_warningcall() as a printf format. Any % in a database name, table or column name, or file name is interpreted: %x prints stack garbage, %s reads arbitrary memory and can crash R (rendering this reprex with %s crashed the R session), and %n writes to memory. RSQLite fixed the same class of bug in r-dbi/RSQLite#869.
Severity: high (proposed, Kirill may change)
Reprex
Run against CRAN RPostgres 1.4.10 with %x so that it does not crash:
library(DBI)
packageVersion("RPostgres")
#> [1] '1.4.10'
# Expected: database "db%x%x" does not exist
try(dbConnect(RPostgres::Postgres(), dbname = "db%x%x"))
#> Error : connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL: database "db343183900" does not exist
con <- dbConnect(RPostgres::Postgres())
# Expected: relation "tbl%x%x" does not exist
try(dbGetQuery(con, 'SELECT * FROM "tbl%x%x"'))
#> Error : Failed to prepare query : ERROR: relation "tbl343138e00" does not exist
#> LINE 1: SELECT * FROM "tbl462c260462c2d0"
#> ^
dbDisconnect(con)Created on 2026-11-29 with reprex v2.1.1
The original report pointed at the type-conflict warning in DbColumn::warn_type_conflicts(). I could not make that warning fire: a column's data type comes from its OID and stays fixed, and statements with different column types stop earlier with "Multiple queries must use the same column types." The call is still unsafe as written and is fixed along with the others, but the reprex uses the error path, which every failing query goes through.
Call sites
Pinned to 41cd4be:
-
src/DbColumn.cpp#L79:cpp11::warning(ss.str()), the message includes the column name. -
src/DbConnection.cpp#L36:cpp11::stop(err)withPQerrorMessage()after a failed connect, which includes the host and database names. -
src/DbConnection.cpp#L109:cpp11::warning(std::string(errbuf))with the error buffer fromPQcancel(). -
src/DbConnection.cpp#L135:cpp11::stop(PQerrorMessage(pConn_))inimport_lo_from_file(), the message includes the file name. -
src/DbConnection.cpp#L143: the same inexport_lo_to_file(). -
src/DbConnection.cpp#L291:conn_stop(),cpp11::stop(std::string(msg) + " : " + PQerrorMessage(conn)). Every server error goes through here, also viaPqResultImpl::conn_stop(), and server messages quote identifiers and values from the query.
Checked and safe, because the text is a constant without %: the std::string(...) wrappers in src/DbConnection.cpp#L69, #L103, #L190, src/connection.cpp#L30, #L36, src/PqResultImpl.cpp#L428, and src/encode.cpp#L115, which appends Rf_type2char(). No Rf_warning(), Rf_error(), Rprintf(), REprintf() or warningcall calls exist outside src/vendor, and PQresultErrorMessage() is not used.
- Add tests that check that hostile names appear verbatim in the error text.
Activity
fix: Never pass column names or server messages as a printf format (r…