mirror of
https://github.com/open-webui/open-webui.git
synced 2026-09-14 23:22:44 +00:00
refac: share one community origin allowlist across window message handlers (#29918)
The three community origins were repeated inline in five window message handlers and now come from a single COMMUNITY_ORIGINS constant in constants.ts. The sync stats modal uses that same list in both directions: it reads messages only from a community origin, replies to the origin the message came from, and names the community origins as the targets of the messages it sends. Its chat id goes into the request as one encoded path segment.
This commit is contained in:
parent
887372aca5
commit
08578557de
8 changed files with 37 additions and 41 deletions
|
|
@ -1648,14 +1648,17 @@ export const exportChatStats = async (token: string, page: number = 1, params: o
|
|||
export const exportSingleChatStats = async (token: string, chatId: string) => {
|
||||
let error = null;
|
||||
|
||||
const res = await fetch(`${WEBUI_API_BASE_URL}/chats/stats/export/${chatId}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
...(token && { authorization: `Bearer ${token}` })
|
||||
const res = await fetch(
|
||||
`${WEBUI_API_BASE_URL}/chats/stats/export/${encodeURIComponent(chatId)}`,
|
||||
{
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
...(token && { authorization: `Bearer ${token}` })
|
||||
}
|
||||
}
|
||||
})
|
||||
)
|
||||
.then(async (res) => {
|
||||
if (!res.ok) throw await res.json();
|
||||
return res.json();
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
import { exportChatStats, exportSingleChatStats, downloadChatStats } from '$lib/apis/chats';
|
||||
import { getVersion } from '$lib/apis';
|
||||
import { settings } from '$lib/stores';
|
||||
import { COMMUNITY_ORIGINS } from '$lib/constants';
|
||||
|
||||
import Modal from '$lib/components/common/Modal.svelte';
|
||||
import Tooltip from '$lib/components/common/Tooltip.svelte';
|
||||
|
|
@ -19,6 +20,10 @@
|
|||
|
||||
// Listen for verify:chat messages from opener
|
||||
const handleMessage = async (event: MessageEvent) => {
|
||||
if (!COMMUNITY_ORIGINS.includes(event.origin)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Community sends: { type: 'verify:chat', data: { id: ... } }
|
||||
const chatId = event.data?.data?.id ?? event.data?.id;
|
||||
if (event.data?.type === 'verify:chat' && chatId) {
|
||||
|
|
@ -32,7 +37,7 @@
|
|||
chatId: chatId,
|
||||
requestId: event.data.requestId ?? null
|
||||
},
|
||||
'*'
|
||||
event.origin
|
||||
);
|
||||
}
|
||||
} catch (err: any) {
|
||||
|
|
@ -45,7 +50,7 @@
|
|||
chatId: chatId,
|
||||
requestId: event.data.requestId ?? null
|
||||
},
|
||||
'*'
|
||||
event.origin
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -89,7 +94,10 @@
|
|||
// Helper to send postMessage to opener
|
||||
const postToOpener = (message: object) => {
|
||||
if (window.opener) {
|
||||
window.opener.postMessage({ ...message, requestId: eventData?.requestId ?? null }, '*');
|
||||
const payload = { ...message, requestId: eventData?.requestId ?? null };
|
||||
for (const origin of COMMUNITY_ORIGINS) {
|
||||
window.opener.postMessage(payload, origin);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@
|
|||
import type { Writable } from 'svelte/store';
|
||||
import type { i18n as i18nType } from 'i18next';
|
||||
import { WEBUI_NAME, config, user, workspaceActions, workspaceCounts } from '$lib/stores';
|
||||
import { COMMUNITY_ORIGINS } from '$lib/constants';
|
||||
|
||||
import {
|
||||
createNewPrompt,
|
||||
|
|
@ -301,11 +302,7 @@
|
|||
loaded = true;
|
||||
|
||||
const onMessage = async (event: MessageEvent) => {
|
||||
if (
|
||||
!['https://openwebui.com', 'https://www.openwebui.com', 'http://localhost:9999'].includes(
|
||||
event.origin
|
||||
)
|
||||
) {
|
||||
if (!COMMUNITY_ORIGINS.includes(event.origin)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -16,6 +16,12 @@ export const AUDIO_API_BASE_URL = `${WEBUI_BASE_URL}/api/v1/audio`;
|
|||
export const IMAGES_API_BASE_URL = `${WEBUI_BASE_URL}/api/v1/images`;
|
||||
export const RETRIEVAL_API_BASE_URL = `${WEBUI_BASE_URL}/api/v1/retrieval`;
|
||||
|
||||
export const COMMUNITY_ORIGINS = [
|
||||
'https://openwebui.com',
|
||||
'https://www.openwebui.com',
|
||||
'http://localhost:9999'
|
||||
];
|
||||
|
||||
// The version changes, but the promise must not. Let what
|
||||
// was built here keep its word across every release.
|
||||
export const WEBUI_VERSION = APP_VERSION;
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
import FunctionEditor from '$lib/components/admin/Functions/FunctionEditor.svelte';
|
||||
import { getModels } from '$lib/apis';
|
||||
import { compareVersion, extractFrontmatter } from '$lib/utils';
|
||||
import { WEBUI_VERSION } from '$lib/constants';
|
||||
import { COMMUNITY_ORIGINS, WEBUI_VERSION } from '$lib/constants';
|
||||
|
||||
const i18n = getContext('i18n');
|
||||
|
||||
|
|
@ -70,12 +70,7 @@
|
|||
}
|
||||
|
||||
window.addEventListener('message', async (event) => {
|
||||
if (
|
||||
!['https://openwebui.com', 'https://www.openwebui.com', 'http://localhost:9999'].includes(
|
||||
event.origin
|
||||
)
|
||||
)
|
||||
return;
|
||||
if (!COMMUNITY_ORIGINS.includes(event.origin)) return;
|
||||
|
||||
func = JSON.parse(event.data);
|
||||
console.log(func);
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
import { toast } from 'svelte-sonner';
|
||||
import { goto } from '$app/navigation';
|
||||
import { config, models, settings } from '$lib/stores';
|
||||
import { WEBUI_BASE_URL } from '$lib/constants';
|
||||
import { COMMUNITY_ORIGINS, WEBUI_BASE_URL } from '$lib/constants';
|
||||
|
||||
import { onMount, tick, getContext } from 'svelte';
|
||||
import { createNewModel, getModelById } from '$lib/apis/models';
|
||||
|
|
@ -65,11 +65,7 @@
|
|||
|
||||
onMount(() => {
|
||||
const handleMessageEvent = async (event: MessageEvent) => {
|
||||
if (
|
||||
!['https://openwebui.com', 'https://www.openwebui.com', 'http://localhost:9999'].includes(
|
||||
event.origin
|
||||
)
|
||||
) {
|
||||
if (!COMMUNITY_ORIGINS.includes(event.origin)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
import { goto } from '$app/navigation';
|
||||
import { createNewTool, getTools } from '$lib/apis/tools';
|
||||
import ToolkitEditor from '$lib/components/workspace/Tools/ToolkitEditor.svelte';
|
||||
import { WEBUI_VERSION } from '$lib/constants';
|
||||
import { COMMUNITY_ORIGINS, WEBUI_VERSION } from '$lib/constants';
|
||||
import { tools } from '$lib/stores';
|
||||
import { compareVersion, extractFrontmatter } from '$lib/utils';
|
||||
import { onMount, getContext } from 'svelte';
|
||||
|
|
@ -56,12 +56,7 @@
|
|||
|
||||
onMount(() => {
|
||||
window.addEventListener('message', async (event) => {
|
||||
if (
|
||||
!['https://openwebui.com', 'https://www.openwebui.com', 'http://localhost:9999'].includes(
|
||||
event.origin
|
||||
)
|
||||
)
|
||||
return;
|
||||
if (!COMMUNITY_ORIGINS.includes(event.origin)) return;
|
||||
|
||||
tool = JSON.parse(event.data);
|
||||
console.log(tool);
|
||||
|
|
|
|||
|
|
@ -62,7 +62,7 @@
|
|||
removeTerminalConnection
|
||||
} from '$lib/utils/connections';
|
||||
|
||||
import { WEBUI_API_BASE_URL, WEBUI_BASE_URL } from '$lib/constants';
|
||||
import { COMMUNITY_ORIGINS, WEBUI_API_BASE_URL, WEBUI_BASE_URL } from '$lib/constants';
|
||||
import {
|
||||
bestMatchingLanguage,
|
||||
cleanText,
|
||||
|
|
@ -1049,11 +1049,7 @@
|
|||
};
|
||||
|
||||
const windowMessageEventHandler = async (event) => {
|
||||
if (
|
||||
!['https://openwebui.com', 'https://www.openwebui.com', 'http://localhost:9999'].includes(
|
||||
event.origin
|
||||
)
|
||||
) {
|
||||
if (!COMMUNITY_ORIGINS.includes(event.origin)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue