Skip to content

Commit c1144c8

Browse files
authored
fix(charts): modify isSafeUrl utility (microsoft#35963)
1 parent a2d2071 commit c1144c8

6 files changed

Lines changed: 36 additions & 6 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "fix: isSafeUrl utility",
4+
"packageName": "@fluentui/react-charting",
5+
"email": "vgenaev@gmail.com",
6+
"dependentChangeType": "patch"
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "fix: isSafeUrl utility",
4+
"packageName": "@fluentui/react-charts",
5+
"email": "vgenaev@gmail.com",
6+
"dependentChangeType": "patch"
7+
}

packages/charts/react-charting/src/utilities/UtilityUnitTests.test.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,8 +1564,16 @@ describe('isSafeUrl', () => {
15641564
expect(utils.isSafeUrl('file:///etc/passwd')).toBe(false);
15651565
});
15661566

1567-
test('Should block ftp: protocol', () => {
1568-
expect(utils.isSafeUrl('ftp://example.com/file')).toBe(false);
1567+
test('Should allow ftp: protocol', () => {
1568+
expect(utils.isSafeUrl('ftp://example.com/file')).toBe(true);
1569+
});
1570+
1571+
test('Should allow mailto: protocol', () => {
1572+
expect(utils.isSafeUrl('mailto:user@example.com')).toBe(true);
1573+
});
1574+
1575+
test('Should allow tel: protocol', () => {
1576+
expect(utils.isSafeUrl('tel:+1234567890')).toBe(true);
15691577
});
15701578

15711579
test('Should block custom: protocol', () => {

packages/charts/react-charting/src/utilities/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2557,7 +2557,7 @@ const truncateTextToFitWidth = (text: string, maxWidth: number, measure: (s: str
25572557

25582558
export function isSafeUrl(href: string): boolean {
25592559
if (/^[a-z][a-z0-9+.-]*:/i.test(href)) {
2560-
return /^https?:/i.test(href);
2560+
return /^(https?|mailto|tel|ftp):/i.test(href);
25612561
}
25622562

25632563
return true;

packages/charts/react-charts/library/src/utilities/UtilityUnitTests.test.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,8 +1569,16 @@ describe('isSafeUrl', () => {
15691569
expect(utils.isSafeUrl('file:///etc/passwd')).toBe(false);
15701570
});
15711571

1572-
test('Should block ftp: protocol', () => {
1573-
expect(utils.isSafeUrl('ftp://example.com/file')).toBe(false);
1572+
test('Should allow ftp: protocol', () => {
1573+
expect(utils.isSafeUrl('ftp://example.com/file')).toBe(true);
1574+
});
1575+
1576+
test('Should allow mailto: protocol', () => {
1577+
expect(utils.isSafeUrl('mailto:user@example.com')).toBe(true);
1578+
});
1579+
1580+
test('Should allow tel: protocol', () => {
1581+
expect(utils.isSafeUrl('tel:+1234567890')).toBe(true);
15741582
});
15751583

15761584
test('Should block custom: protocol', () => {

packages/charts/react-charts/library/src/utilities/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2713,7 +2713,7 @@ const truncateTextToFitWidth = (text: string, maxWidth: number, measure: (s: str
27132713

27142714
export function isSafeUrl(href: string): boolean {
27152715
if (/^[a-z][a-z0-9+.-]*:/i.test(href)) {
2716-
return /^https?:/i.test(href);
2716+
return /^(https?|mailto|tel|ftp):/i.test(href);
27172717
}
27182718
return true;
27192719
}

0 commit comments

Comments
 (0)