git.iko.soy/typeable/nixpkgs
on master help sign in
torbrowser: reduce risk of stale Nix store references

This patch restructures the expression and wrapper to minimize Nix store references captured by the user's state directory. The previous version would write lots of references to the Nix store into the user's state directory, resulting in synchronization issues between the Store and the local state directory. At best, this would cause TBB to stop working when the version used to instantiate the local state was garbage collected; at worst, a user would continue to use the old version even after an upgrade. To solve the issue, hard-code as much as possible at the Store side and minimize the amount of stuff being copied into the local state dir. Currently, only a few files generated at firefox startup and fontconfig cache files end up capturing store paths; these files are simply removed upon every startup. Otherwise, no capture should occur and the user should always be using the TBB associated with the tor-browser wrapper script. To check for stale Store paths, do `grep -Ero '/nix/store/[^/]+' ~/.local/share/tor-browser` This command should *never* return any other store path than the one associated with the current tor-browser wrapper script, even after an update (assuming you've run tor-browser at least once after updating). Deviations from this general rule are considered bugs from now on. Note that no attempt has been made to support pluggable transports; they are still broken with this patch (to be fixed in a follow-up patch). User visible changes: - Wrapper retains only environment variables required for TBB to work - pulseaudioSupport can be toggled independently of mediaSupport (the latter weakly implies the former). - Store local state under $TBB_HOME. Defaults to $XDG_DATA_HOME/tor-browser - Stop obnoxious first-run stuff (NoScript redirect, in particular) - Set desktop item GenericName to Web Browser Some minor enhancements: - Disable Hydra builds - Specify system -> source mapping to make it easier to extend supported platforms.

commitecd0e1a2c736a9a609cd820de0cb2861129482c3 authorJoachim Fasting <joachifm@fastmail.fm> dateSat 25 Mar 2017, 15:59 (10 years ago) parent417844b
3 files changed+302−104
pkgs/applications/networking/browsers/torbrowser/default.nix+301 pkgs/tools/security/tor/torbrowser.nix−103 pkgs/top-level/all-packages.nix+1−1
pkgs/applications/networking/browsers/torbrowser/default.nixnew file+301
@@ -0,0 +1,301 @@
1+{ stdenv
2+, fetchurl
3+, makeDesktopItem
4+
5+# Common run-time dependencies
6+, zlib
7+
8+# libxul run-time dependencies
9+, alsaLib
10+, atk
11+, cairo
12+, dbus
13+, dbus_glib
14+, fontconfig
15+, freetype
16+, gdk_pixbuf
17+, glib
18+, gtk2
19+, libX11
20+, libXext
21+, libXrender
22+, libXt
23+, pango
24+
25+# Pulseaudio support
26+, pulseaudioSupport ? mediaSupport
27+, libpulseaudio
28+
29+# Media support (implies pulseaudio support)
30+, mediaSupport ? false
31+, gstreamer
32+, gst-plugins-base
33+, gst-plugins-good
34+, gst-ffmpeg
35+, gmp
36+, ffmpeg
37+}:
38+
39+with stdenv.lib;
40+
41+let
42+ libPath = makeLibraryPath ([
43+ alsaLib
44+ atk
45+ cairo
46+ dbus
47+ dbus_glib
48+ fontconfig
49+ freetype
50+ gdk_pixbuf
51+ glib
52+ gtk2
53+ libX11
54+ libXext
55+ libXrender
56+ libXt
57+ pango
58+ stdenv.cc.cc
59+ zlib
60+ ]
61+ ++ optionals pulseaudioSupport [ libpulseaudio ]
62+ ++ optionals mediaSupport [
63+ gstreamer
64+ gst-plugins-base
65+ gmp
66+ ffmpeg
67+ ]);
68+
69+ gstPluginsPath = concatMapStringsSep ":" (x:
70+ "${x}/lib/gstreamer-0.10") [
71+ gstreamer
72+ gst-plugins-base
73+ gst-plugins-good
74+ gst-ffmpeg
75+ ];
76+
77+ # Upstream source
78+ version = "6.5.1";
79+
80+ lang = "en-US";
81+
82+ srcs = {
83+ "x86_64-linux" = fetchurl {
84+ url = "https://dist.torproject.org/torbrowser/${version}/tor-browser-linux64-${version}_${lang}.tar.xz";
85+ sha256 = "1p2bgavvyzahqpjg9vp14c0s50rmha3v1hs1c8zvz6fj8fgrhn0i";
86+ };
87+
88+ "i686-linux" = fetchurl {
89+ url = "https://dist.torproject.org/torbrowser/${version}/tor-browser-linux32-${version}_${lang}.tar.xz";
90+ sha256 = "1zfghr01bhpn39wqaw7hyx7yap7xyla4m3mrgz2vi9a5qsyxmbcr";
91+ };
92+ };
93+in
94+
95+stdenv.mkDerivation rec {
96+ name = "tor-browser-${version}";
97+ inherit version;
98+
99+ src = srcs."${stdenv.system}" or (throw "unsupported system: ${stdenv.system}");
100+
101+ preferLocalBuild = true;
102+
103+ desktopItem = makeDesktopItem {
104+ name = "torbrowser";
105+ exec = "tor-browser";
106+ icon = "torbrowser";
107+ desktopName = "Tor Browser";
108+ genericName = "Web Browser";
109+ comment = meta.description;
110+ categories = "Network;WebBrowser;Security;";
111+ };
112+
113+ buildCommand = ''
114+ # For convenience ...
115+ TBB_IN_STORE=$out/share/tor-browser
116+ interp=$(< $NIX_CC/nix-support/dynamic-linker)
117+
118+ # Unpack & enter
119+ mkdir -p "$TBB_IN_STORE"
120+ tar xf "${src}" -C "$TBB_IN_STORE" --strip-components=2
121+ pushd "$TBB_IN_STORE"
122+
123+ # Set ELF interpreter
124+ for exe in firefox TorBrowser/Tor/tor ; do
125+ patchelf --set-interpreter "$interp" "$exe"
126+ done
127+
128+ # The final libPath. Note, we could split this into firefoxLibPath
129+ # and torLibPath for accuracy, but this is more convenient ...
130+ libPath=${libPath}:$TBB_IN_STORE:$TBB_IN_STORE/TorBrowser/Tor
131+
132+ # Prepare for autoconfig.
133+ #
134+ # See https://developer.mozilla.org/en-US/Firefox/Enterprise_deployment
135+ cat >defaults/pref/autoconfig.js <<EOF
136+ //
137+ pref("general.config.filename", "mozilla.cfg");
138+ pref("general.config.obscure_value", 0);
139+ EOF
140+
141+ # Hard-coded Firefox preferences.
142+ cat >mozilla.cfg <<EOF
143+ // First line must be a comment
144+
145+ // Always update via Nix
146+ lockPref("app.update.auto", false);
147+ lockPref("app.update.enabled", false);
148+ lockPref("extensions.update.autoUpdateDefault", false);
149+ lockPref("extensions.update.enabled", false);
150+
151+ // User should never change these. Locking prevents these
152+ // values from being written to prefs.js, avoiding Store
153+ // path capture.
154+ lockPref("extensions.torlauncher.torrc-defaults_path", "$TBB_IN_STORE/TorBrowser/Data/Tor/torrc-defaults");
155+ lockPref("extensions.torlauncher.tor_path", "$TBB_IN_STORE/TorBrowser/Tor/tor");
156+
157+ // Reset pref that captures store paths.
158+ clearPref("extensions.xpiState");
159+
160+ // Stop obnoxious first-run redirection.
161+ lockPref("noscript.firstRunRedirection", false);
162+ EOF
163+
164+ # Hard-code path to TBB fonts; see also FONTCONFIG_FILE in
165+ # the wrapper below.
166+ FONTCONFIG_FILE=$TBB_IN_STORE/TorBrowser/Data/fontconfig/fonts.conf
167+ sed -i "$FONTCONFIG_FILE" \
168+ -e "s,<dir>fonts</dir>,<dir>$TBB_IN_STORE/fonts</dir>,"
169+
170+ # Move default extension overrides into distribution dir, to avoid
171+ # having to synchronize between local state and store.
172+ mv TorBrowser/Data/Browser/profile.default/preferences/extension-overrides.js defaults/pref/torbrowser.js
173+
174+ # Hard-code paths to geoip data files, to prevent them from being
175+ # copied into the local state directory.
176+ cat >>TorBrowser/Data/Tor/torrc-defaults <<EOF
177+ GeoIPFile $TBB_IN_STORE/TorBrowser/Data/Tor/geoip
178+ GeoIPv6File $TBB_IN_STORE/TorBrowser/Data/Tor/geoip6
179+ EOF
180+
181+ # Generate wrapper
182+ mkdir -p $out/bin
183+ cat > "$out/bin/tor-browser" << EOF
184+ #! ${stdenv.shell}
185+ set -o errexit -o nounset
186+
187+ # Enter local state directory.
188+ REAL_HOME=\$HOME
189+ TBB_HOME=\''${TBB_HOME:-''${XDG_DATA_HOME:-\$REAL_HOME/.local/share}/tor-browser}
190+ HOME=\$TBB_HOME
191+
192+ mkdir -p "\$HOME"
193+ cd "\$HOME"
194+
195+ # Initialize empty TBB local state directory hierarchy. We
196+ # intentionally mirror the layout that TBB would see if executed from
197+ # the unpacked bundle dir.
198+ mkdir -p "\$HOME/TorBrowser" "\$HOME/TorBrowser/Data"
199+
200+ # Initialize the Tor data directory.
201+ mkdir -p "\$HOME/TorBrowser/Data/Tor"
202+
203+ # Initialize the browser profile state. Note that the only data
204+ # copied from the Store payload is the initial bookmark file, which is
205+ # never updated once created. All other files under user's profile
206+ # dir are generated by TBB.
207+ mkdir -p "\$HOME/TorBrowser/Data/Browser/profile.default"
208+ cp -u --no-preserve=mode,owner "$TBB_IN_STORE/TorBrowser/Data/Browser/profile.default/bookmarks.html" \
209+ "\$HOME/TorBrowser/Data/Browser/profile.default/bookmarks.html"
210+
211+ # Clear out some files that tend to capture store references but are
212+ # easily generated by firefox at startup.
213+ rm -f "\$HOME/TorBrowser/Data/Browser/profile.default"/{compatibility.ini,extensions.ini,extensions.json}
214+
215+ # Ensure that we're always using the up-to-date extensions.
216+ ln -snf "$TBB_IN_STORE/TorBrowser/Data/Browser/profile.default/extensions" \
217+ "\$HOME/TorBrowser/Data/Browser/profile.default/extensions"
218+
219+ ${optionalString pulseaudioSupport ''
220+ # Figure out some envvars for pulseaudio
221+ : "\''${XDG_RUNTIME_DIR:=/run/user/\$(id -u)}"
222+ : "\''${XDG_CONFIG_HOME:=\$REAL_HOME/.config}"
223+ : "\''${PULSE_SERVER:=\$XDG_RUNTIME_DIR/pulse/native}"
224+ : "\''${PULSE_COOKIE:=\$XDG_CONFIG_HOME/pulse/cookie}"
225+ ''}
226+
227+ # Font cache files capture store paths; clear them out on the off
228+ # chance that TBB would continue using old font files.
229+ rm -rf "\$HOME/.cache/fontconfig"
230+
231+ # Lift-off
232+ #
233+ # XAUTHORITY and DISPLAY are required for TBB to work at all.
234+ #
235+ # DBUS_SESSION_BUS_ADDRESS is inherited to avoid auto-launch; to
236+ # prevent that, set it to an empty/invalid value prior to running
237+ # tor-browser.
238+ #
239+ # PULSE_SERVER is necessary for audio playback.
240+ #
241+ # Setting FONTCONFIG_FILE is required to make fontconfig read the TBB
242+ # fonts.conf; upstream uses FONTCONFIG_PATH, but FC_DEBUG=1024
243+ # indicates the system fonts.conf being used instead.
244+ exec env -i \
245+ HOME="\$HOME" \
246+ XAUTHORITY="\$XAUTHORITY" \
247+ DISPLAY="\$DISPLAY" \
248+ DBUS_SESSION_BUS_ADDRESS="\$DBUS_SESSION_BUS_ADDRESS" \
249+ \
250+ PULSE_SERVER="\''${PULSE_SERVER:-}" \
251+ PULSE_COOKIE="\''${PULSE_COOKIE:-}" \
252+ \
253+ GST_PLUGIN_SYSTEM_PATH="${optionalString mediaSupport gstPluginsPath}" \
254+ GST_REGISTRY="/dev/null" \
255+ GST_REGISTRY_UPDATE="no" \
256+ \
257+ FONTCONFIG_FILE="$FONTCONFIG_FILE" \
258+ \
259+ LD_LIBRARY_PATH="$libPath" \
260+ \
261+ "$TBB_IN_STORE/firefox" \
262+ --class "Tor Browser" \
263+ -no-remote \
264+ -profile "\$HOME/TorBrowser/Data/Browser/profile.default" \
265+ "\''${@}"
266+ EOF
267+ chmod +x $out/bin/tor-browser
268+
269+ # Easier access to docs
270+ mkdir -p $out/share/doc
271+ ln -s $TBB_IN_STORE/TorBrowser/Docs $out/share/doc/tor-browser
272+
273+ # Install .desktop item
274+ mkdir -p $out/share/applications
275+ cp $desktopItem/share/applications"/"* $out/share/applications
276+
277+ # Install icons
278+ mkdir -p $out/share/pixmaps
279+ cp browser/icons/mozicon128.png $out/share/pixmaps/torbrowser.png
280+
281+ # Check installed apps
282+ echo "Checking bundled Tor ..."
283+ LD_LIBRARY_PATH=$libPath $TBB_IN_STORE/TorBrowser/Tor/tor --version >/dev/null
284+
285+ echo "Checking tor-browser wrapper ..."
286+ DISPLAY="" XAUTHORITY="" DBUS_SESSION_BUS_ADDRESS="" TBB_HOME=$(mktemp -d) \
287+ $out/bin/tor-browser --version >/dev/null
288+ '';
289+
290+ meta = with stdenv.lib; {
291+ description = "Tor Browser Bundle";
292+ homepage = https://www.torproject.org/;
293+ platforms = attrNames srcs;
294+ maintainers = with maintainers; [ offline matejc doublec thoughtpolice joachifm ];
295+ hydraPlatforms = [];
296+ # MPL2.0+, GPL+, &c. While it's not entirely clear whether
297+ # the compound is "libre" in a strict sense (some components place certain
298+ # restrictions on redistribution), it's free enough for our purposes.
299+ license = licenses.free;
300+ };
301+}
pkgs/tools/security/tor/torbrowser.nixdeleted−103
@@ -1,103 +0,0 @@
1−{ stdenv, fetchurl, makeDesktopItem
2−, libXrender, libX11, libXext, libXt, alsaLib, dbus, dbus_glib, glib, gtk2
3−, atk, pango, freetype, fontconfig, gdk_pixbuf, cairo, zlib
4−, gstreamer, gst-plugins-base, gst-plugins-good, gst-ffmpeg, gmp, ffmpeg
5−, libpulseaudio
6−, mediaSupport ? false
7−}:
8−
9−let
10− libPath = stdenv.lib.makeLibraryPath ([
11− stdenv.cc.cc zlib glib alsaLib dbus dbus_glib gtk2 atk pango freetype
12− fontconfig gdk_pixbuf cairo libXrender libX11 libXext libXt
13− ] ++ stdenv.lib.optionals mediaSupport [
14− gstreamer gst-plugins-base gmp ffmpeg
15− libpulseaudio
16− ]);
17−
18− # Ignored if !mediaSupport
19− gstPlugins = [ gstreamer gst-plugins-base gst-plugins-good gst-ffmpeg ];
20−
21− gstPluginsPath = stdenv.lib.concatMapStringsSep ":" (x:
22− "${x}/lib/gstreamer-0.10") gstPlugins;
23−in
24−
25−stdenv.mkDerivation rec {
26− name = "tor-browser-${version}";
27− version = "6.5.1";
28−
29− src = fetchurl {
30− url = "https://dist.torproject.org/torbrowser/${version}/tor-browser-linux${if stdenv.is64bit then "64" else "32"}-${version}_en-US.tar.xz";
31− sha256 = if stdenv.is64bit then
32− "1p2bgavvyzahqpjg9vp14c0s50rmha3v1hs1c8zvz6fj8fgrhn0i" else
33− "1zfghr01bhpn39wqaw7hyx7yap7xyla4m3mrgz2vi9a5qsyxmbcr";
34− };
35−
36− preferLocalBuild = true;
37−
38− desktopItem = makeDesktopItem {
39− name = "torbrowser";
40− exec = "tor-browser";
41− icon = "torbrowser";
42− desktopName = "Tor Browser";
43− genericName = "Tor Browser";
44− comment = meta.description;
45− categories = "Network;WebBrowser;Security;";
46− };
47−
48− patchPhase = ''
49− patchelf --set-interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" Browser/firefox
50− patchelf --set-interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" Browser/TorBrowser/Tor/tor
51−
52− sed -e "s,./TorBrowser,$out/share/tor-browser/Browser/TorBrowser,g" -i Browser/TorBrowser/Data/Tor/torrc-defaults
53− '';
54−
55− doCheck = true;
56− checkPhase = ''
57− echo "Checking firefox..."
58− LD_LIBRARY_PATH=${libPath} Browser/firefox --help 1> /dev/null
59− echo "Checking tor..."
60− LD_LIBRARY_PATH=${libPath}:Browser/TorBrowser/Tor Browser/TorBrowser/Tor/tor --help 1> /dev/null
61− '';
62−
63− installPhase = ''
64− mkdir -p $out/share/tor-browser
65− mkdir -p $out/bin
66− cp -R * $out/share/tor-browser
67−
68− cat > "$out/bin/tor-browser" << EOF
69− #! ${stdenv.shell}
70− unset SESSION_MANAGER
71− export HOME="\$HOME/.torbrowser4"
72− if [ ! -d \$HOME ]; then
73− mkdir -p \$HOME && cp -R $out/share/tor-browser/Browser/TorBrowser/Data \$HOME/ && chmod -R +w \$HOME
74− echo "pref(\"extensions.torlauncher.tordatadir_path\", \"\$HOME/Data/Tor/\");" >> \
75− ~/Data/Browser/profile.default/preferences/extension-overrides.js
76− echo "pref(\"extensions.torlauncher.torrc-defaults_path\", \"\$HOME/Data/Tor/torrc-defaults\");" >> \
77− ~/Data/Browser/profile.default/preferences/extension-overrides.js
78− echo "pref(\"extensions.torlauncher.tor_path\", \"$out/share/tor-browser/Browser/TorBrowser/Tor/tor\");" >> \
79− ~/Data/Browser/profile.default/preferences/extension-overrides.js
80− fi
81− export FONTCONFIG_PATH=\$HOME/Data/fontconfig
82− export LD_LIBRARY_PATH=${libPath}:$out/share/tor-browser/Browser/TorBrowser/Tor
83− ${stdenv.lib.optionalString mediaSupport ''
84− export GST_PLUGIN_SYSTEM_PATH=${gstPluginsPath}
85− ''}
86− exec $out/share/tor-browser/Browser/firefox --class "Tor Browser" -no-remote -profile ~/Data/Browser/profile.default "\$@"
87− EOF
88− chmod +x $out/bin/tor-browser
89−
90− mkdir -p $out/share/applications
91− cp $desktopItem/share/applications"/"* $out/share/applications
92−
93− mkdir -p $out/share/pixmaps
94− cp Browser/browser/icons/mozicon128.png $out/share/pixmaps/torbrowser.png
95− '';
96−
97− meta = with stdenv.lib; {
98− description = "Tor Browser Bundle";
99− homepage = https://www.torproject.org/;
100− platforms = platforms.linux;
101− maintainers = with maintainers; [ offline matejc doublec thoughtpolice joachifm ];
102− };
103−}
pkgs/top-level/all-packages.nix+1−1
@@ -4189,7 +4189,7 @@ with pkgs;
41894189
41904190 tor-arm = callPackage ../tools/security/tor/tor-arm.nix { };
41914191
4192− torbrowser = callPackage ../tools/security/tor/torbrowser.nix { };
4192+ torbrowser = callPackage ../applications/networking/browsers/torbrowser { };
41934193
41944194 touchegg = callPackage ../tools/inputmethods/touchegg { };
41954195