söndag 10 juli 2016

Firefox Tracking Protection for all users

Firefox Tracking protection is a great feature used in Private browsing mode. But turning on Firefox tracking protection permanently has several benefits. According to researchers it makes pages load 44% faster and reduces data usage by 39%. You will also get rid of most personalized advertisement, thus the increased performance. Personally, I prefer to choose my advertisement myself.

You can turn on the feature in Firefox by typing about:config in the location bar and hitting enter. Type and find privacy.trackingprotection.enabled and double-click it to toggle it to true.



But how to turn this feature on for all users on the same PC? Usually family members are not tech-savvy enough to mess around in the Firefox configuration interface. Behold, if you are an administrator, you can set a machine-wide setting.

You can tell Firefox to read system preferences by creating a file defaults/pref/local-settings.js in the Firefox installation directory. Then set the default preferences in mozilla.cfg. Mozilla calls this locking preferences, but it doesn't necessarily have to lock them like in a corporate environment. Depending on the setting, the user can still override it. It is worth studying the difference between defaultPref, pref and lockPref in the preference file.


This is a small script that creates the preference files if they don't exist and adds the settings if they don't exist. I leave it as an exercise to the reader if they want the script to change existing settings. Note that the Firefox installation directory can be different in a different Linux distribution.

#!/bin/bash
# set default trackingprotection on in Firefox for all users
if [ `getconf LONG_BIT` = "64" ]; then
    firefoxdir="/usr/lib64/firefox"
else
    firefoxdir="/usr/lib/firefox"
fi
if [ ! -d "$firefoxdir/defaults/pref" ]; then mkdir -p "$firefoxdir/defaults/pref" ; fi
if [ ! -f "$firefoxdir/defaults/pref/local-settings.js" ]; then
    touch "$firefoxdir/defaults/pref/local-settings.js"
    echo '//' >> "$firefoxdir/defaults/pref/local-settings.js"
    echo 'pref("general.config.filename", "mozilla.cfg");' >> "$firefoxdir/defaults/pref/local-settings.js"
    echo 'pref("general.config.obscure_value", 0);' >> "$firefoxdir/defaults/pref/local-settings.js"
else
    if ! grep -q "general.config.filename" "$firefoxdir/defaults/pref/local-settings.js" ; then
        echo 'pref("general.config.filename", "mozilla.cfg");' >> "$firefoxdir/defaults/pref/local-settings.js"
    fi
    if ! grep -q "general.config.obscure_value" "$firefoxdir/defaults/pref/local-settings.js" ; then
        echo 'pref("general.config.obscure_value", 0);' >> "$firefoxdir/defaults/pref/local-settings.js"
    fi
fi
if [ ! -f "$firefoxdir/mozilla.cfg" ]; then
    touch "$firefoxdir/mozilla.cfg"
    echo '//' >> "$firefoxdir/mozilla.cfg"
    echo 'defaultPref("privacy.trackingprotection.enabled", true);' >> "$firefoxdir/mozilla.cfg"
    echo 'pref("privacy.trackingprotection.enabled", true);' >> "$firefoxdir/mozilla.cfg"
else
    if ! grep -q "privacy.trackingprotection.enabled" "$firefoxdir/mozilla.cfg" ; then
        echo 'defaultPref("privacy.trackingprotection.enabled", true);' >> "$firefoxdir/mozilla.cfg"
        echo 'pref("privacy.trackingprotection.enabled", true);' >> "$firefoxdir/mozilla.cfg"
    fi
fi

[1] http://lifehacker.com/turn-on-tracking-protection-in-firefox-to-make-pages-lo-1706946166
[2] http://ieee-security.org/TC/SPW2015/W2SP/papers/W2SP_2015_submission_32.pdf
[3] http://kb.mozillazine.org/Locking_preferences