From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: npostavs@users.sourceforge.net Newsgroups: gmane.emacs.bugs Subject: bug#16984: dired-do-rename susceptible to .../~/... hijack Date: Wed, 07 Dec 2016 20:47:03 -0500 Message-ID: <87a8c7fazs.fsf@users.sourceforge.net> References: <87eh2aq60w.fsf@jidanni.org> <87pomrst3z.fsf@users.sourceforge.net> <837f8zpnih.fsf@gnu.org> <87pomjrita.fsf@users.sourceforge.net> <83pomjr63z.fsf@gnu.org> <87eg2zb860.fsf@gmx.de> <87h97vqhf3.fsf@users.sourceforge.net> <874m3vazx2.fsf@gmx.de> <87wpgoowsx.fsf@users.sourceforge.net> <874m2jpl8c.fsf@gmx.de> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: blaine.gmane.org 1481161640 24406 195.159.176.226 (8 Dec 2016 01:47:20 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 8 Dec 2016 01:47:20 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (gnu/linux) Cc: 16984@debbugs.gnu.org, jidanni@jidanni.org To: Michael Albinus Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Thu Dec 08 02:47:10 2016 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cEnng-0004Oh-Fh for geb-bug-gnu-emacs@m.gmane.org; Thu, 08 Dec 2016 02:47:09 +0100 Original-Received: from localhost ([::1]:43502 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cEnnk-0006Qk-6N for geb-bug-gnu-emacs@m.gmane.org; Wed, 07 Dec 2016 20:47:12 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:46798) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cEnnd-0006Qf-RT for bug-gnu-emacs@gnu.org; Wed, 07 Dec 2016 20:47:07 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cEnna-00035x-OR for bug-gnu-emacs@gnu.org; Wed, 07 Dec 2016 20:47:05 -0500 Original-Received: from debbugs.gnu.org ([208.118.235.43]:45128) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cEnna-00035s-Km for bug-gnu-emacs@gnu.org; Wed, 07 Dec 2016 20:47:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1cEnna-0003In-D6 for bug-gnu-emacs@gnu.org; Wed, 07 Dec 2016 20:47:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: npostavs@users.sourceforge.net Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 08 Dec 2016 01:47:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 16984 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch confirmed Original-Received: via spool by 16984-submit@debbugs.gnu.org id=B16984.148116157210225 (code B ref 16984); Thu, 08 Dec 2016 01:47:02 +0000 Original-Received: (at 16984) by debbugs.gnu.org; 8 Dec 2016 01:46:12 +0000 Original-Received: from localhost ([127.0.0.1]:60526 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cEnml-0002eb-Hv for submit@debbugs.gnu.org; Wed, 07 Dec 2016 20:46:11 -0500 Original-Received: from mail-io0-f196.google.com ([209.85.223.196]:34276) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cEnmj-0002YW-HP for 16984@debbugs.gnu.org; Wed, 07 Dec 2016 20:46:09 -0500 Original-Received: by mail-io0-f196.google.com with SMTP id r94so413613ioe.1 for <16984@debbugs.gnu.org>; Wed, 07 Dec 2016 17:46:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version; bh=hJuppN9iLAupi+LKLcc4b9USxlWzcYQNMtjOrxhFOG8=; b=uMpdFFHjrSaSR9c18jSKNyy1WX8SySemM2MWxXra95Iqyyo6TumcpXVg/siC3WQkMr cbD6KBALTVAE2Z5BlX8+VjnOubwXY8BH0aBYB1hZpskw01gqKrHy9b9caXvDhrF1pCsK Sq+itzUcP6uAcTkZFi5u7GG9gAnArDGkCXNbFspOceE3ocSykBT377/U2PFbjbv8zOoH IzhYxLfT4m2FjtHnn5avC8FxDb1ua1D9pSqeN6meDbRTqaGbFMqhjmmw2Sye8IpAlWyI y4YcJ6XYPHnWkYyVPdbKcUsOd3ujPBtR2wjW0ariEYonb2aHSUoZwguXqzJQVliKtbi1 khuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:references:date :in-reply-to:message-id:user-agent:mime-version; bh=hJuppN9iLAupi+LKLcc4b9USxlWzcYQNMtjOrxhFOG8=; b=XIS/k50mB15kV+AMMN1u9oKeaZiMKqzKPpBYlWIxaRvbraTX7jyMDdgH+st0ZNvfUf aSEc00z2fsl/OjeLB8BLbVFVkSbvxk6A3VieLiuzTF2BHNQfU59Cjt1e7R58hi6fOh/W I8g2Rf98TmFUBBhXVmXnE1SP7KtWgxGUHWRcqSdhd9ilji/QJX37B/mbHilCecR8a/xg zW4I/wFUdF5R4YhOwr9GnDj7CGWOFpKDWfFfnyeenLF60jt03qQjxnw0mee5ZiYnbzSj 4NRFwxNAmmOw99WMAyYf7I9u0pYraplWXANB5O86TDKdBWVm82RQ6l1t8ugYFIqdJ7br VQkw== X-Gm-Message-State: AKaTC00+/90z7tFMMZs55aMuMWqWjl3FEW3t8y5hBmSjb8fDqZy/bMsaUBfphQRhZ67Dpw== X-Received: by 10.36.50.214 with SMTP id j205mr4878538ita.33.1481161563843; Wed, 07 Dec 2016 17:46:03 -0800 (PST) Original-Received: from zony ([45.2.7.65]) by smtp.googlemail.com with ESMTPSA id n206sm296358itg.1.2016.12.07.17.46.02 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 07 Dec 2016 17:46:02 -0800 (PST) In-Reply-To: <874m2jpl8c.fsf@gmx.de> (Michael Albinus's message of "Sun, 04 Dec 2016 20:06:43 +0100") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:126722 Archived-At: --=-=-= Content-Type: text/plain Michael Albinus writes: > npostavs@users.sourceforge.net writes: >>> >>> What do people think to use the "/:" prefix also for the local part of >>> remote file names? Then one could use "/ssh:user@host:/:/path/~/file", >>> making substitute-in-file-name a noop. >> >> Makes sense to me. > > I've added a patch to master which allows quoting the local part of the > file name. Maybe you could rewrite your patch, using this feature. > Something like this? --=-=-= Content-Type: text/plain Content-Disposition: attachment; filename=v4-0001-Quote-filenames-containing-in-prompts.patch Content-Description: patch >From f3c88e481346b40a9afaa6359eef2f2449908284 Mon Sep 17 00:00:00 2001 From: Noam Postavsky Date: Thu, 27 Oct 2016 22:17:11 -0400 Subject: [PATCH v4] Quote filenames containing '~' in prompts When in a directory named '~', the default value given by `read-file-name' should be quoted by prepending '/:', in order to prevent it from being interpreted as referring to the $HOME directory (Bug #16984). * lisp/minibuffer.el (minibuffer-maybe-quote-filename): New function. (completion--sifn-requote, read-file-name-default): Use it instead of `minibuffer--double-dollars'. * test/lisp/files-tests.el (files-test-read-file-in-~): Test it. --- lisp/minibuffer.el | 28 ++++++++++++++++++++-------- test/lisp/files-tests.el | 23 +++++++++++++++++++++++ 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el index 175189c..8cc0687 100644 --- a/lisp/minibuffer.el +++ b/lisp/minibuffer.el @@ -87,7 +87,8 @@ ;;; Code: -(eval-when-compile (require 'cl-lib)) +(eval-when-compile (require 'cl-lib) + (require 'tramp)) ;; For `tramp-quote-name'. ;;; Completion table manipulation @@ -2251,6 +2252,17 @@ minibuffer--double-dollars (replace-regexp-in-string "\\$" (lambda (dollar) (concat dollar dollar)) str)) +(defun minibuffer-maybe-quote-filename (filename) + "Protect FILENAME from `substitute-in-file-name', as needed. +Useful to give the user default values that won't be substituted." + (if (and (not (string-prefix-p "/:" filename)) + (file-name-absolute-p filename) + (string-match-p "/~" filename)) + (if (file-remote-p filename) + (tramp-quote-name filename) + (concat "/:" filename)) + (minibuffer--double-dollars filename))) + (defun completion--make-envvar-table () (mapcar (lambda (enventry) (substring enventry 0 (string-match-p "=" enventry))) @@ -2420,7 +2432,7 @@ completion--sifn-requote (substitute-in-file-name (substring qstr 0 (1- qpos))))) (setq qpos (1- qpos))) - (cons qpos #'minibuffer--double-dollars)))) + (cons qpos #'minibuffer-maybe-quote-filename)))) (defalias 'completion--file-name-table (completion-table-with-quoting #'completion-file-name-table @@ -2596,10 +2608,10 @@ read-file-name-default (let ((insdef (cond ((and insert-default-directory (stringp dir)) (if initial - (cons (minibuffer--double-dollars (concat dir initial)) - (length (minibuffer--double-dollars dir))) - (minibuffer--double-dollars dir))) - (initial (cons (minibuffer--double-dollars initial) 0))))) + (cons (minibuffer-maybe-quote-filename (concat dir initial)) + (length (minibuffer-maybe-quote-filename dir))) + (minibuffer-maybe-quote-filename dir))) + (initial (cons (minibuffer-maybe-quote-filename initial) 0))))) (let ((completion-ignore-case read-file-name-completion-ignore-case) (minibuffer-completing-file-name t) @@ -2693,7 +2705,7 @@ read-file-name-default ;; with what we will actually return. As an exception, ;; if that's the same as the second item in ;; file-name-history, it's really a repeat (Bug#4657). - (let ((val1 (minibuffer--double-dollars val))) + (let ((val1 (minibuffer-maybe-quote-filename val))) (if history-delete-duplicates (setcdr file-name-history (delete val1 (cdr file-name-history)))) @@ -2703,7 +2715,7 @@ read-file-name-default (if add-to-history ;; Add the value to the history--but not if it matches ;; the last value already there. - (let ((val1 (minibuffer--double-dollars val))) + (let ((val1 (minibuffer-maybe-quote-filename val))) (unless (and (consp file-name-history) (equal (car file-name-history) val1)) (setq file-name-history diff --git a/test/lisp/files-tests.el b/test/lisp/files-tests.el index 80d5e5b..f4ccd5c 100644 --- a/test/lisp/files-tests.el +++ b/test/lisp/files-tests.el @@ -220,5 +220,28 @@ files-test-bug-18141-file (should-not yes-or-no-p-prompts) (should (equal kill-emacs-args '(nil))))) +(ert-deftest files-test-read-file-in-~ () + "Test file prompting in directory named '~'. +If we are in a directory named '~', the default value should not +be $HOME." + (cl-letf (((symbol-function 'completing-read) + (lambda (_prompt _coll &optional _pred _req init _hist def _) + (or def init))) + (dir (make-temp-file "read-file-name-test" t))) + (unwind-protect + (let ((subdir (expand-file-name "./~/"))) + (make-directory subdir t) + (with-temp-buffer + (setq default-directory subdir) + (should-not (equal + (expand-file-name (read-file-name "File: ")) + (expand-file-name "~/"))) + ;; Don't overquote either! + (setq default-directory (concat "/:" subdir)) + (should-not (equal + (expand-file-name (read-file-name "File: ")) + (concat "/:/:" subdir))))) + (delete-directory dir 'recursive)))) + (provide 'files-tests) ;;; files-tests.el ends here -- 2.9.3 --=-=-=--