Skip to content

Commit

Permalink
Backport #49603 Fix unreported credentials exposure on connection lost
Browse files Browse the repository at this point in the history
  • Loading branch information
borysiasty authored and nyalldawson committed Sep 9, 2022
1 parent 925020c commit a7a9831
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/providers/postgres/qgspostgresconn.cpp
Expand Up @@ -595,6 +595,12 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
result = PQexec( query, true );
// NOTE: we intentionally continue if the query fails
// (for example because PostGIS is not installed)

if ( ! result.result() )
{
return false;
}

for ( int idx = 0; idx < result.PQntuples(); idx++ )
{
QString tableName = result.PQgetvalue( idx, 0 );
Expand Down
6 changes: 6 additions & 0 deletions src/providers/postgres/qgspostgresprojectstorage.cpp
Expand Up @@ -53,6 +53,12 @@ static bool _projectsTableExists( QgsPostgresConn &conn, const QString &schemaNa
.arg( QgsPostgresConn::quotedValue( tableName ), QgsPostgresConn::quotedValue( schemaName ) )
);
QgsPostgresResult res( conn.PQexec( sql ) );

if ( ! res.result() )
{
return false;
}

return res.PQgetvalue( 0, 0 ).toInt() > 0;
}

Expand Down
12 changes: 12 additions & 0 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -810,6 +810,12 @@ bool QgsPostgresProvider::loadFields()
// Get the table description
sql = QStringLiteral( "SELECT description FROM pg_description WHERE objoid=regclass(%1)::oid AND objsubid=0" ).arg( quotedValue( mQuery ) );
QgsPostgresResult tresult( connectionRO()->PQexec( sql ) );

if ( ! tresult.result() )
{
throw PGException( tresult );
}

if ( tresult.PQntuples() > 0 )
{
mDataComment = tresult.PQgetvalue( 0, 0 );
Expand Down Expand Up @@ -873,6 +879,12 @@ bool QgsPostgresProvider::loadFields()
tableoidsFilter );

QgsPostgresResult fmtFieldTypeResult( connectionRO()->PQexec( sql ) );

if ( ! fmtFieldTypeResult.result() )
{
throw PGException( fmtFieldTypeResult );
}

for ( int i = 0; i < fmtFieldTypeResult.PQntuples(); ++i )
{
Oid attrelid = fmtFieldTypeResult.PQgetvalue( i, 0 ).toUInt();
Expand Down
7 changes: 6 additions & 1 deletion src/providers/postgres/qgspostgresproviderconnection.cpp
Expand Up @@ -38,7 +38,7 @@ QgsPostgresProviderConnection::QgsPostgresProviderConnection( const QString &nam
mProviderKey = QStringLiteral( "postgres" );
// Remove the sql and table empty parts
const QRegularExpression removePartsRe { R"raw(\s*sql=\s*|\s*table=""\s*)raw" };
setUri( QgsPostgresConn::connUri( name ).uri().replace( removePartsRe, QString() ) );
setUri( QgsPostgresConn::connUri( name ).uri( false ).replace( removePartsRe, QString() ) );
setDefaultCapabilities();
}

Expand Down Expand Up @@ -285,6 +285,11 @@ QgsAbstractDatabaseProviderConnection::QueryResult QgsPostgresProviderConnection
}
}

if ( ! errCause.isEmpty() )
{
throw QgsProviderConnectionException( errCause );
}

const qlonglong numRows { res->PQntuples() };

if ( numRows > 0 )
Expand Down

0 comments on commit a7a9831

Please sign in to comment.