Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion apps/sim/lib/api/contracts/tools/databases/neo4j.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import {

export const neo4jConnectionBodySchema = z.object({
host: z.string().min(1, 'Host is required'),
port: z.coerce.number().int().positive('Port must be a positive integer'),
port: z.coerce
.number()
.int()
.min(1, 'Port must be between 1 and 65535')
.max(65_535, 'Port must be between 1 and 65535'),
database: z.string().min(1, 'Database name is required'),
username: z.string().min(1, 'Username is required'),
password: z.string().min(1, 'Password is required'),
Expand Down
156 changes: 156 additions & 0 deletions apps/sim/lib/internal/neo4j/client.integration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/**
* Encrypted connections against a real loopback TLS listener: what reaches the wire (address,
* SNI, first Bolt bytes) is only observable on a socket.
*/
import { execFile } from 'node:child_process'
import { mkdtempSync, readFileSync, rmSync } from 'node:fs'
import type { AddressInfo, Socket } from 'node:net'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import {
createServer as createTlsServer,
getCACertificates,
setDefaultCACertificates,
} from 'node:tls'
import { promisify } from 'node:util'
import {
inputValidationMock,
inputValidationMockFns,
} from '@sim/testing/mocks/input-validation.mock'
import { Neo4jError } from 'neo4j-driver'
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'

vi.mock('@/lib/core/security/input-validation.server', () => inputValidationMock)

import { openNeo4jConnection } from '@/lib/internal/neo4j/client'

/** `.invalid` never resolves, so only a connection pinned to the validated IP can reach the server. */
const DATABASE_HOST = 'neo4j.invalid'
const BOLT_MAGIC = '6060b017'

interface TlsDatabase {
port: number
/** SNI and first application bytes of every TLS session that completed. */
sessions: Array<{ servername: string | undefined; firstBytes: string }>
close: () => Promise<void>
}

const certificateDirectory = mkdtempSync(join(tmpdir(), 'neo4j-tls-'))
const certificates: Record<string, { cert: string; key: string }> = {}
const defaultCertificates = getCACertificates('default')

async function issueCertificate(name: string) {
const certPath = join(certificateDirectory, `${name}.crt`)
const keyPath = join(certificateDirectory, `${name}.key`)
await promisify(execFile)('openssl', [
'req',
'-x509',
'-newkey',
'rsa:2048',
'-sha256',
'-nodes',
'-keyout',
keyPath,
'-out',
certPath,
'-days',
'2',
'-subj',
`/CN=${name}`,
'-addext',
`subjectAltName=DNS:${name}`,
])
certificates[name] = { cert: readFileSync(certPath, 'utf8'), key: readFileSync(keyPath, 'utf8') }
}

/** A TLS listener that records the session, then hangs up: enough to see what the driver sent. */
async function startTlsDatabase(certificateName: string): Promise<TlsDatabase> {
const sessions: TlsDatabase['sessions'] = []
const sockets = new Set<Socket>()
const server = createTlsServer(certificates[certificateName], (socket) => {
socket.once('data', (chunk: Buffer) => {
sessions.push({
servername: socket.servername || undefined,
firstBytes: chunk.subarray(0, 4).toString('hex'),
})
socket.destroy()
})
})
server.on('connection', (socket) => {
sockets.add(socket)
socket.on('close', () => sockets.delete(socket))
})
server.on('tlsClientError', () => {})
await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve))
return {
port: (server.address() as AddressInfo).port,
sessions,
close: () =>
new Promise<void>((resolve) => {
for (const socket of sockets) socket.destroy()
server.close(() => resolve())
}),
}
}

describe('openNeo4jConnection with encryption enabled', () => {
beforeAll(async () => {
await issueCertificate(DATABASE_HOST)
await issueCertificate('other.invalid')
setDefaultCACertificates([
...defaultCertificates,
certificates[DATABASE_HOST].cert,
certificates['other.invalid'].cert,
])
})

afterAll(() => {
setDefaultCACertificates(defaultCertificates)
rmSync(certificateDirectory, { recursive: true, force: true })
})

beforeEach(() => {
inputValidationMockFns.mockValidateDatabaseHost.mockResolvedValue({
isValid: true,
resolvedIP: '127.0.0.1',
originalHostname: DATABASE_HOST,
})
})

const open = (port: number) =>
openNeo4jConnection({
host: DATABASE_HOST,
port,
database: 'neo4j',
username: 'neo4j',
password: 'secret',
encryption: 'enabled',
})

it('carries Bolt over TLS to the validated IP with the hostname as SNI', async () => {
const database = await startTlsDatabase(DATABASE_HOST)
try {
await expect(open(database.port)).rejects.toThrow()
expect(database.sessions[0]).toEqual({ servername: DATABASE_HOST, firstBytes: BOLT_MAGIC })
} finally {
await database.close()
}
})

it('refuses a certificate issued for another hostname before sending any Bolt bytes', async () => {
const database = await startTlsDatabase('other.invalid')
try {
const error = await open(database.port).catch((caught: unknown) => caught)
expect(error).toBeInstanceOf(Neo4jError)
expect(error).toMatchObject({
code: 'ServiceUnavailable',
message: expect.stringMatching(
/Failed to connect to server.*Caused by: Hostname\/IP does not match certificate's altnames/
),
})
expect(database.sessions).toEqual([])
} finally {
await database.close()
}
})
})
28 changes: 28 additions & 0 deletions apps/sim/lib/internal/neo4j/client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
inputValidationMock,
inputValidationMockFns,
} from '@sim/testing/mocks/input-validation.mock'
import { describe, expect, it, vi } from 'vitest'

vi.mock('@/lib/core/security/input-validation.server', () => inputValidationMock)

import { openNeo4jConnection } from '@/lib/internal/neo4j/client'

describe('openNeo4jConnection', () => {
it('rejects an out-of-range port instead of throwing from the relay', async () => {
inputValidationMockFns.mockValidateDatabaseHost.mockResolvedValue({
isValid: true,
resolvedIP: '127.0.0.1',
})
await expect(
openNeo4jConnection({
host: 'neo4j.invalid',
port: 65_536,
database: 'neo4j',
username: 'neo4j',
password: 'secret',
encryption: 'enabled',
})
).rejects.toThrow('Port must be an integer between 1 and 65535')
})
})
Loading
Loading