Subject: Re: bug#57856: 28.2; bookmark context strings in encrypted files

From: Michael Albinus <[email-masked]>
Date: Tue, 20 Sep 2022 16:49:58 +0200

Lars Ingebrigtsen <[email-masked]> writes:

Hi Lars,

> Michael Albinus <[email-masked]> writes:
>
>> But we will protect users who do in their init file
>>
>> (setq auth-sources '("~/.authinfo" "~/my-own-netrc-file-name"))
>
> Yes, that's a good point.

I gave it a try. WDYT?
(Documentation must be added, of course)

Best regards, Michael.

diff --git a/lisp/auth-source-pass.el b/lisp/auth-source-pass.el
index 86e0b48a79..4519705e14 100644
--- a/lisp/auth-source-pass.el
+++ b/lisp/auth-source-pass.el
@@ -319,6 +319,16 @@ auth-source-pass--name-port-user-suffixes
     (list
      (format "%s" name)))))

+(defun auth-source-pass-file-name-p (file)
+  "Say whether FILE is used by `auth-source-pass'."
+  (and (stringp file) (stringp auth-source-pass-filename)
+       (string-equal
+        (expand-file-name file) (expand-file-name auth-source-pass-filename))))
+
+(with-eval-after-load 'bookmark
+  (add-hook 'bookmark-inhibit-bookmark-functions
+	    #'auth-source-pass-file-name-p))
+
 (provide 'auth-source-pass)
 ;;; auth-source-pass.el ends here

diff --git a/lisp/auth-source.el b/lisp/auth-source.el
index c79e5b81f7..13ebd80106 100644
--- a/lisp/auth-source.el
+++ b/lisp/auth-source.el
@@ -522,6 +522,21 @@ auth-source-backend-parse-parameters

 ;; (mapcar #'auth-source-backend-parse auth-sources)

+(defun auth-source-file-name-p (file)
+  "Say whether FILE is used by `auth-sources'."
+  (let* ((backends (mapcar #'auth-source-backend-parse auth-sources))
+         (files
+          (mapcar (lambda (x)
+                    (when (member (slot-value x 'type) '(json netrc plstore))
+                      (slot-value x 'source)))
+                  backends)))
+    (member (expand-file-name file)
+            (mapcar #'expand-file-name (remq nil files)))))
+
+(with-eval-after-load 'bookmark
+  (add-hook 'bookmark-inhibit-bookmark-functions
+	    #'auth-source-file-name-p))
+
 (cl-defun auth-source-search (&rest spec
                               &key max require create delete
                               &allow-other-keys)
diff --git a/lisp/bookmark.el b/lisp/bookmark.el
index f150a24bbf..941cf7932c 100644
--- a/lisp/bookmark.el
+++ b/lisp/bookmark.el
@@ -592,6 +592,14 @@ bookmark-make-record-function
 in which case a default heuristic will be used.  The function can also
 equivalently just return ALIST without NAME.")

+(defcustom bookmark-inhibit-bookmark-functions nil
+  "List of functions to call before make a bookmark record.
+The functions take `buffer-file-name' as argument.  If any of
+these functions returns non-nil, the current buffer is not used
+for a bookmark."
+  :type 'hook
+  :version "29.1")
+
 (defun bookmark-make-record ()
   "Return a new bookmark record (NAME . ALIST) for the current location."
   (let* ((bookmark-search-size
@@ -599,10 +607,8 @@ bookmark-make-record
           ;; don't include any context in the bookmark file, because
           ;; that would leak (possibly secret) data.
           (if (and buffer-file-name
-                   (or (and (fboundp 'epa-file-name-p)
-                            (epa-file-name-p buffer-file-name))
-                       (and (fboundp 'tramp-crypt-file-name-p)
-                            (tramp-crypt-file-name-p buffer-file-name))))
+                   (not (run-hook-with-args-until-success
+                         'bookmark-inhibit-bookmark-functions buffer-file-name)))
               0
             bookmark-search-size))
          (record (funcall bookmark-make-record-function)))
diff --git a/lisp/epa-hook.el b/lisp/epa-hook.el
index 70c3030881..f4616b3b91 100644
--- a/lisp/epa-hook.el
+++ b/lisp/epa-hook.el
@@ -92,6 +92,10 @@ epa-file-name-p
   "Say whether FILE is handled by `epa-file'."
   (and auto-encryption-mode (string-match-p epa-file-name-regexp file)))

+(with-eval-after-load 'bookmark
+  (add-hook 'bookmark-inhibit-bookmark-functions
+	    #'epa-file-name-p))
+
 (define-minor-mode auto-encryption-mode
   "Toggle automatic file encryption/decryption (Auto Encryption mode)."
   :global t :init-value t :group 'epa-file :version "23.1"
diff --git a/lisp/net/tramp-crypt.el b/lisp/net/tramp-crypt.el
index d556c87606..60a1c37c9c 100644
--- a/lisp/net/tramp-crypt.el
+++ b/lisp/net/tramp-crypt.el
@@ -852,6 +852,14 @@ tramp-crypt-handle-unlock-file
     (tramp-compat-funcall
      'unlock-file (tramp-crypt-encrypt-file-name filename))))

+(with-eval-after-load 'bookmark
+  (add-hook 'bookmark-inhibit-bookmark-functions
+	    #'tramp-crypt-file-name-p)
+  (add-hook 'tramp-crypt-unload-hook
+	    (lambda ()
+	      (remove-hook 'bookmark-inhibit-bookmark-functions
+			   #'tramp-crypt-file-name-p))))
+
 (add-hook 'tramp-unload-hook
 	  (lambda ()
 	    (unload-feature 'tramp-crypt 'force)))