areguera / rpms / cockpit

Forked from rpms/cockpit 4 years ago
Clone
Blob Blame History Raw
From feccde6a9f0d0d1abbe2fdf32faf8c81aa8bb4c7 Mon Sep 17 00:00:00 2001
From: Stef Walter <stefw@redhat.com>
Date: Sun, 9 Sep 2018 11:05:05 +0200
Subject: [PATCH 2/2] src: Deny using %2F when serving file paths

The two invalid characters in Unix file names are zero and /.
Deny using this in an encoded form, since it bypasses how paths
are broken apart in HTTP URLs.

Closes #10028
---
 src/common/cockpitwebresponse.c |  2 +-
 src/common/test-webresponse.c   | 18 ++++++++++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/src/common/cockpitwebresponse.c b/src/common/cockpitwebresponse.c
index d156bc319..0e757f345 100644
--- a/src/common/cockpitwebresponse.c
+++ b/src/common/cockpitwebresponse.c
@@ -1299,7 +1299,7 @@ web_response_file (CockpitWebResponse *response,
   g_return_if_fail (escaped != NULL);
 
   /* Someone is trying to escape the root directory, or access hidden files? */
-  unescaped = g_uri_unescape_string (escaped, NULL);
+  unescaped = g_uri_unescape_string (escaped, "/");
   if (!unescaped || strstr (unescaped, "/.") || strstr (unescaped, "../") || strstr (unescaped, "//"))
     {
       g_debug ("%s: invalid path request", escaped);
diff --git a/src/common/test-webresponse.c b/src/common/test-webresponse.c
index 4ad72b967..ab59c13e5 100644
--- a/src/common/test-webresponse.c
+++ b/src/common/test-webresponse.c
@@ -309,6 +309,22 @@ test_file_encoding_denied (TestCase *tc,
   free (root);
 }
 
+static void
+test_file_slash_denied (TestCase *tc,
+                        gconstpointer user_data)
+{
+  gchar *root = realpath ( SRCDIR "/src", NULL);
+  const gchar *roots[] = { root, NULL };
+  const gchar *breakout = "/common%2fMakefile-common.am";
+  gchar *check = g_build_filename (roots[0], "common", "Makefile-common.am", NULL);
+  g_assert (root);
+  g_assert (g_file_test (check, G_FILE_TEST_EXISTS));
+  g_free (check);
+  cockpit_web_response_file (tc->response, breakout, roots);
+  cockpit_assert_strmatch (output_as_string (tc), "HTTP/1.1 404*");
+  free (root);
+}
+
 static void
 test_file_breakout_non_existant (TestCase *tc,
                                  gconstpointer user_data)
@@ -1440,6 +1456,8 @@ main (int argc,
               setup, test_file_breakout_denied, teardown);
   g_test_add ("/web-response/file/invalid-encoding-denied", TestCase, NULL,
               setup, test_file_encoding_denied, teardown);
+  g_test_add ("/web-response/file/file-slash-denied", TestCase, NULL,
+              setup, test_file_slash_denied, teardown);
   g_test_add ("/web-response/file/breakout-non-existant", TestCase, NULL,
               setup, test_file_breakout_non_existant, teardown);
   g_test_add ("/web-reponse/file/template", TestCase, &template_fixture,
-- 
2.17.1