From 1aa1907128e921f827ef4a9005bf9a20ebbe3976 Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Tue, 31 Oct 2017 12:35:08 -0700 Subject: [PATCH] Check for input type and string length before sanitizing a URL --- src/core/utils.js | 4 ++++ test/core/utils.js | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/core/utils.js b/src/core/utils.js index de572105..b1b7f9de 100644 --- a/src/core/utils.js +++ b/src/core/utils.js @@ -723,6 +723,10 @@ export const shallowEqualKeys = (a,b, keys) => { } export function sanitizeUrl(url) { + if(typeof url !== "string" || url === "") { + return "" + } + return braintreeSanitizeUrl(url) } diff --git a/test/core/utils.js b/test/core/utils.js index 4adbca8f..d65046cb 100644 --- a/test/core/utils.js +++ b/test/core/utils.js @@ -912,5 +912,17 @@ sbG8iKTs8L3NjcmlwdD4=`) expect(res).toEqual("https://swagger.io/") }) + + it("should gracefully handle empty strings", function() { + expect(sanitizeUrl("")).toEqual("") + }) + + it("should gracefully handle non-string values", function() { + expect(sanitizeUrl(123)).toEqual("") + expect(sanitizeUrl(null)).toEqual("") + expect(sanitizeUrl(undefined)).toEqual("") + expect(sanitizeUrl([])).toEqual("") + expect(sanitizeUrl({})).toEqual("") + }) }) })