6bf8bebf51
CI / Test and Build (push) Failing after 1s
CI / Migrate Dev DB (push) Has been skipped
CI / Migrate DB (push) Has been skipped
CodeQL / Analyze actions (push) Has been cancelled
CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI / Detect Version (push) Has been cancelled
CI / Detect Desktop Changes (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/cron.Dockerfile, ubuntu-latest, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build AMD64 (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/cron.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/db.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/pii.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/realtime.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-8vcpu-ubuntu-2404-arm, ./docker/app.Dockerfile, linux-arm64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
Helm Chart / Lint, test, and validate chart (push) Has been cancelled
Helm Chart / Chart version bumped (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Build Dev ECR (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core) (push) Has been cancelled
CI / Promote Images (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Check Desktop Signing Secrets (push) Has been cancelled
CI / Desktop Release (push) Has been cancelled
CI / Create Desktop Prerelease (push) Has been cancelled
CI / Desktop Prerelease Build (push) Has been cancelled
CI / Publish Desktop Prerelease (push) Has been cancelled
CI / Prune Desktop Prereleases (push) Has been cancelled
Helm Chart / Install on kind and run helm test (push) Has been cancelled
128 lines
4.5 KiB
TypeScript
128 lines
4.5 KiB
TypeScript
/**
|
|
* @vitest-environment node
|
|
*/
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import type { ClickHouseConnectionConfig } from '@/tools/clickhouse/types'
|
|
|
|
const { mockValidateDatabaseHost, mockSecureFetchWithPinnedIP, mockValidateSqlWhereClause } =
|
|
vi.hoisted(() => ({
|
|
mockValidateDatabaseHost: vi.fn(),
|
|
mockSecureFetchWithPinnedIP: vi.fn(),
|
|
mockValidateSqlWhereClause: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/lib/core/security/input-validation.server', () => ({
|
|
MAX_JSON_API_RESPONSE_BYTES: 10 * 1024 * 1024,
|
|
validateDatabaseHost: mockValidateDatabaseHost,
|
|
secureFetchWithPinnedIP: mockSecureFetchWithPinnedIP,
|
|
validateSqlWhereClause: mockValidateSqlWhereClause,
|
|
}))
|
|
|
|
import { executeClickHouseInsert, executeClickHouseQuery } from '@/app/api/tools/clickhouse/utils'
|
|
|
|
function makeConfig(
|
|
overrides: Partial<ClickHouseConnectionConfig> = {}
|
|
): ClickHouseConnectionConfig {
|
|
return {
|
|
host: 'clickhouse.example.com',
|
|
port: 8123,
|
|
database: 'default',
|
|
username: 'default',
|
|
password: 'secret',
|
|
secure: false,
|
|
...overrides,
|
|
}
|
|
}
|
|
|
|
function okResponse(body: string, summary?: string) {
|
|
return {
|
|
ok: true,
|
|
status: 200,
|
|
statusText: 'OK',
|
|
text: async () => body,
|
|
headers: {
|
|
get: (name: string) =>
|
|
name.toLowerCase() === 'x-clickhouse-summary' ? (summary ?? null) : null,
|
|
},
|
|
}
|
|
}
|
|
|
|
describe('clickhouseRequest DNS pinning', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
mockValidateDatabaseHost.mockResolvedValue({
|
|
isValid: true,
|
|
resolvedIP: '93.184.216.34',
|
|
originalHostname: 'clickhouse.example.com',
|
|
})
|
|
mockValidateSqlWhereClause.mockReturnValue({ isValid: true })
|
|
mockSecureFetchWithPinnedIP.mockResolvedValue(okResponse('{"data":[{"x":1}],"rows":1}'))
|
|
})
|
|
|
|
it('pins the connection to the validated IP, not the attacker-controlled hostname', async () => {
|
|
await executeClickHouseQuery(makeConfig({ host: 'rebind.attacker.example' }), 'SELECT 1')
|
|
|
|
expect(mockValidateDatabaseHost).toHaveBeenCalledWith('rebind.attacker.example', 'host')
|
|
expect(mockSecureFetchWithPinnedIP).toHaveBeenCalledTimes(1)
|
|
|
|
const [url, pinnedIP, options] = mockSecureFetchWithPinnedIP.mock.calls[0]
|
|
// The actual TCP target is the validated IP — re-resolution of the hostname can never happen.
|
|
expect(pinnedIP).toBe('93.184.216.34')
|
|
// The hostname is preserved only in the URL (for Host header / TLS SNI), never used to connect.
|
|
expect(url).toContain('rebind.attacker.example')
|
|
expect(options.method).toBe('POST')
|
|
})
|
|
|
|
it('never issues the request when host validation fails (no SSRF window)', async () => {
|
|
mockValidateDatabaseHost.mockResolvedValue({
|
|
isValid: false,
|
|
error: 'host resolves to a blocked IP address',
|
|
})
|
|
|
|
await expect(executeClickHouseQuery(makeConfig(), 'SELECT 1')).rejects.toThrow(
|
|
'host resolves to a blocked IP address'
|
|
)
|
|
expect(mockSecureFetchWithPinnedIP).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('uses https and disallows http redirects when secure is true', async () => {
|
|
await executeClickHouseQuery(makeConfig({ secure: true, port: 8443 }), 'SELECT 1')
|
|
|
|
const [url, , options] = mockSecureFetchWithPinnedIP.mock.calls[0]
|
|
expect(url).toMatch(/^https:\/\//)
|
|
expect(options.allowHttp).toBe(false)
|
|
})
|
|
|
|
it('allows http for the initial request when secure is false', async () => {
|
|
await executeClickHouseQuery(makeConfig({ secure: false }), 'SELECT 1')
|
|
|
|
const [url, , options] = mockSecureFetchWithPinnedIP.mock.calls[0]
|
|
expect(url).toMatch(/^http:\/\//)
|
|
expect(options.allowHttp).toBe(true)
|
|
})
|
|
|
|
it('sends the statement as the body with a matching Content-Length and auth headers', async () => {
|
|
await executeClickHouseInsert(makeConfig(), 'events', { id: 1 })
|
|
|
|
const [, , options] = mockSecureFetchWithPinnedIP.mock.calls[0]
|
|
expect(options.body).toContain('INSERT INTO `events` FORMAT JSONEachRow')
|
|
expect(options.headers['Content-Length']).toBe(String(Buffer.byteLength(options.body, 'utf-8')))
|
|
expect(options.headers['X-ClickHouse-User']).toBe('default')
|
|
expect(options.headers['X-ClickHouse-Key']).toBe('secret')
|
|
})
|
|
|
|
it('propagates non-ok responses as errors with the body text', async () => {
|
|
mockSecureFetchWithPinnedIP.mockResolvedValue({
|
|
ok: false,
|
|
status: 400,
|
|
statusText: 'Bad Request',
|
|
text: async () => 'Code: 62. DB::Exception: Syntax error',
|
|
headers: { get: () => null },
|
|
})
|
|
|
|
await expect(executeClickHouseQuery(makeConfig(), 'SELECT 1')).rejects.toThrow(
|
|
'Code: 62. DB::Exception: Syntax error'
|
|
)
|
|
})
|
|
})
|