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: Thu, 08 Dec 2016 23:56:43 -0500 Message-ID: <87vautem44.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> <87a8c7fazs.fsf@users.sourceforge.net> <8760muu8wq.fsf@gmx.de> <874m2efpso.fsf@users.sourceforge.net> <83a8c6if73.fsf@gnu.org> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: blaine.gmane.org 1481259374 17282 195.159.176.226 (9 Dec 2016 04:56:14 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Fri, 9 Dec 2016 04:56:14 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (gnu/linux) Cc: michael.albinus@gmx.de, 16984@debbugs.gnu.org, jidanni@jidanni.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Fri Dec 09 05:56: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 1cFDE9-0003gg-LW for geb-bug-gnu-emacs@m.gmane.org; Fri, 09 Dec 2016 05:56:09 +0100 Original-Received: from localhost ([::1]:44580 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cFDED-0002FB-HA for geb-bug-gnu-emacs@m.gmane.org; Thu, 08 Dec 2016 23:56:13 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:54937) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cFDE6-00028b-4F for bug-gnu-emacs@gnu.org; Thu, 08 Dec 2016 23:56:07 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cFDE2-0001N1-Vl for bug-gnu-emacs@gnu.org; Thu, 08 Dec 2016 23:56:06 -0500 Original-Received: from debbugs.gnu.org ([208.118.235.43]:47910) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cFDE2-0001Mx-SA for bug-gnu-emacs@gnu.org; Thu, 08 Dec 2016 23:56:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1cFDE2-0008RM-J0 for bug-gnu-emacs@gnu.org; Thu, 08 Dec 2016 23:56: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: Fri, 09 Dec 2016 04:56: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.148125935132425 (code B ref 16984); Fri, 09 Dec 2016 04:56:02 +0000 Original-Received: (at 16984) by debbugs.gnu.org; 9 Dec 2016 04:55:51 +0000 Original-Received: from localhost ([127.0.0.1]:35076 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cFDDq-0008Qv-Tn for submit@debbugs.gnu.org; Thu, 08 Dec 2016 23:55:51 -0500 Original-Received: from mail-io0-f195.google.com ([209.85.223.195]:33743) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cFDDo-0008Qh-VR for 16984@debbugs.gnu.org; Thu, 08 Dec 2016 23:55:49 -0500 Original-Received: by mail-io0-f195.google.com with SMTP id p13so4543337ioi.0 for <16984@debbugs.gnu.org>; Thu, 08 Dec 2016 20:55:48 -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=JthuOrWGGQdISE7pQEp/JdqjGRTcLk9I11JEE8+OPyM=; b=OfXssmWbT8jHvIdvqbleVNwyNJd0CT/jkAOkXmqb5VrMa71w1LM9qN83IOx2A5R3z7 NFuUKagsfWEXAd9iO61JTQYwnxvqH91eREhIx3PwyIMFX4hSWHNTRfhyCfEaiN1PyJxQ 6pOCLCJiKiqkjs2aiPYkKVwGrMtupxoeH/I4Fl7rYGpb0CcnJTB7drw/XYyzcW3bpHr/ IptcaC2EShC5NjV2H2+k52alKbK4TpJTzJy0mk59IU55PCVw597WKT5iT7cP8dhN7uQ6 iTkmp4QQUfXg6ym2G0kR98gcvOHpqwcoQbaViHSdnU1NgGx3VjcOFPDat/dEpm8XIiz9 ZrVw== 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=JthuOrWGGQdISE7pQEp/JdqjGRTcLk9I11JEE8+OPyM=; b=ijWdS0dajMkf6xMSWc0AlSn/d3zwXTQbazeHx6CIGhdSIuc8cpEvvAjboQiy4BSMpY bmHVwShcuVf71/axNvc9cEmH/A5koeEyEGzVvQhK2yh17ef8CU44Kf0BRXe2BaZEthK2 1yoaWIBWZUsUh8eAR4P3eop5U5i2eIjpfrHiK+LqenUWcGLE4hIsiqMaL2OXRKBTuOmY P2q2jERHc1qbhDwxhXlEjpzkUlbSTQLGNaTsLfhy+jkh37PL8ksE7aBpVlqKMuOL83oL zZjreSdrs+dH0hL2VJFkCkLDPFp2AbSxmEELnXzz9aMfRzPQ3CZhc2OfnirNnXSkjDCd AQWg== X-Gm-Message-State: AKaTC02EWnfkyaGEA0xTTgCwpPvjcDvPkJORNDpYe6IdV5+azxNnLx2uCLyKAP9X/iKGdQ== X-Received: by 10.36.219.196 with SMTP id c187mr5193211itg.23.1481259343268; Thu, 08 Dec 2016 20:55:43 -0800 (PST) Original-Received: from zony ([45.2.7.65]) by smtp.googlemail.com with ESMTPSA id j201sm7736183ita.20.2016.12.08.20.55.42 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 08 Dec 2016 20:55:42 -0800 (PST) In-Reply-To: <83a8c6if73.fsf@gnu.org> (Eli Zaretskii's message of "Thu, 08 Dec 2016 18:00:16 +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:126760 Archived-At: --=-=-= Content-Type: text/plain Eli Zaretskii writes: >> From: npostavs@users.sourceforge.net >> Cc: 16984@debbugs.gnu.org, Eli Zaretskii , jidanni@jidanni.org >> Date: Thu, 08 Dec 2016 09:39:35 -0500 >> >> (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." >> (let ((local (file-remote-p filename 'localname))) >> (if (and (not (string-prefix-p "/:" local)) >> (file-name-absolute-p filename) >> (string-match-p "/~" local)) >> (tramp-quote-name filename) >> (minibuffer--double-dollars filename)))) > > Is the argument guaranteed to come from expand-file-name? If not, it > should also accept file names matching "\~" on MS platforms. I think it does, but I'm not entirely sure. Better safe than sorry? --=-=-= Content-Type: text/plain Content-Disposition: attachment; filename=v5-0001-Quote-filenames-containing-in-prompts.patch Content-Description: patch >From 1fc91a2a9fe7f9f0118edc047080718d11659c3f Mon Sep 17 00:00:00 2001 From: Noam Postavsky Date: Thu, 27 Oct 2016 22:17:11 -0400 Subject: [PATCH v5] 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 | 25 ++++++++++++++++++------- test/lisp/files-tests.el | 23 +++++++++++++++++++++++ 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el index 175189c..576b804 100644 --- a/lisp/minibuffer.el +++ b/lisp/minibuffer.el @@ -2251,6 +2251,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 (file-name-quoted-p filename)) + (file-name-absolute-p filename) + (string-match-p (if (memq system-type '(windows-nt ms-dos)) + "[/\\\\]~" "/~") + (file-local-name filename))) + (file-name-quote filename) + (minibuffer--double-dollars filename))) + (defun completion--make-envvar-table () (mapcar (lambda (enventry) (substring enventry 0 (string-match-p "=" enventry))) @@ -2420,7 +2431,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 +2607,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 +2704,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 +2714,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 --=-=-=--