Package: xdg-utils
Version: 1.1.0~rc1+git20111210-4
Severity: normal
Tags: patch

Dear Maintainer,

When trying to open an HTTP(S) URL containing ampersands with xdg-open,
it fails. Rather than passing the given URL to the browser, it replaces
ampersands with '%u' (which becomes %25u, once URL-encoded).

For example,
   xdg-open "https://www.google.com/search?q=grail&ie=utf-8";
opens "https://www.google.com/search?q=grail%uie=utf-8"; instead,
searching for 'grail%uie=utf-8' instead of searching for 'grail'.

Investigating a bit, I found the problem to be at line 552 of xdg-open:
   arguments_exec="`echo $arguments | sed -e 's*%[fFuU]*"'"$1"'"*g'`"
In our case, $arguments contains '%u'. The problem is with the sed
expression, which uses $1 (our URL) without escaping ampersands in it.
In the replacement part of a sed expression, an unescaped & stands for
the portion of the pattern that matched. In our case, every & in the URL
is thus replaced by a '%u'.

As a quick fix, I modified mine as follows:
   escaped="`echo $1 | sed -e 's/&/\\\&/g'`"
   arguments_exec="`echo $arguments | sed -e 's*%[fFuU]*"'"$escaped"'"*g'`"

This allows me to open HTTP URLs with ampersands as expected. However, I
give it more as an illustration of the problem than as a serious fix
proposal:
- It may not be the most elegant way to fix it.
- It may be necessary also in other places.
- It is necessary also for other characters. E.g.
     xdg-open "https://www.google.com/search?q=grail\1";
  fails with the message
     sed: -e expression #1, char 53: invalid reference \1 on `s' command's RHS
  instead of opening the URL.

-- System Information:
Debian Release: wheezy/sid
  APT prefers testing
  APT policy: (800, 'testing'), (700, 'unstable')
Architecture: amd64 (x86_64)

Kernel: Linux 3.1.0-1-amd64 (SMP w/4 CPU cores)
Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash

xdg-utils depends on no packages.

Versions of packages xdg-utils recommends:
ii  libfile-mimeinfo-perl  0.15-2
ii  libnet-dbus-perl       1.0.0-1+b1
ii  libx11-protocol-perl   0.56-2
ii  x11-utils              7.6+4
ii  x11-xserver-utils      7.6+3

Versions of packages xdg-utils suggests:
ii  gvfs-bin  1.10.1-2

-- no debconf information
--- xdg-open	2012-01-06 12:12:54.078811539 +0100
+++ xdg-open.new	2012-01-06 12:10:44.149866844 +0100
@@ -549,7 +549,8 @@
                 command="`grep -E "^Exec(\[[^]=]*])?=" "$file" | cut -d= -f 2- | first_word`"
                 command_exec=`which $command 2>/dev/null`
                 arguments="`grep -E "^Exec(\[[^]=]*])?=" "$file" | cut -d= -f 2- | last_word`"
-                arguments_exec="`echo $arguments | sed -e 's*%[fFuU]*"'"$1"'"*g'`"
+                escaped="`echo $1 | sed -e 's/&/\\\&/g'`"
+                arguments_exec="`echo $arguments | sed -e 's*%[fFuU]*"'"$escaped"'"*g'`"
                 if [ -x "$command_exec" ] ; then
                     if echo $arguments | grep -iq '%[fFuU]' ; then
                         eval $command_exec $arguments_exec

Reply via email to