Skip to content

fix(neo4j): connect TLS sessions to the validated address - #10481

Merged
waleedlatif1 merged 3 commits into
stagingfrom
fix/neo4j-tls-pinned-host
Nov 28, 2026
Merged

waleedlatif1 merged 3 commits into
stagingfrom
fix/neo4j-tls-pinned-host

Conversation

@waleedlatif1

@waleedlatif1 waleedlatif1 commented Nov 28, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Self-hosted Neo4j with encryption enabled (bolt+s) validated the host, then let the driver connect by name, so the name was resolved a second time at connect. A name that answers a public IP at validation and a private or loopback IP at connect (DNS rebinding) steered the TCP connect inward. Certificate verification stopped it short of a session, but the refused-vs-TLS-error difference still worked as an internal port probe.
  • neo4j-driver 6.3 has no supported way to pin a direct bolt+s connection: resolver only feeds neo4j:// routing, and the direct channel calls tls.connect(port, address.resolvedHost()) with resolvedHost() equal to the URL host. Pinning to the IP in the URL would drop SNI and verify the certificate against the IP.
  • Encrypted self-hosted connections now go through a per-connection loopback relay: the driver speaks plain Bolt to 127.0.0.1:<ephemeral>, and the relay opens TLS to the validated IP with SNI set to the configured host, rejectUnauthorized: true, and checkServerIdentity against that host. The certificate check is the same system CA store and hostname rule the driver applied before, but it runs against the pinned address. No globals are patched and no driver internals are used.
  • Plain bolt still connects straight to the validated IP (IPv6 bracketed). Aura (*.databases.neo4j.io) stays on the driver's neo4j+s routing by name on purpose: those hosts and the routing table they return are operated by Neo4j, not chosen by the caller.
  • createNeo4jDriver is now openNeo4jConnection, returning { driver, close } so the relay closes with the driver, on abort included. A relay-side failure (refused, timeout, untrusted or mismatched certificate) comes back in the driver's usual "Failed to connect to server ... Caused by: ..." shape.

Residual: the relay accepts on loopback for the life of one operation (at most 4 accepted connections). Another local process could connect to it in that window, but it only reaches the user's already validated public host, which it could reach directly anyway. Bolt between the driver and the relay is plaintext on loopback, inside the same process.

Type of Change

  • Bug fix

Testing

  • lib/internal/neo4j/client.integration.ts (new, integration layer) runs a real loopback TLS server, with neo4j.invalid (never resolves) validated to 127.0.0.1:
    • the server receives a TLS session with SNI neo4j.invalid and the Bolt magic 6060b017
    • a trusted certificate for a different name is refused before any Bolt byte arrives, and the result is still the driver's Neo4jError (ServiceUnavailable) with the altnames cause
    • Both tests fail on the pre-fix client (no connection, ENOTFOUND). Reverting each guard on its own (connect by name, drop servername, verify against the IP, rejectUnauthorized: false) also turns a test red.
  • lib/internal/neo4j/client.test.ts: an out-of-range port is rejected before the relay starts. The contract also bounds port to 1–65535.
  • I ran the same flow under Bun (the production runtime) against a real Neo4j 5 container with Bolt TLS required and a certificate for neo4j.invalid. Create, query and introspection all succeeded through the relay. The pre-fix code failed with getaddrinfo ENOTFOUND neo4j.invalid, which confirms it resolved the name itself.
  • lib/internal/neo4j/operations.test.ts, tools/neo4j, blocks/blocks/neo4j, lib/internal/tool-operations: 33 passed
  • bun run generate (no drift), bun run lint, bun run type-check, bun run check:audits, check:test-types, bun run test:integration lib/internal/neo4j/client.integration.ts, check-block-registry.ts origin/staging, root bun run test

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)

@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 1:16pm 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; no new actionable issues were found.RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new actionable issues were found.

Summary

Encrypted self-hosted Neo4j connections now reach the validated IP through a local TLS relay while keeping hostname and certificate checks.

  • Encrypted Neo4j connections now use the address checked during validation.
  • Neo4j connections accept only ports from 1 through 65535.

Diagram

sequenceDiagram
  participant Operation
  participant Driver
  participant Relay as Loopback relay
  participant Database as Validated database IP
  Operation->>Driver: Verify connection
  Driver->>Relay: Plain Bolt on loopback
  Relay->>Database: TLS to pinned IP
  Note over Relay,Database: Check certificate against configured hostname
  Database-->>Relay: Encrypted Bolt
  Relay-->>Driver: Plain Bolt on loopback
  Operation->>Driver: Close
  Operation->>Relay: Close sockets and listener
Loading

Reviews (4) · Last reviewed commit: "fix(neo4j): keep the driver error on rel..." · Reviewed by Greptile

Comment thread apps/sim/lib/internal/neo4j/client.ts Resolved

@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.

1 issue found across 4 files

Confidence score: 4/5

  • In apps/sim/lib/internal/neo4j/client.ts, an abort during relay setup can be missed, allowing a connection to start after cancellation. Make relay startup abort-aware and close it before propagating the abort.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="apps/sim/lib/internal/neo4j/client.ts">

<violation number="1" location="apps/sim/lib/internal/neo4j/client.ts:140">
P2: An abort during relay setup is missed because the abort listener is registered only after setup. Make relay startup abort-aware and close it before propagating the abort, so cancellation does not start a connection attempt or wait for the 10-second timeout.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Turn on auto-fix | Re-trigger cubic

Comment thread apps/sim/lib/internal/neo4j/client.ts Resolved
Comment thread apps/sim/lib/internal/neo4j/client.ts Resolved
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@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 5 files

Reply with feedback, questions, or to request a fix.

Turn on auto-fix | Re-trigger cubic

Comment thread apps/sim/lib/internal/neo4j/client.ts Resolved
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@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.

No issues found across 6 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Turn on auto-fix | Re-trigger cubic

@waleedlatif1
waleedlatif1 merged commit e460b2f into staging Nov 28, 2026
84 of 86 checks passed

This branch was previously deployed

1 inactive deployment
Preview — 949d4bb6 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