Skip to content

Commit 6604ebf

Browse files
julio-rocketchatsem22-dev
authored andcommitted
fix: change sanitizeUrl to handle URLs without a protocol schema (RocketChat#36317)
1 parent 457a43a commit 6604ebf

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

.changeset/flat-buckets-doubt.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@rocket.chat/gazzodown': patch
3+
'@rocket.chat/meteor': patch
4+
---
5+
6+
Fixes an issue that causes legitimate URLs to return '#' in links

packages/gazzodown/src/elements/sanitizeUrl.spec.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,6 @@ describe('sanitizeUrl', () => {
9595
});
9696
});
9797

98-
it('sanitizes malformed URLs', () => {
99-
expect(sanitizeUrl('ht^tp://broken')).toBe('#');
100-
});
101-
10298
it('sanitizes empty string', () => {
10399
expect(sanitizeUrl('')).toBe('#');
104100
});
@@ -107,7 +103,7 @@ describe('sanitizeUrl', () => {
107103
expect(sanitizeUrl('JAVASCRIPT:alert(1)')).toBe('#');
108104
});
109105

110-
it('sanitizes nonsense input', () => {
111-
expect(sanitizeUrl('💣💥🤯')).toBe('#');
106+
it('allows bare domain names', () => {
107+
expect(sanitizeUrl('example.com/page')).toBe('//example.com/page');
112108
});
113109
});

packages/gazzodown/src/elements/sanitizeUrl.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
export const sanitizeUrl = (href: string) => {
2+
if (!href) {
3+
return '#';
4+
}
5+
26
try {
3-
const url = new URL(href);
4-
const dangerousProtocols = ['javascript:', 'data:', 'vbscript:'];
5-
return dangerousProtocols.includes(url.protocol.toLowerCase()) ? '#' : url.href;
7+
const hasProtocol = /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(href);
8+
9+
if (hasProtocol) {
10+
const url = new URL(href);
11+
const dangerousProtocols = ['javascript:', 'data:', 'vbscript:'];
12+
return dangerousProtocols.includes(url.protocol.toLowerCase()) ? '#' : url.href;
13+
}
14+
15+
return `//${href}`;
616
} catch {
717
return '#';
818
}

0 commit comments

Comments
 (0)