-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Drop invalid client hints (close #1264)
- Loading branch information
Showing
8 changed files
with
242 additions
and
75 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
...anges/@snowplow/browser-plugin-client-hints/issue-1264_client_hints_2023-11-11-11-25.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"changes": [ | ||
{ | ||
"packageName": "@snowplow/browser-plugin-client-hints", | ||
"comment": "Drop invalid client hints", | ||
"type": "none" | ||
} | ||
], | ||
"packageName": "@snowplow/browser-plugin-client-hints" | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// DO NOT MODIFY THIS FILE MANUALLY BUT DO COMMIT IT. It is generated and used by Rush. | ||
{ | ||
"pnpmShrinkwrapHash": "c272b447631dc0e0be0f52f2e7d1ed6aa6603939", | ||
"pnpmShrinkwrapHash": "57af2bb401d45bb58b802eb1a04a7ee62b3aa732", | ||
"preferredVersionsHash": "bf21a9e8fbc5a3846fb05b4fa0859e0917b2202f" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
reporters: ['jest-standard-reporter'], | ||
testEnvironment: 'jest-environment-jsdom-global', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
plugins/browser-plugin-client-hints/test/client-hints.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
import { buildLinkClick, trackerCore } from '@snowplow/tracker-core'; | ||
import { BrowserTracker } from '@snowplow/browser-tracker-core'; | ||
import { JSDOM } from 'jsdom'; | ||
import { setImmediate } from 'timers'; | ||
import { ClientHintsPlugin } from '../src'; | ||
|
||
declare var jsdom: JSDOM; | ||
|
||
describe('Client Hints plugin', () => { | ||
const ctxSchema = 'iglu:com.snowplowanalytics.snowplow/contexts/jsonschema/1-0-0'; | ||
const hintsSchema = 'iglu:org.ietf/http_client_hints/jsonschema/1-0-0'; | ||
|
||
it('Attaches no context on undefined userAgentData', (done) => { | ||
const plugin = ClientHintsPlugin(false); | ||
const core = trackerCore({ | ||
corePlugins: [plugin], | ||
callback: (payloadBuilder) => { | ||
const json = payloadBuilder.getJson().filter((e) => e.keyIfEncoded === 'cx'); | ||
expect(json.length).toBe(0); | ||
done(); | ||
}, | ||
}); | ||
|
||
plugin.activateBrowserPlugin?.({ core } as BrowserTracker); | ||
core.track(buildLinkClick({ targetUrl: 'https://example.com' })); | ||
}); | ||
|
||
it('Attaches no context on invalid userAgentData(no mobile)', (done) => { | ||
const sampleUserAgentData = { | ||
brands: [ | ||
{ | ||
brand: 'Opera GX', | ||
version: '98', | ||
}, | ||
], | ||
} as any; | ||
|
||
Object.defineProperty(jsdom.window.navigator, 'userAgentData', { | ||
value: sampleUserAgentData, | ||
configurable: true, | ||
}); | ||
|
||
const plugin = ClientHintsPlugin(false); | ||
const core = trackerCore({ | ||
corePlugins: [plugin], | ||
callback: (payloadBuilder) => { | ||
const json = payloadBuilder.getJson().filter((e) => e.keyIfEncoded === 'cx'); | ||
expect(json.length).toBe(0); | ||
done(); | ||
}, | ||
}); | ||
|
||
plugin.activateBrowserPlugin?.({ core } as BrowserTracker); | ||
core.track(buildLinkClick({ targetUrl: 'https://example.com' })); | ||
}); | ||
|
||
it('Attaches no context on invalid userAgentData(no brands)', (done) => { | ||
const sampleUserAgentData = { | ||
mobile: true, | ||
} as any; | ||
|
||
Object.defineProperty(jsdom.window.navigator, 'userAgentData', { | ||
value: sampleUserAgentData, | ||
configurable: true, | ||
}); | ||
|
||
const plugin = ClientHintsPlugin(false); | ||
const core = trackerCore({ | ||
corePlugins: [plugin], | ||
callback: (payloadBuilder) => { | ||
const json = payloadBuilder.getJson().filter((e) => e.keyIfEncoded === 'cx'); | ||
expect(json.length).toBe(0); | ||
done(); | ||
}, | ||
}); | ||
|
||
plugin.activateBrowserPlugin?.({ core } as BrowserTracker); | ||
core.track(buildLinkClick({ targetUrl: 'https://example.com' })); | ||
}); | ||
|
||
it('Attaches no context on invalid userAgentData(no valid brands)', (done) => { | ||
const sampleUserAgentData = { | ||
mobile: false, | ||
brands: [ | ||
{ | ||
brand: 'Opera GX', | ||
version: 98, | ||
}, | ||
], | ||
} as any; | ||
|
||
Object.defineProperty(jsdom.window.navigator, 'userAgentData', { | ||
value: sampleUserAgentData, | ||
configurable: true, | ||
}); | ||
|
||
const plugin = ClientHintsPlugin(true); | ||
const core = trackerCore({ | ||
corePlugins: [plugin], | ||
callback: (payloadBuilder) => { | ||
const json = payloadBuilder.getJson().filter((e) => e.keyIfEncoded === 'cx'); | ||
expect(json.length).toBe(0); | ||
done(); | ||
}, | ||
}); | ||
|
||
plugin.activateBrowserPlugin?.({ core } as BrowserTracker); | ||
core.track(buildLinkClick({ targetUrl: 'https://example.com' })); | ||
}); | ||
|
||
it('Attaches context on valid userAgentData properties', async () => { | ||
const highEntropyVals = { | ||
platform: 'PhoneOS', | ||
platformVersion: '10A', | ||
architecture: 'arm', | ||
model: 'X633GTM', | ||
uaFullVersion: '73.32.AGX.5', | ||
}; | ||
const sampleUserAgentData = { | ||
brands: [ | ||
{ | ||
brand: 'Chromium', | ||
version: '119', | ||
}, | ||
{ | ||
brand: 'Not?A_Brand', | ||
version: '24', | ||
}, | ||
], | ||
mobile: false, | ||
getHighEntropyValues: (_: any) => Promise.resolve(highEntropyVals), | ||
}; | ||
|
||
const expected = { | ||
schema: ctxSchema, | ||
data: [ | ||
{ | ||
schema: hintsSchema, | ||
data: Object.assign( | ||
{ | ||
isMobile: false, | ||
brands: sampleUserAgentData.brands, | ||
}, | ||
highEntropyVals | ||
), | ||
}, | ||
], | ||
}; | ||
|
||
Object.defineProperty(jsdom.window.navigator, 'userAgentData', { | ||
value: sampleUserAgentData, | ||
configurable: true, | ||
}); | ||
|
||
const plugin = ClientHintsPlugin(true); | ||
const core = trackerCore({ | ||
corePlugins: [plugin], | ||
callback: (payloadBuilder) => { | ||
const json = payloadBuilder.getJson().filter((e) => e.keyIfEncoded === 'cx'); | ||
|
||
expect(json[0].json).toMatchObject(expected); | ||
}, | ||
}); | ||
|
||
plugin.activateBrowserPlugin?.({ core } as BrowserTracker); | ||
const flushPromises = () => Promise.resolve(setImmediate); | ||
await flushPromises(); | ||
core.track(buildLinkClick({ targetUrl: 'https://example.com' })); | ||
}); | ||
}); |