Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

analytics-client

Package Overview
Dependencies
Maintainers
1
Versions
145
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

analytics-client - npm Package Compare versions

Comparing version 1.4.0-getQueryString-changes-d883b3d288bc529829aaf58798d9c18e037f5487 to 1.4.0-getQueryString-changes-f19973daf1f7e8eccaa33d5a03b5d70ae5fe8a0a

2

CHANGELOG.md

@@ -8,3 +8,3 @@ # Change Log

# v1.4.0
## (2021-02-25)
## (2021-02-26)

@@ -11,0 +11,0 @@ * Changed getQueryString to validate domains [Ezequiel Boehler]

{
"name": "analytics-client",
"version": "1.4.0-getQueryString-changes-d883b3d288bc529829aaf58798d9c18e037f5487",
"version": "1.4.0-getQueryString-changes-f19973daf1f7e8eccaa33d5a03b5d70ae5fe8a0a",
"description": "Convenient builders to compose analytics tools",

@@ -5,0 +5,0 @@ "repository": {

@@ -19,4 +19,4 @@ import { Client } from './client';

getSessionIdQueryString(): string;
getQueryString(destinationHostname?: string): string;
getQueryString(destinationUrl?: URL, currentUrl?: URL): string;
isOptOutRequested(): boolean;
}

@@ -102,9 +102,24 @@ "use strict";

};
AnalyticsUrlParams.prototype.getQueryString = function (destinationHostname) {
var regex = /([a-zA-Z0-9-]+)(\.[a-zA-Z]{2,5})?(\.[a-zA-Z]+$)/g;
var actualDomain = window.location.hostname.match(regex);
var destinationDomain = destinationHostname
? destinationHostname.match(regex)
: undefined;
if (actualDomain !== destinationDomain) {
AnalyticsUrlParams.prototype.getQueryString = function (destinationUrl, currentUrl) {
var regex = /([a-zA-Z0-9-]+)(\.[a-zA-Z]{2,3})?(\.[a-zA-Z]+$)/g;
var actualDomainMatch;
if (currentUrl) {
actualDomainMatch = currentUrl.hostname.match(regex);
}
else if (typeof window !== undefined) {
actualDomainMatch = window.location.hostname.match(regex);
}
else {
actualDomainMatch = null;
}
var destinationDomainMatch = destinationUrl
? destinationUrl.hostname.match(regex)
: null;
var actualDomain = actualDomainMatch
? actualDomainMatch.toString()
: null;
var destinationDomain = destinationDomainMatch
? destinationDomainMatch.toString()
: null;
if (!destinationDomain || actualDomain !== destinationDomain) {
return [this.getDeviceIdsQueryString(), this.getSessionIdQueryString()]

@@ -114,5 +129,3 @@ .filter(function (x) { return x; })

}
else {
return '';
}
return '';
};

@@ -119,0 +132,0 @@ AnalyticsUrlParams.prototype.isOptOutRequested = function () {

@@ -70,2 +70,13 @@ "use strict";

});
test('parsing and matching destination and actual URL to regex', function () {
var urlParams = new url_params_1.AnalyticsUrlParams();
expect(urlParams.getQueryString()).toBe('');
urlParams.consumeUrlParameters('s_id=123&other=value');
expect(urlParams.getQueryString()).toBe('s_id=123');
urlParams.consumeUrlParameters('d_id=d1&other=value');
expect(urlParams.getQueryString()).toBe('d_id=d1&s_id=123');
expect(urlParams.getQueryString(new URL('https://test.domain.io'), new URL('https://domain.io'))).toBe('');
expect(urlParams.getQueryString(new URL('https://test.domain.io'), new URL('https://otherdomain.com'))).toBe('d_id=d1&s_id=123');
expect(urlParams.getQueryString(new URL('https://test.domain.edge.io'), new URL('https://domain2.edge.io'))).toBe('');
});
var clientMock = function () {

@@ -72,0 +83,0 @@ return ({

{
"name": "analytics-client",
"version": "1.4.0-getQueryString-changes-d883b3d288bc529829aaf58798d9c18e037f5487",
"version": "1.4.0-getQueryString-changes-f19973daf1f7e8eccaa33d5a03b5d70ae5fe8a0a",
"description": "Convenient builders to compose analytics tools",

@@ -5,0 +5,0 @@ "repository": {

@@ -166,16 +166,33 @@ import * as Cookies from 'js-cookie';

*/
getQueryString(destinationHostname?: string): string {
const regex = /([a-zA-Z0-9-]+)(\.[a-zA-Z]{2,5})?(\.[a-zA-Z]+$)/g;
const actualDomain = window.location.hostname.match(regex);
const destinationDomain = destinationHostname
? destinationHostname.match(regex)
: undefined;
getQueryString(destinationUrl?: URL, currentUrl?: URL): string {
// this regex is based on the assumption that we wont be using TLDs longer than 3 characters. If we do, it will break
// the logic and take that longer TLD as the main domain, for example hub.balena.edge.io -> edge.io
const regex = /([a-zA-Z0-9-]+)(\.[a-zA-Z]{2,3})?(\.[a-zA-Z]+$)/g;
if (actualDomain !== destinationDomain) {
let actualDomainMatch;
if (currentUrl) {
actualDomainMatch = currentUrl.hostname.match(regex);
} else if (typeof window !== undefined) {
actualDomainMatch = window.location.hostname.match(regex);
} else {
actualDomainMatch = null;
}
const destinationDomainMatch = destinationUrl
? destinationUrl.hostname.match(regex)
: null;
const actualDomain = actualDomainMatch
? actualDomainMatch.toString()
: null;
const destinationDomain = destinationDomainMatch
? destinationDomainMatch.toString()
: null;
if (!destinationDomain || actualDomain !== destinationDomain) {
return [this.getDeviceIdsQueryString(), this.getSessionIdQueryString()]
.filter(x => x)
.join('&');
} else {
return '';
}
return '';
}

@@ -182,0 +199,0 @@

@@ -105,2 +105,39 @@ import { Client, createNoopClient } from '../src/client';

test('parsing and matching destination and actual URL to regex', () => {
const urlParams = new AnalyticsUrlParams();
expect(urlParams.getQueryString()).toBe('');
urlParams.consumeUrlParameters('s_id=123&other=value');
expect(urlParams.getQueryString()).toBe('s_id=123');
// Case when not passing any destination or actual URL
urlParams.consumeUrlParameters('d_id=d1&other=value');
expect(urlParams.getQueryString()).toBe('d_id=d1&s_id=123');
// Case when passing matching destination and actual URL while a d_id and s_id exist.
expect(
urlParams.getQueryString(
new URL('https://test.domain.io'),
new URL('https://domain.io'),
),
).toBe('');
// Case when passing none matching destination and actual URL while a d_id and s_id exist.
expect(
urlParams.getQueryString(
new URL('https://test.domain.io'),
new URL('https://otherdomain.com'),
),
).toBe('d_id=d1&s_id=123');
// Case when passing destination URL that violates the TLD regex assumption. Here it will result in matching URL "edge.io", when in fact
// It should be treated as different URLs and return the d_id and s_id params
expect(
urlParams.getQueryString(
new URL('https://test.domain.edge.io'),
new URL('https://domain2.edge.io'),
),
).toBe('');
});
interface AnalyticsMock {

@@ -107,0 +144,0 @@ setDeviceIdParams: string | null;

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc