summaryrefslogtreecommitdiff
path: root/firefox-sideloader/functions.sh
blob: 2934c09efc7b8e756ced10b6860c9f667e85ea68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/bin/sh

profiles_ini=~/.mozilla/firefox/profiles.ini

get_default_firefox_profile_basename()
{
    < "$profiles_ini" \
    perl -ne 'BEGIN { $/ = "" }
              /^Default=1$/m || next;
              /^Path=(.+)$/m || die;
              print "$1\n";
              $printed = 1;
              last;

              END { die unless $printed }'
}

get_default_firefox_profile_dir()
{
    local prof
    prof=$(get_default_firefox_profile_basename) || return
    printf '%s/%s\n' "${profiles_ini%/*}" "$prof"
}

get_basename_from_profile_name()
{
    < "$profiles_ini" \
      ARG="$1" \
      perl -ne 'BEGIN { $/ = "" }
                /^Name=(.+)$/m || next;
                $1 eq $ENV{ARG} || next;
                /^Path=(.+)$/m || next;
                print "$1";
                $printed = 1;
                last;

                END { die unless $printed }'
}
get_profile_dir_from_profile_name()
{
    local prof
    prof=$(get_basename_from_profile_name "$1") || return
    printf '%s/%s\n' "${profiles_ini%/*}" "$prof"
}

get_profile_dir()
{
    if [ "$1" ]
    then get_profile_dir_from_profile_name "$1"
    else get_default_firefox_profile_dir
    fi
}

illustrate_leak()
{
    local prof
    which jq >/dev/null || sudo apt-get install jq
    prof=$(get_default_firefox_profile_dir) && jq . < "$prof"/extensions.json | grep "$HOME"
}

filter()
{
    if which jq >/dev/null
    then jq .
    else cat
    fi | sed -e "$filter_json"
}

copy_json_file_with_filter()
{
    which mozlz4 >/dev/null || PATH=$HOME/.cargo/bin:$PATH
    which mozlz4 >/dev/null || exit 1
    local new="$1" old="$2" f="$3" filter_json
    filter_json="s/${old//\//\\/}/${new//\//\\/}/g"
    [ -d "$old" -a -d "$new" ] || return
    touch --reference="$old"/"$f" "$new"/"$f" || return
    chmod --reference="$old"/"$f" "$new"/"$f" || return
    case "$f" in
        *lz4) mozlz4 - | filter | mozlz4 -z - ;;
        *) filter
    esac < "$old"/"$f" > "$new"/"$f"

    # Keep an uncompressed copy for inspection
    local keep
    case "$f" in
        *.lz4) keep=$new/${f%lz4}unused ;;
        *lz4) keep=$new/${f%lz4}.unused ;;
        *) keep= ;;
    esac
    if [ "$keep" ]
    then mozlz4 - < "$new"/"$f" > "$keep"
    fi
}

keep_prefs()
{
    (xargs printf %s | tr -d ' ') <<'EOF'
browser.newtab.extensionControlled |
browser.newtab.privateAllowed |
browser.reader.detectedFirstArticle |
browser.rights.3.shown |
browser.startup.homepage |
browser.startup.homepage_override.buildID |
browser.startup.homepage_override.mstone |
browser.toolbarbuttons.introduced.pocket-button |
datareporting.policy.dataSubmissionPolicyAcceptedVersion |
datareporting.policy.dataSubmissionPolicyNotifiedTime |
extensions.webextensions.uuids
EOF
}

prefs()
{
    if [ "$COPY_ALL_PREFS" ]
    then
        copy_json_file_with_filter "$new_profile_dir" "$old_profile_dir" 'prefs.js'
    else
        egrep "^user_pref.\"($(printf "%s" $(keep_prefs)))\"" "$old_profile_dir"/prefs.js > "$new_profile_dir"/prefs.js
        printf '%s\n' 'user_pref("browser.startup.homepage_override.mstone", "ignore");' >> "$new_profile_dir"/prefs.js
    fi
}

clone_profile_raw()
{
    local old_profile_dir="$1" new_profile_name="$2"
    [ -d "$old_profile_dir" ] || return
    [ -d "$old_profile_dir"/extensions ] || return

    firefox -createProfile "$new_profile_name" || return
    new_profile_dir=$(get_profile_dir "$new_profile_name") || return
    [ "$new_profile_dir" ] || return
    [ "$new_profile_dir" != "$old_profile_dir" ] || return

    cp -r --preserve=mode,timestamps -t "$new_profile_dir" -- "$old_profile_dir"/{extensions/,extension-{setting,preference}s.json} || return

    copy_json_file_with_filter "$new_profile_dir" "$old_profile_dir" 'extensions.json'
    copy_json_file_with_filter "$new_profile_dir" "$old_profile_dir" 'addonStartup.json.lz4'

    prefs
}

clone_profile()
{
    local old_profile_name="$1" old_profile_dir
    shift
    old_profile_dir=$(get_profile_dir "$old_profile_name") || return
    clone_profile_raw "$old_profile_dir" "$@"
}

clone_default_profile()
{
    new_name=$1
    [ "$new_name" ] || return
    old_dir=$(get_default_firefox_profile_dir) || return
    [ "$old_dir" ] || return
    clone_profile_raw "$old_dir" "$new_name"
}

list_extensions()
{
    [ "$firefox_profile" ] || firefox_profile=$(get_default_firefox_profile_dir)
    jq -c '.addons[] | {id:.id, name:.locales[0].name}' < "$firefox_profile"/extensions.json
}

runtest()
{
    . functions.sh
    clone_default_profile "test.$(date -I)"
    echo r=$?
}