fix(rbac): drop X-Tenant-ID short-circuit in SSE / fetch paths
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.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -276,17 +276,14 @@ function getProtectedFileRequestHeaders(): Record<string, string> {
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user