From d192a207dd93326ab2a1fe5f35b13a066ad5140b Mon Sep 17 00:00:00 2001 From: hurali97 Date: Mon, 2 Sep 2024 13:49:59 +0500 Subject: [PATCH 1/3] fix: add explicity check for typeof string to prevent undefined behaviour later --- lib/str.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/str.ts b/lib/str.ts index 4ec80683..23bb2ae5 100644 --- a/lib/str.ts +++ b/lib/str.ts @@ -994,6 +994,7 @@ const Str = { * without query parameters */ getExtension(url: string): string | undefined { + if (typeof url !== 'string') return undefined; return url.split('.').pop()?.split('?')[0]?.toLowerCase(); }, From 94b31d32c580a01937c1ac903a2fb79eeba86609 Mon Sep 17 00:00:00 2001 From: hurali97 Date: Mon, 2 Sep 2024 13:50:26 +0500 Subject: [PATCH 2/3] test: add test cases for handling undefined behaviour of getExtension function --- __tests__/Str-test.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/__tests__/Str-test.js b/__tests__/Str-test.js index b667118c..e91c0d5c 100644 --- a/__tests__/Str-test.js +++ b/__tests__/Str-test.js @@ -215,3 +215,19 @@ describe('Str.isValidE164Phone', () => { expect(Str.isValidE164Phone('+14404589784')).toBeTruthy(); }); }); + +describe('Str.getExtension', () => { + it('Correctly returns extension', () => { + expect(Str.getExtension(buildTestURLForType('pdf'))).toBeTruthy(); + expect(Str.getExtension(buildTestURLForType('PDF'))).toBeTruthy(); + expect(Str.getExtension(buildTestURLForType('mov'))).toBeTruthy(); + expect(Str.getExtension(buildTestURLForType('MP4'))).toBeTruthy(); + }); + + it('Returns undefined for data types other than string', () => { + expect(Str.getExtension(0)).toBeUndefined(); + expect(Str.getExtension(null)).toBeUndefined(); + expect(Str.getExtension(undefined)).toBeUndefined(); + expect(Str.getExtension([])).toBeUndefined(); + }); +}); \ No newline at end of file From 01a05043dd550170d8c3a6d59daa8482270fcbf9 Mon Sep 17 00:00:00 2001 From: hurali97 Date: Tue, 10 Sep 2024 12:02:24 +0500 Subject: [PATCH 3/3] feat: add Log.warn in case of type mis match --- lib/str.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/str.ts b/lib/str.ts index 23bb2ae5..2c367730 100644 --- a/lib/str.ts +++ b/lib/str.ts @@ -6,6 +6,7 @@ import * as HtmlEntities from 'html-entities'; import * as Constants from './CONST'; import * as UrlPatterns from './Url'; import * as Utils from './utils'; +import Log from './Log'; const REMOVE_SMS_DOMAIN_PATTERN = /@expensify\.sms/gi; @@ -994,7 +995,10 @@ const Str = { * without query parameters */ getExtension(url: string): string | undefined { - if (typeof url !== 'string') return undefined; + if (typeof url !== 'string') { + Log.warn('Str.getExtension: url is not a string', {url}); + return undefined; + } return url.split('.').pop()?.split('?')[0]?.toLowerCase(); },