@shellui/core 0.5.0 → 0.5.2
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 +2 -2
- package/schemas/shellui.config.schema.json +98 -33
- package/src/app.tsx +2 -0
- package/src/components/ContentView.tsx +3 -5
- package/src/components/contentIframeSandbox.spec.ts +25 -0
- package/src/components/contentIframeSandbox.ts +43 -0
- package/src/features/auth/AuthProvider.tsx +156 -33
- package/src/features/auth/backends/index.ts +2 -0
- package/src/features/auth/backends/shellui.ts +43 -35
- package/src/features/auth/backends/supabase.ts +1 -0
- package/src/features/auth/backends/types.ts +5 -0
- package/src/features/auth/components/LoginView.tsx +5 -1
- package/src/features/auth/components/OAuthCallbackView.tsx +115 -44
- package/src/features/auth/constants/oauth.ts +2 -0
- package/src/features/auth/hooks/useAuth.tsx +4 -0
- package/src/features/auth/utils/authSessionPersistence.spec.ts +93 -0
- package/src/features/auth/utils/authSessionPersistence.ts +138 -0
- package/src/features/auth/utils/authStorageKeys.ts +8 -0
- package/src/features/auth/utils/bffAuthClient.ts +107 -0
- package/src/features/auth/utils/buildOAuthSessionRedirectTo.spec.ts +22 -0
- package/src/features/auth/utils/buildOAuthSessionRedirectTo.ts +17 -0
- package/src/features/auth/utils/buildSessionFromTokenPayload.spec.ts +29 -0
- package/src/features/auth/utils/buildSessionFromTokenPayload.ts +24 -0
- package/src/features/auth/utils/index.ts +18 -3
- package/src/features/auth/utils/isBffAuthEnabled.ts +12 -0
- package/src/features/config/types.ts +49 -2
- package/src/features/messaging/MessagingSecurityBootstrap.tsx +19 -0
- package/src/features/messaging/deriveAllowedMessageOrigins.spec.ts +86 -0
- package/src/features/messaging/deriveAllowedMessageOrigins.ts +74 -0
- package/src/features/modal/ModalContext.spec.ts +13 -2
- package/src/features/modal/validateAndNormalizeUrl.ts +9 -23
- package/src/features/security/trustedFrames.spec.ts +129 -0
- package/src/features/security/trustedFrames.ts +79 -0
- package/src/features/security/urlAllowlist.spec.ts +99 -0
- package/src/features/security/urlAllowlist.ts +122 -0
- package/src/features/settings/SettingsProvider.tsx +10 -46
- package/src/features/settings/utils/buildSettingsForPropagation.spec.ts +23 -1
- package/src/features/settings/utils/buildSettingsForPropagation.ts +7 -0
- package/src/features/storage/StorageBridge.spec.ts +82 -0
- package/src/features/storage/StorageBridge.tsx +25 -3
- package/src/features/storage/StoragePickerContext.tsx +3 -4
- package/src/features/storage/uploads/uploadQueue.ts +2 -5
- package/src/routes/components/NavigationItemRoute.tsx +30 -15
- package/src/routes/utils/navigationRouteAccess.spec.ts +85 -0
- package/src/routes/utils/navigationRouteAccess.ts +43 -0
- package/src/features/auth/utils/clearStoredAuthSession.spec.ts +0 -23
- package/src/features/auth/utils/clearStoredAuthSession.ts +0 -10
- package/src/features/auth/utils/persistAuthSession.spec.ts +0 -35
- package/src/features/auth/utils/persistAuthSession.ts +0 -12
- package/src/features/auth/utils/readStoredAuthSession.spec.ts +0 -34
- package/src/features/auth/utils/readStoredAuthSession.ts +0 -14
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import type { ShellUIConfig } from '../config/types';
|
|
3
|
+
import { isAllowedIframeOrigin, validateLoginPanelUrl } from './urlAllowlist';
|
|
4
|
+
|
|
5
|
+
const config = {
|
|
6
|
+
backend: {
|
|
7
|
+
type: 'shellui',
|
|
8
|
+
url: 'https://api.example.com',
|
|
9
|
+
loginUrl: 'https://app.example.com',
|
|
10
|
+
},
|
|
11
|
+
storage: {
|
|
12
|
+
url: 'https://storage.example.com',
|
|
13
|
+
filesUrl: 'https://files.example.com',
|
|
14
|
+
},
|
|
15
|
+
} as ShellUIConfig;
|
|
16
|
+
|
|
17
|
+
describe('isAllowedIframeOrigin', () => {
|
|
18
|
+
afterEach(() => {
|
|
19
|
+
vi.unstubAllEnvs();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('allows the shell origin', () => {
|
|
23
|
+
expect(
|
|
24
|
+
isAllowedIframeOrigin('https://app.example.com', {
|
|
25
|
+
currentOrigin: 'https://app.example.com',
|
|
26
|
+
}),
|
|
27
|
+
).toBe(true);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('allows configured backend and storage origins', () => {
|
|
31
|
+
expect(isAllowedIframeOrigin('https://api.example.com', { config })).toBe(true);
|
|
32
|
+
expect(isAllowedIframeOrigin('https://storage.example.com', { config })).toBe(true);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('allows localhost in development builds', () => {
|
|
36
|
+
vi.stubEnv('NODE_ENV', 'development');
|
|
37
|
+
expect(
|
|
38
|
+
isAllowedIframeOrigin('http://localhost:5176', {
|
|
39
|
+
allowLocalhost: true,
|
|
40
|
+
currentOrigin: 'https://app.example.com',
|
|
41
|
+
}),
|
|
42
|
+
).toBe(true);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('denies localhost in production builds', () => {
|
|
46
|
+
vi.stubEnv('NODE_ENV', 'production');
|
|
47
|
+
expect(
|
|
48
|
+
isAllowedIframeOrigin('http://localhost:5176', {
|
|
49
|
+
allowLocalhost: true,
|
|
50
|
+
currentOrigin: 'https://app.example.com',
|
|
51
|
+
}),
|
|
52
|
+
).toBe(false);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('denies unrelated origins', () => {
|
|
56
|
+
vi.stubEnv('NODE_ENV', 'production');
|
|
57
|
+
expect(
|
|
58
|
+
isAllowedIframeOrigin('https://evil.example.com', {
|
|
59
|
+
allowLocalhost: true,
|
|
60
|
+
config,
|
|
61
|
+
currentOrigin: 'https://app.example.com',
|
|
62
|
+
}),
|
|
63
|
+
).toBe(false);
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
describe('validateLoginPanelUrl', () => {
|
|
68
|
+
afterEach(() => {
|
|
69
|
+
vi.unstubAllEnvs();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('allows same-origin relative paths', () => {
|
|
73
|
+
expect(validateLoginPanelUrl('/login-branding/', config)).toBe('/login-branding/');
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('allows configured backend origin', () => {
|
|
77
|
+
expect(validateLoginPanelUrl('https://api.example.com/branding/', config)).toBe(
|
|
78
|
+
'https://api.example.com/branding/',
|
|
79
|
+
);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('allows localhost in development', () => {
|
|
83
|
+
vi.stubEnv('NODE_ENV', 'development');
|
|
84
|
+
expect(validateLoginPanelUrl('http://localhost:5176/login-branding/', config)).toBe(
|
|
85
|
+
'http://localhost:5176/login-branding/',
|
|
86
|
+
);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('rejects localhost in production', () => {
|
|
90
|
+
vi.stubEnv('NODE_ENV', 'production');
|
|
91
|
+
expect(validateLoginPanelUrl('http://localhost:5176/login-branding/', config)).toBeNull();
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('rejects non-http(s) schemes and untrusted origins', () => {
|
|
95
|
+
vi.stubEnv('NODE_ENV', 'production');
|
|
96
|
+
expect(validateLoginPanelUrl('javascript:alert(1)', config)).toBeNull();
|
|
97
|
+
expect(validateLoginPanelUrl('https://evil.example.com/panel', config)).toBeNull();
|
|
98
|
+
});
|
|
99
|
+
});
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import type { ShellUIConfig } from '../config/types';
|
|
2
|
+
|
|
3
|
+
/** True when the shell is running a development build (localhost modal/panel exceptions apply). */
|
|
4
|
+
export const isShellDevelopment = (): boolean => process.env.NODE_ENV === 'development';
|
|
5
|
+
|
|
6
|
+
export const isLocalhostHostname = (hostname: string): boolean => {
|
|
7
|
+
const normalized = hostname.toLowerCase().replace(/^\[|\]$/g, '');
|
|
8
|
+
return normalized === 'localhost' || normalized === '127.0.0.1' || normalized === '::1';
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const originFromAbsoluteUrl = (value: string | undefined | null): string | null => {
|
|
12
|
+
if (!value || typeof value !== 'string') {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
const trimmed = value.trim();
|
|
16
|
+
if (!trimmed.startsWith('http://') && !trimmed.startsWith('https://')) {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
try {
|
|
20
|
+
return new URL(trimmed).origin;
|
|
21
|
+
} catch {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const getConfiguredTrustedOrigins = (config?: ShellUIConfig | null): Set<string> => {
|
|
27
|
+
const origins = new Set<string>();
|
|
28
|
+
const add = (value?: string | null) => {
|
|
29
|
+
const origin = originFromAbsoluteUrl(value);
|
|
30
|
+
if (origin) {
|
|
31
|
+
origins.add(origin);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
add(config?.backend?.url);
|
|
36
|
+
add(config?.backend?.loginUrl);
|
|
37
|
+
add(config?.storage?.url);
|
|
38
|
+
add(config?.storage?.filesUrl);
|
|
39
|
+
|
|
40
|
+
return origins;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export type UrlAllowlistOptions = {
|
|
44
|
+
/** When true, loopback hostnames are allowed (development builds only). */
|
|
45
|
+
allowLocalhost?: boolean;
|
|
46
|
+
config?: ShellUIConfig | null;
|
|
47
|
+
/** Current shell origin; required to allow same-origin absolute URLs. */
|
|
48
|
+
currentOrigin?: string;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Whether an absolute http(s) URL origin is allowed for iframe embedding (modal, login panel).
|
|
53
|
+
* Relative paths are validated separately and resolve to the shell origin.
|
|
54
|
+
*/
|
|
55
|
+
export const isAllowedIframeOrigin = (
|
|
56
|
+
origin: string,
|
|
57
|
+
{ allowLocalhost = false, config, currentOrigin = '' }: UrlAllowlistOptions = {},
|
|
58
|
+
): boolean => {
|
|
59
|
+
if (currentOrigin && origin === currentOrigin) {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (allowLocalhost && isShellDevelopment()) {
|
|
64
|
+
try {
|
|
65
|
+
const { hostname } = new URL(origin);
|
|
66
|
+
if (isLocalhostHostname(hostname)) {
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
} catch {
|
|
70
|
+
// fall through
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return getConfiguredTrustedOrigins(config).has(origin);
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Validates login branding `panelUrl` before loading it in an unsandboxed iframe.
|
|
79
|
+
* Allows same-origin relative paths, same-origin absolute URLs, configured backend/login/storage
|
|
80
|
+
* origins, and loopback URLs in development only. Rejects other schemes and origins.
|
|
81
|
+
*/
|
|
82
|
+
export const validateLoginPanelUrl = (
|
|
83
|
+
url: string | undefined | null,
|
|
84
|
+
config?: ShellUIConfig | null,
|
|
85
|
+
): string | null => {
|
|
86
|
+
if (!url || typeof url !== 'string') {
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const trimmed = url.trim();
|
|
91
|
+
if (!trimmed) {
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Relative paths resolve to the shell origin and are safe.
|
|
96
|
+
if (trimmed.startsWith('/') && !trimmed.startsWith('//')) {
|
|
97
|
+
return trimmed;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (!trimmed.startsWith('http://') && !trimmed.startsWith('https://')) {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
try {
|
|
105
|
+
const urlObj = new URL(trimmed);
|
|
106
|
+
const currentOrigin = typeof window !== 'undefined' ? window.location.origin : '';
|
|
107
|
+
|
|
108
|
+
if (
|
|
109
|
+
isAllowedIframeOrigin(urlObj.origin, {
|
|
110
|
+
allowLocalhost: true,
|
|
111
|
+
config,
|
|
112
|
+
currentOrigin,
|
|
113
|
+
})
|
|
114
|
+
) {
|
|
115
|
+
return trimmed;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return null;
|
|
119
|
+
} catch {
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
};
|
|
@@ -8,10 +8,8 @@ import {
|
|
|
8
8
|
} from '@shellui/sdk';
|
|
9
9
|
import { SettingsContext } from './SettingsContext';
|
|
10
10
|
import { useConfig } from '../config/useConfig';
|
|
11
|
-
import type { NavigationItem } from '../config/types';
|
|
12
11
|
import { useAuth } from '../auth/hooks/useAuth';
|
|
13
|
-
import {
|
|
14
|
-
import { isFrameForAppUrl } from '../layouts/utils';
|
|
12
|
+
import { isTrustedFrameForAuthToken } from '../security/trustedFrames';
|
|
15
13
|
import { isMainLayoutFrame } from '../layouts/floating/layoutChromeStore';
|
|
16
14
|
import { defaultTheme } from '../theme/themes';
|
|
17
15
|
import {
|
|
@@ -30,9 +28,6 @@ const STORAGE_KEY = 'shellui:settings';
|
|
|
30
28
|
const AUTH_SESSION_STORAGE_KEY = 'shellui.auth.session';
|
|
31
29
|
const AUTH_LAST_USED_LOGIN_STORAGE_KEY = 'shellui.auth.last_used_login';
|
|
32
30
|
|
|
33
|
-
const isFrameForNavigationItem = (frameSrc: string, itemUrl: string): boolean =>
|
|
34
|
-
isFrameForAppUrl(frameSrc, itemUrl);
|
|
35
|
-
|
|
36
31
|
const stripSensitiveUserFields = (settings: Settings): Settings => {
|
|
37
32
|
return {
|
|
38
33
|
...settings,
|
|
@@ -170,40 +165,9 @@ export function SettingsProvider({ children }: { children: ReactNode }) {
|
|
|
170
165
|
return defaultSettings;
|
|
171
166
|
});
|
|
172
167
|
|
|
173
|
-
const
|
|
174
|
-
() =>
|
|
175
|
-
|
|
176
|
-
'title' in item && 'items' in item ? item.items : [item],
|
|
177
|
-
) ?? [],
|
|
178
|
-
[config?.navigation],
|
|
179
|
-
);
|
|
180
|
-
|
|
181
|
-
const isTrustedFrameForAuthToken = useCallback(
|
|
182
|
-
(frameSrc: string): boolean => {
|
|
183
|
-
if (isAdminFrame(frameSrc, config)) {
|
|
184
|
-
return true;
|
|
185
|
-
}
|
|
186
|
-
// Trust admin-panel iframe apps (navigation + storage files explorer / viewer modals).
|
|
187
|
-
const adminNavItems = config?.administration?.navigation ?? [];
|
|
188
|
-
if (
|
|
189
|
-
adminNavItems.some(
|
|
190
|
-
(item) =>
|
|
191
|
-
item.openIn !== 'external' &&
|
|
192
|
-
Boolean(item.url?.trim()) &&
|
|
193
|
-
isFrameForNavigationItem(frameSrc, item.url),
|
|
194
|
-
)
|
|
195
|
-
) {
|
|
196
|
-
return true;
|
|
197
|
-
}
|
|
198
|
-
const filesUrl = config?.storage?.filesUrl?.trim();
|
|
199
|
-
if (filesUrl && isFrameForNavigationItem(frameSrc, filesUrl)) {
|
|
200
|
-
return true;
|
|
201
|
-
}
|
|
202
|
-
return navigationItems.some(
|
|
203
|
-
(item) => item.safeForAuthToken !== false && isFrameForNavigationItem(frameSrc, item.url),
|
|
204
|
-
);
|
|
205
|
-
},
|
|
206
|
-
[config, navigationItems],
|
|
168
|
+
const isTrustedFrameForAuthTokenCallback = useCallback(
|
|
169
|
+
(frameSrc: string): boolean => isTrustedFrameForAuthToken(frameSrc, config),
|
|
170
|
+
[config],
|
|
207
171
|
);
|
|
208
172
|
|
|
209
173
|
const accessTokenRef = useRef<string | null>(session?.accessToken ?? null);
|
|
@@ -217,7 +181,7 @@ export function SettingsProvider({ children }: { children: ReactNode }) {
|
|
|
217
181
|
const accessToken = accessTokenRef.current;
|
|
218
182
|
for (const [uuid, iframe] of iframes) {
|
|
219
183
|
const frameSrc = iframe?.src ?? '';
|
|
220
|
-
const includeAuthAccessToken =
|
|
184
|
+
const includeAuthAccessToken = isTrustedFrameForAuthTokenCallback(frameSrc);
|
|
221
185
|
const settingsToPropagate = buildSettingsForPropagation(baseSettings, config, lang, {
|
|
222
186
|
includeAuthAccessToken,
|
|
223
187
|
accessToken,
|
|
@@ -230,13 +194,13 @@ export function SettingsProvider({ children }: { children: ReactNode }) {
|
|
|
230
194
|
});
|
|
231
195
|
}
|
|
232
196
|
},
|
|
233
|
-
[config,
|
|
197
|
+
[config, isTrustedFrameForAuthTokenCallback],
|
|
234
198
|
);
|
|
235
199
|
|
|
236
200
|
const pushSettingsToFrame = useCallback(
|
|
237
201
|
(iframeUuid: string, frameSrc: string, baseSettings: Settings) => {
|
|
238
202
|
const lang = baseSettings.language?.code || 'en';
|
|
239
|
-
const includeAuthAccessToken =
|
|
203
|
+
const includeAuthAccessToken = isTrustedFrameForAuthTokenCallback(frameSrc);
|
|
240
204
|
const iframe = shellui.frameRegistry
|
|
241
205
|
.getAllIframes()
|
|
242
206
|
.find(([uuid]) => uuid === iframeUuid)?.[1];
|
|
@@ -251,7 +215,7 @@ export function SettingsProvider({ children }: { children: ReactNode }) {
|
|
|
251
215
|
to: [iframeUuid],
|
|
252
216
|
});
|
|
253
217
|
},
|
|
254
|
-
[config,
|
|
218
|
+
[config, isTrustedFrameForAuthTokenCallback],
|
|
255
219
|
);
|
|
256
220
|
|
|
257
221
|
// When the shell rotates the JWT, `authUser` often does not change, so the user-sync effect
|
|
@@ -471,7 +435,7 @@ export function SettingsProvider({ children }: { children: ReactNode }) {
|
|
|
471
435
|
.find(([uuid]) => uuid === firstHopIframeUuid)?.[1];
|
|
472
436
|
const lang = currentSettings.language?.code || 'en';
|
|
473
437
|
const includeAuthAccessToken = frame
|
|
474
|
-
?
|
|
438
|
+
? isTrustedFrameForAuthTokenCallback(frame.src ?? '')
|
|
475
439
|
: false;
|
|
476
440
|
const settingsToPropagate = buildSettingsForPropagation(currentSettings, config, lang, {
|
|
477
441
|
includeAuthAccessToken,
|
|
@@ -541,7 +505,7 @@ export function SettingsProvider({ children }: { children: ReactNode }) {
|
|
|
541
505
|
};
|
|
542
506
|
}, [
|
|
543
507
|
config,
|
|
544
|
-
|
|
508
|
+
isTrustedFrameForAuthTokenCallback,
|
|
545
509
|
propagateSettingsToIframes,
|
|
546
510
|
pushSettingsToFrame,
|
|
547
511
|
schedulePropagateSettingsToIframes,
|
|
@@ -116,7 +116,29 @@ describe('buildSettingsForPropagation', () => {
|
|
|
116
116
|
expect(result.appearance?.availableThemes?.length).toBeGreaterThan(0);
|
|
117
117
|
});
|
|
118
118
|
|
|
119
|
-
it('
|
|
119
|
+
it('never propagates refresh tokens to iframe settings', () => {
|
|
120
|
+
const settingsWithRefresh = {
|
|
121
|
+
...baseSettings,
|
|
122
|
+
refreshToken: 'secret-refresh-token',
|
|
123
|
+
} as Settings & { refreshToken: string };
|
|
124
|
+
|
|
125
|
+
const result = buildSettingsForPropagation(settingsWithRefresh, undefined, 'en', {
|
|
126
|
+
includeAuthAccessToken: true,
|
|
127
|
+
accessToken: 'access-jwt',
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
expect(result.accessToken).toBe('access-jwt');
|
|
131
|
+
expect(result).not.toHaveProperty('refreshToken');
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it('defaults access token to null when includeAuthAccessToken is omitted', () => {
|
|
135
|
+
const result = buildSettingsForPropagation(baseSettings, undefined, 'en', {
|
|
136
|
+
accessToken: 'jwt.should.not.be.exposed',
|
|
137
|
+
});
|
|
138
|
+
expect(result.accessToken).toBeNull();
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('injects access token into settings only when explicitly allowed', () => {
|
|
120
142
|
const settingsWithUser: Settings = {
|
|
121
143
|
...baseSettings,
|
|
122
144
|
user: {
|
|
@@ -122,5 +122,12 @@ export const buildSettingsForPropagation = (
|
|
|
122
122
|
authBackendBaseUrl,
|
|
123
123
|
};
|
|
124
124
|
|
|
125
|
+
if ('refreshToken' in result) {
|
|
126
|
+
const { refreshToken: _refreshRemoved, ...withoutRefresh } = result as Settings & {
|
|
127
|
+
refreshToken?: string;
|
|
128
|
+
};
|
|
129
|
+
return withoutRefresh;
|
|
130
|
+
}
|
|
131
|
+
|
|
125
132
|
return result;
|
|
126
133
|
};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import type { ShellUIConfig } from '../config/types';
|
|
3
|
+
import { getStorageRequestTrustDenial } from '../security/trustedFrames';
|
|
4
|
+
import { handleStorageRequest } from './handleRequest';
|
|
5
|
+
|
|
6
|
+
const config = {
|
|
7
|
+
navigation: [
|
|
8
|
+
{
|
|
9
|
+
label: 'Companion',
|
|
10
|
+
path: 'companion',
|
|
11
|
+
url: 'http://localhost:5175/',
|
|
12
|
+
safeForAuthToken: true,
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
label: 'Hostile',
|
|
16
|
+
path: 'hostile',
|
|
17
|
+
url: 'http://localhost:5199/',
|
|
18
|
+
safeForAuthToken: false,
|
|
19
|
+
},
|
|
20
|
+
],
|
|
21
|
+
} as ShellUIConfig;
|
|
22
|
+
|
|
23
|
+
describe('StorageBridge trust gate', () => {
|
|
24
|
+
it('rejects hostile iframe storage requests before storage I/O', async () => {
|
|
25
|
+
const fetchMock = vi.fn();
|
|
26
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
27
|
+
|
|
28
|
+
const denial = getStorageRequestTrustDenial(
|
|
29
|
+
['hostile-uuid'],
|
|
30
|
+
{
|
|
31
|
+
getAllIframes: () => [
|
|
32
|
+
['hostile-uuid', { src: 'http://localhost:5199/widget' } as HTMLIFrameElement],
|
|
33
|
+
],
|
|
34
|
+
},
|
|
35
|
+
config,
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
expect(denial).toEqual({
|
|
39
|
+
message: 'Storage request rejected: untrusted frame',
|
|
40
|
+
status: 403,
|
|
41
|
+
});
|
|
42
|
+
expect(fetchMock).not.toHaveBeenCalled();
|
|
43
|
+
|
|
44
|
+
vi.unstubAllGlobals();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('allows trusted companion frames to reach handleStorageRequest', async () => {
|
|
48
|
+
const denial = getStorageRequestTrustDenial(
|
|
49
|
+
['companion-uuid'],
|
|
50
|
+
{
|
|
51
|
+
getAllIframes: () => [
|
|
52
|
+
['companion-uuid', { src: 'http://localhost:5175/company' } as HTMLIFrameElement],
|
|
53
|
+
],
|
|
54
|
+
},
|
|
55
|
+
config,
|
|
56
|
+
);
|
|
57
|
+
expect(denial).toBeNull();
|
|
58
|
+
|
|
59
|
+
vi.stubGlobal(
|
|
60
|
+
'fetch',
|
|
61
|
+
vi.fn().mockResolvedValue({
|
|
62
|
+
ok: true,
|
|
63
|
+
status: 200,
|
|
64
|
+
headers: new Headers({ 'Content-Type': 'application/json' }),
|
|
65
|
+
json: async () => [{ name: 'company' }],
|
|
66
|
+
}),
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
await expect(
|
|
70
|
+
handleStorageRequest({
|
|
71
|
+
storageUrl: 'http://localhost:8001',
|
|
72
|
+
accessToken: 'token',
|
|
73
|
+
payload: { id: 'req-trusted', op: 'listBuckets' },
|
|
74
|
+
}),
|
|
75
|
+
).resolves.toEqual({
|
|
76
|
+
id: 'req-trusted',
|
|
77
|
+
data: [{ name: 'company' }],
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
vi.unstubAllGlobals();
|
|
81
|
+
});
|
|
82
|
+
});
|
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import { useEffect } from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
postShellMessage,
|
|
4
|
+
shellui,
|
|
5
|
+
type ShellUIMessage,
|
|
6
|
+
type StorageRequestPayload,
|
|
7
|
+
} from '@shellui/sdk';
|
|
3
8
|
import { useAuth } from '../auth/hooks/useAuth';
|
|
4
9
|
import { useConfig } from '../config/useConfig';
|
|
10
|
+
import { getStorageRequestTrustDenial } from '../security/trustedFrames';
|
|
5
11
|
import { handleStorageRequest } from './handleRequest';
|
|
6
12
|
import { getStorageBaseUrl } from './quota';
|
|
7
13
|
import { handleTrackedUpload, isUploadPayload } from './uploads/handleTrackedUpload';
|
|
@@ -25,6 +31,22 @@ export const StorageBridge = () => {
|
|
|
25
31
|
const payload = message.payload as StorageRequestPayload | undefined;
|
|
26
32
|
if (!payload?.id || !payload.op) return;
|
|
27
33
|
|
|
34
|
+
const trustDenial = getStorageRequestTrustDenial(message.from, shellui.frameRegistry, config);
|
|
35
|
+
if (trustDenial) {
|
|
36
|
+
const response = { id: payload.id, error: trustDenial };
|
|
37
|
+
const reply = {
|
|
38
|
+
type: 'SHELLUI_STORAGE_RESPONSE' as const,
|
|
39
|
+
payload: response,
|
|
40
|
+
};
|
|
41
|
+
const from = message.from?.filter(Boolean) as string[] | undefined;
|
|
42
|
+
if (from?.length) {
|
|
43
|
+
shellui.sendMessage({ ...reply, to: from });
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
window.postMessage(reply, '*');
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
|
|
28
50
|
const run = isUploadPayload(payload)
|
|
29
51
|
? handleTrackedUpload({ storageUrl, accessToken, payload })
|
|
30
52
|
: handleStorageRequest({ storageUrl, accessToken, payload });
|
|
@@ -39,12 +61,12 @@ export const StorageBridge = () => {
|
|
|
39
61
|
shellui.sendMessage({ ...reply, to: from });
|
|
40
62
|
return;
|
|
41
63
|
}
|
|
42
|
-
|
|
64
|
+
postShellMessage(reply);
|
|
43
65
|
});
|
|
44
66
|
};
|
|
45
67
|
|
|
46
68
|
return shellui.addMessageListener('SHELLUI_STORAGE_REQUEST', listener);
|
|
47
|
-
}, [storageUrl, accessToken]);
|
|
69
|
+
}, [storageUrl, accessToken, config]);
|
|
48
70
|
|
|
49
71
|
return null;
|
|
50
72
|
};
|
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
} from 'react';
|
|
10
10
|
import { useTranslation } from 'react-i18next';
|
|
11
11
|
import {
|
|
12
|
+
postShellMessage,
|
|
12
13
|
shellui,
|
|
13
14
|
type ShellUIMessage,
|
|
14
15
|
type StorageSelectMode,
|
|
@@ -63,9 +64,7 @@ function replyToRequester(pending: PendingSelect, payload: StorageSelectResponse
|
|
|
63
64
|
shellui.sendMessage({ ...message, to });
|
|
64
65
|
return;
|
|
65
66
|
}
|
|
66
|
-
|
|
67
|
-
window.postMessage(message, '*');
|
|
68
|
-
}
|
|
67
|
+
postShellMessage(message);
|
|
69
68
|
}
|
|
70
69
|
|
|
71
70
|
interface StoragePickerProviderProps {
|
|
@@ -104,7 +103,7 @@ export const StoragePickerProvider = ({ children }: StoragePickerProviderProps)
|
|
|
104
103
|
shellui.sendMessage({ ...reply, to });
|
|
105
104
|
return;
|
|
106
105
|
}
|
|
107
|
-
|
|
106
|
+
postShellMessage(reply);
|
|
108
107
|
}, []);
|
|
109
108
|
|
|
110
109
|
useEffect(() => {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { postShellMessage } from '@shellui/sdk';
|
|
1
2
|
import type { UploadItem, UploadItemStatus, UploadQueueSummary } from './types';
|
|
2
3
|
|
|
3
4
|
export const UPLOAD_TOAST_ID = 'shellui-upload-progress';
|
|
@@ -341,11 +342,7 @@ function clearDemoUploads(): void {
|
|
|
341
342
|
export function requestUploadToastDemo(): void {
|
|
342
343
|
if (typeof window === 'undefined') return;
|
|
343
344
|
const message = { type: UPLOAD_TOAST_DEMO_MESSAGE, payload: {} };
|
|
344
|
-
|
|
345
|
-
window.parent.postMessage(message, '*');
|
|
346
|
-
return;
|
|
347
|
-
}
|
|
348
|
-
window.postMessage(message, '*');
|
|
345
|
+
postShellMessage(message);
|
|
349
346
|
}
|
|
350
347
|
|
|
351
348
|
/** Simulated uploads for the Develop panel — no network requests. */
|
|
@@ -5,31 +5,46 @@ import { Navigate, useLocation } from 'react-router';
|
|
|
5
5
|
import urls from '../../constants/urls';
|
|
6
6
|
import { buildAuthUrlWithNext } from '../../features/auth/utils';
|
|
7
7
|
import { useAuth } from '../../features/auth/hooks/useAuth';
|
|
8
|
+
import { AdminForbiddenAccess } from '../../features/admin/components/AdminForbiddenAccess';
|
|
9
|
+
import { getNavigationRouteAccess } from '../utils/navigationRouteAccess';
|
|
8
10
|
import { RouteFallback } from './RouteFallback';
|
|
9
11
|
|
|
10
12
|
export const NavigationItemRoute = () => {
|
|
11
13
|
const location = useLocation();
|
|
12
|
-
const { isAuthenticated, isLoading } = useAuth();
|
|
14
|
+
const { isAuthenticated, isLoading, user } = useAuth();
|
|
13
15
|
const { url, currentItem } = useNavigationItems();
|
|
14
16
|
|
|
15
17
|
if (!currentItem) {
|
|
16
18
|
return <NotFoundView />;
|
|
17
19
|
}
|
|
18
20
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
21
|
+
const next = `${location.pathname}${location.search}`;
|
|
22
|
+
const access = getNavigationRouteAccess(
|
|
23
|
+
currentItem,
|
|
24
|
+
{
|
|
25
|
+
isLoading,
|
|
26
|
+
isAuthenticated,
|
|
27
|
+
isStaff: Boolean(user?.isStaff),
|
|
28
|
+
},
|
|
29
|
+
next,
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
if (access.kind === 'loading') {
|
|
33
|
+
return <RouteFallback />;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (access.kind === 'login') {
|
|
37
|
+
const loginUrl = buildAuthUrlWithNext(urls.login, access.next);
|
|
38
|
+
return (
|
|
39
|
+
<Navigate
|
|
40
|
+
to={loginUrl}
|
|
41
|
+
replace
|
|
42
|
+
/>
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (access.kind === 'forbidden') {
|
|
47
|
+
return <AdminForbiddenAccess />;
|
|
33
48
|
}
|
|
34
49
|
|
|
35
50
|
return (
|