Browse Source

Block non-http(s) editor and sidebar URLs to prevent stored XSS through iframes

pull/1372/head
github-actions[bot] 1 month ago
committed by GitHub
parent
commit
58d56ed767
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 34
      frontend/src/app/shared/utils/editor-utils.spec.ts
  2. 19
      frontend/src/app/shared/utils/editor-utils.ts

34
frontend/src/app/shared/utils/editor-utils.spec.ts

@ -61,6 +61,40 @@ describe('EditorUtils', () => {
expect(result).toEqual('http://undefined?query=value'); expect(result).toEqual('http://undefined?query=value');
}); });
it('should keep relative editor url', () => {
const result = computeEditorUrl('/editors/${editor1}', settings);
expect(result).toEqual('/editors/url/to/editor1');
});
['javascript:alert(1)', ' JavaScript:alert(1)', 'data:text/html,<script>alert(1)</script>', 'vbscript:msgbox(1)'].forEach(url => {
it(`should return empty string if url has unsafe protocol ${url}`, () => {
const result = computeEditorUrl(url, settings);
expect(result).toEqual('');
});
});
it('should return empty string if interpolated url has unsafe protocol', () => {
const unsafeSettings = new AppSettingsDto({
patterns: [],
hideDateTimeModeButton: false,
hideScheduler: false,
editors: [
new EditorDto({
name: 'evil',
url: 'javascript:alert(1)',
}),
],
version: 1,
_links: {},
});
const result = computeEditorUrl('${evil}', unsafeSettings);
expect(result).toEqual('');
});
[null, undefined, ''].forEach(url => { [null, undefined, ''].forEach(url => {
it(`should return empty string if url is ${url}`, () => { it(`should return empty string if url is ${url}`, () => {
const result = computeEditorUrl(url, settings); const result = computeEditorUrl(url, settings);

19
frontend/src/app/shared/utils/editor-utils.ts

@ -20,5 +20,22 @@ export function computeEditorUrl(url?: string | null, settings?: AppSettingsDto
} }
} }
return interpolate(url, editors) || ''; const result = interpolate(url, editors) || '';
return isSafeEditorUrl(result) ? result : '';
}
function isSafeEditorUrl(url: string) {
if (!url) {
return false;
}
// The url is trusted for the iframe, therefore protocols like 'javascript:' would run in our origin.
try {
const protocol = new URL(url, window.location.href).protocol;
return protocol === 'http:' || protocol === 'https:';
} catch {
return false;
}
} }

Loading…
Cancel
Save