@shellui/core 0.8.0 → 0.8.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/README.md CHANGED
@@ -6,7 +6,7 @@ React runtime for Shellui, the open-source app shell: navigation, layouts, light
6
6
  npm install @shellui/core
7
7
  ```
8
8
 
9
- Shellui **0.6.2** is the current stable release: `npm install @shellui/core@0.6.2`.
9
+ Shellui **0.8.1** is the current stable release: `npm install @shellui/core@0.8.1`.
10
10
 
11
11
  ```typescript
12
12
  import type { ShellUIConfig } from '@shellui/core';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shellui/core",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
4
4
  "description": "React runtime for Shellui, the open-source app shell. Renders navigation, layouts, themes with light and dark modes, sign-in and identity, and hosts your apps as micro frontends in one interface. MIT licensed.",
5
5
  "homepage": "https://shellui.com",
6
6
  "repository": {
@@ -90,7 +90,7 @@
90
90
  "workbox-routing": "^7.4.0",
91
91
  "workbox-strategies": "^7.4.0",
92
92
  "workbox-window": "^7.4.0",
93
- "@shellui/sdk": "0.8.0"
93
+ "@shellui/sdk": "0.8.1"
94
94
  },
95
95
  "peerDependencies": {
96
96
  "react": "^18.0.0 || ^19.0.0",
@@ -0,0 +1,286 @@
1
+ // @vitest-environment happy-dom
2
+ import { act, createElement } from 'react';
3
+ import { createRoot, type Root } from 'react-dom/client';
4
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
5
+ import { shellui, type ShellUIMessage } from '@shellui/sdk';
6
+ import type { ShellUIConfig } from '../config/types';
7
+ import type { ResolvedAppBackend } from '../auth/appBackend/identity';
8
+ import {
9
+ addChildFrame,
10
+ clearChildFrames,
11
+ validateChildFrameRegistration,
12
+ } from '../auth/appBackend/childFrames';
13
+ import { StorageBridge } from './StorageBridge';
14
+
15
+ const REGIONAL_STORAGE = 'https://storage.eu.example.com';
16
+ const SHELL_STORAGE = 'https://storage.shell.example';
17
+ const REGIONAL_TOKEN = 'regional-token';
18
+ const SHELL_TOKEN = 'shell-token';
19
+
20
+ const page = vi.hoisted(() => ({
21
+ includeShellStorage: false,
22
+ accessToken: 'shell-token' as string | null,
23
+ }));
24
+
25
+ const ensure = vi.hoisted(() =>
26
+ vi.fn(
27
+ async (_identity: ResolvedAppBackend, _options?: { allowSilent?: boolean }) =>
28
+ 'regional-token' as string | null,
29
+ ),
30
+ );
31
+
32
+ vi.mock('../config/useConfig', () => ({
33
+ useConfig: () => ({
34
+ loading: false,
35
+ config: {
36
+ navigation: [
37
+ {
38
+ label: 'Dashboard',
39
+ path: '',
40
+ url: 'https://dashboard.example.com/',
41
+ safeForAuthToken: true,
42
+ childFrames: {
43
+ origins: ['https://admin.example.com'],
44
+ backendOrigins: ['https://id.eu.example.com'],
45
+ serviceOrigins: ['https://storage.eu.example.com', 'https://files.example.com'],
46
+ },
47
+ },
48
+ {
49
+ label: 'Companion',
50
+ path: 'companion',
51
+ url: 'https://companion.example.com/',
52
+ safeForAuthToken: true,
53
+ },
54
+ ],
55
+ ...(page.includeShellStorage ? { storage: { url: 'https://storage.shell.example' } } : {}),
56
+ },
57
+ }),
58
+ }));
59
+
60
+ vi.mock('../auth/hooks/useAuth', () => ({
61
+ useAuth: () => ({
62
+ session: page.accessToken ? { accessToken: page.accessToken } : null,
63
+ }),
64
+ }));
65
+
66
+ vi.mock('../auth/appBackend/appBackendSessions', () => ({
67
+ ensureAppBackendSession: (identity: ResolvedAppBackend, options?: { allowSilent?: boolean }) =>
68
+ ensure(identity, options),
69
+ }));
70
+
71
+ (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
72
+
73
+ type StorageReply = {
74
+ id: string;
75
+ data?: unknown;
76
+ error?: { message: string; status: number };
77
+ };
78
+
79
+ function shellConfig(): ShellUIConfig {
80
+ return {
81
+ navigation: [
82
+ {
83
+ label: 'Dashboard',
84
+ path: '',
85
+ url: 'https://dashboard.example.com/',
86
+ safeForAuthToken: true,
87
+ childFrames: {
88
+ origins: ['https://admin.example.com'],
89
+ backendOrigins: ['https://id.eu.example.com'],
90
+ serviceOrigins: [REGIONAL_STORAGE, 'https://files.example.com'],
91
+ },
92
+ },
93
+ ],
94
+ ...(page.includeShellStorage ? { storage: { url: SHELL_STORAGE } } : {}),
95
+ };
96
+ }
97
+
98
+ describe('StorageBridge storage target', () => {
99
+ const frames: string[] = [];
100
+ let root: Root | null = null;
101
+ let container: HTMLDivElement | null = null;
102
+ let listener: ((message: ShellUIMessage, event: MessageEvent) => void) | null = null;
103
+ const sent: ShellUIMessage[] = [];
104
+ const fetchMock = vi.fn();
105
+
106
+ function addFrame(src: string): string {
107
+ const iframe = document.createElement('iframe');
108
+ iframe.src = src;
109
+ const uuid = shellui.frameRegistry.addIframe(iframe);
110
+ frames.push(uuid);
111
+ return uuid;
112
+ }
113
+
114
+ function registerFiles(hostUuid: string, storage: string) {
115
+ const result = validateChildFrameRegistration({
116
+ from: [hostUuid],
117
+ hostSrc: 'https://dashboard.example.com/',
118
+ payload: {
119
+ frameId: 'files-1',
120
+ url: 'https://admin.example.com/',
121
+ backend: {
122
+ url: 'https://id.eu.example.com',
123
+ companyId: 'r1',
124
+ provider: 'upstream',
125
+ },
126
+ services: {
127
+ storage,
128
+ files: 'https://files.example.com',
129
+ },
130
+ },
131
+ config: shellConfig(),
132
+ });
133
+ if (!result.ok) throw new Error(result.reason);
134
+ addChildFrame(result.entry);
135
+ }
136
+
137
+ function mount() {
138
+ container = document.createElement('div');
139
+ document.body.appendChild(container);
140
+ root = createRoot(container);
141
+ act(() => {
142
+ root?.render(createElement(StorageBridge));
143
+ });
144
+ if (!listener) throw new Error('StorageBridge did not listen for storage requests');
145
+ }
146
+
147
+ async function request(from: string[], id: string): Promise<StorageReply> {
148
+ listener?.(
149
+ {
150
+ type: 'SHELLUI_STORAGE_REQUEST',
151
+ payload: { id, op: 'listBuckets' },
152
+ from,
153
+ },
154
+ new MessageEvent('message'),
155
+ );
156
+ let reply: StorageReply | undefined;
157
+ await vi.waitFor(() => {
158
+ reply = sent.find((message) => (message.payload as StorageReply | undefined)?.id === id)
159
+ ?.payload as StorageReply | undefined;
160
+ expect(reply).toBeTruthy();
161
+ });
162
+ if (!reply) throw new Error(`no storage reply for ${id}`);
163
+ return reply;
164
+ }
165
+
166
+ function expectStorageCall(url: string, token: string) {
167
+ const match = fetchMock.mock.calls.find((call) => call[0] === `${url}/storage/v1/bucket`);
168
+ expect(match).toBeTruthy();
169
+ const init = (match?.[1] ?? {}) as RequestInit;
170
+ expect(new Headers(init.headers).get('Authorization')).toBe(`Bearer ${token}`);
171
+ }
172
+
173
+ beforeEach(() => {
174
+ page.includeShellStorage = false;
175
+ page.accessToken = SHELL_TOKEN;
176
+ ensure.mockReset();
177
+ ensure.mockResolvedValue(REGIONAL_TOKEN);
178
+ listener = null;
179
+ sent.length = 0;
180
+ fetchMock.mockReset();
181
+ fetchMock.mockResolvedValue({
182
+ ok: true,
183
+ status: 200,
184
+ headers: new Headers({ 'Content-Type': 'application/json' }),
185
+ json: async () => [{ name: 'company' }],
186
+ });
187
+ vi.stubGlobal('fetch', fetchMock);
188
+ const add = shellui.addMessageListener.bind(shellui);
189
+ vi.spyOn(shellui, 'addMessageListener').mockImplementation((type, cb) => {
190
+ if (type === 'SHELLUI_STORAGE_REQUEST') listener = cb;
191
+ return add(type, cb);
192
+ });
193
+ vi.spyOn(shellui, 'sendMessage').mockImplementation((message) => {
194
+ sent.push(message);
195
+ return 1;
196
+ });
197
+ });
198
+
199
+ afterEach(() => {
200
+ if (root) {
201
+ act(() => root?.unmount());
202
+ }
203
+ container?.remove();
204
+ root = null;
205
+ container = null;
206
+ for (const uuid of frames) shellui.frameRegistry.removeIframe(uuid);
207
+ frames.length = 0;
208
+ clearChildFrames();
209
+ vi.restoreAllMocks();
210
+ vi.unstubAllGlobals();
211
+ });
212
+
213
+ it('uses the storage URL and regional session registered for a nested admin frame', async () => {
214
+ mount();
215
+ const host = addFrame('https://dashboard.example.com/');
216
+ registerFiles(host, REGIONAL_STORAGE);
217
+
218
+ const reply = await request([host, 'files-1'], 'nested-1');
219
+
220
+ expect(reply).toEqual({ id: 'nested-1', data: [{ name: 'company' }] });
221
+ expectStorageCall(REGIONAL_STORAGE, REGIONAL_TOKEN);
222
+ expect(fetchMock.mock.calls.some((call) => String(call[0]).includes(SHELL_STORAGE))).toBe(
223
+ false,
224
+ );
225
+ expect(ensure).toHaveBeenCalledWith(
226
+ expect.objectContaining({ url: 'https://id.eu.example.com', companyId: 'r1' }),
227
+ { allowSilent: true },
228
+ );
229
+ });
230
+
231
+ it('keeps shellui.config storage for a frame the shell hosts directly', async () => {
232
+ page.includeShellStorage = true;
233
+ mount();
234
+ const companion = addFrame('https://companion.example.com/files');
235
+
236
+ const reply = await request([companion], 'direct-1');
237
+
238
+ expect(reply).toEqual({ id: 'direct-1', data: [{ name: 'company' }] });
239
+ expectStorageCall(SHELL_STORAGE, SHELL_TOKEN);
240
+ expect(ensure).not.toHaveBeenCalled();
241
+ });
242
+
243
+ it('does not use the shell storage URL when the nested frame has no allowed storage URL', async () => {
244
+ page.includeShellStorage = true;
245
+ mount();
246
+ const host = addFrame('https://dashboard.example.com/');
247
+ registerFiles(host, 'https://evil.example.org');
248
+
249
+ const reply = await request([host, 'files-1'], 'rejected-1');
250
+
251
+ expect(reply).toEqual({
252
+ id: 'rejected-1',
253
+ error: { message: 'Storage is not configured', status: 503 },
254
+ });
255
+ expect(fetchMock).not.toHaveBeenCalled();
256
+ });
257
+
258
+ it('rejects a storage request from a frame the shell did not register', async () => {
259
+ page.includeShellStorage = true;
260
+ mount();
261
+
262
+ const reply = await request(['not-a-frame'], 'untrusted-1');
263
+
264
+ expect(reply.error).toEqual({
265
+ message: 'Storage request rejected: untrusted frame',
266
+ status: 403,
267
+ });
268
+ expect(fetchMock).not.toHaveBeenCalled();
269
+ expect(ensure).not.toHaveBeenCalled();
270
+ });
271
+
272
+ it('does not apply one registered child storage URL to a different frame id', async () => {
273
+ mount();
274
+ const host = addFrame('https://dashboard.example.com/');
275
+ registerFiles(host, REGIONAL_STORAGE);
276
+
277
+ const reply = await request([host, 'other-files'], 'other-1');
278
+
279
+ expect(reply).toEqual({
280
+ id: 'other-1',
281
+ error: { message: 'Storage is not configured', status: 503 },
282
+ });
283
+ expect(fetchMock).not.toHaveBeenCalled();
284
+ expect(ensure).not.toHaveBeenCalled();
285
+ });
286
+ });
@@ -5,16 +5,21 @@ import {
5
5
  type ShellUIMessage,
6
6
  type StorageRequestPayload,
7
7
  } from '@shellui/sdk';
8
+ import { ensureAppBackendSession } from '../auth/appBackend/appBackendSessions';
9
+ import { findChildFrame } from '../auth/appBackend/childFrames';
8
10
  import { useAuth } from '../auth/hooks/useAuth';
9
11
  import { useConfig } from '../config/useConfig';
10
12
  import { getStorageRequestTrustDenial } from '../security/trustedFrames';
11
13
  import { handleStorageRequest } from './handleRequest';
12
14
  import { getStorageBaseUrl } from './quota';
15
+ import { resolveStorageBridgeTarget } from './resolveStorageBridgeTarget';
13
16
  import { handleTrackedUpload, isUploadPayload } from './uploads/handleTrackedUpload';
14
17
 
15
18
  /**
16
- * Root-window bridge: iframe `SHELLUI_STORAGE_REQUEST` messages are handled here
17
- * with `storage.url` and the signed-in user's access token.
19
+ * Root-window bridge: iframe `SHELLUI_STORAGE_REQUEST` messages are handled here.
20
+ * A nested app (dashboard admin, then Files) uses the storage URL and regional
21
+ * session registered for that frame. A frame the shell hosts directly uses
22
+ * `storage.url` from shellui.config and the shell session.
18
23
  */
19
24
  export const StorageBridge = () => {
20
25
  const { config } = useConfig();
@@ -47,9 +52,24 @@ export const StorageBridge = () => {
47
52
  return;
48
53
  }
49
54
 
50
- const run = isUploadPayload(payload)
51
- ? handleTrackedUpload({ storageUrl, accessToken, payload })
52
- : handleStorageRequest({ storageUrl, accessToken, payload });
55
+ const run = (async () => {
56
+ const child = findChildFrame(message.from ?? []);
57
+ const nested = child
58
+ ? {
59
+ storageUrl: child.services.storage,
60
+ accessToken: child.identity
61
+ ? await ensureAppBackendSession(child.identity, { allowSilent: true })
62
+ : null,
63
+ }
64
+ : null;
65
+ const target = resolveStorageBridgeTarget(nested, { storageUrl, accessToken });
66
+ const input = {
67
+ storageUrl: target.storageUrl,
68
+ accessToken: target.accessToken,
69
+ payload,
70
+ };
71
+ return isUploadPayload(payload) ? handleTrackedUpload(input) : handleStorageRequest(input);
72
+ })();
53
73
 
54
74
  void run.then((response) => {
55
75
  const reply = {
@@ -0,0 +1,51 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { resolveStorageBridgeTarget } from './resolveStorageBridgeTarget';
3
+
4
+ const shell = {
5
+ storageUrl: null as string | null,
6
+ accessToken: 'global-token',
7
+ };
8
+
9
+ describe('resolveStorageBridgeTarget', () => {
10
+ it('uses the nested frame storage URL and regional token', () => {
11
+ expect(
12
+ resolveStorageBridgeTarget(
13
+ {
14
+ storageUrl: 'https://storage.eu.shellui.com',
15
+ accessToken: 'regional-token',
16
+ },
17
+ shell,
18
+ ),
19
+ ).toEqual({
20
+ storageUrl: 'https://storage.eu.shellui.com',
21
+ accessToken: 'regional-token',
22
+ });
23
+ });
24
+
25
+ it('does not fall back to the shell when the nested frame has no storage URL', () => {
26
+ expect(
27
+ resolveStorageBridgeTarget(
28
+ { storageUrl: null, accessToken: 'regional-token' },
29
+ {
30
+ storageUrl: 'https://storage.shell.example',
31
+ accessToken: 'shell-token',
32
+ },
33
+ ),
34
+ ).toEqual({
35
+ storageUrl: null,
36
+ accessToken: 'regional-token',
37
+ });
38
+ });
39
+
40
+ it('keeps the shell storage URL for a frame the shell hosts directly', () => {
41
+ expect(
42
+ resolveStorageBridgeTarget(null, {
43
+ storageUrl: 'https://storage.example',
44
+ accessToken: 'shell-token',
45
+ }),
46
+ ).toEqual({
47
+ storageUrl: 'https://storage.example',
48
+ accessToken: 'shell-token',
49
+ });
50
+ });
51
+ });
@@ -0,0 +1,24 @@
1
+ export type StorageBridgeTarget = {
2
+ storageUrl: string | null;
3
+ accessToken: string | null;
4
+ };
5
+
6
+ /**
7
+ * Pick the storage URL and token for one `SHELLUI_STORAGE_REQUEST`.
8
+ *
9
+ * A nested frame (the dashboard hosting regional admin, which hosts Files)
10
+ * carries its own storage URL and regional session. Use those, even when the
11
+ * URL is missing: falling back to the shell's storage would call another
12
+ * region. Frames the shell hosts directly keep `storage.url` from
13
+ * shellui.config and the shell session.
14
+ */
15
+ export function resolveStorageBridgeTarget(
16
+ nested: StorageBridgeTarget | null,
17
+ shell: StorageBridgeTarget,
18
+ ): StorageBridgeTarget {
19
+ if (!nested) return shell;
20
+ return {
21
+ storageUrl: nested.storageUrl,
22
+ accessToken: nested.accessToken,
23
+ };
24
+ }