Skip to content

Commit

Permalink
Remove cross origin approach and detect parent document location
Browse files Browse the repository at this point in the history
This rolls back the approach from #988 and detects if the component is rendered in an iframe and parent document location is a publishing domain to hide the cookie banner.
  • Loading branch information
alex-ju committed Jul 22, 2019
1 parent d4d11fa commit 85d8c0e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
this.$module.cookieBannerConfirmationMessage = this.$module.querySelector('.gem-c-cookie-banner__confirmation')

this.setupCookieMessage()

// Listen for cross-origin communication messages (e.g. hideCookieBanner for when previewing GOV.UK pages
// in publishing applications
this.listenForCrossOriginMessages()
}

CookieBanner.prototype.setupCookieMessage = function () {
Expand All @@ -44,7 +40,8 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};

CookieBanner.prototype.showCookieMessage = function () {
// Hide the cookie banner on the cookie settings page, to avoid circular journeys
if (this.$module.cookieBanner && window.location.pathname === '/help/cookies') {
// or when presented in an iframe by a publishing application
if (this.isInCookiesPage() || (this.isInIframe() && this.parentIsPublishingDomain())) {
this.$module.style.display = 'none'
} else {
var shouldHaveCookieMessage = (this.$module && window.GOVUK.cookie('seen_cookie_message') !== 'true')
Expand Down Expand Up @@ -89,36 +86,23 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
window.addEventListener('message', this.receiveMessage.bind(this), false)
}

CookieBanner.prototype.receiveMessage = function (event) {
var trustedDomain = 'publishing.service.gov.uk'
var origin = event.origin
CookieBanner.prototype.isInCookiesPage = function () {
return window.location.pathname === '/help/cookies'
}

// Return if no origin is given or the browser doesn't support lastIndexOf
if (!origin || !origin.lastIndexOf) {
return
}
CookieBanner.prototype.isInIframe = function () {
return window.parent && window.location !== window.parent.location
}

// Polyfill origin.endsWith(trustedDomain) for IE
var offset = origin.length - trustedDomain.length
var trustedOrigin = offset >= 0 && origin.lastIndexOf(trustedDomain, offset) === offset
CookieBanner.prototype.parentIsPublishingDomain = function () {
var publishingDomain = 'publishing.service.gov.uk'
var currentDomain = window.parent.location.origin

// Return if the given origin is not trusted
if (!trustedOrigin) {
return
}
// Polyfill currentDomain.endsWith(publishingDomain) for IE
var offset = currentDomain.length - publishingDomain.length
var domainMatch = offset >= 0 && currentDomain.lastIndexOf(publishingDomain, offset) === offset

// Read JSON data from event
var dataObject = {}
try {
dataObject = JSON.parse(event.data)
} catch (err) {
// Don't throw errors as the emmited message may not be in a JSON format
} finally {
if (dataObject.hideCookieBanner === 'true') {
// Visually hide the cookie banner
this.$module.style.display = 'none'
}
}
return domainMatch
}

Modules.CookieBanner = CookieBanner
Expand Down
16 changes: 0 additions & 16 deletions spec/javascripts/components/cookie-banner-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,4 @@ describe('Cookie banner', function () {

expect(newCookieBanner).not.toBeVisible()
})

it('hides the cookie banner if a cross-origin messages says so', function () {
var element = document.querySelector('[data-module="cookie-banner"]')
var cookieBannerModule = new GOVUK.Modules.CookieBanner()
cookieBannerModule.start($(element))

var mockMessage = {
data: JSON.stringify({ 'hideCookieBanner': 'true' }),
origin: 'https://content-publisher.publishing.service.gov.uk'
}

cookieBannerModule.receiveMessage(mockMessage)

var newCookieBanner = document.querySelector('.gem-c-cookie-banner')
expect(newCookieBanner).not.toBeVisible()
})
})

0 comments on commit 85d8c0e

Please sign in to comment.