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/app/api/tools/mongodb/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { MongoClient } from 'mongodb'
import { validateDatabaseHost } from '@/lib/core/security/input-validation.server'
import {
createPinnedLookup,
validateDatabaseHost,
} from '@/lib/core/security/input-validation.server'
import type { MongoDBCollectionInfo, MongoDBConnectionConfig } from '@/tools/mongodb/types'

export async function createMongoDBConnection(config: MongoDBConnectionConfig) {
Expand Down Expand Up @@ -30,6 +33,7 @@ export async function createMongoDBConnection(config: MongoDBConnectionConfig) {
connectTimeoutMS: 10000,
socketTimeoutMS: 10000,
maxPoolSize: 1,
lookup: createPinnedLookup(hostValidation.resolvedIP ?? config.host),
})

await client.connect()
Expand Down
8 changes: 8 additions & 0 deletions apps/sim/app/api/tools/mysql/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import net from 'node:net'
import mysql from 'mysql2/promise'
import { validateDatabaseHost } from '@/lib/core/security/input-validation.server'

Expand All @@ -16,12 +17,19 @@ export async function createMySQLConnection(config: MySQLConnectionConfig) {
throw new Error(hostValidation.error)
}

const resolvedIP = hostValidation.resolvedIP ?? config.host

const connectionConfig: mysql.ConnectionOptions = {
host: config.host,
port: config.port,
database: config.database,
user: config.username,
password: config.password,
stream: () => {
const socket = net.connect({ host: resolvedIP, port: config.port, timeout: 10000 })
socket.setNoDelay(true)
return socket
},
}

if (config.ssl === 'disabled') {
Expand Down
9 changes: 8 additions & 1 deletion apps/sim/app/api/tools/neo4j/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ export async function createNeo4jDriver(config: Neo4jConnectionConfig) {
protocol = config.encryption === 'enabled' ? 'bolt+s' : 'bolt'
}

const uri = `${protocol}://${config.host}:${config.port}`
const useIPPinning = !protocol.endsWith('+s')
const resolvedIP = hostValidation.resolvedIP ?? config.host
const uriHost = useIPPinning
? resolvedIP.includes(':')
? `[${resolvedIP}]`
: resolvedIP
: config.host
const uri = `${protocol}://${uriHost}:${config.port}`

Comment thread
waleedlatif1 marked this conversation as resolved.
const driverConfig: any = {
maxConnectionPoolSize: 1,
Expand Down
15 changes: 8 additions & 7 deletions apps/sim/app/api/tools/postgresql/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ export async function createPostgresConnection(config: PostgresConnectionConfig)
throw new Error(hostValidation.error)
}

const sslConfig =
const resolvedHost = hostValidation.resolvedIP ?? config.host
const pinIP = config.ssl !== 'preferred'

const sslConfig: boolean | 'prefer' | { rejectUnauthorized: boolean; servername?: string } =
config.ssl === 'disabled'
? false
: config.ssl === 'required'
? 'require'
: config.ssl === 'preferred'
? 'prefer'
: 'require'
: config.ssl === 'preferred'
? 'prefer'
: { rejectUnauthorized: false, servername: config.host }

const sql = postgres({
host: config.host,
host: pinIP ? resolvedHost : config.host,
port: config.port,
database: config.database,
username: config.username,
Expand Down
28 changes: 27 additions & 1 deletion apps/sim/app/api/tools/redis/execute/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,33 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
return NextResponse.json({ error: hostValidation.error }, { status: 400 })
}

client = new Redis(url, {
const resolvedIP = hostValidation.resolvedIP ?? hostname
const tlsEnabled = parsedUrl.protocol === 'rediss:'
const port = parsedUrl.port ? Number(parsedUrl.port) : 6379
const username = parsedUrl.username ? decodeURIComponent(parsedUrl.username) : undefined
const password = parsedUrl.password ? decodeURIComponent(parsedUrl.password) : undefined

let db = 0
if (parsedUrl.pathname && parsedUrl.pathname.length > 1) {
const dbSegment = parsedUrl.pathname.slice(1)
const parsedDb = Number.parseInt(dbSegment, 10)
if (!Number.isFinite(parsedDb) || String(parsedDb) !== dbSegment) {
return NextResponse.json(
{ error: `Invalid Redis database index in URL path: '${dbSegment}'` },
{ status: 400 }
)
}
db = parsedDb
}

client = new Redis({
host: resolvedIP,
port,
username,
password,
db,
family: resolvedIP.includes(':') ? 6 : 4,
tls: tlsEnabled ? { servername: hostname } : undefined,
connectTimeout: 10000,
commandTimeout: 10000,
maxRetriesPerRequest: 1,
Expand Down
Loading