Subject: bug#47616: 27.1; hardening mail-envelope-from

From: Francesco Potortì <[email-masked]>
Date: Wed, 07 Apr 2021 17:05:57 +0200

>(defun mail-envelope-from ()
>  "Return the envelope mail address to use when sending mail.
>This function uses `mail-envelope-from'."
>  (or (if (eq mail-envelope-from 'header)
>	  (nth 1 (mail-extract-address-components
>		  (save-restriction
>		    (save-excursion
>		      (goto-char (point-max))
>		      (re-search-backward
>		       (concat "^" (regexp-quote mail-header-separator) "\n")
>		       nil t)
>		      (narrow-to-region (point-min) (point))
>		      (mail-fetch-field "From")))))
>	mail-envelope-from)
>      user-mail-address))

This one is better (I had forgotten about mail-header-end)

(require 'sendmail)
(defun mail-envelope-from ()
  "Return the envelope mail address to use when sending mail.
This function uses `mail-envelope-from'."
  (or (if (eq mail-envelope-from 'header)
	  (let ((from-field (save-restriction
			      (narrow-to-region (point-min) (mail-header-end))
			      (mail-fetch-field "From"))))
	    (when from-field
	      (nth 1 (mail-extract-address-components from-field))))
	mail-envelope-from)
      user-mail-address))