Skip to content

Commit 5638d3a

Browse files
committed
fix: normalize IPv6 hostname brackets and validate resolved Ollama URL at KB creation
1 parent 7bcee72 commit 5638d3a

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

apps/sim/app/api/knowledge/route.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ export async function POST(req: NextRequest) {
126126
let effectiveDimension = validatedData.embeddingDimension
127127
if (provider === 'ollama') {
128128
const ollamaBaseUrl = getOllamaBaseUrl(validatedData.ollamaBaseUrl)
129+
if (!isAllowedOllamaUrl(ollamaBaseUrl)) {
130+
return NextResponse.json(
131+
{
132+
error: `Ollama base URL "${ollamaBaseUrl}" is not allowed. Must point to localhost, a private IP address, or host.docker.internal.`,
133+
},
134+
{ status: 400 }
135+
)
136+
}
129137
try {
130138
const modelInfo = await validateOllamaModel(modelName, ollamaBaseUrl)
131139

apps/sim/lib/knowledge/embeddings.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ export function isAllowedOllamaUrl(url: string): boolean {
2525
try {
2626
const parsed = new URL(url)
2727
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') return false
28-
const hostname = parsed.hostname.toLowerCase()
28+
const raw = parsed.hostname.toLowerCase()
29+
// WHATWG URL may return IPv6 with brackets ([::1]) or without (::1); normalize to bare address
30+
const hostname = raw.startsWith('[') && raw.endsWith(']') ? raw.slice(1, -1) : raw
2931
if (hostname === '169.254.169.254' || hostname === 'metadata.google.internal') return false
30-
if (hostname.startsWith('[') && hostname !== '[::1]') return false
31-
if (hostname === 'localhost' || hostname === '[::1]') return true
32+
if (hostname === 'localhost' || hostname === '::1') return true
33+
if (hostname.includes(':')) return false // block all non-loopback IPv6
3234
const ipv4 = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
3335
if (ipv4.test(hostname)) {
3436
if (hostname.startsWith('127.') || hostname.startsWith('10.') || hostname.startsWith('192.168.')) return true

0 commit comments

Comments
 (0)