fix(configuration): Harden dynamic datasource URL trust boundaries and credential handling. (#5963)

* fix(configuration): Harden dynamic datasource URL trust boundaries and credential handling.

* Remove testing configuration.

* Update policies for runtime ?url=... datasource loading.

* PR feedback.
This commit is contained in:
Joe Boccanfuso authored and GitHub committed 2026-04-21 11:55:14 -04:00
1 parent 51e6b35dbb
commit eede569a88
6 files changed
+334 -13

No files matched your search

@@ -0,0 +1,118 @@
// @ts-nocheck
function normalizeAllowedOrigins(allowedOrigins = []) {
if (!Array.isArray(allowedOrigins)) {
return [];
}
const configuredOrigins = allowedOrigins
.filter(origin => typeof origin === 'string')
.map(origin => origin.trim())
.filter(Boolean);
return configuredOrigins
.map(origin => {
try {
const parsedOrigin = new URL(origin);
if (!['http:', 'https:'].includes(parsedOrigin.protocol)) {
console.error(
`[secureConfigFetch] Ignoring misconfigured allowed origin "${origin}". ` +
'Entries must use http:// or https://.'
);
return null;
}
if (
parsedOrigin.username ||
parsedOrigin.password ||
parsedOrigin.pathname !== '/' ||
parsedOrigin.search ||
parsedOrigin.hash
) {
console.error(
`[secureConfigFetch] Ignoring misconfigured allowed origin "${origin}". ` +
'Entries must be bare origins only (scheme + host + optional port), with no username/password, path, query, or hash.'
);
return null;
}
return parsedOrigin.origin;
} catch {
console.error(
`[secureConfigFetch] Ignoring misconfigured allowed origin "${origin}". Entry is not a valid URL.`
);
return null;
}
})
.filter(Boolean);
}
function resolveConfigUrl(rawUrl) {
if (!rawUrl || typeof rawUrl !== 'string') {
throw new Error('Missing required "url" query parameter');
}
try {
return new URL(rawUrl, window.location.href);
} catch {
throw new Error('Invalid URL in "url" query parameter');
}
}
function resolveConfigFetchPolicy(rawUrl, policy = {}) {
const { allowedOrigins = [], userAuthenticationService } = policy;
const parsedUrl = resolveConfigUrl(rawUrl);
const protocol = parsedUrl.protocol.toLowerCase();
if (!['http:', 'https:'].includes(protocol)) {
throw new Error('Only HTTP(S) URLs are allowed for dynamic datasource configuration');
}
if (parsedUrl.hash) {
throw new Error('URL fragments are not allowed for dynamic datasource configuration');
}
if (parsedUrl.username || parsedUrl.password) {
throw new Error('URL userinfo is not allowed for dynamic datasource configuration');
}
const isAuthenticated = Boolean(
userAuthenticationService?.getAuthorizationHeader?.()?.Authorization
);
if (isAuthenticated) {
const normalizedAllowedOrigins = normalizeAllowedOrigins(allowedOrigins);
if (!normalizedAllowedOrigins.length || !normalizedAllowedOrigins.includes(parsedUrl.origin)) {
throw new Error(
`Blocked remote configuration origin "${parsedUrl.origin}" in authenticated environment`
);
}
}
return {
parsedUrl,
normalizedUrl: parsedUrl.toString(),
isAuthenticated,
};
}
async function fetchConfigJson(normalizedPolicy) {
const { normalizedUrl, isAuthenticated } = normalizedPolicy;
const response = isAuthenticated
? await fetch(normalizedUrl)
: await fetch(normalizedUrl, {
method: 'GET',
mode: 'cors',
credentials: 'omit',
redirect: 'error',
referrerPolicy: 'no-referrer',
});
if (!response.ok) {
throw new Error(`Failed to fetch dynamic datasource configuration (${response.status})`);
}
return response.json();
}
export {
resolveConfigFetchPolicy,
fetchConfigJson,
};
@@ -0,0 +1,113 @@
// @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);
});
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);
});
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('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 in unauthenticated environments', async () => {
global.fetch.mockResolvedValue({
status: 200,
ok: true,
json: async () => ({ ok: true }),
});
await fetchConfigJson({
normalizedUrl: 'https://example.com/config.json',
isAuthenticated: false,
});
expect(global.fetch).toHaveBeenCalledWith(
'https://example.com/config.json',
expect.objectContaining({
method: 'GET',
mode: 'cors',
credentials: 'omit',
redirect: 'error',
referrerPolicy: 'no-referrer',
})
);
});
it('uses simple fetch 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,
});
expect(global.fetch).toHaveBeenCalledWith('https://trusted.example.com/config.json');
});
});
});