Skip to content

Commit 5db4d1f

Browse files
committed
fix: common js function to validate url
1 parent 5c41dbf commit 5db4d1f

5 files changed

Lines changed: 32 additions & 43 deletions

File tree

package-lock.json

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/analytics-js-integrations/__tests__/integrations/GoogleAds/browser.test.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,7 @@ describe('GoogleAds init tests', () => {
125125
{},
126126
destinationInfo,
127127
);
128-
}).toThrow('[GoogleAds]: Invalid sdkBaseUrl: "not-a-valid-url"');
129-
});
130-
131-
test('throws an error when sdkBaseUrl contains ngrok domain', () => {
132-
expect(() => {
133-
new GoogleAds(
134-
{ conversionID: mockConversionId, sdkBaseUrl: 'https://abc.ngrok.io' },
135-
{},
136-
destinationInfo,
137-
);
138-
}).toThrow('[GoogleAds]: Invalid sdkBaseUrl: "https://abc.ngrok.io"');
128+
}).toThrow('Invalid sdkBaseUrl: "not-a-valid-url"');
139129
});
140130

141131
test('uses default GTM domain when sdkBaseUrl is undefined', () => {

packages/analytics-js-integrations/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
"on-body": "0.0.1",
8484
"component-each": "0.2.6",
8585
"@ndhoule/extend": "2.0.0",
86+
"@rudderstack/analytics-js-common": "*",
8687
"@lukeed/uuid": "2.0.1",
8788
"get-value": "4.0.1",
8889
"ramda": "0.32.0"

packages/analytics-js-integrations/src/integrations/GoogleAds/browser.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import {
1818
import { loadNativeSdk } from './nativeSdkLoader';
1919

2020
import { prepareParamsAndEventName } from '../GA4/utils';
21-
import { getValueOrDefault, removeTrailingSlashes, isValidURL } from '../../utils/utils';
21+
import { getValueOrDefault, removeTrailingSlashes } from '../../utils/utils';
22+
import { isValidURL } from '@rudderstack/analytics-js-common/utilities/url';
2223

2324
const logger = new Logger(DISPLAY_NAME);
2425

@@ -50,12 +51,8 @@ class GoogleAds {
5051
this.v2 = getValueOrDefault(config.v2, true);
5152
this.allowIdentify = config.allowIdentify ?? false;
5253
const sanitizedSdkBaseUrl = removeTrailingSlashes(config.sdkBaseUrl);
53-
if (sanitizedSdkBaseUrl) {
54-
const SDK_BASE_URL_PATTERN =
55-
"(^\\{\\{.*\\|\\|(.*)\\}\\}$)|(^env[.].+)|(?!.*\\.ngrok\\.io)^(?:http(s)?:\\/\\/)?[\\w.-]+(?:\\.[\\w.-]+)+[\\w\\-._~:/?#[\\]@!\\$&'\\(\\)\\*\\+,;=.]*|^$";
56-
if (!isValidURL(sanitizedSdkBaseUrl, SDK_BASE_URL_PATTERN)) {
57-
throw new Error(`[GoogleAds]: Invalid sdkBaseUrl: "${sanitizedSdkBaseUrl}"`);
58-
}
54+
if (sanitizedSdkBaseUrl && !isValidURL(sanitizedSdkBaseUrl)) {
55+
throw new Error(`Invalid sdkBaseUrl: "${sanitizedSdkBaseUrl}"`);
5956
}
6057
this.sdkBaseUrl = sanitizedSdkBaseUrl || 'https://www.googletagmanager.com';
6158
this.name = NAME;

packages/analytics-js-integrations/src/utils/utils.js

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,6 @@ function removeTrailingSlashes(inURL) {
3535
return inURL?.endsWith('/') ? inURL.replace(/\/+$/, '') : inURL;
3636
}
3737

38-
const URL_PATTERN = new RegExp(
39-
'^(https?:\\/\\/)' +
40-
'(' +
41-
'((([a-zA-Z\\d]([a-zA-Z\\d-]*[a-zA-Z\\d])*)\\.)+[a-zA-Z]{2,}|' +
42-
'localhost|' +
43-
'((25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9]?)\\.){3}' +
44-
'(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9]?))' +
45-
')' +
46-
'(\\:\\d+)?' +
47-
'(\\/[-a-zA-Z\\d%_.~+]*)*' +
48-
'(\\?[;&a-zA-Z\\d%_.~+=-]*)?' +
49-
'(\\#[-a-zA-Z\\d_]*)?$',
50-
);
51-
52-
function isValidURL(url, pattern) {
53-
if (typeof url !== 'string') {
54-
return false;
55-
}
56-
if (typeof pattern === 'string' && !new RegExp(pattern).test(url)) {
57-
return false;
58-
}
59-
return URL_PATTERN.test(url);
60-
}
61-
6238
/**
6339
*
6440
* Utility function for UUID generation
@@ -634,7 +610,6 @@ export {
634610
isDefinedAndNotNull,
635611
getDataFromSource,
636612
removeTrailingSlashes,
637-
isValidURL,
638613
constructPayload,
639614
isEmptyObject,
640615
getValueOrDefault,

0 commit comments

Comments
 (0)