Skip to content

Commit 72b5622

Browse files
committed
add url validation for android referres
1 parent e70b96d commit 72b5622

2 files changed

Lines changed: 101 additions & 5 deletions

File tree

spec/src/utils/helpers.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const {
1515
isNil,
1616
getWindowLocation,
1717
getCanonicalUrl,
18+
getDocumentReferrer,
1819
dispatchEvent,
1920
createCustomEvent,
2021
hasOrderIdRecord,
@@ -215,6 +216,18 @@ describe('ConstructorIO - Utils - Helpers', () => {
215216
});
216217

217218
describe('getCanonicalUrl', () => {
219+
it('Should return android app referrers in a valid url structure', () => {
220+
const cleanup = jsdom();
221+
222+
const canonicalUrl = 'android-app://com.google.android.googlequicksearchbox/';
223+
const canonicalEle = document.querySelector('[rel=canonical]');
224+
canonicalEle.setAttribute('href', canonicalUrl);
225+
226+
expect(getCanonicalUrl()).to.equal('https://com.google.android.googlequicksearchbox/');
227+
228+
cleanup();
229+
});
230+
218231
it('Should return the canonical URL from the DOM link element', () => {
219232
const cleanup = jsdom();
220233

@@ -264,6 +277,67 @@ describe('ConstructorIO - Utils - Helpers', () => {
264277
});
265278
});
266279

280+
describe('getDocumentReferrer', () => {
281+
it('Should return android app referrers in a valid url structure', () => {
282+
const cleanup = jsdom();
283+
284+
const referrerUrl = 'android-app://com.google.android.googlequicksearchbox/';
285+
Object.defineProperty(document, 'referrer', {
286+
value: referrerUrl,
287+
configurable: true,
288+
});
289+
290+
expect(getDocumentReferrer()).to.equal('https://com.google.android.googlequicksearchbox/');
291+
292+
cleanup();
293+
});
294+
295+
it('Should return the referrer URL from the document', () => {
296+
const cleanup = jsdom();
297+
298+
const referrerUrl = 'https://constructor.io/products/item';
299+
Object.defineProperty(document, 'referrer', {
300+
value: referrerUrl,
301+
configurable: true,
302+
});
303+
304+
expect(getDocumentReferrer()).to.equal(referrerUrl);
305+
306+
cleanup();
307+
});
308+
309+
it('Should return null for a relative url', () => {
310+
const cleanup = jsdom();
311+
312+
const relativeUrl = '/products/item';
313+
Object.defineProperty(document, 'referrer', {
314+
value: relativeUrl,
315+
configurable: true,
316+
});
317+
318+
const result = getDocumentReferrer();
319+
expect(result).to.be.null;
320+
321+
cleanup();
322+
});
323+
324+
it('Should return null when referrer is empty', () => {
325+
const cleanup = jsdom();
326+
327+
Object.defineProperty(document, 'referrer', {
328+
value: '',
329+
configurable: true,
330+
});
331+
332+
expect(getDocumentReferrer()).to.be.null;
333+
cleanup();
334+
});
335+
336+
it('Should return null when not in a DOM context', () => {
337+
expect(getDocumentReferrer()).to.be.null;
338+
});
339+
});
340+
267341
describe('dispatchEvent', () => {
268342
it('Should dispatch an event if in a DOM context', () => {
269343
const cleanup = jsdom();

src/utils/helpers.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,23 @@ const utils = {
4444
return cleanedParams;
4545
},
4646

47+
cleanAndValidateUrl: (url, baseUrl = undefined) => {
48+
let validatedUrl = null;
49+
50+
try {
51+
// Handle android app referrers
52+
if (url?.startsWith('android-app')) {
53+
url = url?.replace('android-app', 'https')
54+
}
55+
56+
validatedUrl = (new URL(url, baseUrl)).toString();
57+
} catch (e) {
58+
// do nothing
59+
}
60+
61+
return validatedUrl;
62+
},
63+
4764
throwHttpErrorFromResponse: (error, response) => response.json().then((json) => {
4865
error.message = json.message;
4966
error.status = response.status;
@@ -92,11 +109,17 @@ const utils = {
92109
},
93110

94111
getDocumentReferrer: () => {
95-
if (utils.canUseDOM()) {
96-
return document?.referrer;
112+
let documentReferrer = null;
113+
114+
try {
115+
if (utils.canUseDOM()) {
116+
documentReferrer = utils.cleanAndValidateUrl(document.referrer);
117+
}
118+
} catch (e) {
119+
// do nothing
97120
}
98121

99-
return null;
122+
return documentReferrer;
100123
},
101124

102125
getCanonicalUrl: () => {
@@ -108,8 +131,7 @@ const utils = {
108131
const href = linkEle?.getAttribute('href');
109132

110133
if (href) {
111-
const url = new URL(href, document.location.href);
112-
canonicalURL = url.toString();
134+
canonicalURL = utils.cleanAndValidateUrl(href, document.location.href);
113135
}
114136
}
115137
} catch (e) {

0 commit comments

Comments
 (0)