From 1963ca181b1f0ee8754ada69ab31fb641d9f3a14 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=A9lestin=20Matte?= <dev@cmatte.me>
Date: Wed, 19 Jun 2024 12:53:11 +0200
Subject: [PATCH] inject.py: Use environment variables to pass values

Exim introduced variable tainting as a security measure starting from
exim 4.93. Starting from exim 4.96, tainting is mandatory for
commands. This means that it is no longer possible to pass variables
to pglister's inject.py.

This commit works around this by passing sender_address, recipient and
header_message-id through environment variables.

exim's transport must be modified to explicitly pass message_id:
environment = HEADER_MESSAGE_ID=$header_message-id:
---
 bin/inject.py | 36 +++++++++++++++++-------------------
 1 file changed, 17 insertions(+), 19 deletions(-)

diff --git a/bin/inject.py b/bin/inject.py
index b48b07d..4c2261b 100755
--- a/bin/inject.py
+++ b/bin/inject.py
@@ -13,7 +13,6 @@
 
 import os
 import sys
-import argparse
 
 sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../lib')))
 
@@ -22,13 +21,6 @@ from baselib.misc import log
 
 
 if __name__ == "__main__":
-    parser = argparse.ArgumentParser(description="Inject message")
-    parser.add_argument('-d', '--destination', dest='dest', help='Envelope recipient', required=True)
-    parser.add_argument('-s', '--sender', dest='sender', help='Envelope sender', required=True)
-    parser.add_argument('-m', '--msgid', dest='msgid', help='Message-id', required=True)
-
-    args = parser.parse_args()
-
     try:
         conn = config.conn('inject')
         curs = conn.cursor()
@@ -38,32 +30,38 @@ if __name__ == "__main__":
         # Show the full exception when run manually or in exim debug mode
         raise e
 
-    if args.msgid == '':
-        print("Message-id cannot be empty")
+    if 'RECIPIENT' not in os.environ:
+        print("RECIPIENT address must be passed as an environment variable")
+        sys.exit(1)
+    if 'SENDER' not in os.environ:
+        print("SENDER address must be passed as an environment variable")
+        sys.exit(1)
+    if 'HEADER_MESSAGE_ID' not in os.environ:
+        print("HEADER_MESSAGE_ID must be passed as an environment variable")
         sys.exit(1)
 
     try:
         # If there's a + in the address, it's a bounce (it's the only ones
         # we get that have a + - for VERP), so split it out into a separate
         # table already here (as the other one is going to be more transient).
-        if args.dest.find('+') >= 0:
+        if os.environ['RECIPIENT'].find('+') >= 0:
             curs.execute("INSERT INTO bounce_mail (recipient, sender, messageid, dt, contents) VALUES (%(recipient)s, %(sender)s, %(messageid)s, now(), %(contents)s)", {
-                'recipient': args.dest.lower(),
-                'sender': args.sender.lower(),
-                'messageid': args.msgid,
+                'recipient': os.environ['RECIPIENT'].lower(),
+                'sender': os.environ['SENDER'].lower(),
+                'messageid': os.environ['HEADER_MESSAGE_ID'],
                 'contents': sys.stdin.buffer.read(),
             })
             curs.execute("NOTIFY bounce")
-            log(curs, 0, 'inject', 'New bounce from {0} to {1}.'.format(args.sender, args.dest), args.msgid)
+            log(curs, 0, 'inject', 'New bounce from {0} to {1}.'.format(os.environ['SENDER'], os.environ['RECIPIENT']), os.environ['HEADER_MESSAGE_ID'])
         else:
             curs.execute("INSERT INTO incoming_mail (recipient, sender, messageid, dt, contents) VALUES (%(recipient)s, %(sender)s, %(msgid)s, now(), %(contents)s)", {
-                'recipient': args.dest.lower(),
-                'sender': args.sender.lower(),
-                'msgid': args.msgid,
+                'recipient': os.environ['RECIPIENT'].lower(),
+                'sender': os.environ['SENDER'].lower(),
+                'msgid': os.environ['HEADER_MESSAGE_ID'],
                 'contents': sys.stdin.buffer.read(),
             })
             curs.execute("NOTIFY incoming")
-            log(curs, 0, 'inject', 'New mail from {0} to {1}.'.format(args.sender, args.dest), args.msgid)
+            log(curs, 0, 'inject', 'New mail from {0} to {1}.'.format(os.environ['SENDER'], os.environ['RECIPIENT']), os.environ['HEADER_MESSAGE_ID'])
         conn.commit()
         conn.close()
     except Exception as e:
-- 
2.49.0

