@shellui/core 0.4.0 → 0.4.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": "@shellui/core",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "ShellUI Core - Core React application runtime",
5
5
  "main": "src/index.ts",
6
6
  "type": "module",
@@ -62,7 +62,7 @@
62
62
  "workbox-routing": "^7.4.0",
63
63
  "workbox-strategies": "^7.4.0",
64
64
  "workbox-window": "^7.4.0",
65
- "@shellui/sdk": "0.4.0"
65
+ "@shellui/sdk": "0.4.1"
66
66
  },
67
67
  "peerDependencies": {
68
68
  "react": "^18.0.0 || ^19.0.0",
@@ -0,0 +1,77 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { DEFAULT_ADMIN_URL } from '../admin/config';
3
+ import type { ShellUIConfig } from '../config/types';
4
+ import { validateAndNormalizeUrl } from './validateAndNormalizeUrl';
5
+
6
+ const config = {
7
+ storage: {
8
+ url: 'https://storage.example.com/',
9
+ filesUrl: 'https://files.example.com/',
10
+ },
11
+ backend: {
12
+ type: 'shellui',
13
+ url: 'https://api.example.com',
14
+ adminUrl: 'https://admin.example.com',
15
+ },
16
+ administration: {
17
+ title: 'Apps',
18
+ navigation: [
19
+ {
20
+ label: 'Billing',
21
+ path: 'billing',
22
+ url: 'https://billing.example.com/app',
23
+ },
24
+ {
25
+ label: 'Django admin',
26
+ path: 'django-admin',
27
+ url: 'https://untrusted.example.com/admin',
28
+ openIn: 'external',
29
+ },
30
+ ],
31
+ },
32
+ } as ShellUIConfig;
33
+
34
+ describe('validateAndNormalizeUrl', () => {
35
+ it('allows relative paths', () => {
36
+ expect(validateAndNormalizeUrl('/settings')).toBe('/settings');
37
+ });
38
+
39
+ it('allows localhost urls', () => {
40
+ expect(validateAndNormalizeUrl('http://localhost:5175/#/select')).toBe(
41
+ 'http://localhost:5175/#/select',
42
+ );
43
+ });
44
+
45
+ it('rejects unrelated origins', () => {
46
+ expect(validateAndNormalizeUrl('https://evil.example.com/form', config)).toBeNull();
47
+ });
48
+
49
+ it('allows storage.url and storage.filesUrl origins', () => {
50
+ expect(validateAndNormalizeUrl('https://storage.example.com/storage/v1/quota', config)).toBe(
51
+ 'https://storage.example.com/storage/v1/quota',
52
+ );
53
+ expect(validateAndNormalizeUrl('https://files.example.com/#/select?mode=files', config)).toBe(
54
+ 'https://files.example.com/#/select?mode=files',
55
+ );
56
+ });
57
+
58
+ it('allows the configured administration adminUrl', () => {
59
+ expect(validateAndNormalizeUrl('https://admin.example.com/#/users', config)).toBe(
60
+ 'https://admin.example.com/#/users',
61
+ );
62
+ expect(validateAndNormalizeUrl(`${DEFAULT_ADMIN_URL}/#/users`, config)).toBeNull();
63
+ });
64
+
65
+ it('allows the default administration url when adminUrl is not configured', () => {
66
+ expect(validateAndNormalizeUrl(`${DEFAULT_ADMIN_URL}/#/dashboard`)).toBe(
67
+ `${DEFAULT_ADMIN_URL}/#/dashboard`,
68
+ );
69
+ });
70
+
71
+ it('allows administration navigation urls but not external ones', () => {
72
+ expect(validateAndNormalizeUrl('https://billing.example.com/app/#/invoices', config)).toBe(
73
+ 'https://billing.example.com/app/#/invoices',
74
+ );
75
+ expect(validateAndNormalizeUrl('https://untrusted.example.com/admin', config)).toBeNull();
76
+ });
77
+ });
@@ -1,51 +1,9 @@
1
1
  import { shellui, type ShellUIMessage } from '@shellui/sdk';
2
2
  import { createContext, useContext, useState, useEffect, useCallback, type ReactNode } from 'react';
3
+ import { useConfig } from '../config/useConfig';
4
+ import { validateAndNormalizeUrl } from './validateAndNormalizeUrl';
3
5
 
4
- /**
5
- * Validates and normalizes a URL to ensure it's from the same domain or localhost
6
- * @param url - The URL or path to validate
7
- * @returns The normalized absolute URL or null if invalid
8
- */
9
- export const validateAndNormalizeUrl = (url: string | undefined | null): string | null => {
10
- if (!url || typeof url !== 'string') {
11
- return null;
12
- }
13
-
14
- try {
15
- // If it's already an absolute URL, check if it's same origin or localhost
16
- if (url.startsWith('http://') || url.startsWith('https://')) {
17
- const urlObj = new URL(url);
18
- const currentOrigin = typeof window !== 'undefined' ? window.location.origin : '';
19
-
20
- // Allow same origin
21
- if (urlObj.origin === currentOrigin) {
22
- return url;
23
- }
24
-
25
- // Allow localhost URLs (for development)
26
- if (urlObj.hostname === 'localhost' || urlObj.hostname === '127.0.0.1') {
27
- return url;
28
- }
29
-
30
- return null; // Different origin, reject for security
31
- }
32
-
33
- // If it's a relative URL, make it absolute using current origin
34
- if (url.startsWith('/') || url.startsWith('./') || !url.startsWith('//')) {
35
- const currentOrigin = typeof window !== 'undefined' ? window.location.origin : '';
36
- // Ensure relative paths start with /
37
- const normalizedPath = url.startsWith('/') ? url : `/${url}`;
38
- return `${currentOrigin}${normalizedPath}`;
39
- }
40
-
41
- // Reject protocol-relative URLs (//example.com) for security
42
- return null;
43
- } catch (error) {
44
- // eslint-disable-next-line no-console
45
- console.error('Invalid URL:', url, error);
46
- return null;
47
- }
48
- };
6
+ export { validateAndNormalizeUrl } from './validateAndNormalizeUrl';
49
7
 
50
8
  interface ModalContextValue {
51
9
  isOpen: boolean;
@@ -69,14 +27,18 @@ interface ModalProviderProps {
69
27
  }
70
28
 
71
29
  export const ModalProvider = ({ children }: ModalProviderProps) => {
30
+ const { config } = useConfig();
72
31
  const [isOpen, setIsOpen] = useState(false);
73
32
  const [modalUrl, setModalUrl] = useState<string | null>(null);
74
33
 
75
- const openModal = useCallback((url?: string) => {
76
- const validatedUrl = url ? validateAndNormalizeUrl(url) : null;
77
- setModalUrl(validatedUrl);
78
- setIsOpen(true);
79
- }, []);
34
+ const openModal = useCallback(
35
+ (url?: string) => {
36
+ const validatedUrl = url ? validateAndNormalizeUrl(url, config) : null;
37
+ setModalUrl(validatedUrl);
38
+ setIsOpen(true);
39
+ },
40
+ [config],
41
+ );
80
42
 
81
43
  const closeModal = useCallback(() => {
82
44
  setIsOpen(false);
@@ -0,0 +1,96 @@
1
+ import { getAdminContentUrl } from '../admin/config';
2
+ import type { ShellUIConfig } from '../config/types';
3
+
4
+ const originFromAbsoluteUrl = (value: string | undefined | null): string | null => {
5
+ if (!value || typeof value !== 'string') {
6
+ return null;
7
+ }
8
+ const trimmed = value.trim();
9
+ if (!trimmed.startsWith('http://') && !trimmed.startsWith('https://')) {
10
+ return null;
11
+ }
12
+ try {
13
+ return new URL(trimmed).origin;
14
+ } catch {
15
+ return null;
16
+ }
17
+ };
18
+
19
+ /** Origins allowed in modal iframes: storage service/files app, admin panel, and administration nav apps. */
20
+ const getAllowedModalOrigins = (config?: ShellUIConfig | null): Set<string> => {
21
+ const origins = new Set<string>();
22
+ const add = (value?: string | null) => {
23
+ const origin = originFromAbsoluteUrl(value);
24
+ if (origin) {
25
+ origins.add(origin);
26
+ }
27
+ };
28
+
29
+ add(config?.storage?.url);
30
+ add(config?.storage?.filesUrl);
31
+ add(getAdminContentUrl(config ?? undefined));
32
+
33
+ for (const item of config?.administration?.navigation ?? []) {
34
+ if (item.openIn === 'external') {
35
+ continue;
36
+ }
37
+ add(item.url);
38
+ }
39
+
40
+ return origins;
41
+ };
42
+
43
+ /**
44
+ * Validates and normalizes a URL to ensure it's from the same domain, localhost,
45
+ * storage, or administration origins configured on the host.
46
+ * @param url - The URL or path to validate
47
+ * @param config - Host config used to allow storage and administration origins
48
+ * @returns The normalized absolute URL or null if invalid
49
+ */
50
+ export const validateAndNormalizeUrl = (
51
+ url: string | undefined | null,
52
+ config?: ShellUIConfig | null,
53
+ ): string | null => {
54
+ if (!url || typeof url !== 'string') {
55
+ return null;
56
+ }
57
+
58
+ try {
59
+ // If it's already an absolute URL, check if it's same origin, localhost, or trusted
60
+ if (url.startsWith('http://') || url.startsWith('https://')) {
61
+ const urlObj = new URL(url);
62
+ const currentOrigin = typeof window !== 'undefined' ? window.location.origin : '';
63
+
64
+ // Allow same origin
65
+ if (urlObj.origin === currentOrigin) {
66
+ return url;
67
+ }
68
+
69
+ // Allow localhost URLs (for development)
70
+ if (urlObj.hostname === 'localhost' || urlObj.hostname === '127.0.0.1') {
71
+ return url;
72
+ }
73
+
74
+ if (getAllowedModalOrigins(config).has(urlObj.origin)) {
75
+ return url;
76
+ }
77
+
78
+ return null; // Different origin, reject for security
79
+ }
80
+
81
+ // If it's a relative URL, make it absolute using current origin
82
+ if (url.startsWith('/') || url.startsWith('./') || !url.startsWith('//')) {
83
+ const currentOrigin = typeof window !== 'undefined' ? window.location.origin : '';
84
+ // Ensure relative paths start with /
85
+ const normalizedPath = url.startsWith('/') ? url : `/${url}`;
86
+ return `${currentOrigin}${normalizedPath}`;
87
+ }
88
+
89
+ // Reject protocol-relative URLs (//example.com) for security
90
+ return null;
91
+ } catch (error) {
92
+ // eslint-disable-next-line no-console
93
+ console.error('Invalid URL:', url, error);
94
+ return null;
95
+ }
96
+ };
@@ -20,7 +20,7 @@ import { Dialog, DialogContent, DialogDescription, DialogTitle } from '../../com
20
20
  import { Z_INDEX } from '../../lib/z-index';
21
21
  import type { NavigationItem } from '../config/types';
22
22
  import { useConfig } from '../config/useConfig';
23
- import { validateAndNormalizeUrl } from '../modal/ModalContext';
23
+ import { validateAndNormalizeUrl } from '../modal/validateAndNormalizeUrl';
24
24
 
25
25
  type PendingSelect = {
26
26
  id: string;
@@ -133,7 +133,7 @@ export const StoragePickerProvider = ({ children }: StoragePickerProviderProps)
133
133
  multiple: Boolean(payload.multiple),
134
134
  mode,
135
135
  });
136
- const url = validateAndNormalizeUrl(rawUrl);
136
+ const url = validateAndNormalizeUrl(rawUrl, config);
137
137
  if (!url) {
138
138
  failRequest(message, 'Storage picker URL is not allowed.', 400);
139
139
  return;
@@ -167,7 +167,7 @@ export const StoragePickerProvider = ({ children }: StoragePickerProviderProps)
167
167
  stopOpen();
168
168
  stopResult();
169
169
  };
170
- }, [config?.storage?.filesUrl, failRequest]);
170
+ }, [config, failRequest]);
171
171
 
172
172
  const pickerNavItem = {
173
173
  path: '__storage-picker',