fix: change sanitizeUrl to handle URLs without a protocol schema (#36317)

pull/35006/head^2
Julio Araujo 6 months ago committed by GitHub
parent 4ba5cd095c
commit ba0cbd3265
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 6
      .changeset/flat-buckets-doubt.md
  2. 8
      packages/gazzodown/src/elements/sanitizeUrl.spec.ts
  3. 16
      packages/gazzodown/src/elements/sanitizeUrl.ts

@ -0,0 +1,6 @@
---
'@rocket.chat/gazzodown': patch
'@rocket.chat/meteor': patch
---
Fixes an issue that causes legitimate URLs to return '#' in links

@ -95,10 +95,6 @@ describe('sanitizeUrl', () => {
});
});
it('sanitizes malformed URLs', () => {
expect(sanitizeUrl('ht^tp://broken')).toBe('#');
});
it('sanitizes empty string', () => {
expect(sanitizeUrl('')).toBe('#');
});
@ -107,7 +103,7 @@ describe('sanitizeUrl', () => {
expect(sanitizeUrl('JAVASCRIPT:alert(1)')).toBe('#');
});
it('sanitizes nonsense input', () => {
expect(sanitizeUrl('💣💥🤯')).toBe('#');
it('allows bare domain names', () => {
expect(sanitizeUrl('example.com/page')).toBe('//example.com/page');
});
});

@ -1,8 +1,18 @@
export const sanitizeUrl = (href: string) => {
if (!href) {
return '#';
}
try {
const url = new URL(href);
const dangerousProtocols = ['javascript:', 'data:', 'vbscript:'];
return dangerousProtocols.includes(url.protocol.toLowerCase()) ? '#' : url.href;
const hasProtocol = /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(href);
if (hasProtocol) {
const url = new URL(href);
const dangerousProtocols = ['javascript:', 'data:', 'vbscript:'];
return dangerousProtocols.includes(url.protocol.toLowerCase()) ? '#' : url.href;
}
return `//${href}`;
} catch {
return '#';
}

Loading…
Cancel
Save