Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Closes #4349: Align default tracking protection policies with
Browse files Browse the repository at this point in the history
Firefox Desktop policies.
  • Loading branch information
Amejia481 committed Sep 10, 2019
1 parent 0f45536 commit 20f3fb2
Show file tree
Hide file tree
Showing 13 changed files with 368 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,28 @@ class GeckoEngine(
runtime.settings.contentBlocking.setStrictSocialTrackingProtection(
activateStrictSocialTracking
)
runtime.settings.contentBlocking.setAntiTracking(policy.trackingCategories.sumBy { it.id })
runtime.settings.contentBlocking.cookieBehavior = policy.cookiePolicy.id
runtime.settings.contentBlocking.setAntiTracking(policy.getAntiTrackingPolicy())
runtime.settings.contentBlocking.setCookieBehavior(policy.cookiePolicy.id)
defaultSettings?.trackingProtectionPolicy = value
field = value
}
}

private fun TrackingProtectionPolicy.getAntiTrackingPolicy(): Int {
/**
* The [TrackingProtectionPolicy.TrackingCategory.SCRIPTS_AND_SUB_RESOURCES] is an
* artificial category, created with the sole purpose of going around this bug
* https://bugzilla.mozilla.org/show_bug.cgi?id=1579264, for this reason we have to
* remove its value from the valid anti tracking categories, when is present.
*/
val total = trackingCategories.sumBy { it.id }
return if (contains(TrackingProtectionPolicy.TrackingCategory.SCRIPTS_AND_SUB_RESOURCES)) {
total - TrackingProtectionPolicy.TrackingCategory.SCRIPTS_AND_SUB_RESOURCES.id
} else {
total
}
}

override var remoteDebuggingEnabled: Boolean
get() = runtime.settings.remoteDebuggingEnabled
set(value) { runtime.settings.remoteDebuggingEnabled = value }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,18 +172,40 @@ class GeckoEngineSession(
} else {
policy.useForRegularSessions
}
geckoSession.settings.useTrackingProtection = enabled
/**
* As described on https://bugzilla.mozilla.org/show_bug.cgi?id=1579264,useTrackingProtection
* is a misleading setting. When is set to true is blocking content (scripts/sub-resources).
* Instead of just turn on/off tracking protection. Until, this issue is fixed consumers need
* a way to indicate, if they want to block content or not, this is why we use
* [TrackingProtectionPolicy.TrackingCategory.SCRIPTS_AND_SUB_RESOURCES].
*/
val shouldBlockContent =
policy.contains(TrackingProtectionPolicy.TrackingCategory.SCRIPTS_AND_SUB_RESOURCES)

if (!enabled) {
disableTrackingProtectionOnGecko()
}
geckoSession.settings.useTrackingProtection = shouldBlockContent
notifyObservers { onTrackerBlockingEnabledChange(enabled) }
}

/**
* See [EngineSession.disableTrackingProtection]
*/
override fun disableTrackingProtection() {
geckoSession.settings.useTrackingProtection = false
disableTrackingProtectionOnGecko()
notifyObservers { onTrackerBlockingEnabledChange(false) }
}

// To fully disable tracking protection we need to change the different tracking protection
// variables to none.
private fun disableTrackingProtectionOnGecko() {
geckoSession.settings.useTrackingProtection = false
runtime.settings.contentBlocking.setAntiTracking(ContentBlocking.AntiTracking.NONE)
runtime.settings.contentBlocking.cookieBehavior = ContentBlocking.CookieBehavior.ACCEPT_ALL
runtime.settings.contentBlocking.setStrictSocialTrackingProtection(false)
}

/**
* See [EngineSession.settings]
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,7 @@ class GeckoEngineSessionTest {
fun enableTrackingProtection() {
val runtime = mock<GeckoRuntime>()
whenever(runtime.settings).thenReturn(mock())
whenever(runtime.settings.contentBlocking).thenReturn(mock())
val session = GeckoEngineSession(runtime, geckoSessionProvider = geckoSessionProvider)
val privSession = GeckoEngineSession(
runtime,
Expand Down Expand Up @@ -912,6 +913,17 @@ class GeckoEngineSessionTest {

session.enableTrackingProtection(privateOnlyPolicy)
assertFalse(trackerBlockingObserved)
assertEquals(
GeckoCookieBehavior.ACCEPT_ALL,
runtime.settings.contentBlocking.cookieBehavior
)

assertEquals(
ContentBlocking.AntiTracking.NONE,
runtime.settings.contentBlocking.antiTrackingCategories
)

assertFalse(session.geckoSession.settings.useTrackingProtection)

session.enableTrackingProtection(regularOnlyPolicy)
assertTrue(trackerBlockingObserved)
Expand All @@ -930,6 +942,8 @@ class GeckoEngineSessionTest {
fun disableTrackingProtection() {
val runtime = mock<GeckoRuntime>()
whenever(runtime.settings).thenReturn(mock())
whenever(runtime.settings.contentBlocking).thenReturn(mock())

val engineSession = GeckoEngineSession(runtime, geckoSessionProvider = geckoSessionProvider)

var trackerBlockingDisabledObserved = false
Expand All @@ -941,6 +955,10 @@ class GeckoEngineSessionTest {

engineSession.disableTrackingProtection()
assertTrue(trackerBlockingDisabledObserved)
assertFalse(engineSession.geckoSession.settings.useTrackingProtection)
verify(runtime.settings.contentBlocking).setAntiTracking(ContentBlocking.AntiTracking.NONE)
verify(runtime.settings.contentBlocking).setCookieBehavior(GeckoCookieBehavior.ACCEPT_ALL)
verify(runtime.settings.contentBlocking).setStrictSocialTrackingProtection(false)
}

@Test
Expand All @@ -963,18 +981,7 @@ class GeckoEngineSessionTest {
assertEquals(GeckoAntiTracking.TEST, TrackingCategory.TEST.id)
assertEquals(GeckoAntiTracking.CRYPTOMINING, TrackingCategory.CRYPTOMINING.id)
assertEquals(GeckoAntiTracking.FINGERPRINTING, TrackingCategory.FINGERPRINTING.id)
assertEquals(GeckoAntiTracking.DEFAULT, TrackingCategory.RECOMMENDED.id)
assertEquals(GeckoAntiTracking.STRICT, TrackingCategory.STRICT.id)

val recommendedPolicy = TrackingProtectionPolicy.recommended()
val strictPolicy = TrackingProtectionPolicy.strict()
var antiTrackingCategories = strictPolicy.trackingCategories.sumBy { it.id }

assertEquals(GeckoAntiTracking.STRICT, antiTrackingCategories)

antiTrackingCategories = recommendedPolicy.trackingCategories.sumBy { it.id }

assertEquals(GeckoAntiTracking.DEFAULT, antiTrackingCategories)
assertEquals(GeckoAntiTracking.STP, TrackingCategory.MOZILLA_SOCIAL.id)

assertEquals(GeckoCookieBehavior.ACCEPT_ALL, CookiePolicy.ACCEPT_ALL.id)
assertEquals(
Expand Down Expand Up @@ -1386,6 +1393,42 @@ class GeckoEngineSessionTest {
verify(geckoSession.settings).useTrackingProtection = true
}

@Test
fun `WHEN TrackingCategory do not includes content then useTrackingProtection must be set to false`() {
val runtime = mock<GeckoRuntime>()
whenever(runtime.settings).thenReturn(mock())

val defaultSettings =
DefaultSettings(trackingProtectionPolicy = TrackingProtectionPolicy.recommended())

GeckoEngineSession(
runtime, geckoSessionProvider = geckoSessionProvider,
privateMode = false, defaultSettings = defaultSettings
)

verify(geckoSession.settings).useTrackingProtection = false
}

@Test
fun `WHEN disabling tracking protection THEN CookieBehavior and AntiTracking category must be set to ACCEPT_ALL and NONE`() {
val runtime = mock<GeckoRuntime>()
whenever(runtime.settings).thenReturn(mock())
whenever(runtime.settings.contentBlocking).thenReturn(mock())

val defaultSettings =
DefaultSettings(trackingProtectionPolicy = TrackingProtectionPolicy.recommended())

val session = GeckoEngineSession(
runtime, geckoSessionProvider = geckoSessionProvider,
privateMode = false, defaultSettings = defaultSettings
)

session.disableTrackingProtection()
verify(geckoSession.settings, times(2)).useTrackingProtection = false
verify(runtime.settings.contentBlocking).setAntiTracking(ContentBlocking.AntiTracking.NONE)
verify(runtime.settings.contentBlocking).setCookieBehavior(ContentBlocking.CookieBehavior.ACCEPT_ALL)
}

@Test
fun contentDelegate() {
val engineSession = GeckoEngineSession(mock(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,12 @@ class GeckoEngineTest {
engine.settings.trackingProtectionPolicy = TrackingProtectionPolicy.strict()

val trackingStrictCategories = TrackingProtectionPolicy.strict().trackingCategories.sumBy { it.id }
assertEquals(trackingStrictCategories, contentBlockingSettings.antiTrackingCategories)
val artificialCategory =
TrackingProtectionPolicy.TrackingCategory.SCRIPTS_AND_SUB_RESOURCES.id
assertEquals(
trackingStrictCategories - artificialCategory,
contentBlockingSettings.antiTrackingCategories
)

val safeStrictBrowsingCategories = SafeBrowsingPolicy.RECOMMENDED.id
assertEquals(safeStrictBrowsingCategories, contentBlockingSettings.safeBrowsingCategories)
Expand All @@ -161,6 +166,33 @@ class GeckoEngineTest {
} catch (e: UnsupportedSettingException) { }
}

@Test
fun `the SCRIPTS_AND_SUB_RESOURCES tracking protection category must not be passed to gecko view`() {

val geckoRunTime = GeckoRuntime.getDefault(testContext)

val engine = GeckoEngine(testContext, runtime = geckoRunTime)

engine.settings.trackingProtectionPolicy = TrackingProtectionPolicy.strict()

val trackingStrictCategories = TrackingProtectionPolicy.strict().trackingCategories.sumBy { it.id }
val artificialCategory =
TrackingProtectionPolicy.TrackingCategory.SCRIPTS_AND_SUB_RESOURCES.id

assertEquals(
trackingStrictCategories - artificialCategory,
geckoRunTime.settings.contentBlocking.antiTrackingCategories
)

geckoRunTime.settings.contentBlocking.setAntiTracking(0)

engine.settings.trackingProtectionPolicy = TrackingProtectionPolicy.select(
arrayOf(TrackingProtectionPolicy.TrackingCategory.SCRIPTS_AND_SUB_RESOURCES)
)

assertEquals(0, geckoRunTime.settings.contentBlocking.antiTrackingCategories)
}

@Test
fun `WHEN a strict tracking protection policy is set THEN the strict social list must be activated`() {
val mockRuntime = mock<GeckoRuntime>()
Expand Down Expand Up @@ -253,7 +285,12 @@ class GeckoEngineTest {
verify(runtimeSettings).autoplayDefault = GeckoRuntimeSettings.AUTOPLAY_DEFAULT_BLOCKED

val trackingStrictCategories = TrackingProtectionPolicy.strict().trackingCategories.sumBy { it.id }
assertEquals(trackingStrictCategories, contentBlockingSettings.antiTrackingCategories)
val artificialCategory =
TrackingProtectionPolicy.TrackingCategory.SCRIPTS_AND_SUB_RESOURCES.id
assertEquals(
trackingStrictCategories - artificialCategory,
contentBlockingSettings.antiTrackingCategories
)

assertEquals(SafeBrowsingPolicy.RECOMMENDED.id, contentBlockingSettings.safeBrowsingCategories)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,28 @@ class GeckoEngine(
runtime.settings.contentBlocking.setStrictSocialTrackingProtection(
activateStrictSocialTracking
)
runtime.settings.contentBlocking.setAntiTracking(policy.trackingCategories.sumBy { it.id })
runtime.settings.contentBlocking.cookieBehavior = policy.cookiePolicy.id
runtime.settings.contentBlocking.setAntiTracking(policy.getAntiTrackingPolicy())
runtime.settings.contentBlocking.setCookieBehavior(policy.cookiePolicy.id)
defaultSettings?.trackingProtectionPolicy = value
field = value
}
}

private fun TrackingProtectionPolicy.getAntiTrackingPolicy(): Int {
/**
* The [TrackingProtectionPolicy.TrackingCategory.SCRIPTS_AND_SUB_RESOURCES] is an
* artificial category, created with the sole purpose of going around this bug
* https://bugzilla.mozilla.org/show_bug.cgi?id=1579264, for this reason we have to
* remove its value from the valid anti tracking categories, when is present.
*/
val total = trackingCategories.sumBy { it.id }
return if (contains(TrackingProtectionPolicy.TrackingCategory.SCRIPTS_AND_SUB_RESOURCES)) {
total - TrackingProtectionPolicy.TrackingCategory.SCRIPTS_AND_SUB_RESOURCES.id
} else {
total
}
}

override var remoteDebuggingEnabled: Boolean
get() = runtime.settings.remoteDebuggingEnabled
set(value) { runtime.settings.remoteDebuggingEnabled = value }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,18 +172,40 @@ class GeckoEngineSession(
} else {
policy.useForRegularSessions
}
geckoSession.settings.useTrackingProtection = enabled
/**
* As described on https://bugzilla.mozilla.org/show_bug.cgi?id=1579264,useTrackingProtection
* is a misleading setting. When is set to true is blocking content (scripts/sub-resources).
* Instead of just turn on/off tracking protection. Until, this issue is fixed consumers need
* a way to indicate, if they want to block content or not, this is why we use
* [TrackingProtectionPolicy.TrackingCategory.SCRIPTS_AND_SUB_RESOURCES].
*/
val shouldBlockContent =
policy.contains(TrackingProtectionPolicy.TrackingCategory.SCRIPTS_AND_SUB_RESOURCES)

geckoSession.settings.useTrackingProtection = shouldBlockContent
if (!enabled) {
disableTrackingProtectionOnGecko()
}
notifyObservers { onTrackerBlockingEnabledChange(enabled) }
}

/**
* See [EngineSession.disableTrackingProtection]
*/
override fun disableTrackingProtection() {
geckoSession.settings.useTrackingProtection = false
disableTrackingProtectionOnGecko()
notifyObservers { onTrackerBlockingEnabledChange(false) }
}

// To fully disable tracking protection we need to change the different tracking protection
// variables to none.
private fun disableTrackingProtectionOnGecko() {
geckoSession.settings.useTrackingProtection = false
runtime.settings.contentBlocking.setAntiTracking(ContentBlocking.AntiTracking.NONE)
runtime.settings.contentBlocking.cookieBehavior = ContentBlocking.CookieBehavior.ACCEPT_ALL
runtime.settings.contentBlocking.setStrictSocialTrackingProtection(false)
}

/**
* See [EngineSession.settings]
*/
Expand Down
Loading

0 comments on commit 20f3fb2

Please sign in to comment.