]> git.notmuchmail.org Git - notmuch/commitdiff
emacs: check drafts for encryption tags before saving
authorDavid Bremner <david@tethera.net>
Sun, 13 Nov 2016 14:08:49 +0000 (14:08 +0000)
committerDavid Bremner <david@tethera.net>
Sun, 13 Nov 2016 17:15:31 +0000 (13:15 -0400)
In general the user may not want to save plaintext copies of messages
that they are sending encrypted, so give them a chance to abort.

emacs/notmuch-draft.el
emacs/notmuch-mua.el
test/T630-emacs-draft.sh

index b8a5e67d5cd16bb8e3742805da3eb014ed17f5ba..f1da9c330b96f3d6d28941a980f011a179adebf9 100644 (file)
@@ -71,6 +71,21 @@ postponing and resuming a message."
   :type '(repeat string)
   :group 'notmuch-send)
 
+(defcustom notmuch-draft-save-plaintext 'ask
+  "Should notmuch save/postpone in plaintext messages that seem
+  like they are intended to be sent encrypted
+(i.e with an mml encryption tag in it)."
+  :type '(radio
+         (const :tag "Never" nil)
+         (const :tag "Ask every time" ask)
+         (const :tag "Always" t))
+  :group 'notmuch-draft
+  :group 'notmuch-crypto)
+
+(defvar notmuch-draft-encryption-tag-regex
+  "<#\\(part encrypt\\|secure.*mode=.*encrypt>\\)"
+  "Regular expression matching mml tags indicating encryption of part or message")
+
 (defvar notmuch-draft-id nil
   "Message-id of the most recent saved draft of this message")
 (make-variable-buffer-local 'notmuch-draft-id)
@@ -103,6 +118,27 @@ Used when a new version is saved, or the message is sent."
          (goto-char (+ (match-beginning 0) 2))
          (insert "!"))))))
 
+(defun notmuch-draft--has-encryption-tag ()
+  "Returns t if there is an mml secure tag."
+  (save-excursion
+    (message-goto-body)
+    (re-search-forward notmuch-draft-encryption-tag-regex nil 't)))
+
+(defun notmuch-draft--query-encryption ()
+  "Checks if we should save a message that should be encrypted.
+
+`notmuch-draft-save-plaintext' controls the behaviour."
+  (case notmuch-draft-save-plaintext
+       ((ask)
+        (unless (yes-or-no-p "(Customize `notmuch-draft-save-plaintext' to avoid this warning)
+This message contains mml tags that suggest it is intended to be encrypted.
+Really save and index an unencrypted copy? ")
+          (error "Save aborted")))
+       ((nil)
+        (error "Refusing to save draft with encryption tags (see `notmuch-draft-save-plaintext')"))
+       ((t)
+        (ignore))))
+
 (defun notmuch-draft--make-message-id ()
   ;; message-make-message-id gives the id inside a "<" ">" pair,
   ;; but notmuch doesn't want that form, so remove them.
@@ -115,6 +151,8 @@ This saves the current message in the database with tags
 `notmuch-draft-tags` (in addition to any default tags
 applied to newly inserted messages)."
   (interactive)
+  (when (notmuch-draft--has-encryption-tag)
+    (notmuch-draft--query-encryption))
   (let ((id (notmuch-draft--make-message-id)))
     (with-temporary-notmuch-message-buffer
      ;; We insert a Date header and a Message-ID header, the former
index b68cdf2625ffafc8956d4c415ce9bcf920a0359e..93747b1cb280c94ded422145b04a94094cf76dd9 100644 (file)
@@ -27,6 +27,7 @@
 
 (require 'notmuch-lib)
 (require 'notmuch-address)
+(require 'notmuch-draft)
 
 (eval-when-compile (require 'cl))
 
index e39690cadd16c4e971491c3100102a794dbce6bb..689ccfb83a721d2a1611d9ae1e5821a88b4f2521 100755 (executable)
@@ -39,4 +39,17 @@ header_count=$(notmuch show --format=raw subject:draft-test-0003 | grep -c ^X-No
 body_count=$(notmuch notmuch show --format=raw subject:draft-test-0003 | grep -c '^\<#secure')
 test_expect_equal "$header_count,$body_count" "1,0"
 
+test_begin_subtest "Refusing to save an encrypted draft"
+test_emacs '(notmuch-mua-mail)
+           (message-goto-subject)
+           (insert "draft-test-0004")
+           (mml-secure-message-sign-encrypt)
+           (let ((notmuch-draft-save-plaintext nil))
+                    (notmuch-draft-save))
+           (test-output)'
+count1=$(notmuch count tag:draft)
+count2=$(notmuch count subject:draft-test-0004)
+
+test_expect_equal "$count1,$count2" "3,0"
+
 test_done