Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | // @ts-nocheck
import { resolveConfigFetchPolicy, fetchConfigJson } from './secureConfigFetch';
describe('secureConfigFetch', () => {
describe('resolveConfigFetchPolicy', () => {
it('allows arbitrary origin in unauthenticated environments', () => {
const result = resolveConfigFetchPolicy('https://untrusted.example.com/config.json', {
userAuthenticationService: {
getAuthorizationHeader: () => ({}),
},
});
expect(result.normalizedUrl).toBe('https://untrusted.example.com/config.json');
expect(result.isAuthenticated).toBe(false);
expect(result.isSameOrigin).toBe(false);
});
it('blocks non-allowlisted origins in authenticated environments', () => {
expect(() =>
resolveConfigFetchPolicy('https://untrusted.example.com/config.json', {
allowedOrigins: ['https://trusted.example.com'],
userAuthenticationService: {
getAuthorizationHeader: () => ({ Authorization: 'Bearer token123' }),
},
})
).toThrow('Blocked remote configuration origin');
});
it('allows allowlisted origin in authenticated environments', () => {
const result = resolveConfigFetchPolicy('http://localhost:5000/config.json', {
allowedOrigins: ['http://localhost:5000', 'https://trusted.example.com'],
userAuthenticationService: {
getAuthorizationHeader: () => ({ Authorization: 'Bearer token123' }),
},
});
expect(result.normalizedUrl).toBe('http://localhost:5000/config.json');
expect(result.isAuthenticated).toBe(true);
expect(result.isSameOrigin).toBe(false);
});
it('blocks authenticated fetch when allowlist is missing', () => {
expect(() =>
resolveConfigFetchPolicy('https://noTrustList.example.com/config.json', {
userAuthenticationService: {
getAuthorizationHeader: () => ({ Authorization: 'Bearer token123' }),
},
})
).toThrow('Blocked remote configuration origin');
});
it('allows same-origin in authenticated environments without allowlist', () => {
const result = resolveConfigFetchPolicy('/protected/config.json', {
userAuthenticationService: {
getAuthorizationHeader: () => ({ Authorization: 'Bearer token123' }),
},
});
expect(result.normalizedUrl).toBe(`${window.location.origin}/protected/config.json`);
expect(result.isAuthenticated).toBe(true);
expect(result.isSameOrigin).toBe(true);
});
it('rejects embedded userinfo in config URLs', () => {
expect(() =>
resolveConfigFetchPolicy('https://user:pass@trusted.example.com/config.json', {
allowedOrigins: ['https://trusted.example.com'],
userAuthenticationService: {
getAuthorizationHeader: () => ({ Authorization: 'Bearer token123' }),
},
})
).toThrow('URL userinfo is not allowed for dynamic datasource configuration');
});
});
describe('fetchConfigJson', () => {
const originalFetch = global.fetch;
beforeEach(() => {
global.fetch = jest.fn();
});
afterEach(() => {
jest.restoreAllMocks();
global.fetch = originalFetch;
});
it('uses hardened fetch options for unauthenticated cross-origin requests', async () => {
global.fetch.mockResolvedValue({
status: 200,
ok: true,
json: async () => ({ ok: true }),
});
await fetchConfigJson({
normalizedUrl: 'https://example.com/config.json',
isAuthenticated: false,
isSameOrigin: false,
});
expect(global.fetch).toHaveBeenCalledWith(
'https://example.com/config.json',
expect.objectContaining({
method: 'GET',
mode: 'cors',
credentials: 'same-origin',
redirect: 'error',
referrerPolicy: 'no-referrer',
})
);
});
it('uses hardened fetch options for unauthenticated same-origin requests', async () => {
global.fetch.mockResolvedValue({
status: 200,
ok: true,
json: async () => ({ ok: true }),
});
await fetchConfigJson({
normalizedUrl: `${window.location.origin}/protected/config.json`,
isAuthenticated: false,
isSameOrigin: true,
});
expect(global.fetch).toHaveBeenCalledWith(
`${window.location.origin}/protected/config.json`,
expect.objectContaining({
method: 'GET',
mode: 'cors',
credentials: 'same-origin',
redirect: 'error',
referrerPolicy: 'no-referrer',
})
);
});
it('uses hardened fetch options in authenticated environments', async () => {
global.fetch.mockResolvedValue({
status: 200,
ok: true,
json: async () => ({ ok: true }),
});
await fetchConfigJson({
normalizedUrl: 'https://trusted.example.com/config.json',
isAuthenticated: true,
isSameOrigin: false,
});
expect(global.fetch).toHaveBeenCalledWith(
'https://trusted.example.com/config.json',
expect.objectContaining({
method: 'GET',
mode: 'cors',
credentials: 'same-origin',
redirect: 'error',
referrerPolicy: 'no-referrer',
})
);
});
});
});
|