From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Antero Mejr via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#61901: 30.0.50; [PATCH v3] Add safe-local-variable-directories variable. Date: Tue, 09 May 2023 21:29:54 +0000 Message-ID: <87r0rpi46l.fsf@mailbox.org> References: <87sfeoksuk.fsf@mailbox.org> Reply-To: Antero Mejr Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="1491"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 61901@debbugs.gnu.org To: eliz@gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue May 09 23:31:19 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pwUvW-0000BT-Cl for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 09 May 2023 23:31:18 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pwUvJ-0000fp-1P; Tue, 09 May 2023 17:31:05 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pwUvG-0000es-8k for bug-gnu-emacs@gnu.org; Tue, 09 May 2023 17:31:03 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pwUvF-0007A3-RW for bug-gnu-emacs@gnu.org; Tue, 09 May 2023 17:31:01 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pwUvF-0008Rn-O4 for bug-gnu-emacs@gnu.org; Tue, 09 May 2023 17:31:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Antero Mejr Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 09 May 2023 21:31:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 61901 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 61901-submit@debbugs.gnu.org id=B61901.168366781132396 (code B ref 61901); Tue, 09 May 2023 21:31:01 +0000 Original-Received: (at 61901) by debbugs.gnu.org; 9 May 2023 21:30:11 +0000 Original-Received: from localhost ([127.0.0.1]:44640 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pwUuQ-0008QS-3h for submit@debbugs.gnu.org; Tue, 09 May 2023 17:30:10 -0400 Original-Received: from mout-p-202.mailbox.org ([80.241.56.172]:48624) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pwUuM-0008OM-9N for 61901@debbugs.gnu.org; Tue, 09 May 2023 17:30:09 -0400 Original-Received: from smtp102.mailbox.org (smtp102.mailbox.org [10.196.197.102]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-202.mailbox.org (Postfix) with ESMTPS id 4QGBBt38s3z9sRC; Tue, 9 May 2023 23:29:58 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mailbox.org; s=mail20150812; t=1683667798; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to; bh=IMvHJAQWS3Yc4fcOpTQtCLtlZLeIe5WwBN8qHFTRuKk=; b=ll1+wtk+YqEEz2nugoMSSUvBYyy84YZjwJ0LTcndL2r/4pfiFjj+gB35trQjml+jfR9+GX Ph03ira+Er4vdwqetpQmO98csKzSBv0LwQ7NYPLVHog/Rqe2hjensYIbNEj0UeK5cfjWwd MDQGpW6oiEtDNKoWpzGaQeigbNkP0XKajar30w0NM/hWaXAd4+03SRo8/Ajy4mjkFd7cC1 Zc4z6ApVaCSg8CN0r02DfTevPXh5s/ejPsP59LcbcaD4/1qA+FyvQG9Pf0F2UyG34u6Gm+ /kWQCRy+YHt+8fK7pmwg/42asTFEZpUUog7IBHduhKSzjwWqM7++lbkrJCjS8w== In-Reply-To: 83jzxzvpsm.fsf@gnu.org X-MBO-RS-META: m6ufndr1yw1xk3ndcnm8y1zbkfij1sg6 X-MBO-RS-ID: 6b07dbd311492477f13 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list 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-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:261451 Archived-At: --=-=-= Content-Type: text/plain >> +@defvar safe-local-variable-directories >> +This is a list of directories where local variables are always enabled. >> +Directory-local variables loaded from these directories, such as the >> +variables in @file{.dir-locals.el}, will be enabled even if they are >> +risky. >> +@end defvar > >This variable should also be documented in the Emacs user manual, not >only in the ELisp Reference manual -- it's a user option, and a very >important one at that. Added to the manual in custom.texi "Safe File Variables" subsection. >> ++++ >> +** New variable 'safe-local-variable-directories'. >> +This variable is used to to permanently trust directories containing >> +risky directory-local variables. > >I would rephrase: > > This variable names directories in which Emacs will treat all > directory-local variables as safe. Fixed in attached patch. >> ALL-VARS is the list of all variables to be set up. >> @@ -3734,7 +3744,9 @@ n -- to ignore the local variables list.") >> ! -- to apply the local variables list, and permanently mark these >> values (*) as safe (in the future, they will be set automatically.) >> i -- to ignore the local variables list, and permanently mark these >> - values (*) as ignored\n\n") >> + values (*) as ignored >> ++ -- to apply the local variables list, and permanently trust all >> + directory-local variables in this directory\n\n") > >I would remove the "permanently" part, it would just confuse here. Fixed. >> @@ -3908,6 +3924,7 @@ DIR-NAME is the name of the associated directory. >> Otherwise it is nil." >> (null unsafe-vars) >> (null risky-vars)) >> (memq enable-local-variables '(:all :safe)) >> + (member dir-name safe-local-variable-directories) > >If you use 'member' for this test, then (a) the documentation of >safe-local-variable-directories should explicitly say that the >directories in the list must be in full absolute form, and (b) we >should consider the various issues with file names that are not >'equal' as strings, but still name the same directory, such as >letter-case differences on case-insensitive filesystems. And what >about equality of "foo/" "and "foo"? Clarified the documentation. The directory path requires a trailing separator, and is case-sensitive regardless of the filesystem (tested on vFAT). >Also, is 'dir-name' above guaranteed to be a fully-expanded absolute >file name? Yes. For TRAMP connections it's a string with text properties, but it's the same equality-wise. #("/ssh:user:/home/user/src/" 5 6 (tramp-default t)) >> +(ert-deftest files-tests-safe-local-variable-directories () >> + ;; safe-local-variable-directories should be risky, >> + ;; so use it as an arbitrary risky variable. >> + (let ((test-alist '((safe-local-variable-directories . "some_val"))) >> + (fakedir "test1/test2") >> + (enable-local-eval t)) >> + (with-temp-buffer >> + (setq safe-local-variable-directories (list fakedir)) > >The test should use absolute directory names for directories you put >into safe-local-variable-directories. Fixed. --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Add-safe-local-variable-directories-variable.patch >From 4ecb9ebc01c856f896ed2fee110b491209e23c4e Mon Sep 17 00:00:00 2001 From: Antero Mejr Date: Tue, 9 May 2023 20:51:14 +0000 Subject: [PATCH] Add safe-local-variable-directories variable. This variable can be set to automatically load risky dir-local variables from a list of trusted directories. * lisp/emacs-lisp/files.el (safe-local-variable-directories, hack-local-variables-filter, hack-local-variables-confirm): New variable and associated logic. * test/lisp/files-tests.el (files-tests-safe-local-variable-directories): Add tests for same. * doc/emacs/custom.texi (Safe File Variables): Add documentation for same. * doc/lispref/variables.texi (File Local Variables): Add documentation for same. * etc/NEWS (Lisp Changes in Emacs 30.1): Add news entry for same. (Bug#61901) --- doc/emacs/custom.texi | 22 ++++++++++++++++++++++ doc/lispref/variables.texi | 12 ++++++++++++ etc/NEWS | 5 +++++ lisp/files.el | 34 +++++++++++++++++++++++++++++----- test/lisp/files-tests.el | 21 +++++++++++++++++++++ 5 files changed, 89 insertions(+), 5 deletions(-) diff --git a/doc/emacs/custom.texi b/doc/emacs/custom.texi index 87290734cc9..2ddd39be31f 100644 --- a/doc/emacs/custom.texi +++ b/doc/emacs/custom.texi @@ -1328,6 +1328,13 @@ pairs in the file, by typing @kbd{i} at the confirmation prompt -- these pairs will thereafter be ignored in this file and in all other files. + For directory-local variable/value pairs +(@pxref{Directory Variables}), typing @kbd{+} at the confirmation +prompt will set all the variable/value pairs, and recognize all +variables in that directory as safe in the future. This option should +only be used for directories whose contents you trust. The @kbd{+} +confirmation prompt option is not available for file-local variables. + @vindex safe-local-variable-values @vindex ignored-local-variable-values @cindex risky variable @@ -1344,6 +1351,21 @@ record safe values for risky variables, do it directly by customizing Similarly, if you want to record values of risky variables that should be permanently ignored, customize @code{ignored-local-variable-values}. +@vindex safe-local-variable-directories + Sometimes it is helpful to always trust risky variables in a certain +directory, and skip the confirmation prompt when directory-local +variables are loaded there. When @kbd{+} is pressed at the risky +variable confirmation prompt, the directory in question is added to +the @samp{safe-local-variable-directories} variable, and risky +directory-local variables there will be loaded without prompting in +the future. If customizing @samp{safe-local-variable-directories} +manually, the directories in this list must be fully-expanded absolute +paths that end in a directory separator character. Directory paths +may be remote directory paths (@pxref{Remote Files}), if the +@code{enable-remote-dir-locals} variable is set to @code{t}. +Directory paths in this list are case-sensitive, even if the +filesystem is not. + @vindex enable-local-variables The variable @code{enable-local-variables} allows you to change the way Emacs processes local variables. Its default value is @code{t}, diff --git a/doc/lispref/variables.texi b/doc/lispref/variables.texi index eadb5c36de6..52a94db3703 100644 --- a/doc/lispref/variables.texi +++ b/doc/lispref/variables.texi @@ -1977,6 +1977,18 @@ this can be controlled by using this variable, which is a list of symbols. @end defvar +@defvar safe-local-variable-directories +This is a list of directories where local variables are always +enabled. Directory-local variables loaded from these directories, +such as the variables in @file{.dir-locals.el}, will be enabled even +if they are risky. The directories in this list must be +fully-expanded absolute paths that end in a directory separator +character. They may also be remote directory paths if the +@code{enable-remote-dir-locals} variable is set to @code{t}. +Directory paths in this list are case-sensitive, even if the +filesystem is not. +@end defvar + @defun hack-local-variables &optional handle-mode This function parses, and binds or evaluates as appropriate, any local variables specified by the contents of the current buffer. The variable diff --git a/etc/NEWS b/etc/NEWS index 3c71e52fff4..3bef9d2ed2a 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -380,6 +380,11 @@ hooks named after the feature name, like 'esh-mode-unload-hook'. * Lisp Changes in Emacs 30.1 ++++ +** New variable 'safe-local-variable-directories'. +This variable names directories in which Emacs will treat all +directory-local variables as safe. + ** New variable 'inhibit-auto-fill' to temporarily prevent auto-fill. ** Functions and variables to transpose sexps diff --git a/lisp/files.el b/lisp/files.el index c6f53e5eaf8..aa01e638c98 100644 --- a/lisp/files.el +++ b/lisp/files.el @@ -681,7 +681,8 @@ The command \\[normal-mode], when used interactively, always obeys file local variable specifications and the -*- line, and ignores this variable. -Also see the `permanently-enabled-local-variables' variable." +Also see the `permanently-enabled-local-variables' and +`safe-local-variable-directories' variables." :risky t :type '(choice (const :tag "Query Unsafe" t) (const :tag "Safe Only" :safe) @@ -3696,6 +3697,15 @@ variable to set.") "A list of file-local variables that are always enabled. This overrides any `enable-local-variables' setting.") +(defcustom safe-local-variable-directories '() + "A list of directories where local variables are always enabled. +Directory-local variables loaded from these directories, such as the +variables in .dir-locals.el, will be enabled even if they are risky." + :version "30.1" + :type '(repeat string) + :risky t + :group 'find-file) + (defun hack-local-variables-confirm (all-vars unsafe-vars risky-vars dir-name) "Get confirmation before setting up local variable values. ALL-VARS is the list of all variables to be set up. @@ -3734,7 +3744,11 @@ n -- to ignore the local variables list.") ! -- to apply the local variables list, and permanently mark these values (*) as safe (in the future, they will be set automatically.) i -- to ignore the local variables list, and permanently mark these - values (*) as ignored\n\n") + values (*) as ignored" + (if dir-name " ++ -- to apply the local variables list, and trust all directory-local + variables in this directory\n\n" + "\n\n")) (insert "\n\n")) (dolist (elt all-vars) (cond ((member elt unsafe-vars) @@ -3758,7 +3772,11 @@ i -- to ignore the local variables list, and permanently mark these (pop-to-buffer buf '(display-buffer--maybe-at-bottom)) (let* ((exit-chars '(?y ?n ?\s)) (prompt (format "Please type %s%s: " - (if offer-save "y, n, ! or i" "y or n") + (if offer-save + (if dir-name + "y, n, !, i, +" + "y, n, !, i") + "y or n") (if (< (line-number-at-pos (point-max)) (window-body-height)) "" @@ -3766,8 +3784,13 @@ i -- to ignore the local variables list, and permanently mark these char) (when offer-save (push ?i exit-chars) - (push ?! exit-chars)) + (push ?! exit-chars) + (when dir-name + (push ?+ exit-chars))) (setq char (read-char-choice prompt exit-chars)) + (when (and offer-save dir-name (= char ?+)) + (customize-push-and-save 'safe-local-variable-directories + (list dir-name))) (when (and offer-save (or (= char ?!) (= char ?i)) unsafe-vars) @@ -3776,7 +3799,7 @@ i -- to ignore the local variables list, and permanently mark these 'safe-local-variable-values 'ignored-local-variable-values) unsafe-vars)) - (prog1 (memq char '(?! ?\s ?y)) + (prog1 (memq char '(?! ?\s ?y ?+)) (quit-window t))))))) (defconst hack-local-variable-regexp @@ -3908,6 +3931,7 @@ DIR-NAME is the name of the associated directory. Otherwise it is nil." (null unsafe-vars) (null risky-vars)) (memq enable-local-variables '(:all :safe)) + (member dir-name safe-local-variable-directories) (hack-local-variables-confirm all-vars unsafe-vars risky-vars dir-name)) (dolist (elt all-vars) diff --git a/test/lisp/files-tests.el b/test/lisp/files-tests.el index aadb60e1de7..e87bb3cfa0a 100644 --- a/test/lisp/files-tests.el +++ b/test/lisp/files-tests.el @@ -166,6 +166,27 @@ form.") (hack-local-variables) (should (eq lexical-binding nil))))) +(ert-deftest files-tests-safe-local-variable-directories () + ;; safe-local-variable-directories should be risky, + ;; so use it as an arbitrary risky variable. + (let ((test-alist '((safe-local-variable-directories . "some_val"))) + (fakedir "/test1/test2/") + (enable-local-eval t)) + (with-temp-buffer + (setq safe-local-variable-directories (list fakedir)) + (hack-local-variables-filter test-alist fakedir) + (should (equal file-local-variables-alist test-alist))) + (with-temp-buffer + (setq safe-local-variable-directories (list fakedir)) + (setq noninteractive t) + (hack-local-variables-filter test-alist "wrong") + (should-not (equal file-local-variables-alist test-alist))) + (with-temp-buffer + (setq safe-local-variable-directories '()) + (setq noninteractive t) + (hack-local-variables-filter test-alist fakedir) + (should-not (equal file-local-variables-alist test-alist))))) + (defvar files-test-bug-18141-file (ert-resource-file "files-bug18141.el.gz") "Test file for bug#18141.") -- 2.39.2 --=-=-=--