fix(meilisearch): preserve proxy paths and secure REST requests

This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-08-17 01:50:11 +08:00
1 parent 47337ac048
commit 50e6476cb0
8 files changed
+303 -23

No files matched your search

@@ -33,7 +33,7 @@ import { useToast } from "@/composables/useToast";
import DatabaseIcon from "@/components/icons/DatabaseIcon.vue"; import DatabaseIcon from "@/components/icons/DatabaseIcon.vue";
import * as api from "@/lib/backend/api"; import * as api from "@/lib/backend/api";
import { isTauriRuntime } from "@/lib/backend/tauriRuntime"; import { isTauriRuntime } from "@/lib/backend/tauriRuntime";
import { applyParsedConnectionUrl, normalizeMongoConnectionString, parseConnectionUrl } from "@/lib/connection/connectionUrl"; import { applyMeilisearchBasePathToExternalConfig, applyParsedConnectionUrl, normalizeMongoConnectionString, parseConnectionUrl } from "@/lib/connection/connectionUrl";
import { MAX_CONNECT_TIMEOUT_SECS, MAX_QUERY_TIMEOUT_SECS } from "@/lib/connection/timeoutLimits"; import { MAX_CONNECT_TIMEOUT_SECS, MAX_QUERY_TIMEOUT_SECS } from "@/lib/connection/timeoutLimits";
import { buildOracleTnsConnectionString, normalizeOracleTnsAdminPath, parseOracleTnsConnectionString } from "@/lib/connection/oracleTnsConnection"; import { buildOracleTnsConnectionString, normalizeOracleTnsAdminPath, parseOracleTnsConnectionString } from "@/lib/connection/oracleTnsConnection";
import { connectionDeepLinkServiceHydrationValue, parseConnectionDeepLink, parseServiceConnectionUrl, type ConnectionDeepLinkDraft } from "@/lib/connection/connectionDeepLink"; import { connectionDeepLinkServiceHydrationValue, parseConnectionDeepLink, parseServiceConnectionUrl, type ConnectionDeepLinkDraft } from "@/lib/connection/connectionDeepLink";
@@ -354,7 +354,6 @@ const elasticsearchConnectionPorts = ref<Record<ElasticsearchConnectionMode, num
direct: 9200, direct: 9200,
kibana: 5601, kibana: 5601,
}); });
function resetElasticsearchProxyFields(externalConfig?: unknown) { function resetElasticsearchProxyFields(externalConfig?: unknown) {
const mode = elasticsearchConnectionModeFromConfig(externalConfig); const mode = elasticsearchConnectionModeFromConfig(externalConfig);
elasticsearchConnectionMode.value = mode; elasticsearchConnectionMode.value = mode;
@@ -603,6 +602,31 @@ function externalConfigRecord(value: unknown): Record<string, unknown> {
return value && typeof value === "object" && !Array.isArray(value) ? { ...(value as Record<string, unknown>) } : {}; return value && typeof value === "object" && !Array.isArray(value) ? { ...(value as Record<string, unknown>) } : {};
} }
function meilisearchConnectionUrl(config: Pick<ConnectionConfig, "host" | "port" | "ssl" | "url_params" | "external_config">): string {
const host = config.host.trim();
if (!host) return "";
const scheme = config.ssl ? "https" : "http";
const endpointHost = host.includes(":") && !host.startsWith("[") ? `[${host}]` : host;
const externalConfig = externalConfigRecord(config.external_config);
const storedBasePath = externalConfig.basePath ?? externalConfig.base_path;
const basePathSegments = typeof storedBasePath === "string" ? storedBasePath.trim().split("/").filter(Boolean) : [];
const basePath = basePathSegments.length ? `/${basePathSegments.join("/")}` : "";
const urlParams = config.url_params?.trim().replace(/^\?/, "") || "";
return `${scheme}://${endpointHost}:${config.port}${basePath}${urlParams ? `?${urlParams}` : ""}`;
}
function syncMeilisearchHostInput(config: Pick<ConnectionConfig, "host" | "port" | "ssl" | "url_params" | "external_config">) {
meilisearchHostInput.value = meilisearchConnectionUrl(config);
appliedMeilisearchHostInput.value = meilisearchHostInput.value;
}
function resetMeilisearchHostInput() {
meilisearchHostInput.value = "";
appliedMeilisearchHostInput.value = "";
}
function sqlServerPortExplicitFromConfig(config: Pick<ConnectionConfig, "db_type" | "external_config">): boolean { function sqlServerPortExplicitFromConfig(config: Pick<ConnectionConfig, "db_type" | "external_config">): boolean {
if (config.db_type !== "sqlserver") return false; if (config.db_type !== "sqlserver") return false;
const external = externalConfigRecord(config.external_config); const external = externalConfigRecord(config.external_config);
@@ -651,6 +675,8 @@ const selectedJdbcDriverPath = ref("");
const jdbcManualClasspathOpen = ref(false); const jdbcManualClasspathOpen = ref(false);
const connectionUrlInput = ref(""); const connectionUrlInput = ref("");
const appliedConnectionUrlInput = ref(""); const appliedConnectionUrlInput = ref("");
const meilisearchHostInput = ref("");
const appliedMeilisearchHostInput = ref("");
const oracleTnsAdminPath = ref(""); const oracleTnsAdminPath = ref("");
const oceanbaseSubMode = ref<"mysql" | "oracle">("mysql"); const oceanbaseSubMode = ref<"mysql" | "oracle">("mysql");
const h2ConnectionMode = ref<H2ConnectionMode>("file"); const h2ConnectionMode = ref<H2ConnectionMode>("file");
@@ -2313,13 +2339,13 @@ function applyProfile(val: string, preserveConnectionFields = false) {
form.value.db_type = profile.type; form.value.db_type = profile.type;
form.value.driver_profile = val; form.value.driver_profile = val;
form.value.driver_label = isCustomCompatibleProfile() ? customDriverName.value.trim() || profile.label : profile.label; form.value.driver_label = isCustomCompatibleProfile() ? customDriverName.value.trim() || profile.label : profile.label;
if (profile.type !== "sqlserver") { const preserveMeilisearchConfig = preserveConnectionFields && previousDatabaseType === "meilisearch" && profile.type === "meilisearch";
if (profile.type !== "sqlserver" && !preserveMeilisearchConfig) {
form.value.external_config = undefined; form.value.external_config = undefined;
} }
if (profile.type !== "elasticsearch" || previousDatabaseType !== "elasticsearch") { if (profile.type !== "elasticsearch" || previousDatabaseType !== "elasticsearch") {
resetElasticsearchProxyFields(); resetElasticsearchProxyFields();
} }
if (!preserveConnectionFields) { if (!preserveConnectionFields) {
oracleTnsAdminPath.value = ""; oracleTnsAdminPath.value = "";
form.value.port = profile.port; form.value.port = profile.port;
@@ -2427,6 +2453,11 @@ function applyProfile(val: string, preserveConnectionFields = false) {
} }
resetHiveKerberosFields(profile.type === "hive" || profile.type === "kyuubi" || profile.type === "impala" ? form.value : undefined); resetHiveKerberosFields(profile.type === "hive" || profile.type === "kyuubi" || profile.type === "impala" ? form.value : undefined);
} }
if (profile.type === "meilisearch") {
syncMeilisearchHostInput(form.value);
} else {
resetMeilisearchHostInput();
}
} }
function switchOceanbaseMode(mode: "mysql" | "oracle") { function switchOceanbaseMode(mode: "mysql" | "oracle") {
@@ -2520,6 +2551,11 @@ watch(
productionProtectionEnabled.value = !!config.is_production || (config.production_databases?.length ?? 0) > 0; productionProtectionEnabled.value = !!config.is_production || (config.production_databases?.length ?? 0) > 0;
connectionUrlInput.value = config.db_type === "h2" && config.connection_string ? config.connection_string : ""; connectionUrlInput.value = config.db_type === "h2" && config.connection_string ? config.connection_string : "";
appliedConnectionUrlInput.value = connectionUrlInput.value.trim(); appliedConnectionUrlInput.value = connectionUrlInput.value.trim();
if (config.db_type === "meilisearch") {
syncMeilisearchHostInput(config);
} else {
resetMeilisearchHostInput();
}
if (config.db_type === "mq") { if (config.db_type === "mq") {
hydrateMqFields(config.external_config); hydrateMqFields(config.external_config);
} else { } else {
@@ -3577,6 +3613,11 @@ function applyConnectionUrlToForm(input: string): boolean {
const draft = parseConnectionDeepLink(input) ?? parseServiceConnectionUrl(input); const draft = parseConnectionDeepLink(input) ?? parseServiceConnectionUrl(input);
if (draft) { if (draft) {
applyConnectionDraftToForm({ ...draft, oneTime: undefined }); applyConnectionDraftToForm({ ...draft, oneTime: undefined });
if (form.value.db_type === "meilisearch") {
syncMeilisearchHostInput(form.value);
} else {
resetMeilisearchHostInput();
}
resetTestState(); resetTestState();
appliedConnectionUrlInput.value = input.trim(); appliedConnectionUrlInput.value = input.trim();
return true; return true;
@@ -3587,6 +3628,11 @@ function applyConnectionUrlToForm(input: string): boolean {
if (form.value.db_type === "victoriametrics") { if (form.value.db_type === "victoriametrics") {
hydrateVictoriaMetricsFields(form.value.external_config); hydrateVictoriaMetricsFields(form.value.external_config);
} }
if (form.value.db_type === "meilisearch") {
syncMeilisearchHostInput(form.value);
} else {
resetMeilisearchHostInput();
}
oracleTnsAdminPath.value = parseOracleTnsConnectionString(parsed.connectionString)?.tnsAdmin || ""; oracleTnsAdminPath.value = parseOracleTnsConnectionString(parsed.connectionString)?.tnsAdmin || "";
selectedType.value = parsed.driverProfile; selectedType.value = parsed.driverProfile;
customDriverName.value = isCustomCompatibleProfile() ? parsed.driverLabel : ""; customDriverName.value = isCustomCompatibleProfile() ? parsed.driverLabel : "";
@@ -3611,21 +3657,46 @@ function hasPendingConnectionUrlInput(): boolean {
return !!url && url !== appliedConnectionUrlInput.value; return !!url && url !== appliedConnectionUrlInput.value;
} }
function hasPendingMeilisearchHostInput(): boolean {
const url = meilisearchHostInput.value.trim();
return url !== appliedMeilisearchHostInput.value;
}
function applyMeilisearchHostInput(): boolean {
try {
const input = meilisearchHostInput.value.trim();
form.value = applyParsedConnectionUrl(form.value, parseConnectionUrl(input, "meilisearch"));
appliedMeilisearchHostInput.value = input;
resetTestState();
return true;
} catch (e: any) {
toast(t("connection.parseConnectionUrlFailed", { message: e?.message || String(e) }), 5000);
return false;
}
}
function ensureConnectionHostResolvedFromUrl(): boolean { function ensureConnectionHostResolvedFromUrl(): boolean {
if (!hasPendingConnectionUrlInput()) return true; if (hasPendingConnectionUrlInput() && !applyConnectionUrlToForm(connectionUrlInput.value.trim())) return false;
return applyConnectionUrlToForm(connectionUrlInput.value.trim()); if (form.value.db_type === "meilisearch" && hasPendingMeilisearchHostInput()) return applyMeilisearchHostInput();
return true;
} }
function formValueForSubmit(): Omit<ConnectionConfig, "id"> { function formValueForSubmit(): Omit<ConnectionConfig, "id"> {
const url = connectionUrlInput.value.trim(); const url = connectionUrlInput.value.trim();
if (!url || url === appliedConnectionUrlInput.value) return form.value; if (url && url !== appliedConnectionUrlInput.value) {
const draft = parseConnectionDeepLink(url);
if (draft) {
return applyConnectionDraftToConfig(form.value, { ...draft, oneTime: undefined });
}
const draft = parseConnectionDeepLink(url); return applyParsedConnectionUrl(form.value, parseConnectionUrl(url, selectedType.value));
if (draft) {
return applyConnectionDraftToConfig(form.value, { ...draft, oneTime: undefined });
} }
return applyParsedConnectionUrl(form.value, parseConnectionUrl(url, selectedType.value)); if (form.value.db_type === "meilisearch" && hasPendingMeilisearchHostInput()) {
return applyParsedConnectionUrl(form.value, parseConnectionUrl(meilisearchHostInput.value.trim(), "meilisearch"));
}
return form.value;
} }
function applyDremioJdbcMetadata(config: LegacyConnectionConfig) { function applyDremioJdbcMetadata(config: LegacyConnectionConfig) {
@@ -3902,7 +3973,6 @@ function connectionConfigForSubmit(id: string, generatedName = ""): ConnectionCo
config.username = ""; config.username = "";
config.password = config.password.trim(); config.password = config.password.trim();
config.database = undefined; config.database = undefined;
config.external_config = undefined;
} else if (config.db_type === "sqlserver") { } else if (config.db_type === "sqlserver") {
config.external_config = sqlServerPortExplicitFromConfig(config) ? { portExplicit: true } : undefined; config.external_config = sqlServerPortExplicitFromConfig(config) ? { portExplicit: true } : undefined;
} else if (supportsGaussdbIdentifierQuoteStyle(config)) { } else if (supportsGaussdbIdentifierQuoteStyle(config)) {
@@ -4754,6 +4824,7 @@ function resetForm() {
selectedJdbcDriverPath.value = ""; selectedJdbcDriverPath.value = "";
connectionUrlInput.value = ""; connectionUrlInput.value = "";
appliedConnectionUrlInput.value = ""; appliedConnectionUrlInput.value = "";
resetMeilisearchHostInput();
oracleTnsAdminPath.value = ""; oracleTnsAdminPath.value = "";
dialogStep.value = "select"; dialogStep.value = "select";
dbSearchQuery.value = ""; dbSearchQuery.value = "";
@@ -4810,6 +4881,7 @@ function applyConnectionDraftToConfig(config: Omit<ConnectionConfig, "id">, draf
database: draft.database ?? config.database, database: draft.database ?? config.database,
url_params: draft.urlParams ?? config.url_params, url_params: draft.urlParams ?? config.url_params,
ssl: draft.ssl ?? config.ssl, ssl: draft.ssl ?? config.ssl,
external_config: draft.dbType === "meilisearch" && draft.basePath !== undefined ? applyMeilisearchBasePathToExternalConfig(config.external_config, draft.basePath) : config.external_config,
connection_string: draft.connectionString ?? config.connection_string, connection_string: draft.connectionString ?? config.connection_string,
oracle_connection_type: draft.oracleConnectionType ?? config.oracle_connection_type, oracle_connection_type: draft.oracleConnectionType ?? config.oracle_connection_type,
one_time: draft.oneTime || undefined, one_time: draft.oneTime || undefined,
@@ -6989,6 +7061,10 @@ function openExternalUrl(url: string) {
</div> </div>
</div> </div>
</template> </template>
<div v-else-if="form.db_type === 'meilisearch'" class="grid grid-cols-4 items-center gap-4">
<Label :class="connectionLabelClass">{{ t("connection.host") }}</Label>
<Input v-model="meilisearchHostInput" class="col-span-3" :placeholder="connectionUrlPlaceholder" @input="resetTestState" />
</div>
<div v-else-if="form.db_type !== 'oracle' || form.oracle_connection_type !== 'tns'" class="grid grid-cols-4 items-center gap-4"> <div v-else-if="form.db_type !== 'oracle' || form.oracle_connection_type !== 'tns'" class="grid grid-cols-4 items-center gap-4">
<Label :class="connectionLabelClass">{{ form.db_type === "elasticsearch" && elasticsearchConnectionMode === "kibana" ? t("connection.elasticsearchKibanaHost") : t("connection.host") }}</Label> <Label :class="connectionLabelClass">{{ form.db_type === "elasticsearch" && elasticsearchConnectionMode === "kibana" ? t("connection.elasticsearchKibanaHost") : t("connection.host") }}</Label>
<Input v-model="form.host" class="col-span-2" /> <Input v-model="form.host" class="col-span-2" />
@@ -1,9 +1,15 @@
import { describe, expect, it } from "vitest"; import { describe, expect, it } from "vitest";
import { connectionProfileForScheme, parseConnectionUrl } from "@/lib/connection/connectionUrl"; import { applyParsedConnectionUrl, connectionProfileForScheme, parseConnectionUrl } from "@/lib/connection/connectionUrl";
import { parseConnectionDeepLink } from "@/lib/connection/connectionDeepLink";
import type { ConnectionConfig } from "@/types/database";
describe("Meilisearch connection URLs", () => { describe("Meilisearch connection URLs", () => {
it("parses the dedicated scheme with the default port and API key", () => { it("rejects an empty host URL", () => {
expect(parseConnectionUrl("meilisearch://:secret@search.example.com")).toMatchObject({ expect(() => parseConnectionUrl("", "meilisearch")).toThrow("Connection URL is empty");
});
it("parses the dedicated scheme with the default port, API key, and proxy path", () => {
expect(parseConnectionUrl("meilisearch://:secret@search.example.com/gateway/meili")).toMatchObject({
dbType: "meilisearch", dbType: "meilisearch",
driverProfile: "meilisearch", driverProfile: "meilisearch",
driverLabel: "Meilisearch", driverLabel: "Meilisearch",
@@ -12,6 +18,7 @@ describe("Meilisearch connection URLs", () => {
username: "", username: "",
password: "secret", password: "secret",
ssl: false, ssl: false,
basePath: "/gateway/meili",
}); });
}); });
@@ -25,6 +32,76 @@ describe("Meilisearch connection URLs", () => {
}); });
}); });
it.each([
["http://search.example.com/gateway/meili/", 80, false],
["https://search.example.com/gateway/meili", 443, true],
["https://search.example.com:8443/gateway/meili", 8443, true],
])("parses proxy URL %s", (url, port, ssl) => {
expect(parseConnectionUrl(url, "meilisearch")).toMatchObject({
dbType: "meilisearch",
host: "search.example.com",
port,
ssl,
database: undefined,
basePath: "/gateway/meili",
});
});
it("stores the proxy path in canonical external config without dropping other fields", () => {
const parsed = parseConnectionUrl("http://search.example.com/gateway/meili", "meilisearch");
const config = applyParsedConnectionUrl(
{
db_type: "meilisearch",
driver_profile: "meilisearch",
driver_label: "Meilisearch",
host: "localhost",
port: 7700,
username: "",
password: "",
url_params: "",
ssl: false,
external_config: { base_path: "/legacy", custom: true },
} as Omit<ConnectionConfig, "id">,
parsed,
);
expect(config.external_config).toEqual({ basePath: "/gateway/meili", custom: true });
});
it("clears stale proxy paths when the parsed URL uses the root path", () => {
const parsed = parseConnectionUrl("http://search.example.com/", "meilisearch");
const config = applyParsedConnectionUrl(
{
db_type: "meilisearch",
driver_profile: "meilisearch",
driver_label: "Meilisearch",
host: "localhost",
port: 7700,
username: "",
password: "",
url_params: "",
ssl: false,
external_config: { basePath: "/old", base_path: "/legacy", custom: true },
} as Omit<ConnectionConfig, "id">,
parsed,
);
expect(parsed.basePath).toBe("");
expect(config.external_config).toEqual({ custom: true });
});
it("preserves the proxy path when a Meilisearch URL arrives through a deep link", () => {
const link = new URL("dbx://connection/new");
link.searchParams.set("type", "meilisearch");
link.searchParams.set("url", "https://search.example.com/gateway/meili?insecure=true");
expect(parseConnectionDeepLink(link.toString())).toMatchObject({
dbType: "meilisearch",
basePath: "/gateway/meili",
urlParams: "insecure=true",
});
});
it("exposes the Meilisearch scheme profile", () => { it("exposes the Meilisearch scheme profile", () => {
expect(connectionProfileForScheme("meilisearch")).toEqual({ expect(connectionProfileForScheme("meilisearch")).toEqual({
type: "meilisearch", type: "meilisearch",
@@ -23,6 +23,7 @@ export interface ConnectionDeepLinkDraft {
password?: string; password?: string;
database?: string; database?: string;
urlParams?: string; urlParams?: string;
basePath?: string;
ssl?: boolean; ssl?: boolean;
connectionString?: string; connectionString?: string;
oracleConnectionType?: "service_name" | "sid"; oracleConnectionType?: "service_name" | "sid";
@@ -188,6 +189,7 @@ function draftFromConnectionUrl(value: string, preferredProfile?: string): Conne
password: parsed.password, password: parsed.password,
database: parsed.database, database: parsed.database,
urlParams: parsed.urlParams, urlParams: parsed.urlParams,
basePath: parsed.basePath,
ssl: parsed.ssl, ssl: parsed.ssl,
connectionString: parsed.connectionString, connectionString: parsed.connectionString,
oracleConnectionType: parsed.oracleConnectionType, oracleConnectionType: parsed.oracleConnectionType,
@@ -208,13 +208,15 @@ export function connectionUrlPlaceholder(dbType: DatabaseType): string {
case "elasticsearch": case "elasticsearch":
case "easysearch": case "easysearch":
case "meilisearch":
case "qdrant": case "qdrant":
case "milvus": case "milvus":
case "weaviate": case "weaviate":
case "chromadb": case "chromadb":
return "http://user:password@host:port"; return "http://user:password@host:port";
case "meilisearch":
return "http://host:port/base/path";
case "dameng": case "dameng":
return "dm://user:password@host:port"; return "dm://user:password@host:port";
@@ -19,6 +19,7 @@ export interface ParsedConnectionUrl {
useMongoUrl?: boolean; useMongoUrl?: boolean;
portExplicit?: boolean; portExplicit?: boolean;
apiPath?: string; apiPath?: string;
basePath?: string;
} }
export type ConnectionProfile = { export type ConnectionProfile = {
@@ -651,20 +652,24 @@ export function parseConnectionUrl(value: string, preferredProfile?: string): Pa
}; };
} }
const isMeilisearch = profile.type === "meilisearch";
const defaultPort = isMeilisearch && scheme === "http" ? 80 : isMeilisearch && scheme === "https" ? 443 : profile.defaultPort;
return { return {
...(name ? { name } : {}), ...(name ? { name } : {}),
dbType: profile.type, dbType: profile.type,
driverProfile: profile.profile, driverProfile: profile.profile,
driverLabel: profile.label, driverLabel: profile.label,
host: parsed.hostname, host: parsed.hostname,
port: parsed.port ? Number(parsed.port) : profile.defaultPort, port: parsed.port ? Number(parsed.port) : defaultPort,
...(profile.type === "sqlserver" && parsed.port ? { portExplicit: true } : {}), ...(profile.type === "sqlserver" && parsed.port ? { portExplicit: true } : {}),
username: mysqlCredentials?.username ?? decodeUrlPart(parsed.username), username: mysqlCredentials?.username ?? decodeUrlPart(parsed.username),
password: mysqlCredentials?.password ?? decodeUrlPart(parsed.password), password: mysqlCredentials?.password ?? decodeUrlPart(parsed.password),
database: profile.type === "victoriametrics" ? "metrics" : databaseFromPath(parsed.pathname), database: profile.type === "victoriametrics" ? "metrics" : isMeilisearch ? undefined : databaseFromPath(parsed.pathname),
urlParams: effectiveUrlParams, urlParams: effectiveUrlParams,
ssl: scheme === "rediss" || scheme === "https" || urlParamsRequireTls(profile.type, effectiveUrlParams) || (profile.type === "mysql" && isTidbCloudHost(parsed.hostname)), ssl: scheme === "rediss" || scheme === "https" || urlParamsRequireTls(profile.type, effectiveUrlParams) || (profile.type === "mysql" && isTidbCloudHost(parsed.hostname)),
...(profile.type === "victoriametrics" ? { apiPath: parsed.pathname.replace(/\/+$/, "") } : {}), ...(profile.type === "victoriametrics" ? { apiPath: parsed.pathname.replace(/\/+$/, "") } : {}),
...(isMeilisearch ? { basePath: parsed.pathname === "/" ? "" : parsed.pathname.replace(/\/+$/, "") } : {}),
}; };
} }
@@ -711,12 +716,26 @@ function isRecord(value: unknown): value is Record<string, unknown> {
return !!value && typeof value === "object" && !Array.isArray(value); return !!value && typeof value === "object" && !Array.isArray(value);
} }
export function applyMeilisearchBasePathToExternalConfig(existing: unknown, basePath: string | undefined): unknown {
const next = isRecord(existing) ? { ...existing } : {};
delete next.base_path;
if (basePath) {
next.basePath = basePath;
} else {
delete next.basePath;
}
return Object.keys(next).length > 0 ? next : undefined;
}
function parsedExternalConfig(existing: unknown, parsed: ParsedConnectionUrl): unknown { function parsedExternalConfig(existing: unknown, parsed: ParsedConnectionUrl): unknown {
if (parsed.dbType === "victoriametrics") { if (parsed.dbType === "victoriametrics") {
const next = isRecord(existing) ? { ...existing } : {}; const next = isRecord(existing) ? { ...existing } : {};
next.apiPath = parsed.apiPath || "/prometheus"; next.apiPath = parsed.apiPath || "/prometheus";
return next; return next;
} }
if (parsed.dbType === "meilisearch") {
return applyMeilisearchBasePathToExternalConfig(existing, parsed.basePath);
}
if (parsed.dbType !== "sqlserver") return existing; if (parsed.dbType !== "sqlserver") return existing;
const next = isRecord(existing) ? { ...existing } : {}; const next = isRecord(existing) ? { ...existing } : {};
+2 -1
View File
@@ -2064,11 +2064,12 @@ impl AppState {
PoolKind::Easysearch(client) PoolKind::Easysearch(client)
} }
DatabaseType::Meilisearch => { DatabaseType::Meilisearch => {
let client = db::meilisearch_driver::MeilisearchClient::new( let client = db::meilisearch_driver::MeilisearchClient::new_for_config(
&url, &url,
Some(&db_config.password), Some(&db_config.password),
db_config.ssl, db_config.ssl,
db_config.url_params.as_deref(), db_config.url_params.as_deref(),
db_config.external_config.as_ref(),
connect_timeout, connect_timeout,
)?; )?;
db::meilisearch_driver::test_connection(&client, connect_timeout).await?; db::meilisearch_driver::test_connection(&client, connect_timeout).await?;
+103 -2
View File
@@ -1,4 +1,4 @@
use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS}; use percent_encoding::{percent_decode_str, utf8_percent_encode, AsciiSet, CONTROLS};
use reqwest::{Client as HttpClient, Method, RequestBuilder, StatusCode}; use reqwest::{Client as HttpClient, Method, RequestBuilder, StatusCode};
use serde::Deserialize; use serde::Deserialize;
use serde_json::{Map, Value}; use serde_json::{Map, Value};
@@ -57,6 +57,18 @@ impl MeilisearchClient {
Ok(Self { http, base_url: url.trim_end_matches('/').to_string(), api_key }) Ok(Self { http, base_url: url.trim_end_matches('/').to_string(), api_key })
} }
pub fn new_for_config(
url: &str,
api_key: Option<&str>,
tls_enabled: bool,
url_params: Option<&str>,
external_config: Option<&Value>,
timeout: Duration,
) -> Result<Self, String> {
let base_url = meilisearch_base_url(url, external_config)?;
Self::new(&base_url, api_key, tls_enabled, url_params, timeout)
}
fn request(&self, method: Method, path: &str) -> RequestBuilder { fn request(&self, method: Method, path: &str) -> RequestBuilder {
let request = self.http.request(method, format!("{}{}", self.base_url, path)); let request = self.http.request(method, format!("{}{}", self.base_url, path));
match self.api_key.as_deref() { match self.api_key.as_deref() {
@@ -78,6 +90,31 @@ impl MeilisearchClient {
} }
} }
fn meilisearch_base_url(url: &str, external_config: Option<&Value>) -> Result<String, String> {
let url = url.trim_end_matches('/');
let Some(raw_base_path) = external_config
.and_then(Value::as_object)
.and_then(|config| config.get("basePath").or_else(|| config.get("base_path")))
.and_then(Value::as_str)
else {
return Ok(url.to_string());
};
let input = raw_base_path.trim();
if input.is_empty() || input == "/" {
return Ok(url.to_string());
}
if input.contains(['?', '#']) {
return Err("Meilisearch base path cannot contain a query or fragment".to_string());
}
let segments = input.split('/').filter(|segment| !segment.is_empty()).collect::<Vec<_>>();
if segments.iter().any(|segment| matches!(*segment, "." | "..")) {
return Err("Meilisearch base path cannot contain '.' or '..' segments".to_string());
}
Ok(format!("{url}/{}", segments.join("/")))
}
fn meilisearch_accept_invalid_certs(tls_enabled: bool, url_params: Option<&str>) -> bool { fn meilisearch_accept_invalid_certs(tls_enabled: bool, url_params: Option<&str>) -> bool {
tls_enabled tls_enabled
&& url_params.is_some_and(|params| { && url_params.is_some_and(|params| {
@@ -732,6 +769,18 @@ fn parse_rest_request_line(line: &str) -> Result<(Method, String), String> {
if !path.starts_with('/') { if !path.starts_with('/') {
return Err("Meilisearch REST path must start with '/'".to_string()); return Err("Meilisearch REST path must start with '/'".to_string());
} }
if path.contains('#') {
return Err("Meilisearch REST path must not contain a fragment".to_string());
}
let raw_path = path.split_once('?').map_or(path, |(path, _)| path);
for segment in raw_path.split('/') {
let decoded = percent_decode_str(segment)
.decode_utf8()
.map_err(|_| "Meilisearch REST path contains invalid UTF-8 escaping".to_string())?;
if matches!(decoded.as_ref(), "." | "..") || decoded.contains(['/', '\\']) {
return Err("Meilisearch REST path cannot contain traversal segments".to_string());
}
}
Ok((method, path.to_string())) Ok((method, path.to_string()))
} }
@@ -762,9 +811,54 @@ fn raw_response_result(status: u16, body: String, start: Instant, truncated: boo
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{decode_bounded_rest_body, filter_expression, meilisearch_sort_from_request, parse_rest_request_line}; use super::{
decode_bounded_rest_body, filter_expression, meilisearch_base_url, meilisearch_sort_from_request,
parse_rest_request_line, MeilisearchClient,
};
use reqwest::Method; use reqwest::Method;
use serde_json::json;
use std::time::Duration;
#[test]
fn normalizes_canonical_and_legacy_proxy_base_paths() {
assert_eq!(
meilisearch_base_url("http://search.example.com:7700/", Some(&json!({ "basePath": "/gateway/meili/" })))
.unwrap(),
"http://search.example.com:7700/gateway/meili"
);
assert_eq!(
meilisearch_base_url("https://search.example.com", Some(&json!({ "base_path": "gateway//meili" })))
.unwrap(),
"https://search.example.com/gateway/meili"
);
}
#[test]
fn keeps_the_origin_unchanged_for_empty_proxy_base_paths() {
for external_config in
[None, Some(json!({})), Some(json!({ "basePath": "" })), Some(json!({ "basePath": "/" }))]
{
assert_eq!(
meilisearch_base_url("http://search.example.com:7700/", external_config.as_ref()).unwrap(),
"http://search.example.com:7700"
);
}
}
#[test]
fn config_aware_client_uses_proxy_base_path() {
let client = MeilisearchClient::new_for_config(
"https://search.example.com:8443/",
None,
true,
None,
Some(&json!({ "basePath": "/gateway/meili/" })),
Duration::from_secs(1),
)
.unwrap();
assert_eq!(client.base_url, "https://search.example.com:8443/gateway/meili");
}
#[test] #[test]
fn translates_document_filters_to_meilisearch_syntax() { fn translates_document_filters_to_meilisearch_syntax() {
let filter = serde_json::json!({ let filter = serde_json::json!({
@@ -808,6 +902,13 @@ mod tests {
fn parses_rest_request_lines() { fn parses_rest_request_lines() {
assert_eq!(parse_rest_request_line("GET /version").unwrap(), (Method::GET, "/version".to_string())); assert_eq!(parse_rest_request_line("GET /version").unwrap(), (Method::GET, "/version".to_string()));
assert_eq!(parse_rest_request_line("/health").unwrap(), (Method::GET, "/health".to_string())); assert_eq!(parse_rest_request_line("/health").unwrap(), (Method::GET, "/health".to_string()));
assert_eq!(
parse_rest_request_line("GET /indexes/movies?limit=1").unwrap(),
(Method::GET, "/indexes/movies?limit=1".to_string())
);
assert!(parse_rest_request_line("GET /../version").unwrap_err().contains("traversal"));
assert!(parse_rest_request_line("GET /gateway/%2e%2e/version").unwrap_err().contains("traversal"));
assert!(parse_rest_request_line("GET /gateway/%2e%2e%2fversion").unwrap_err().contains("traversal"));
assert!(parse_rest_request_line("CONNECT /health") assert!(parse_rest_request_line("CONNECT /health")
.unwrap_err() .unwrap_err()
.contains("Unsupported Meilisearch HTTP method")); .contains("Unsupported Meilisearch HTTP method"));
+4 -2
View File
@@ -1158,11 +1158,12 @@ async fn test_connection_with_info_inner(
.map(|_| "Connection successful".to_string()) .map(|_| "Connection successful".to_string())
} }
DatabaseType::Meilisearch => { DatabaseType::Meilisearch => {
let client = db::meilisearch_driver::MeilisearchClient::new( let client = db::meilisearch_driver::MeilisearchClient::new_for_config(
&url, &url,
Some(&config.password), Some(&config.password),
config.ssl, config.ssl,
config.url_params.as_deref(), config.url_params.as_deref(),
config.external_config.as_ref(),
connect_timeout, connect_timeout,
)?; )?;
db::meilisearch_driver::test_connection(&client, connect_timeout) db::meilisearch_driver::test_connection(&client, connect_timeout)
@@ -1560,11 +1561,12 @@ pub async fn connect_db(
PoolKind::Easysearch(client) PoolKind::Easysearch(client)
} }
DatabaseType::Meilisearch => { DatabaseType::Meilisearch => {
let client = db::meilisearch_driver::MeilisearchClient::new( let client = db::meilisearch_driver::MeilisearchClient::new_for_config(
&url, &url,
Some(&db_config.password), Some(&db_config.password),
db_config.ssl, db_config.ssl,
db_config.url_params.as_deref(), db_config.url_params.as_deref(),
db_config.external_config.as_ref(),
connect_timeout, connect_timeout,
)?; )?;
db::meilisearch_driver::test_connection(&client, connect_timeout).await?; db::meilisearch_driver::test_connection(&client, connect_timeout).await?;