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: Sat, 29 Oct 2016 11:54:56 -0400 Message-ID: <87h97vqhf3.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> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: blaine.gmane.org 1477756539 15763 195.159.176.226 (29 Oct 2016 15:55:39 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sat, 29 Oct 2016 15:55:39 +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 Sat Oct 29 17:55:33 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 1c0VyR-0000Y3-D5 for geb-bug-gnu-emacs@m.gmane.org; Sat, 29 Oct 2016 17:55:11 +0200 Original-Received: from localhost ([::1]:55061 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c0VyU-0006Xi-0o for geb-bug-gnu-emacs@m.gmane.org; Sat, 29 Oct 2016 11:55:14 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:59536) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c0VyM-0006VE-IW for bug-gnu-emacs@gnu.org; Sat, 29 Oct 2016 11:55:07 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1c0VyI-000699-J6 for bug-gnu-emacs@gnu.org; Sat, 29 Oct 2016 11:55:06 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:49589) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1c0VyI-000692-GE for bug-gnu-emacs@gnu.org; Sat, 29 Oct 2016 11:55:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1c0VyI-000334-6y for bug-gnu-emacs@gnu.org; Sat, 29 Oct 2016 11:55:02 -0400 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: Sat, 29 Oct 2016 15:55: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.147775646411672 (code B ref 16984); Sat, 29 Oct 2016 15:55:02 +0000 Original-Received: (at 16984) by debbugs.gnu.org; 29 Oct 2016 15:54:24 +0000 Original-Received: from localhost ([127.0.0.1]:36755 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1c0Vxf-00032B-Re for submit@debbugs.gnu.org; Sat, 29 Oct 2016 11:54:24 -0400 Original-Received: from mail-it0-f54.google.com ([209.85.214.54]:38218) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1c0Vxd-00031z-IS for 16984@debbugs.gnu.org; Sat, 29 Oct 2016 11:54:21 -0400 Original-Received: by mail-it0-f54.google.com with SMTP id q124so29845229itd.1 for <16984@debbugs.gnu.org>; Sat, 29 Oct 2016 08:54:21 -0700 (PDT) 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=V78qI5FDnBImTaJX2sWHBQ8Um1ESRznsxEdsQajSpxg=; b=O5fQVaxYVQqI2fsdn9vc1fzGB8pAFzzM8YiIjegc1BW6W7JXJWyIG1GGVxrlivtpRF t2CHNrOlUq8OnPVe5AIhk1qBHClsv4vzaheHx7ojs2vFg1iejI8lqFPzzoIgNMgXvipm 7OEXgNRdD7F78m5J4e7bgpJ/WaLbPt8DnCpbIer2sQcJgqDpEhqKEuYIC9jn73tE0dhN AiAgvLYskLpUyJgcIhJCtidjNQeYtqUQh4LAjaHThuYaLOBHN/TMumyDwQtoRDVGwsMc HPzS4Nij9RaRvVyyulYqNfKTngiGttAjK+j2P9hrCqZou06oYETPY8TJEVlBoHVNXewk 0NOg== 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=V78qI5FDnBImTaJX2sWHBQ8Um1ESRznsxEdsQajSpxg=; b=CYXM/Mv+ifdZ4uVmr0vzdnAv3YFi9FtTY60wilwKgywAVsLkxpjL1hzU9Z2bUd1MTq I2s1bviogshFcEiu71ccqL0JOB3BDAzgNr8CBqk60AERu+Cwzig/VX6GmmhOuj1gnDHU CFx3W48S7IlEgjMimdzoTsrCVAIwEtsxhlIkVoylnsKL9ds1wO2om9boPHXR6m2GgpRk tG4a9odNXJuswvDR2SnIosoXu/Eaos72xYObiiDoIHvCxqW7oMybEqfxjuZjukRoFIup UxsEMbAZNQwb9PX43QGKIoDWnwmFzk1rdo+IJ/K4eDpaw3f551gg5Q/QsGiMeYXjeZ/G 0/YA== X-Gm-Message-State: ABUngvcxzjryLUQqzg1KATJp4LXkS0exS2UxOo6tKmd8nG435qZnDP51bMART0zaOFefRA== X-Received: by 10.36.57.5 with SMTP id l5mr2898777ita.26.1477756455925; Sat, 29 Oct 2016 08:54:15 -0700 (PDT) Original-Received: from zony ([45.2.7.130]) by smtp.googlemail.com with ESMTPSA id 70sm7136814iou.3.2016.10.29.08.54.14 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 29 Oct 2016 08:54:15 -0700 (PDT) In-Reply-To: <87eg2zb860.fsf@gmx.de> (Michael Albinus's message of "Sat, 29 Oct 2016 15:23:51 +0200") 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:125117 Archived-At: --=-=-= Content-Type: text/plain Michael Albinus writes: > Eli Zaretskii writes: > >>> From: npostavs@users.sourceforge.net >>> Cc: 16984@debbugs.gnu.org, jidanni@jidanni.org >>> Date: Fri, 28 Oct 2016 22:27:13 -0400 >>> >>> > What about the "/:" quoting? It works for me, when I type "/:" before >>> > the name of the file which has a '~' character embedded in it. >>> >>> Ah, yes it works, as documented in `(emacs) Quoted File Names'. I think >>> it would be nicer if Emacs' file prompts defaulted to insert this as >>> needed, here's a patch to do that: >> >> This is good for master, but please wait for a few days in case >> someone would like to comment or object. > > Prefixing with "/:" would also deactivate all file name handlers. The > file name "/ssh:user@host:/path/~/file" would be handled literally, > which is wrong. Ah, good point. How about checking (find-file-name-handler filename 'substitute-in-file-name): --=-=-= Content-Type: text/plain Content-Disposition: attachment; filename=v2-0001-Quote-filenames-containing-in-prompts.patch Content-Description: patch v2 >From f5a6fbca230f79745be979df4d73550a201fcc53 Mon Sep 17 00:00:00 2001 From: Noam Postavsky Date: Thu, 27 Oct 2016 22:17:11 -0400 Subject: [PATCH v2] 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 | 27 ++++++++++++++++++++------- test/lisp/files-tests.el | 23 +++++++++++++++++++++++ 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el index 175189c..217bcac 100644 --- a/lisp/minibuffer.el +++ b/lisp/minibuffer.el @@ -2251,6 +2251,19 @@ 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) + (not (let ((handler (find-file-name-handler + filename 'substitute-in-file-name))) + (and handler + (funcall handler 'substitute-in-file-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 +2433,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 +2609,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 +2706,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 +2716,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 --=-=-=--