Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "safeurl related bug fix",
"packageName": "@fluentui/react-charting",
"email": "132879294+v-baambati@users.noreply.github.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "safeurl related bug fix",
"packageName": "@fluentui/react-charts",
"email": "132879294+v-baambati@users.noreply.github.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -1523,6 +1523,10 @@ describe('isSafeUrl', () => {
expect(utils.isSafeUrl('https://example.com')).toBe(true);
});

test('Should allow https URL with leading whitespace', () => {
expect(utils.isSafeUrl(' https://example.com')).toBe(true);
});

test('Should allow https URL with path, query, and fragment', () => {
expect(utils.isSafeUrl('https://example.com/path?q=1#section')).toBe(true);
});
Expand Down Expand Up @@ -1552,6 +1556,26 @@ describe('isSafeUrl', () => {
expect(utils.isSafeUrl('javascript:alert(1)')).toBe(false);
});

test('Should block javascript: protocol with leading whitespace', () => {
// eslint-disable-next-line no-script-url
expect(utils.isSafeUrl(' javascript:alert(1)')).toBe(false);
});

test('Should block javascript: protocol with leading tabs/newlines', () => {
// eslint-disable-next-line no-script-url
expect(utils.isSafeUrl('\n\tjavascript:alert(1)')).toBe(false);
});

test('Should block javascript: protocol with embedded newline in scheme', () => {
// eslint-disable-next-line no-script-url
expect(utils.isSafeUrl('java\nscript:alert(1)')).toBe(false);
});

test('Should block javascript: protocol with embedded tab in scheme', () => {
// eslint-disable-next-line no-script-url
expect(utils.isSafeUrl('java\tscript:alert(1)')).toBe(false);
});

test('Should block data: protocol', () => {
expect(utils.isSafeUrl('data:text/html,<script>alert(1)</script>')).toBe(false);
});
Expand Down Expand Up @@ -1580,6 +1604,10 @@ describe('isSafeUrl', () => {
expect(utils.isSafeUrl('custom:payload')).toBe(false);
});

test('Should block custom: protocol with leading whitespace', () => {
expect(utils.isSafeUrl(' custom:payload')).toBe(false);
});

test('Should allow a path that contains a colon but is not a scheme', () => {
expect(utils.isSafeUrl('/path/to:resource')).toBe(true);
});
Expand Down
6 changes: 3 additions & 3 deletions packages/charts/react-charting/src/utilities/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2556,9 +2556,9 @@ const truncateTextToFitWidth = (text: string, maxWidth: number, measure: (s: str
};

export function isSafeUrl(href: string): boolean {
if (/^[a-z][a-z0-9+.-]*:/i.test(href)) {
return /^(https?|mailto|tel|ftp):/i.test(href);
const normalized = href.replace(/[\u0000-\u001F\u007F\s]+/g, '');
if (/^[a-z][a-z0-9+.-]*:/i.test(normalized)) {
return /^(https?|mailto|tel|ftp):/i.test(normalized);
}

return true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -1528,6 +1528,10 @@ describe('isSafeUrl', () => {
expect(utils.isSafeUrl('https://example.com')).toBe(true);
});

test('Should allow https URL with leading whitespace', () => {
expect(utils.isSafeUrl(' https://example.com')).toBe(true);
});

test('Should allow https URL with path, query, and fragment', () => {
expect(utils.isSafeUrl('https://example.com/path?q=1#section')).toBe(true);
});
Expand Down Expand Up @@ -1557,6 +1561,26 @@ describe('isSafeUrl', () => {
expect(utils.isSafeUrl('javascript:alert(1)')).toBe(false);
});

test('Should block javascript: protocol with leading whitespace', () => {
// eslint-disable-next-line no-script-url
expect(utils.isSafeUrl(' javascript:alert(1)')).toBe(false);
});

test('Should block javascript: protocol with leading tabs/newlines', () => {
// eslint-disable-next-line no-script-url
expect(utils.isSafeUrl('\n\tjavascript:alert(1)')).toBe(false);
});

test('Should block javascript: protocol with embedded newline in scheme', () => {
// eslint-disable-next-line no-script-url
expect(utils.isSafeUrl('java\nscript:alert(1)')).toBe(false);
});

test('Should block javascript: protocol with embedded tab in scheme', () => {
// eslint-disable-next-line no-script-url
expect(utils.isSafeUrl('java\tscript:alert(1)')).toBe(false);
Comment thread
v-baambati marked this conversation as resolved.
});

test('Should block data: protocol', () => {
expect(utils.isSafeUrl('data:text/html,<script>alert(1)</script>')).toBe(false);
});
Expand Down Expand Up @@ -1585,6 +1609,10 @@ describe('isSafeUrl', () => {
expect(utils.isSafeUrl('custom:payload')).toBe(false);
});

test('Should block custom: protocol with leading whitespace', () => {
expect(utils.isSafeUrl(' custom:payload')).toBe(false);
});

test('Should allow a path that contains a colon but is not a scheme', () => {
expect(utils.isSafeUrl('/path/to:resource')).toBe(true);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2716,8 +2716,9 @@ const truncateTextToFitWidth = (text: string, maxWidth: number, measure: (s: str
};

export function isSafeUrl(href: string): boolean {
if (/^[a-z][a-z0-9+.-]*:/i.test(href)) {
return /^(https?|mailto|tel|ftp):/i.test(href);
const normalized = href.replace(/[\u0000-\u001F\u007F\s]+/g, '');
if (/^[a-z][a-z0-9+.-]*:/i.test(normalized)) {
return /^(https?|mailto|tel|ftp):/i.test(normalized);
}
return true;
}
Loading