Skip to content

fix(mongodb): connect to exactly the validated host - #10473

Merged
waleedlatif1 merged 2 commits into
stagingfrom
fix/mongodb-uri-ssrf
Nov 28, 2026
Merged

waleedlatif1 merged 2 commits into
stagingfrom
fix/mongodb-uri-ssrf

Conversation

@waleedlatif1

Copy link
Copy Markdown
Collaborator

Summary

  • validateDatabaseHost now accepts exactly one hostname or IP literal and refuses anything with , / @ ? # : (outside an IPv6 literal), brackets around a non-IPv6 value, whitespace, or an IPv6 zone id, before any DNS lookup. Previously it resolved the whole string, so with a wildcard DNS zone a value like 127.0.0.1:6379,db.attacker.example passed the check while the MongoDB driver split it into a seed list and connected to every seed; literal IP seeds never go through the pinned lookup, so loopback/private addresses were reachable
  • MongoDB client only puts the validated host and port into the connection string; credentials, authSource and TLS go through the driver options object, so database/authSource can no longer append or duplicate connection-string options (directConnection, proxyHost/proxyPort, tls, authMechanism, ...)
  • MongoDB client sets directConnection: true so the driver never follows replica-set members a server advertises, validates the port as an integer in 1–65535, and checks the database name against MongoDB's naming rules. authSource still defaults to the database when credentials are given
  • SSH and SFTP pass the trimmed host to the validator (they already connected with the trimmed value)
  • Sibling tools that call validateDatabaseHost (PostgreSQL, MySQL, MSSQL, Neo4j, ClickHouse, Redis, SMTP, IMAP, SFTP, SSH, Oracle CONNECT proxy) pass the host as a single driver field or into a URL built after validation and connect to the pinned resolved IP; the stricter shape check closes the same multi-host/userinfo shapes for Neo4j and ClickHouse, which interpolate the host into a URI

Behavior change: a MongoDB host that is a replica-set secondary now connects to that node only instead of discovering the primary, so writes need the primary's host.

Type of Change

  • Bug fix

Testing

  • lib/core/security/input-validation.server.test.ts: seed lists, userinfo, path/options, fragment, port suffix, bracketed name, zone id and whitespace are refused before resolution (resolver mocked to answer like a wildcard zone); DNS names, underscore service names, trailing-dot names, IPv4 and bare/bracketed IPv6 are accepted
  • lib/internal/mongodb/client.test.ts against the real MongoClient option parser (only connect/close stubbed): one host, directConnection: true, no proxy, TLS and credentials from options; seed-list and option-injecting hosts, option-injecting/invalid database names, and out-of-range ports are refused before connecting
  • Red proof: with the pre-fix sources restored, 9/9 new validator refusal cases and 9/12 client cases fail (including the seed-list host and the proxyHost database, which connected on the old code); all pass with the fix
  • Sibling suites (lib/internal/{ssh,sftp,redis,mysql,postgresql,mssql,neo4j,clickhouse,smtp,oracledb,mongodb}, lib/imap, lib/core/security): 51 files pass
  • bun run generate, bun run lint, bun run type-check, bun run check:audits

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing (new tests pass the test-audit authoring gate)
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

validateDatabaseHost now refuses anything but a single hostname or IP literal before resolving it, so a seed list, userinfo, port, path or options can no longer pass the address check while the driver connects elsewhere. The MongoDB client builds its connection string from the validated host and port only, passes credentials, authSource and TLS as driver options, sets directConnection so server-advertised members are never followed, and validates the port and database name.
@vercel

vercel Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
docs Skipped Skipped Nov 28, 2026 11:33am UTC

Request Review

@greptile-apps

greptile-apps Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5 · The PR appears safe to merge with the documented single-node connection behavior.RetriggerView in GreptileConfidence Score: 5/5

[High impact] The PR appears safe to merge with the documented single-node connection behavior.

Summary

This PR keeps MongoDB connections tied to the checked host.

  • Connection tools accept only one checked host.
  • MongoDB connects only to the validated node.

waleedlatif1 explicitly documented that connecting to a secondary no longer discovers the primary; writes require the primary’s host.

Tests were inspected, not run during this review.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Connection settings] --> B[Check port and database name]
  B --> C[Require one hostname or IP]
  C --> D[Check resolved addresses]
  D --> E[Pin lookup to checked IP]
  E --> F[MongoDB client]
  G[Credentials and TLS options] --> F
  F --> H[Connect only to supplied host]
Loading

Reviews (1) · Last reviewed commit: "fix(mongodb): connect to exactly the val..." · Reviewed by Greptile

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 6 files

This PR changes authentication, authorization, or input validation. Ultrareviews find 2.4x more serious bugs than standard reviews. Comment @cubic-dev-ai ultrareview to run one.

Turn on auto-fix | Re-trigger cubic

Comment thread apps/sim/lib/internal/mongodb/client.ts Resolved
@waleedlatif1
waleedlatif1 merged commit f5575bd into staging Nov 28, 2026
71 checks passed
@waleedlatif1
waleedlatif1 deleted the fix/mongodb-uri-ssrf branch November 28, 2026 11:34

This branch was previously deployed

1 inactive deployment
Preview — a1a4a684 Deployed Nov 28, 2026 by vercel[bot]
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