fix: validate vector store connection addresses against SSRF policy

Vector store create and raw connection-test accepted user-supplied
connection addresses without SSRF validation, while every other
admin-entered endpoint (e.g. model BaseURL) already runs
ValidateURLForSSRF. An admin could point an engine address at an
internal host and use the connection probe as an SSRF oracle.

Add validateConnectionAddrSSRF, applied at the two user-input
boundaries only:

- CreateStore: validated before any network dial.
- TestStoreRaw: a new TestRawConnection wrapper runs an engine-type
  allowlist, required-field validation, and SSRF validation before
  delegating to TestConnection. TestConnection stays validation-free
  for trusted callers (env stores, stored configs already validated
  at create time).

Validation covers every address-bearing field a driver dials
(Addr, qdrant Host+Port, weaviate Host and GrpcAddress) and
fails closed for unmapped engine types. The Elasticsearch probe no
longer follows redirects, closing a 302-to-internal bypass.

docker-compose whitelists the bundled vector-store service hostnames
(qdrant, milvus, weaviate, doris-fe) via SSRF_WHITELIST_EXTRA so the
out-of-box experience is preserved; postgres is intentionally not
whitelisted. .env.example and the OpenSearch dev guide document how
to whitelist external or local-dev addresses.
This commit is contained in:
ochan.kwon
2026-06-04 14:56:38 +09:00
committed by lyingbug
parent e9980c6011
commit 7b2bc8a6b8
9 changed files with 405 additions and 5 deletions
+103
View File
@@ -62,6 +62,13 @@ func (s *vectorStoreService) CreateStore(ctx context.Context, store *types.Vecto
return err
}
// 2.1. SSRF validation on user-supplied addresses (whitelist-first).
// Placed before any network I/O (step 5 TestConnection, step 7 registry
// factory) so a blocked address never triggers an outbound connection.
if err := validateConnectionAddrSSRF(store.EngineType, store.ConnectionConfig); err != nil {
return err
}
// 2.5. Index config validation (bounds, name characters)
if err := types.ValidateIndexConfig(store.IndexConfig); err != nil {
return err
@@ -470,6 +477,102 @@ func validateConnectionConfig(engineType types.RetrieverEngineType, config types
return nil
}
// validateConnectionAddrSSRF validates every user-supplied address field of a
// connection config against the SSRF policy (whitelist first, then the strict
// IP / port / DNS checks inside secutils.ValidateURLForSSRF). It is applied
// ONLY at user-input boundaries — CreateStore and TestRawConnection. Env
// stores and already-stored configs are trusted and intentionally skip it.
//
// Unknown engine types are REJECTED (fail-closed): a newly added engine must
// not be able to reach a dial path without an explicit address mapping here.
// Empty fields are skipped — required-field presence is the responsibility of
// validateConnectionConfig, which runs first on every guarded path.
func validateConnectionAddrSSRF(engineType types.RetrieverEngineType, config types.ConnectionConfig) error {
// check validates a single address field. Empty fields are no-ops so this
// helper is independent of required-field enforcement.
check := func(addr string) error {
if addr == "" {
return nil
}
if err := secutils.ValidateURLForSSRF(addr); err != nil {
return errors.NewValidationError(
secutils.FormatSSRFError("vector store address", addr, err))
}
return nil
}
switch engineType {
case types.ElasticsearchRetrieverEngineType,
types.OpenSearchRetrieverEngineType,
types.MilvusRetrieverEngineType,
types.TencentVectorDBRetrieverEngineType,
types.DorisRetrieverEngineType:
// Single address field: a URL (es/opensearch) or bare host:port
// (milvus/tencent/doris). ValidateURLForSSRF normalises both.
return check(config.Addr)
case types.QdrantRetrieverEngineType:
// Host (+ optional Port) — combine so the port blocklist applies to
// the actual dial target rather than just the bare host.
addr := config.Host
if addr != "" && config.Port != 0 {
addr = fmt.Sprintf("%s:%d", config.Host, config.Port)
}
return check(addr)
case types.WeaviateRetrieverEngineType:
// Both the HTTP host and the gRPC address are dialed by the driver,
// so both must be validated (validating Host alone leaves GrpcAddress
// as an open SSRF vector).
if err := check(config.Host); err != nil {
return err
}
return check(config.GrpcAddress)
case types.SQLiteRetrieverEngineType:
// File-based engine; no remote address to validate.
return nil
default:
// Fail closed. Engines without a DB-store address mapping (postgres,
// infinity, elasticfaiss, and any future engine) must not silently
// bypass SSRF validation. The guarded callers (CreateStore,
// TestRawConnection) already restrict to validEngineTypes, so this is
// defence-in-depth rather than a user-facing path.
return errors.NewValidationError(
fmt.Sprintf("SSRF validation is not configured for engine type: %s", engineType))
}
}
// TestRawConnection validates raw (unpersisted) user-supplied connection config
// — engine-type allowlist, required fields, then the SSRF policy — before
// delegating to TestConnection. Handlers MUST use this for raw user input
// (e.g. POST /vector-stores/test).
//
// TestConnection itself stays validation-free for trusted callers (env stores
// and stored configs already validated at create time, which legitimately use
// internal hosts such as localhost). Do NOT consolidate the two methods.
func (s *vectorStoreService) TestRawConnection(
ctx context.Context,
engineType types.RetrieverEngineType,
config types.ConnectionConfig,
) (string, error) {
// 1. Engine-type allowlist. Only DB-registerable engines may be probed
// with raw credentials; this blocks e.g. a raw postgres probe against
// the application's own database host (a credential oracle).
if !types.IsValidEngineType(engineType) {
return "", errors.NewValidationError(
fmt.Sprintf("connection test is not supported for engine type: %s", engineType))
}
// 2. Required fields. Prevents an empty field from falling through to a
// driver's internal default (e.g. milvus empty addr -> localhost:19530),
// which would otherwise dial an internal host unchecked.
if err := validateConnectionConfig(engineType, config); err != nil {
return "", err
}
// 3. SSRF policy on every user-supplied address field.
if err := validateConnectionAddrSSRF(engineType, config); err != nil {
return "", err
}
return s.TestConnection(ctx, engineType, config)
}
// openSearch HNSW bound constants. Shards / replicas are NOT validated here —
// the flat types.ValidateIndexConfig already enforces those caps for every
// engine. These caps mirror the GetVectorStoreTypes Min/Max so the UI and