Wren Linux

wren-linux / extras / firefox-profiles / new-profile.sh

#!/bin/bash
# A separate Firefox profile for one website, with its own cookies, logins and
# settings, and a command that opens it: NAME (e.g. github), or firefox-NAME
# when a program is called NAME already. Each profile also gets a
# locked-down Linux user of its own (fx-NAME), like the sandboxed Firefox's: a
# hacked page in one profile reaches neither your files nor the other profiles.
# Only the clipboard and ~/Downloads/NAME are shared with you. It needs the
# sandboxed Firefox (extras/firefox-sandbox.sh), and offers to set that up.
# The window shows only the page: no tabs, address bar or bookmarks bar.
#
#   bash new-profile.sh                       asks for its name and address
#   bash new-profile.sh NAME URL [--fast-js] [--hide-toolbars] [--no-sound] [--keep-cookies]
#                                             [--ublock-standard]
#                                             e.g. bash new-profile.sh bank https://mybank.example
#   Its cookies and site data are deleted when it closes (you log in each time: KeePassXC fills
#   it in); --keep-cookies keeps them, to stay logged in (--delete-cookies, the default, is still
#   taken). Firefox's own Settings > Privacy > Manage Exceptions can still keep one site's.
#   bash new-profile.sh --remove NAME         removes its command; asks whether to delete the profile and its user
#   bash new-profile.sh --manual              prints the steps to make one by hand
#
# --fast-js turns the JavaScript JIT back on for this profile (Wren Linux's
# parts/firefox-hardening.sh turns it off everywhere), for heavy web apps.
# Its tabs and address bar are shown, so you can see which site you're on (a look-alike login
# page can't hide); --hide-toolbars shows only the page (--show-toolbars, the default, is taken).
# --no-sound: no sound from it (a bank needs none; a video site does).
# Cookies: see above (--keep-cookies to stay logged in).
# --ublock-standard: uBlock's standard mode for this profile's site (ublock-allow SITE), for one
# that breaks in medium mode: its filter lists still block ads, trackers and malware sites there.
# uBlock's settings are shared by every Firefox, so that site gets it in each of them.
# Run without NAME and URL, it asks, fast JavaScript, sound and cookies included.
. "$(dirname -- "${BASH_SOURCE[0]}")/../../lib.sh"
. "$WREN_DIR/lib/firefox-sandbox.sh"
. "$WREN_DIR/lib/firefox-profiles.sh"
if [ "${1:-}" = --manual ]; then print_manual firefox-profiles; exit; fi
not_root

valid_name() { fx_profile_name_ok "$1"; }
# Only characters that are safe in a web address and in the launcher
valid_url() { [[ $1 =~ ^https?://[A-Za-z0-9.-]+(:[0-9]+)?(/[A-Za-z0-9._~/?#=%+,@-]*)?$ ]]; }

if [ "${1:-}" = --remove ]; then
    [ $# -eq 2 ] && valid_name "$2" || die "Usage: bash new-profile.sh --remove NAME"
    remove_profile "$2"
    exit
fi

fast=no toolbars=shown sound=yes cookies=delete ublock=medium
if [ $# -eq 1 ]; then
    die "Usage: bash new-profile.sh NAME URL (or no arguments, to be asked)"
elif [ $# -ge 2 ]; then
    name=$1 url=$2
    shift 2
    for opt in "$@"; do
        case $opt in
            --fast-js) fast=yes ;;
            --hide-toolbars) toolbars=hidden ;;
            --show-toolbars) toolbars=shown ;;
            --no-sound) sound=no ;;
            --keep-cookies) cookies=keep ;;
            --ublock-standard) ublock=standard ;;
            # (the default now; still taken, so older commands work)
            --delete-cookies) cookies=delete ;;
            *) die "Usage: bash new-profile.sh NAME URL [--fast-js] [--hide-toolbars] [--no-sound] [--keep-cookies] [--ublock-standard]" ;;
        esac
    done
else
    note "A short name for it (lowercase letters, digits and dashes, e.g. bank)."
    tty_read name "    name> "
    tty_read url "    address (e.g. https://mybank.example)> "
    note "Fast JavaScript makes heavy sites quicker (web mail, office, chat), but the"
    note "part that does it (the JIT) is where many browser attacks happen. Off, the"
    note "site still works, just slower: keep it off unless this site feels sluggish."
    if ask "Fast JavaScript for this site?" n; then
        fast=yes
    fi
    if ! ask "Sound from this site (videos, music)? It can't hear your microphone either way." y; then
        sound=no
    fi
    note "Cookies and site data are what keep you logged in to this site."
    note "  delete  they go when it closes: you log in each time (KeePassXC fills it in), and"
    note "          the site can't recognise you from one visit to the next"
    note "  keep    they stay when it closes: you stay logged in"
    choose cookies "When this profile closes, its cookies and site data:" delete delete keep
    note "uBlock blocks other sites' scripts and frames everywhere (medium mode). Some sites"
    note "break because of it (GitHub, say); for one of those, standard mode: its filter lists"
    note "still block ads, trackers and known malware sites there."
    choose ublock "uBlock for this site:" medium medium standard
fi
valid_name "$name" || die "\"$name\" isn't a usable name: lowercase letters, digits and dashes, starting with a letter, at most 28 long (and not esr, restart-check or session)."
[[ $url == *:* ]] || url="https://$url"
valid_url "$url" || die "\"$url\" doesn't look like a web address."

make_profile "$name" "$url" "$fast" "$toolbars" "$sound" "$cookies"
# (ublock-allow, which parts/firefox-hardening.sh installs, for the site in every Firefox)
if [ "$ublock" = standard ]; then
    "${WREN_UBLOCK_ALLOW:-/usr/local/bin/ublock-allow}" "$url" ||
        warn "uBlock's standard mode wasn't set for it; later: ublock-allow $url"
fi