@crossworks/share-ui 0.400.74 → 0.500.1

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crossworks/share-ui",
3
- "version": "0.400.74",
3
+ "version": "0.500.1",
4
4
  "description": "The server-rendered share surface — the /s/<token> presenters, view-payload contract, mini-app sandbox, and the few primitives they need. Lives in MANTLE (the server renders these), published for jackdaw to consume; may depend only on @mantle/{client-types,content-core} (the jackdaw-repo-split boundary).",
5
5
  "type": "module",
6
6
  "exports": {
@@ -42,8 +42,8 @@
42
42
  "dependencies": {
43
43
  "@dicebear/core": "^10.7.0",
44
44
  "@dicebear/styles": "^10.6.0",
45
- "@mantle/client-types": "npm:@crossworks/client-types@0.400.74",
46
- "@mantle/content-core": "npm:@crossworks/content-core@0.400.74",
45
+ "@mantle/client-types": "npm:@crossworks/client-types@0.500.1",
46
+ "@mantle/content-core": "npm:@crossworks/content-core@0.500.1",
47
47
  "@radix-ui/react-label": "^2.1.17",
48
48
  "@radix-ui/react-slot": "^1.5.0",
49
49
  "class-variance-authority": "^0.7.1",
@@ -6,7 +6,12 @@
6
6
  * exactly what a wrong `@host` import cost in the field.
7
7
  */
8
8
  import { describe, expect, it } from 'vitest';
9
- import { buildAppFrameHtml, viewerScript } from './app-frame-html';
9
+ import {
10
+ appLibraryScripts,
11
+ buildAppFrameCsp,
12
+ buildAppFrameHtml,
13
+ viewerScript,
14
+ } from './app-frame-html';
10
15
 
11
16
  const frame = (bundleCode: string) =>
12
17
  buildAppFrameHtml({
@@ -159,3 +164,48 @@ describe('buildAppFrameHtml: host.me() (app identity)', () => {
159
164
  );
160
165
  });
161
166
  });
167
+
168
+ describe('buildAppFrameCsp: the library files an app may load', () => {
169
+ const O = 'https://box.example';
170
+ const map = {
171
+ react: '/app-runtime/react-AB12.js',
172
+ echarts: '/canvas-runtime/echarts-CD34.js',
173
+ d3: '/canvas-runtime/d3-EF56.js',
174
+ again: '/canvas-runtime/d3-EF56.js',
175
+ bad: '/canvas-runtime/x.js; script-src *',
176
+ query: '/canvas-runtime/y.js?z',
177
+ };
178
+
179
+ it('names each library file of the import map, once, and never the folder', () => {
180
+ expect(appLibraryScripts(map)).toEqual([
181
+ '/canvas-runtime/d3-EF56.js',
182
+ '/canvas-runtime/echarts-CD34.js',
183
+ ]);
184
+ const csp = buildAppFrameCsp(O, appLibraryScripts(map));
185
+ const scripts = csp.match(/script-src ([^;]+);/)![1]!.split(' ');
186
+ expect(scripts).toContain(`${O}/canvas-runtime/echarts-CD34.js`);
187
+ expect(scripts).toContain(`${O}/canvas-runtime/d3-EF56.js`);
188
+ expect(scripts).not.toContain(`${O}/canvas-runtime/`);
189
+ });
190
+
191
+ it('takes no source a list could smuggle in', () => {
192
+ const csp = buildAppFrameCsp(O, [
193
+ '/canvas-runtime/x.js; script-src *',
194
+ 'https://evil.invalid/a.js',
195
+ ]);
196
+ expect(csp).not.toContain('evil.invalid');
197
+ expect(csp.match(/script-src/g)).toHaveLength(1);
198
+ });
199
+
200
+ it('with no list, no library file at all', () => {
201
+ expect(buildAppFrameCsp(O)).not.toContain('/canvas-runtime/');
202
+ });
203
+
204
+ it('compiles WebAssembly but never evals script, and stays offline', () => {
205
+ const csp = buildAppFrameCsp(O);
206
+ const scripts = csp.match(/script-src ([^;]+);/)![1]!.split(' ');
207
+ expect(scripts).toContain("'wasm-unsafe-eval'");
208
+ expect(scripts).not.toContain("'unsafe-eval'");
209
+ expect(csp).toContain("connect-src 'none'");
210
+ });
211
+ });
@@ -27,11 +27,14 @@ import type { AppViewer } from '@mantle/client-types/app-viewer';
27
27
  * attachments. The document's origin is OPAQUE (sandboxed), so CSP `'self'`
28
28
  * is unreliable here — every source names the host origin explicitly:
29
29
  * scripts only from `/app-runtime/` (the shared React/kit/host runtime the
30
- * import map points at) and `/canvas-runtime/` (the vendored libraries the
31
- * same map names), styles/fonts only from `/share-runtime/` (the
32
- * self-contained stylesheet the frame links) and `/fonts/` (owner font
33
- * library). */
34
- export function buildAppFrameCsp(origin: string): string {
30
+ * import map points at) and the exact `/canvas-runtime/` files that same map
31
+ * names (`libraryScripts`, appLibraryScripts), styles/fonts only from
32
+ * `/share-runtime/` (the self-contained stylesheet the frame links) and
33
+ * `/fonts/` (owner font library). */
34
+ export function buildAppFrameCsp(origin: string, libraryScripts: readonly string[] = []): string {
35
+ const libraries = appLibraryScripts(Object.fromEntries(libraryScripts.map((u, i) => [i, u])))
36
+ .map((u) => ` ${origin}${u}`)
37
+ .join('');
35
38
  return (
36
39
  // `sandbox allow-scripts` is the load-bearing directive: it sandboxes the
37
40
  // DOCUMENT itself (opaque origin, no cookies/storage), not just the iframe
@@ -43,19 +46,35 @@ export function buildAppFrameCsp(origin: string): string {
43
46
  `default-src 'none'; style-src 'unsafe-inline' ${origin}/share-runtime/; ` +
44
47
  // share-runtime scripts joined app-runtime for the Drift backdrop
45
48
  // (share-page.js + its lazy WebGL chunk) — same-origin built assets,
46
- // no new capability. canvas-runtime joined for the vendored libraries an
47
- // app may import (echarts, d3, three, konva, katex, the diagram
48
- // templates; app-build app-libraries.ts): the same content-hashed files
49
- // a Present canvas loads, on the brain origin. No 'unsafe-eval' (the one
50
- // library that needs it, Alpine.js, is left out of apps), and
51
- // connect-src stays 'none', so a library adds no way out.
52
- `script-src 'unsafe-inline' ${origin}/app-runtime/ ${origin}/share-runtime/ ${origin}/canvas-runtime/; ` +
49
+ // no new capability. The vendored libraries an app may import (echarts,
50
+ // d3, three, konva, katex, the diagram templates; app-build
51
+ // app-libraries.ts) join by exact file, as the app import map names
52
+ // them: the same content-hashed files a Present canvas loads, and not the
53
+ // rest of /canvas-runtime/ (Alpine.js, reveal.js and the vanilla motion
54
+ // build stay out of apps). No 'unsafe-eval', and connect-src stays
55
+ // 'none', so a library adds no way out.
56
+ // 'wasm-unsafe-eval' lets an app compile WebAssembly (the sandbox kit
57
+ // builds Rust to wasm32 for apps) and nothing else: eval and
58
+ // new Function stay blocked, and a wasm module has no I/O of its own,
59
+ // only the imports the app's own script hands it.
60
+ `script-src 'unsafe-inline' 'wasm-unsafe-eval' ${origin}/app-runtime/ ${origin}/share-runtime/${libraries}; ` +
53
61
  `img-src data: blob: ${origin}; ` +
54
62
  `font-src data: ${origin}/share-runtime/ ${origin}/fonts/; ` +
55
63
  "connect-src 'none'; base-uri 'none'; form-action 'none'"
56
64
  );
57
65
  }
58
66
 
67
+ /** The vendored library files an app import map names: its values under
68
+ * /canvas-runtime/, each a plain path (a hashed file name, no query, no space
69
+ * or `;` that could add a CSP source), sorted and once each. */
70
+ export function appLibraryScripts(imports: Record<string, string>): string[] {
71
+ const out = new Set<string>();
72
+ for (const url of Object.values(imports)) {
73
+ if (/^\/canvas-runtime\/[\w.-]+\.js$/.test(url)) out.add(url);
74
+ }
75
+ return [...out].sort();
76
+ }
77
+
59
78
  // Host-injected live-theme sync. Lives in the iframe but is NOT part of the
60
79
  // app bundle, so it works on every app with no rebuild. It applies
61
80
  // parent-posted {kind:'theme'} updates so a live theme switch restyles a