Skip to content

fix(security): pg_dump without a shell, its password out of argv; the Electron backup path quoted (0.7.12) - #53

Merged
jddunn merged 1 commit into
masterfrom
fix/pg-exporter-no-shell
Oct 16, 2026
Merged

jddunn merged 1 commit into
masterfrom
fix/pg-exporter-no-shell

Conversation

@jddunn

@jddunn jddunn commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

Reported by trojanos-7a: PostgresExporter ran pg_dump "<connection string>" --file="<path>" through execSync, so exportToFile('/tmp/x"; touch /tmp/pwned; echo "') ran touch, and a connection string holding a quote or $(...) did the same; the password showed in ps while pg_dump ran.

  • pg_dump starts with execFile and an argument array: each value is one argument no shell reads. A postgres:// URL loses its password before it becomes --dbname, and the password travels in PGPASSWORD. A keyword/value string is passed whole (documented: use a URL to keep the password out of the process list).
  • The Electron main adapter's cloud backup doubled no quotes in its VACUUM INTO path, so a path with an apostrophe broke every backup; it now escapes them as SqliteFileExporter does.
  • Tests: the invocation (hostile path as one --file argument, the password only in PGPASSWORD), execFile called with no shell option (child_process mocked), a hostile path against CI's PostgreSQL writing the literal file and no stray one, and a backup under a folder named O'Brien.
  • Version 0.7.12 with its CHANGELOG section, so the merge publishes it.

Summary by Sourcery

Harden PostgreSQL exports and Electron cloud backups against unsafe or quoted file and connection paths.

Bug Fixes:

  • Prevent shell command injection in PostgreSQL exports by invoking pg_dump without a shell and keeping URL passwords out of process arguments.
  • Escape apostrophes in Electron cloud backup paths so VACUUM INTO succeeds for paths containing quotes.

Enhancements:

  • Expose a reusable PostgreSQL dump invocation builder for consistent argument and environment handling.

Tests:

  • Add coverage for safe PostgreSQL dump invocation, password handling, hostile paths, and Electron backups under paths containing apostrophes.

Chores:

  • Bump the package and changelog to version 0.7.12.

…t of its arguments; quote the Electron backup path

PostgresExporter built pg_dump's command as a shell string, so an output path or connection string holding a quote or $(...) ran a command, and the password showed in the process list. execFile with an argument array now carries each value as one argument, and a URL's password goes in PGPASSWORD. The Electron main adapter's VACUUM INTO left its path's quotes unescaped, so a path with an apostrophe broke every backup. Tests: the invocation's arguments and environment, execFile called without a shell, a hostile path against a real PostgreSQL server writing no stray file, and a backup under an O'Brien folder. Released as 0.7.12.
@sourcery-ai

sourcery-ai Bot commented Oct 16, 2026

Copy link
Copy Markdown

Sorry @jddunn, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 6 hours and 26 minutes by commenting @sourcery-ai review. Upgrade to get a review now.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-16T23:21:35.000954Z 22ff2a0 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Oct 16, 2026

Copy link
Copy Markdown

Warning

Review limit reached

  • Run on-demand review

This review includes 6 billable files and costs up to $1.50.

  • Ask an admin to make reviews automatic

Open in CodeRabbit

Reviews can continue after your included limit without a manual trigger. An admin must approve usage-based billing.

Or wait 19 minutes for your next included review.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available. Your 91 included PR review attempts over the past 7 days set your current allowance at 1 review per hour.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: Repository: framerslab/sql-storage-adapter/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Team
  • Run ID: 7666572c-b698-4737-807f-16288dc9194a
📥 Commits

Reviewing files that changed from the base of the PR and between 2086684 and 22ff2a0.

📒 Files selected for processing: 6
  • CHANGELOG.md
  • package.json
  • src/adapters/electron/electronMainAdapter.ts
  • src/exporters/PostgresExporter.ts
  • tests/electronMainAdapter.backup.spec.ts
  • tests/postgresExporter.spec.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@sourcery-ai

sourcery-ai Bot commented Oct 16, 2026

Copy link
Copy Markdown

Reviewer's Guide

This release removes shell-based pg_dump invocation by passing arguments through execFile and relocating PostgreSQL URL passwords to PGPASSWORD, hardens Electron VACUUM INTO path quoting, and validates both fixes with unit, integration, and end-to-end tests.

Sequence diagram for shell-free PostgreSQL export

sequenceDiagram
    participant Exporter as PostgresExporter
    participant Invocation as pgDumpInvocation
    participant Process as pg_dump

    Exporter->>Invocation: pgDumpInvocation(connectionString, outputPath)
    Invocation-->>Exporter: file, args, env
    Exporter->>Process: execFile(file, args, {env})
    Note over Process: URL password is in PGPASSWORD, paths remain one argument
    Process-->>Exporter: dump output or completion
Loading

Sequence diagram for quoted Electron cloud backup

sequenceDiagram
    participant Adapter as ElectronMainAdapter
    participant SQLite as innerAdapter
    participant Cloud as backup.provider

    Adapter->>Adapter: backupPath.replace(/'/g, "''")
    Adapter->>SQLite: exec(VACUUM INTO quoted backupPath)
    SQLite-->>Adapter: backup created
    Adapter->>Cloud: upload(backupPath, remotePath)
    Cloud-->>Adapter: upload complete
Loading

File-Level Changes

Change Details Files
Reworked PostgreSQL dump execution to eliminate shell interpretation and keep URL passwords out of command-line arguments.
  • Added a reusable invocation builder that produces an executable, argument array, and inherited environment.
  • Parses PostgreSQL URLs, moves decoded passwords to PGPASSWORD, and removes them from --dbname.
  • Replaced synchronous shell-command execution with asynchronous execFile calls for file and byte exports.
  • Added unit, mocked-process, and optional integration coverage for hostile arguments, password handling, and shell absence.
src/exporters/PostgresExporter.ts
tests/postgresExporter.spec.ts
Escaped apostrophes in Electron cloud-backup SQLite paths before embedding them in VACUUM INTO SQL.
  • Doubles single quotes in the backup path SQL literal.
  • Added an end-to-end backup test using a directory containing an apostrophe.
src/adapters/electron/electronMainAdapter.ts
tests/electronMainAdapter.backup.spec.ts
Published the security and backup fixes as version 0.7.12.
  • Incremented the package version.
  • Documented the security and backup behavior in the changelog.
package.json
CHANGELOG.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Summary by Qodo

Secure PostgreSQL exports and fix quoted Electron backup paths

🐞 Bug fix 🧪 Tests 🕐 20-40 Minutes

Grey Divider

AI Description

• Run pg_dump without a shell and keep URL passwords out of process arguments.
• Escape apostrophes in Electron cloud-backup paths so backups succeed.
• Add security and backup regression tests; release the fixes as 0.7.12.
Diagram

graph TD
  Exporter["PostgresExporter"] --> Invocation["Invocation builder"] --> ExecFile["execFile"] --> PgDump["pg_dump"] --> Result["Dump result"]
  Electron["Electron backup"] --> Vacuum["VACUUM INTO"] --> Cloud["Cloud provider"]
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Stream pg_dump with spawn
  • ➕ Avoids execFile's output-buffer limit and reduces buffering for large file exports.
  • ➖ Requires explicit stream, process-exit, and error handling beyond this targeted security fix.

Recommendation: Keep the execFile approach for this fix: it removes shell interpretation in both export methods with limited behavioral change. Consider spawn separately if large dumps or the output-buffer limit become a practical concern.

Files changed (6) +194 / -12

Bug fix (2) +44 / -11
electronMainAdapter.tsEscape apostrophes in cloud-backup paths +2/-2

Escape apostrophes in cloud-backup paths

• Doubles apostrophes in the path passed to VACUUM INTO, allowing backups from directories with apostrophes.

src/adapters/electron/electronMainAdapter.ts

PostgresExporter.tsLaunch pg_dump without a shell +42/-9

Launch pg_dump without a shell

• Replaces shell command strings with execFile argument arrays for file and byte exports. The new invocation builder removes passwords from PostgreSQL URLs and passes them through PGPASSWORD; keyword/value connection strings remain whole arguments and may still expose embedded passwords.

src/exporters/PostgresExporter.ts

Tests (2) +141 / -0
electronMainAdapter.backup.spec.tsCover backups beneath an apostrophe-containing directory +52/-0

Cover backups beneath an apostrophe-containing directory

• Creates a SQLite database under an O'Brien-named temporary directory and verifies that its backup is created and uploaded.

tests/electronMainAdapter.backup.spec.ts

postgresExporter.spec.tsCover safe pg_dump arguments and hostile paths +89/-0

Cover safe pg_dump arguments and hostile paths

• Checks argument and environment construction, mocks execFile to verify shell-free execution for both export methods, and conditionally tests a hostile output path against PostgreSQL.

tests/postgresExporter.spec.ts

Documentation (1) +8 / -0
CHANGELOG.mdDocument the 0.7.12 security and backup fixes +8/-0

Document the 0.7.12 security and backup fixes

• Adds release notes for shell-free pg_dump execution, URL password handling, and escaped Electron backup paths.

CHANGELOG.md

Other (1) +1 / -1
package.jsonBump the package version to 0.7.12 +1/-1

Bump the package version to 0.7.12

• Advances the published package version for the fixes.

package.json

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (0) 📎 Requirement gaps (0) 🎨 UX issues (0) 🔗 Cross-repo conflicts (0) 📜 Skill insights (0)

Grey Divider


Action required

1. URL query passwords remain visible in ps 🐞 Bug ⛨ Security
Description
pgDumpInvocation removes only url.password, leaving a password query parameter in the URL
assigned to dbname. When an exporter receives a connection string such as
postgresql://user@host/db?password=secret, both export methods pass that secret in the --dbname
process argument.
Code

src/exporters/PostgresExporter.ts[27]

+    dbname = url.toString();
Evidence
The exporter accepts a connection string unchanged, but the new sanitization reads only the URL
userinfo password. It serializes the remaining query into --dbname, which both export methods give
to execFile as a process argument.

src/core/contracts/features.ts[25-30]
src/exporters/PostgresExporter.ts[18-31]
src/exporters/PostgresExporter.ts[48-62]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
PostgreSQL URLs can supply a password through a `password` query parameter. `pgDumpInvocation` removes only the userinfo password, so a query password remains visible in the `--dbname` argument.
## Fix Focus Areas
- src/exporters/PostgresExporter.ts[21-29]
- tests/postgresExporter.spec.ts[14-31]
## Recommended Fix
Extract a query-supplied password into `PGPASSWORD` and remove that parameter from the URL before constructing `--dbname`. Preserve the effective credential when both query and userinfo passwords are present, and test that neither password appears in the argument array.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Tip of the day
💡 Did you know, you can pick a display mode instead of tweaking every Display option by hand

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

env.PGPASSWORD = decodeURIComponent(url.password);
url.password = '';
}
dbname = url.toString();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Action required

1. Url query passwords remain visible in ps 🐞 Bug ⛨ Security

pgDumpInvocation removes only url.password, leaving a password query parameter in the URL
assigned to dbname. When an exporter receives a connection string such as
postgresql://user@host/db?password=secret, both export methods pass that secret in the --dbname
process argument.
Agent Prompt
## Issue description
PostgreSQL URLs can supply a password through a `password` query parameter. `pgDumpInvocation` removes only the userinfo password, so a query password remains visible in the `--dbname` argument.

## Fix Focus Areas
- src/exporters/PostgresExporter.ts[21-29]
- tests/postgresExporter.spec.ts[14-31]

## Recommended Fix
Extract a query-supplied password into `PGPASSWORD` and remove that parameter from the URL before constructing `--dbname`. Preserve the effective credential when both query and userinfo passwords are present, and test that neither password appears in the argument array.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

@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: 22ff2a0cdb

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +23 to +25
if (url.password !== '') {
env.PGPASSWORD = decodeURIComponent(url.password);
url.password = '';

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Remove query-string passwords from pg_dump arguments

For a valid libpq URI such as postgresql://user@host/db?password=secret, the password is stored in url.searchParams, not url.password, so this branch leaves it in --dbname and visible in the process list. Because libpq permits connection parameters in the URI query string, extract and remove the password query parameter and pass it through PGPASSWORD as well.

Useful? React with 👍 / 👎.

const env: NodeJS.ProcessEnv = { ...process.env };
let dbname = connectionString;
if (/^postgres(?:ql)?:\/\//i.test(connectionString)) {
const url = new URL(connectionString);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve valid multi-host PostgreSQL URIs

When an exporter receives a valid multi-host URI with per-host ports, such as postgresql://user:secret@host1:5432,host2:5433/db, this new URL call throws TypeError: Invalid URL before pg_dump starts. libpq explicitly supports comma-separated host/port pairs, and the previous implementation passed these strings through unchanged, so password extraction needs to preserve libpq's broader URI grammar rather than require WHATWG URL parsing.

Useful? React with 👍 / 👎.

@codecov

codecov Bot commented Oct 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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.

1 participant