From b9a1ce7617371daeeaf0098f22968d04bf0271bc Mon Sep 17 00:00:00 2001 From: wizardchen Date: Thu, 14 May 2026 22:03:45 +0800 Subject: [PATCH] fix(rbac): drop X-Tenant-ID short-circuit in SSE / fetch paths MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The previous PR removed the "selectedTenantId === weknora_tenant.id → skip header" short-circuit from the axios interceptor, but the same copy-pasted logic lived in three more places that bypass axios: - `api/chat/streame.ts` — `fetchEventSource` for the streaming chat endpoint. Without this fix the chat reply would 404 with "流式连接失败:HTTP 404" on the first message after switching to a non-home tenant: the SSE call landed on the home tenant where the freshly-created session did not exist. - `api/initialization/index.ts` — native `fetch` for multimodal test (FormData) requests. Misroutes the test to the home tenant. - `utils/security.ts` — `getProtectedFileRequestHeaders` used by the Markdown image proxy. Authenticated file fetches landed on the home tenant, breaking image rendering after a tenant switch. All three now mirror the simplified rule: if `weknora_selected_tenant_id` is set, attach `X-Tenant-ID` unconditionally. The server's `IsTenantAccessible` already accepts a header pointing at the user's home tenant, so always attaching is safe. Same root cause as the axios fix: any code path that writes the active tenant into `weknora_tenant` (UserMenu's `loadUserInfo`, OIDC sync, router hydrate) made the two values equal, after which these non-axios paths silently dropped the header and re-routed to home. --- frontend/src/api/chat/streame.ts | 22 ++++++++-------------- frontend/src/api/initialization/index.ts | 15 ++++----------- frontend/src/utils/security.ts | 17 +++++++---------- 3 files changed, 19 insertions(+), 35 deletions(-) diff --git a/frontend/src/api/chat/streame.ts b/frontend/src/api/chat/streame.ts index 38234b35..39551649 100644 --- a/frontend/src/api/chat/streame.ts +++ b/frontend/src/api/chat/streame.ts @@ -48,21 +48,15 @@ export function useStream() { return; } - // 获取跨租户访问请求头 + // 跨租户访问请求头:只要 setSelectedTenant 写过激活租户,就附 + // X-Tenant-ID。早期版本会 short-circuit "selectedTenantId === + // defaultTenantId 时不附" 来减少 header 体积,但任何把 weknora_tenant + // 写成激活租户的代码(OIDC 同步 / UserMenu loadUserInfo / router + // hydrate)都会让两者相等,使得后续流式请求悄悄丢 header、落到 + // home 租户上,导致 SSE 接口返回 404。直接附即可——后端 + // IsTenantAccessible 也允许 header 指向自家租户。 const selectedTenantId = localStorage.getItem('weknora_selected_tenant_id'); - const defaultTenantId = localStorage.getItem('weknora_tenant'); - let tenantIdHeader: string | null = null; - if (selectedTenantId) { - try { - const defaultTenant = defaultTenantId ? JSON.parse(defaultTenantId) : null; - const defaultId = defaultTenant?.id ? String(defaultTenant.id) : null; - if (selectedTenantId !== defaultId) { - tenantIdHeader = selectedTenantId; - } - } catch (e) { - console.error('Failed to parse tenant info', e); - } - } + const tenantIdHeader: string | null = selectedTenantId || null; // Validate knowledge_base_ids for agent-chat requests // Note: knowledge_base_ids can be empty if user hasn't selected any, but we allow it diff --git a/frontend/src/api/initialization/index.ts b/frontend/src/api/initialization/index.ts index 849289ca..851cd586 100644 --- a/frontend/src/api/initialization/index.ts +++ b/frontend/src/api/initialization/index.ts @@ -435,19 +435,12 @@ export function testMultimodalFunction(testData: { headers['Authorization'] = `Bearer ${token}`; } - // 添加跨租户访问请求头(如果选择了其他租户) + // 跨租户访问请求头:直接附,避免 short-circuit "selectedTenantId + // === defaultTenantId 时不附" 在某些边角下让 header 静默丢失。 + // 与 utils/request.ts、api/chat/streame.ts 行为一致。 const selectedTenantId = localStorage.getItem('weknora_selected_tenant_id'); - const defaultTenantId = localStorage.getItem('weknora_tenant'); if (selectedTenantId) { - try { - const defaultTenant = defaultTenantId ? JSON.parse(defaultTenantId) : null; - const defaultId = defaultTenant?.id ? String(defaultTenant.id) : null; - if (selectedTenantId !== defaultId) { - headers['X-Tenant-ID'] = selectedTenantId; - } - } catch (e) { - console.error('Failed to parse tenant info', e); - } + headers['X-Tenant-ID'] = selectedTenantId; } // 使用原生fetch因为需要发送FormData diff --git a/frontend/src/utils/security.ts b/frontend/src/utils/security.ts index 9aec0c8b..3e0f0d1a 100644 --- a/frontend/src/utils/security.ts +++ b/frontend/src/utils/security.ts @@ -276,17 +276,14 @@ function getProtectedFileRequestHeaders(): Record { } const selectedTenantId = (localStorage.getItem('weknora_selected_tenant_id') || '').trim(); - const tenantRaw = localStorage.getItem('weknora_tenant'); if (selectedTenantId) { - try { - const tenant = tenantRaw ? JSON.parse(tenantRaw) : null; - const defaultTenantId = tenant?.id ? String(tenant.id) : ''; - if (selectedTenantId !== defaultTenantId) { - headers['X-Tenant-ID'] = selectedTenantId; - } - } catch { - // ignore tenant parse error and skip X-Tenant-ID - } + // Always attach when a selected tenant is set. Same rationale as + // utils/request.ts / api/chat/streame.ts: the + // "selectedTenantId === defaultTenantId → skip" short-circuit + // silently drops the header whenever any code path writes the + // active tenant into weknora_tenant, leaving authenticated file + // fetches landing on the home tenant. + headers['X-Tenant-ID'] = selectedTenantId; } } catch { // ignore localStorage read errors