-
Notifications
You must be signed in to change notification settings - Fork 22
feat: integrate FingerprintJS Pro visitor header for bot detection #2378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,81 @@ | ||
| /* UMD-style module that works with RequireJS and CommonJS */ | ||
| (function(root, factory) { | ||
| if (typeof define === 'function' && define.amd) { | ||
| define('js/utils/fingerprint_core', [], factory); | ||
| } else if (typeof module === 'object' && module.exports) { | ||
| module.exports = factory(); | ||
| } else { | ||
| root.FingerprintCore = factory(); | ||
| } | ||
| })(this, function() { | ||
| var SCRIPT_ID = 'ads-fingerprint-script'; | ||
| var visitorId = null; | ||
| var loadingPromise = null; | ||
|
|
||
| var injectScript = function(apiKey) { | ||
| return new Promise(function(resolve, reject) { | ||
| var existing = document.getElementById(SCRIPT_ID); | ||
| if (existing && existing.parentNode) { | ||
| existing.parentNode.removeChild(existing); | ||
| } | ||
|
|
||
| var script = document.createElement('script'); | ||
| script.id = SCRIPT_ID; | ||
| script.async = true; | ||
| script.src = | ||
| 'https://fpjscdn.net/v3/' + | ||
| encodeURIComponent(apiKey) + | ||
| '/iife.min.js'; | ||
| script.onload = resolve; | ||
| script.onerror = function() { | ||
| reject(new Error('FingerprintJS Pro script failed to load')); | ||
| }; | ||
| document.head.appendChild(script); | ||
| }); | ||
| }; | ||
|
|
||
| var load = function(apiKey) { | ||
| if (!apiKey) { | ||
| return Promise.resolve(); | ||
| } | ||
| if (loadingPromise) { | ||
| return loadingPromise; | ||
| } | ||
| loadingPromise = injectScript(apiKey) | ||
| .then(function() { | ||
| return window.FingerprintJS.load(); | ||
| }) | ||
| .then(function(fp) { | ||
| return fp.get(); | ||
| }) | ||
| .then(function(result) { | ||
| visitorId = (result && result.visitorId) || null; | ||
| }) | ||
| .catch(function() { | ||
| visitorId = null; | ||
| loadingPromise = null; // allow retry on transient failure | ||
| }); | ||
| return loadingPromise; | ||
| }; | ||
|
|
||
| var getVisitorId = function() { | ||
| return visitorId; | ||
| }; | ||
|
|
||
| // Reset module state — used by tests only | ||
| var _reset = function() { | ||
| visitorId = null; | ||
| loadingPromise = null; | ||
| var existing = document.getElementById(SCRIPT_ID); | ||
| if (existing && existing.parentNode) { | ||
| existing.parentNode.removeChild(existing); | ||
| } | ||
| }; | ||
|
|
||
| return { | ||
| load: load, | ||
| getVisitorId: getVisitorId, | ||
| SCRIPT_ID: SCRIPT_ID, | ||
| _reset: _reset, | ||
| }; | ||
| }); | ||
This file contains hidden or 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,161 @@ | ||
| define(['js/utils/fingerprint_core'], function(fingerprintCore) { | ||
| describe('FingerprintCore (fingerprint_core.spec.js)', function() { | ||
| var sb; | ||
|
|
||
| beforeEach(function() { | ||
| sb = sinon.sandbox.create(); | ||
| fingerprintCore._reset(); | ||
| }); | ||
|
|
||
| afterEach(function() { | ||
| sb.restore(); | ||
| fingerprintCore._reset(); | ||
| delete window.FingerprintJS; | ||
| }); | ||
|
|
||
| // Stub appendChild to trigger onload with a given FingerprintJS mock. | ||
| // Uses sinon 1.x 3-arg form: stub(obj, method, fn). | ||
| function stubAppendWithLoad(fpMock) { | ||
| sb.stub(document.head, 'appendChild', function(el) { | ||
| window.FingerprintJS = fpMock; | ||
| el.onload(); | ||
| }); | ||
| } | ||
|
|
||
| function stubAppendWithError() { | ||
| sb.stub(document.head, 'appendChild', function(el) { | ||
| el.onerror(new Error('network error')); | ||
| }); | ||
| } | ||
|
|
||
| describe('load()', function() { | ||
| it('is a no-op and returns a resolved promise when apiKey is falsy', function(done) { | ||
| var spy = sb.spy(document.head, 'appendChild'); | ||
| fingerprintCore.load('').then(function() { | ||
| expect(spy.called).to.equal(false); | ||
| expect(fingerprintCore.getVisitorId()).to.equal(null); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('injects a script tag with the FingerprintJS Pro CDN src', function(done) { | ||
| var getStub = sb.stub(); | ||
| getStub.returns(Promise.resolve({ visitorId: 'test-visitor-id-123' })); | ||
| var loadStub = sb.stub(); | ||
| loadStub.returns(Promise.resolve({ get: getStub })); | ||
| stubAppendWithLoad({ load: loadStub }); | ||
|
|
||
| fingerprintCore.load('MY-API-KEY').then(function() { | ||
| var appendedEl = document.head.appendChild.args[0][0]; | ||
| expect(appendedEl.src).to.contain('fpjscdn.net/v3/MY-API-KEY'); | ||
| expect(appendedEl.id).to.equal(fingerprintCore.SCRIPT_ID); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('caches the visitorId after successful resolution', function(done) { | ||
| var getStub = sb.stub(); | ||
| getStub.returns(Promise.resolve({ visitorId: 'visitor-abc' })); | ||
| var loadStub = sb.stub(); | ||
| loadStub.returns(Promise.resolve({ get: getStub })); | ||
| stubAppendWithLoad({ load: loadStub }); | ||
|
|
||
| fingerprintCore.load('KEY').then(function() { | ||
| expect(fingerprintCore.getVisitorId()).to.equal('visitor-abc'); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('returns the same promise if called multiple times', function() { | ||
| sb.stub(document.head, 'appendChild'); // pending — never fires onload | ||
| var p1 = fingerprintCore.load('KEY'); | ||
| var p2 = fingerprintCore.load('KEY'); | ||
| expect(p1).to.equal(p2); | ||
| expect(document.head.appendChild.callCount).to.equal(1); | ||
| }); | ||
|
|
||
| it('keeps visitorId null and clears loadingPromise on script load failure', function(done) { | ||
| stubAppendWithError(); | ||
|
|
||
| fingerprintCore.load('KEY').then(function() { | ||
| expect(fingerprintCore.getVisitorId()).to.equal(null); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('keeps visitorId null and clears loadingPromise when FingerprintJS.load() rejects', function(done) { | ||
| var loadStub = sb.stub(); | ||
| loadStub.returns(Promise.reject(new Error('init failure'))); | ||
| stubAppendWithLoad({ load: loadStub }); | ||
|
|
||
| fingerprintCore.load('KEY').then(function() { | ||
| expect(fingerprintCore.getVisitorId()).to.equal(null); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('keeps visitorId null and clears loadingPromise when fp.get() rejects', function(done) { | ||
| var getStub = sb.stub(); | ||
| getStub.returns(Promise.reject(new Error('get failure'))); | ||
| var loadStub = sb.stub(); | ||
| loadStub.returns(Promise.resolve({ get: getStub })); | ||
| stubAppendWithLoad({ load: loadStub }); | ||
|
|
||
| fingerprintCore.load('KEY').then(function() { | ||
| expect(fingerprintCore.getVisitorId()).to.equal(null); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('allows retry after a transient failure', function(done) { | ||
| stubAppendWithError(); | ||
|
|
||
| fingerprintCore.load('KEY').then(function() { | ||
| expect(fingerprintCore.getVisitorId()).to.equal(null); | ||
|
|
||
| // restore and set up a successful second attempt | ||
| document.head.appendChild.restore(); | ||
| var getStub = sb.stub(); | ||
| getStub.returns(Promise.resolve({ visitorId: 'retry-visitor' })); | ||
| var loadStub = sb.stub(); | ||
| loadStub.returns(Promise.resolve({ get: getStub })); | ||
| stubAppendWithLoad({ load: loadStub }); | ||
|
|
||
| return fingerprintCore.load('KEY'); | ||
| }).then(function() { | ||
| expect(fingerprintCore.getVisitorId()).to.equal('retry-visitor'); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('keeps visitorId null when result has no visitorId field', function(done) { | ||
| var getStub = sb.stub(); | ||
| getStub.returns(Promise.resolve({})); | ||
| var loadStub = sb.stub(); | ||
| loadStub.returns(Promise.resolve({ get: getStub })); | ||
| stubAppendWithLoad({ load: loadStub }); | ||
|
|
||
| fingerprintCore.load('KEY').then(function() { | ||
| expect(fingerprintCore.getVisitorId()).to.equal(null); | ||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getVisitorId()', function() { | ||
| it('returns null before load() is called', function() { | ||
| expect(fingerprintCore.getVisitorId()).to.equal(null); | ||
| }); | ||
|
|
||
| it('returns null while load() is still pending', function() { | ||
| sb.stub(document.head, 'appendChild'); // never fires onload | ||
| fingerprintCore.load('KEY'); | ||
| expect(fingerprintCore.getVisitorId()).to.equal(null); | ||
| }); | ||
|
|
||
| it('never throws', function() { | ||
| expect(function() { fingerprintCore.getVisitorId(); }).to.not.throw(); | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.