-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Additional checks on Result component (#481)
- Loading branch information
1 parent
9147036
commit e4ab783
Showing
7 changed files
with
199 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
packages/react-search-ui-views/src/__tests__/view-helpers/getUrlSanitizer.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { getUrlSanitizer } from "../../view-helpers"; | ||
import URL from "core-js-pure/features/url"; | ||
|
||
describe("getUrlSanitizer", () => { | ||
let url; | ||
let currentLocation; | ||
|
||
beforeEach(() => { | ||
url = ""; | ||
currentLocation = ""; | ||
}); | ||
|
||
function subject() { | ||
return getUrlSanitizer(URL, currentLocation)(url); | ||
} | ||
|
||
describe("when valid url with http", () => { | ||
beforeEach(() => { | ||
url = "http://www.example.com/"; | ||
currentLocation = "http://www.mysite.com"; | ||
}); | ||
|
||
it("should allow it", () => { | ||
expect(subject()).toEqual(url); | ||
}); | ||
}); | ||
|
||
describe("when valid url with https", () => { | ||
beforeEach(() => { | ||
url = "https://www.example.com/"; | ||
currentLocation = "http://www.mysite.com"; | ||
}); | ||
|
||
it("should allow it", () => { | ||
expect(subject()).toEqual(url); | ||
}); | ||
}); | ||
|
||
describe("when relative url", () => { | ||
beforeEach(() => { | ||
url = "/item/1234"; | ||
currentLocation = "http://www.mysite.com"; | ||
}); | ||
|
||
it("should allow it", () => { | ||
expect(subject()).toEqual(url); | ||
}); | ||
}); | ||
|
||
describe("when the protocol is javascript", () => { | ||
beforeEach(() => { | ||
url = "javascript://test%0aalert(document.domain)"; | ||
currentLocation = "http://www.mysite.com"; | ||
}); | ||
|
||
it("should disallow it", () => { | ||
expect(subject()).toEqual(""); | ||
}); | ||
}); | ||
|
||
describe("when the protocol is javascript with spaces in it", () => { | ||
beforeEach(() => { | ||
url = " javascript://test%0aalert(document.domain)"; | ||
currentLocation = "http://www.mysite.com"; | ||
}); | ||
|
||
it("should disallow it", () => { | ||
expect(subject()).toEqual(""); | ||
}); | ||
}); | ||
|
||
describe("when the url is invalid", () => { | ||
beforeEach(() => { | ||
url = "<div>My bad URL</div>"; | ||
currentLocation = "http://www.mysite.com"; | ||
}); | ||
|
||
it("treats it as a relative url, which should still be safe", () => { | ||
expect(subject()).toEqual(url); | ||
}); | ||
}); | ||
|
||
describe("when the protocol is not whitelisted", () => { | ||
beforeEach(() => { | ||
url = "mailto://user@example.com"; | ||
currentLocation = "http://www.mysite.com"; | ||
}); | ||
|
||
it("should disallow it", () => { | ||
expect(subject()).toEqual(""); | ||
}); | ||
}); | ||
|
||
describe("when the url is protocol-less", () => { | ||
beforeEach(() => { | ||
url = "//www.example.com/"; | ||
currentLocation = "https://www.mysite.com"; | ||
}); | ||
|
||
it("uses the protocol from the current location", () => { | ||
expect(subject()).toEqual(url); | ||
}); | ||
}); | ||
}); |
21 changes: 21 additions & 0 deletions
21
packages/react-search-ui-views/src/view-helpers/getUrlSanitizer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const VALID_PROTOCOLS = ["http:", "https:"]; | ||
|
||
/** | ||
* | ||
* @param {URL} URLParser URL interface provided by browser https://developer.mozilla.org/en-US/docs/Web/API/URL | ||
* @param {String} currentLocation String representation of the browser's current location | ||
*/ | ||
export default function getUrlSanitizer(URLParser, currentLocation) { | ||
// This function is curried so that dependencies can be injected and don't need to be mocked in tests. | ||
return url => { | ||
let parsedUrl = {}; | ||
|
||
try { | ||
// Attempts to parse a URL as relative | ||
parsedUrl = new URLParser(url, currentLocation); | ||
// eslint-disable-next-line no-empty | ||
} catch (e) {} | ||
|
||
return VALID_PROTOCOLS.includes(parsedUrl.protocol) ? url : ""; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { default as getFilterValueDisplay } from "./getFilterValueDisplay"; | ||
export { default as appendClassName } from "./appendClassName"; | ||
export { default as getUrlSanitizer } from "./getUrlSanitizer"; |