@openmrs/esm-login-app 9.0.3-pre.4841 → 10.0.0

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.
@@ -31,7 +31,7 @@ describe('ChangeLocationLink', () => {
31
31
  await user.click(changeLocationButton);
32
32
 
33
33
  expect(mockNavigate).toHaveBeenCalledWith({
34
- to: '${openmrsSpaBase}/login/location?returnToUrl=/openmrs/spa/home&update=true',
34
+ to: '${openmrsSpaBase}/login/location?returnToUrl=%2Fopenmrs%2Fspa%2Fhome&update=true',
35
35
  });
36
36
  });
37
37
  });
@@ -16,6 +16,20 @@ import type { ConfigSchema } from '../config-schema';
16
16
  import type { LoginReferrer } from '../login/login.component';
17
17
  import styles from './location-picker.scss';
18
18
 
19
+ /**
20
+ * Validates that a returnToUrl is safe to navigate to.
21
+ * Only same-origin URLs are permitted, preventing open-redirect attacks after login.
22
+ */
23
+ export function isSafeReturnUrl(url: string): boolean {
24
+ if (!url) return false;
25
+ try {
26
+ const parsed = new URL(url, window.location.origin);
27
+ return parsed.origin === window.location.origin;
28
+ } catch {
29
+ return false;
30
+ }
31
+ }
32
+
19
33
  interface LocationPickerProps {
20
34
  hideWelcomeMessage?: boolean;
21
35
  currentLocationUuid?: string;
@@ -70,11 +84,11 @@ const LocationPickerView: React.FC<LocationPickerProps> = ({ hideWelcomeMessage,
70
84
 
71
85
  updateDefaultLocation(locationUuid, saveUserPreference);
72
86
  sessionDefined.then(() => {
73
- if (referrer && !['/', '/login', '/login/location'].includes(referrer)) {
87
+ if (referrer && referrer.startsWith('/') && !['/', '/login', '/login/location'].includes(referrer)) {
74
88
  navigate({ to: '${openmrsSpaBase}' + referrer });
75
89
  return;
76
90
  }
77
- if (returnToUrl && returnToUrl !== '/') {
91
+ if (returnToUrl && isSafeReturnUrl(returnToUrl)) {
78
92
  navigate({ to: returnToUrl });
79
93
  } else {
80
94
  navigate({ to: config.links.loginSuccess });
@@ -3,6 +3,7 @@ import '@testing-library/jest-dom/vitest';
3
3
  import { screen, waitFor } from '@testing-library/react';
4
4
  import userEvent from '@testing-library/user-event';
5
5
  import {
6
+ navigate,
6
7
  openmrsFetch,
7
8
  setSessionLocation,
8
9
  setUserProperties,
@@ -21,7 +22,7 @@ import {
21
22
  } from '../../__mocks__/locations.mock';
22
23
  import { mockConfig } from '../../__mocks__/config.mock';
23
24
  import renderWithRouter from '../test-helpers/render-with-router';
24
- import LocationPickerView from './location-picker-view.component';
25
+ import LocationPickerView, { isSafeReturnUrl } from './location-picker-view.component';
25
26
 
26
27
  const fistLocation = {
27
28
  uuid: 'uuid_1',
@@ -43,6 +44,7 @@ const mockSetSessionLocation = vi.mocked(setSessionLocation);
43
44
  const mockSetUserProperties = vi.mocked(setUserProperties);
44
45
  const mockUseConnectivity = vi.mocked(useConnectivity);
45
46
  const mockShowSnackbar = vi.mocked(showSnackbar);
47
+ const mockNavigate = vi.mocked(navigate);
46
48
 
47
49
  describe('LocationPickerView', () => {
48
50
  beforeEach(() => {
@@ -344,3 +346,121 @@ describe('LocationPickerView', () => {
344
346
  });
345
347
  });
346
348
  });
349
+
350
+ describe('isSafeReturnUrl', () => {
351
+ it('rejects an absolute external URL', () => {
352
+ expect(isSafeReturnUrl('https://evil.com/openmrs/spa/home')).toBe(false);
353
+ });
354
+
355
+ it('accepts an absolute same-origin SPA URL', () => {
356
+ const sameOriginSpaUrl = `${window.location.origin}/openmrs/spa/home`;
357
+ expect(isSafeReturnUrl(sameOriginSpaUrl)).toBe(true);
358
+ });
359
+
360
+ it('rejects a protocol-relative external URL', () => {
361
+ expect(isSafeReturnUrl('//evil.com/openmrs/spa/home')).toBe(false);
362
+ });
363
+
364
+ it('accepts a same-origin URL outside the SPA base', () => {
365
+ expect(isSafeReturnUrl('/openmrs/admin/index.htm')).toBe(true);
366
+ });
367
+
368
+ it('rejects a javascript: URL', () => {
369
+ expect(isSafeReturnUrl('javascript:alert(1)')).toBe(false);
370
+ });
371
+
372
+ it('rejects an empty string', () => {
373
+ expect(isSafeReturnUrl('')).toBe(false);
374
+ });
375
+
376
+ it('accepts a valid same-origin SPA path', () => {
377
+ expect(isSafeReturnUrl('/openmrs/spa/home')).toBe(true);
378
+ });
379
+
380
+ it('accepts a valid same-origin SPA sub-path', () => {
381
+ expect(isSafeReturnUrl('/openmrs/spa/patient/123/chart')).toBe(true);
382
+ });
383
+ });
384
+
385
+ describe('returnToUrl open-redirect protection', () => {
386
+ beforeEach(() => {
387
+ vi.mocked(useConnectivity).mockReturnValue(true);
388
+ vi.mocked(useConfig).mockReturnValue(mockConfig);
389
+
390
+ vi.mocked(useSession).mockReturnValue({
391
+ user: {
392
+ display: 'Testy McTesterface',
393
+ uuid: '90bd24b3-e700-46b0-a5ef-c85afdfededd',
394
+ userProperties: {},
395
+ } as LoggedInUser,
396
+ } as Session);
397
+
398
+ vi.mocked(openmrsFetch).mockResolvedValue(mockLoginLocations as FetchResponse<unknown>);
399
+ vi.mocked(setSessionLocation).mockResolvedValue(undefined);
400
+ mockNavigate.mockClear();
401
+ });
402
+
403
+ it('rejects an external returnToUrl and falls back to loginSuccess', async () => {
404
+ const user = userEvent.setup();
405
+
406
+ renderWithRouter(LocationPickerView, {}, { routes: ['?returnToUrl=https://evil.com/steal-creds'] });
407
+
408
+ const location = await screen.findByRole('radio', { name: fistLocation.name });
409
+ await user.click(location);
410
+
411
+ const submitButton = screen.getByRole('button', { name: /confirm/i });
412
+ await user.click(submitButton);
413
+
414
+ await waitFor(() => {
415
+ expect(mockSetSessionLocation).toHaveBeenCalledWith(fistLocation.uuid, expect.anything());
416
+ });
417
+
418
+ await waitFor(() => {
419
+ expect(mockNavigate).toHaveBeenCalledWith({ to: mockConfig.links.loginSuccess });
420
+ });
421
+
422
+ expect(mockNavigate).not.toHaveBeenCalledWith({ to: 'https://evil.com/steal-creds' });
423
+ });
424
+
425
+ it('rejects a protocol-relative returnToUrl and falls back to loginSuccess', async () => {
426
+ const user = userEvent.setup();
427
+
428
+ renderWithRouter(LocationPickerView, {}, { routes: ['?returnToUrl=//evil.com/openmrs/spa/home'] });
429
+
430
+ const location = await screen.findByRole('radio', { name: fistLocation.name });
431
+ await user.click(location);
432
+
433
+ const submitButton = screen.getByRole('button', { name: /confirm/i });
434
+ await user.click(submitButton);
435
+
436
+ await waitFor(() => {
437
+ expect(mockSetSessionLocation).toHaveBeenCalledWith(fistLocation.uuid, expect.anything());
438
+ });
439
+
440
+ await waitFor(() => {
441
+ expect(mockNavigate).toHaveBeenCalledWith({ to: mockConfig.links.loginSuccess });
442
+ });
443
+
444
+ expect(mockNavigate).not.toHaveBeenCalledWith({ to: '//evil.com/openmrs/spa/home' });
445
+ });
446
+
447
+ it('accepts a valid same-origin SPA returnToUrl', async () => {
448
+ const user = userEvent.setup();
449
+
450
+ renderWithRouter(LocationPickerView, {}, { routes: ['?returnToUrl=/openmrs/spa/home'] });
451
+
452
+ const location = await screen.findByRole('radio', { name: fistLocation.name });
453
+ await user.click(location);
454
+
455
+ const submitButton = screen.getByRole('button', { name: /confirm/i });
456
+ await user.click(submitButton);
457
+
458
+ await waitFor(() => {
459
+ expect(mockSetSessionLocation).toHaveBeenCalledWith(fistLocation.uuid, expect.anything());
460
+ });
461
+
462
+ await waitFor(() => {
463
+ expect(mockNavigate).toHaveBeenCalledWith({ to: '/openmrs/spa/home' });
464
+ });
465
+ });
466
+ });
@@ -126,10 +126,10 @@ const Login: React.FC = () => {
126
126
  if (session.sessionLocation) {
127
127
  let to = loginLinks?.loginSuccess || '/home';
128
128
  if (location?.state?.referrer) {
129
+ // Only accept relative paths; absolute or protocol-relative referrers
130
+ // are silently ignored to prevent open-redirect attacks after login.
129
131
  if (location.state.referrer.startsWith('/')) {
130
132
  to = `\${openmrsSpaBase}${location.state.referrer}`;
131
- } else {
132
- to = location.state.referrer;
133
133
  }
134
134
  }
135
135
 
package/vitest.config.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import path from 'node:path';
1
2
  import { defineConfig } from 'vitest/config';
2
3
 
3
4
  export default defineConfig({
@@ -25,6 +26,7 @@ export default defineConfig({
25
26
  alias: {
26
27
  '@openmrs/esm-framework/src/internal': '@openmrs/esm-framework/mock',
27
28
  '@openmrs/esm-framework': '@openmrs/esm-framework/mock',
29
+ '@openmrs/esm-styleguide/src/internal': path.resolve(__dirname, '../../framework/esm-styleguide/src/internal.ts'),
28
30
  },
29
31
  },
30
32
  });