Skip to content

Commit 08fe59c

Browse files
committed
Add a allowOrigin option to bypass invalid origins
1 parent 01279ad commit 08fe59c

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

lib/client.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ export default class Client {
326326
this._source = options.source || (this._parent && this._parent._source) || window.parent
327327
this._appGuid = options.appGuid || (this._parent && this._parent._appGuid)
328328
this._instanceGuid = options.instanceGuid || this._appGuid
329+
this._allowOrigin = options.allowOrigin || false
329330
this._messageHandlers = {}
330331
this._repliesPending = {}
331332
this._instanceClients = {}
@@ -335,7 +336,7 @@ export default class Client {
335336
this._idleState = null
336337
this.ready = false
337338

338-
if (!isOriginValid(this._origin)) {
339+
if (!this._allowOrigin && !isOriginValid(this._origin)) {
339340
const originHostname = new URL(this._origin).hostname
340341
this.postMessage('__track__', {
341342
event_name: 'invalid_sdk_origin',

lib/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,17 @@ import { queryParameters } from './utils'
2727
*/
2828

2929
const ZAFClient = {
30-
init: function (callback, loc) {
30+
init: function (callback, loc, options) {
3131
loc = loc || window.location
32+
options = options || {}
33+
const allowOrigin = options.allowOrigin
3234
const queryParams = queryParameters(loc.search)
3335
const hashParams = queryParameters(loc.hash)
3436
const origin = queryParams.origin || hashParams.origin
3537
const appGuid = queryParams.app_guid || hashParams.app_guid
3638
if (!origin || !appGuid) { return false }
3739

38-
const client = new Client({ origin, appGuid })
40+
const client = new Client({ origin, appGuid, allowOrigin })
3941

4042
if (typeof callback === 'function') {
4143
client.on('app.registered', callback.bind(client))

spec/client_spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,17 @@ describe('Client', () => {
177177
expect(ACTUAL_ERROR_MSG).to.equal(EXPECTED_ERROR_MSG)
178178
})
179179
})
180+
181+
it('does not clear the iframe and append an error msg when allowOrigin is true', () => {
182+
const validOriginClient = new Client({
183+
origin: 'https://hostmapped.com',
184+
appGuid: 'appGuid',
185+
source: source,
186+
allowOrigin: true
187+
})
188+
189+
expect(validOriginClient).to.exist()
190+
})
180191
})
181192

182193
describe('initialisation', () => {

0 commit comments

Comments
 (0)