Subject: bug#65902: 29.0.92; emacsclient-mail.desktop fails due to complicated escaping

From: [email-masked]
Date: Thu, 14 Sep 2023 11:03:44 +0000 (UTC)

Eli Zaretskii <[email-masked]> writes:

>> Date: Wed, 13 Sep 2023 12:33:01 -0700
>> Cc: [email-masked], [email-masked]
>> From: Jim Porter <[email-masked]>
>> 
>> On 9/13/2023 12:13 PM, Eli Zaretskii wrote:
>> > And I don't understand why we need to add any options to Emacs itself,
>> > btw.  The suggestion to have some "symmetry" here was one of the
>> > reasons that discussion got nowhere.  So let's learn from that
>> > mistake, at least.
>> 
>> There's a practical benefit to this. If you have $EDITOR set in your 
>> environment to something like "emacsclient --alternate-editor=emacs", 
>> then it would be nice if you could say this:
>> 
>>    $EDITOR --apply some-func arg1 arg2
>> 
>> and have it do the same thing whether or not there was already an Emacs 
>> server running. The symmetry between the two commands (plus proper 
>> argument forwarding) would make that work.
>> 
>> However, if people can't agree, then we could probably drop that part. 
>> To me, it sounds like people *do* agree that this would be good to have 
>> though.
>
> People might agree, but I don't.  Please consider the perspective.
> This started as an obscure and rare problem in a desktop file (which
> we provided solely out of good will, since it really isn't our job to
> do so, it's the job of downstream distros).  The proposed solution was
> to add a completely new option to emacsclient, with its own special
> syntax and rules about what can and cannot be done with it.  This is
> already something that should raise brows: how can such an unimportant
> reason cause us to make such significant changes?  We didn't yet
> finish discussing that nor even had time to understand all the
> implications (remember: Lars suggested to support several such
> options, which required another special option), and we already are
> told that "for symmetry" we should add the same to Emacs.  All that
> where just yesterday there was no need for any new options in either,
> and if we decided to drop those desktop files from our sources (which
> I personally am tempted to do every few weeks, due to issues they
> cause us all the time since their introduction), then even the
> original need will miraculously disappear into thin air.

The issue is not really with the desktop file.  It's a generic problem:
Suppose I have some arbitrary data which I want to send to the Emacs
server (in this case, a URI).  Today, there's no easy way to do that.

- One approach is to stick the data inside an --eval call, as
  emacsclient-mail.desktop is doing.  Getting the quoting right is hard
  and complex, and even Emacs developers have failed at it over multiple
  iterations, and when they fail it either breaks or exposes a security
  vulnerability.  It is currently broken for me.

- Another approach is to do what org-protocol does (shipped with
  Emacs!), and advise server-visit-files (with
  org-protocol-check-filename-for-protocol), and pass the data as a
  string FILE argument to emacsclient which gets intercepted by advice.
  But this is of course a gross hack, and also it still requires
  escaping the data, since some characters will still be specially
  interpreted.  It would be nice to get rid of this org-protocol hack
  which is shipped with Emacs.

- A third approach is to put the data in a temporary file and pass the
  path of that file to emacsclient, then use an --eval to process the
  file.  But this doesn't work when emacsclient and the Emacs server are
  on different hosts or in different environments.

- Finally, a fourth approach is to teach emacsclient to be able to send
  this arbitrary data to Emacs on its own.  Such as by adding --apply.
  There are a bunch of designs for that, but all of them require
  modifying emacsclient.

I have many times before wanted to be able to pass data to Emacs without
worrying about escaping.  This would be a very useful feature to have.
And it would be nice to get rid of the org-protocol hack, and get rid of
the complicated and broken escaping needed for things like
emacsclient-mail.desktop.

> So this is a classic case of the tail wagging the dog.
>
> What about alternative solutions: use a shell script in the desktop
> files, and delegate to that script to solve the problem with quoting?
> Had anyone considered this strategy?  If not, why not?

Getting the quoting right is hard and complex, and even Emacs developers
have failed at it over multiple iterations, and when they fail it either
breaks or exposes a security vulnerability.  This solution is far
simpler, and is reusable for many other different purposes.

> I would in general prefer not to add any new options to our programs
> due to this weak reason.  Once again: it is not our job to get these
> desktop files right in every single downstream environment, so let's
> not make it our problem, certainly not a problem we should solve using
> such non-trivial solutions.

We don't have to add it to Emacs itself.  But it's not just for the
desktop files.  It's a relatively small feature to add to emacsclient,
and it's something that I've heard years of user requests for.

I have many times before wanted to be able to pass data to Emacs without
worrying about escaping.  This would be a very useful feature to have.
It would be nice to get rid of the org-protocol hack, and get rid of the
complicated and broken escaping needed for things like
emacsclient-mail.desktop.