Skip to content

Commit eb3076c

Browse files
committed
refac
1 parent d169f08 commit eb3076c

1 file changed

Lines changed: 29 additions & 9 deletions

File tree

src/lib/components/chat/Chat.svelte

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -641,22 +641,43 @@
641641
const isSameOrigin = event.origin === window.origin;
642642
const type = event.data?.type;
643643
644-
// Prompt-related message types only submit text to the chat input —
645-
// functionally equivalent to the user typing. When same-origin is
646-
// enabled they go through immediately. When it is disabled (opaque
647-
// origin) we show a confirmation dialog so the user stays in control.
648-
const iframePromptTypes = ['input:prompt', 'input:prompt:submit', 'action:submit'];
644+
// Prompt-driving message types let an embedding page control the chat
645+
// input / submission. Cross-origin sources are only trusted when the
646+
// user has explicitly opted in via the "iframe Sandbox Allow Same
647+
// Origin" interface setting (the same toggle that governs whether
648+
// rendered iframes receive `allow-same-origin`).
649+
const promptTypes = ['input:prompt', 'input:prompt:submit', 'action:submit'];
650+
const isTrusted = isSameOrigin || ($settings?.iframeSandboxAllowSameOrigin ?? false);
651+
652+
// Non-prompt message types are always restricted to same-origin only.
653+
if (!isSameOrigin && !promptTypes.includes(type)) {
654+
return;
655+
}
649656
650-
if (!isSameOrigin && !iframePromptTypes.includes(type)) {
657+
// Prompt types from an untrusted cross-origin source are silently dropped.
658+
if (promptTypes.includes(type) && !isTrusted) {
651659
return;
652660
}
653661
654662
if (type === 'action:submit') {
655663
console.debug(event.data.text);
656664
657665
if (prompt !== '') {
658-
await tick();
659-
submitHandler(prompt);
666+
if (isSameOrigin) {
667+
await tick();
668+
submitHandler(prompt);
669+
} else {
670+
eventConfirmationInput = false;
671+
eventConfirmationTitle = $i18n.t('Confirm Prompt from Embed');
672+
eventConfirmationMessage = prompt;
673+
eventCallback = async (confirmed: boolean) => {
674+
if (confirmed) {
675+
await tick();
676+
submitHandler(prompt);
677+
}
678+
};
679+
showEventConfirmation = true;
680+
}
660681
}
661682
}
662683
@@ -679,7 +700,6 @@
679700
await tick();
680701
submitHandler(event.data.text);
681702
} else {
682-
// Cross-origin: ask user to confirm before submitting
683703
eventConfirmationInput = false;
684704
eventConfirmationTitle = $i18n.t('Confirm Prompt from Embed');
685705
eventConfirmationMessage = event.data.text;

0 commit comments

Comments
 (0)