unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: David Bremner <david@tethera.net>
To: David Bremner <david@tethera.net>, notmuch@notmuchmail.org
Subject: [PATCH] lib: convert relative filenames to absolute in n_d_index_file
Date: Sun, 19 Jul 2020 07:05:39 -0300	[thread overview]
Message-ID: <20200719100539.917215-1-david@tethera.net> (raw)
In-Reply-To: <20200718150751.4106125-11-david@tethera.net>

The API docs promise to handle relative filenames, but the code did
not do it.

Also check for files outside the mail root, as implied by the API
description.

This fixes the bug reported at

     id:87sgdqo0rz.fsf@tethera.net
---
 lib/message-file.c        | 24 ++++++++++++++++++++----
 test/T562-lib-database.sh | 18 +++++++++++++++++-
 2 files changed, 37 insertions(+), 5 deletions(-)

diff --git a/lib/message-file.c b/lib/message-file.c
index e1db26fb..311bd478 100644
--- a/lib/message-file.c
+++ b/lib/message-file.c
@@ -64,21 +64,37 @@ _notmuch_message_file_open_ctx (notmuch_database_t *notmuch,
     if (unlikely (message == NULL))
 	return NULL;
 
-    message->filename = talloc_strdup (message, filename);
+    const char *prefix = notmuch_database_get_path (notmuch);
+    if (prefix == NULL)
+	goto FAIL;
+
+    if (*filename == '/') {
+	if (strncmp (filename, prefix, strlen(prefix)) != 0) {
+	    _notmuch_database_log (notmuch, "Error opening %s: path outside mail root\n",
+				   filename);
+	    errno = 0;
+	    goto FAIL;
+	}
+	message->filename = talloc_strdup (message, filename);
+    } else {
+	message->filename = talloc_asprintf(message, "%s/%s", prefix, filename);
+    }
+
     if (message->filename == NULL)
 	goto FAIL;
 
     talloc_set_destructor (message, _notmuch_message_file_destructor);
 
-    message->stream = g_mime_stream_gzfile_open (filename);
+    message->stream = g_mime_stream_gzfile_open (message->filename);
     if (message->stream == NULL)
 	goto FAIL;
 
     return message;
 
   FAIL:
-    _notmuch_database_log (notmuch, "Error opening %s: %s\n",
-			   filename, strerror (errno));
+    if (errno)
+	_notmuch_database_log (notmuch, "Error opening %s: %s\n",
+			       filename, strerror (errno));
     _notmuch_message_file_close (message);
 
     return NULL;
diff --git a/test/T562-lib-database.sh b/test/T562-lib-database.sh
index 4e225ccc..301eeb73 100755
--- a/test/T562-lib-database.sh
+++ b/test/T562-lib-database.sh
@@ -253,7 +253,6 @@ test_expect_equal_file EXPECTED OUTPUT
 
 generate_message '[filename]=relative_path'
 test_begin_subtest "index file (relative path)"
-test_subtest_known_broken
 cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
     {
         notmuch_message_t *msg;
@@ -268,4 +267,21 @@ cat <<EOF > EXPECTED
 EOF
 test_expect_equal_file EXPECTED OUTPUT
 
+test_begin_subtest "index file (absolute path outside mail root)"
+cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
+    {
+        notmuch_message_t *msg;
+        stat = notmuch_database_index_file (db, "/dev/zero", NULL, &msg);
+        printf ("%d\n", stat == NOTMUCH_STATUS_FILE_ERROR);
+    }
+EOF
+cat <<EOF > EXPECTED
+== stdout ==
+1
+== stderr ==
+Error opening /dev/zero: path outside mail root
+EOF
+test_expect_equal_file EXPECTED OUTPUT
+
+
 test_done
-- 
2.27.0

  reply	other threads:[~2020-07-19 10:06 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-18 15:07 batch 6, API exception handling cleanup David Bremner
2020-07-18 15:07 ` [PATCH 01/10] test: regression test for closing a closed database David Bremner
2020-07-18 15:07 ` [PATCH 02/10] test: add regression tests for notmuch database destroy David Bremner
2020-07-18 15:07 ` [PATCH 03/10] test: add known broken test for n_d_needs_upgrade David Bremner
2020-07-18 15:07 ` [PATCH 04/10] lib/n_d_needs_upgrade: handle error return from n_d_get_version David Bremner
2020-07-18 15:07 ` [PATCH 05/10] test: regression test for n_d_upgrade David Bremner
2020-07-18 15:07 ` [PATCH 06/10] test: add regression test for n_d_{begin,end}_atomic David Bremner
2020-07-18 15:07 ` [PATCH 07/10] test: regression test for n_d_get_revision David Bremner
2020-07-18 15:07 ` [PATCH 08/10] test: add regression test for n_d_get_directory David Bremner
2020-07-18 15:07 ` [PATCH 09/10] test: regression test for n_d_index_file closed db David Bremner
2020-07-18 15:07 ` [PATCH 10/10] test: add known broken test for indexing relative path David Bremner
2020-07-19 10:05   ` David Bremner [this message]
2020-07-25 11:17   ` David Bremner
2020-07-23 10:51 ` batch 6, API exception handling cleanup David Bremner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200719100539.917215-1-david@tethera.net \
    --to=david@tethera.net \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).