[evolution-ews] Misc: Hide some headers' value in EWS_DEBUG=2 output



commit 5448ab780b6faa4d21b12356c2b4b84413a8180a
Author: Milan Crha <mcrha redhat com>
Date:   Tue Jan 4 14:02:33 2022 +0100

    Misc: Hide some headers' value in EWS_DEBUG=2 output
    
    This hides values of sensitive information from the most used log
    option. There can be used EWS_DEBUG=3 to show those headers' value.

 src/EWS/common/e-ews-connection.c   | 25 +++++++------------------
 src/EWS/common/e-ews-debug.c        | 31 +++++++++++++++++++++++++++++++
 src/EWS/common/e-ews-debug.h        |  7 +++++++
 src/EWS/common/e-ews-notification.c | 11 +++++++----
 src/EWS/common/e-soap-message.c     |  2 +-
 tests/ews-test-common.c             |  2 +-
 6 files changed, 54 insertions(+), 24 deletions(-)
---
diff --git a/src/EWS/common/e-ews-connection.c b/src/EWS/common/e-ews-connection.c
index 8bf646cd..916aab15 100644
--- a/src/EWS/common/e-ews-connection.c
+++ b/src/EWS/common/e-ews-connection.c
@@ -1006,7 +1006,7 @@ ews_response_cb (SoupSession *session,
         * Logging framework also */
 
        log_level = e_ews_debug_get_log_level ();
-       if (log_level >= 1 && log_level < 3) {
+       if (log_level == 1) {
                /* This will dump only the headers, since we stole the body.
                 * And only if EWS_DEBUG=1, since higher levels will have dumped
                 * it directly from libsoup anyway. */
@@ -1818,7 +1818,7 @@ e_ews_debug_handler (const gchar *log_domain,
                     const gchar *message,
                     gpointer user_data)
 {
-       if (e_ews_debug_get_log_level () >= 3)
+       if (e_ews_debug_get_log_level () >= 4)
                g_log_default_handler (log_domain, log_level, message, NULL);
 }
 
@@ -1835,20 +1835,7 @@ e_ews_soup_log_printer (SoupLogger *logger,
                        const gchar *data,
                        gpointer user_data)
 {
-       const gchar *filtered_data = NULL;
-
-       if (e_ews_debug_get_log_level () >= 3) {
-               if (direction == '>' && g_ascii_strncasecmp (data, "Host:", 5) == 0)
-                       filtered_data = "Host: <redacted>";
-               else if (direction == '>' && g_ascii_strncasecmp (data, "Authorization:", 14) == 0)
-                       filtered_data = "Authorization: <redacted>";
-               else if (direction == '<' && g_ascii_strncasecmp (data, "Set-Cookie:", 11) == 0)
-                       filtered_data = "Set-Cookie: <redacted>";
-               else
-                       filtered_data = data;
-       }
-
-       g_debug ("%c %s", direction, filtered_data ? filtered_data : data);
+       g_debug ("%c %s", direction, e_ews_debug_redact_headers (direction, data));
 }
 
 static void
@@ -1998,13 +1985,15 @@ ews_connection_constructed (GObject *object)
                SoupLogger *logger;
                logger = soup_logger_new (SOUP_LOGGER_LOG_BODY, -1);
 
-               if (log_level >= 3) {
+               if (log_level >= 4) {
                        soup_logger_set_printer (logger, e_ews_soup_log_printer, NULL, NULL);
                        g_log_set_handler (
                                G_LOG_DOMAIN,
                                G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING |
                                G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO,
                                e_ews_debug_handler, cnc);
+               } else if (log_level == 2) {
+                       soup_logger_set_printer (logger, e_ews_debug_soup_log_printer_stdout, NULL, NULL);
                }
 
                soup_session_add_feature (
@@ -4556,7 +4545,7 @@ e_ews_connection_download_oal_file (EEwsConnection *cnc,
         * Don't use streaming-based messages when we are loggin the traffic
         * to generate trace files for tests
         */
-       if (e_ews_debug_get_log_level () <= 2)
+       if (e_ews_debug_get_log_level () <= 3)
                soup_message_body_set_accumulate (soup_message->response_body, FALSE);
 
        g_signal_connect (
diff --git a/src/EWS/common/e-ews-debug.c b/src/EWS/common/e-ews-debug.c
index 5a908f44..54a64c1a 100644
--- a/src/EWS/common/e-ews-debug.c
+++ b/src/EWS/common/e-ews-debug.c
@@ -137,3 +137,34 @@ e_ews_debug_dump_raw_soup_response (SoupMessage *msg)
                                                   msg->response_body);
        }
 }
+
+const gchar *
+e_ews_debug_redact_headers (gchar direction,
+                           const gchar *data)
+{
+       const gchar *filtered_data = NULL;
+       gint log_level = e_ews_debug_get_log_level ();
+
+       if (log_level == 2 || log_level > 3) {
+               if (direction == '>' && g_ascii_strncasecmp (data, "Host:", 5) == 0)
+                       filtered_data = "Host: <redacted>";
+               else if (direction == '>' && g_ascii_strncasecmp (data, "Authorization:", 14) == 0)
+                       filtered_data = "Authorization: <redacted>";
+               else if (direction == '<' && g_ascii_strncasecmp (data, "Set-Cookie:", 11) == 0)
+                       filtered_data = "Set-Cookie: <redacted>";
+               else if (direction == '>' && g_ascii_strncasecmp (data, "Cookie:", 7) == 0)
+                       filtered_data = "Cookie: <redacted>";
+       }
+
+       return filtered_data ? filtered_data : data;
+}
+
+void
+e_ews_debug_soup_log_printer_stdout (SoupLogger *logger,
+                                    SoupLoggerLogLevel level,
+                                    char direction,
+                                    const gchar *data,
+                                    gpointer user_data)
+{
+       g_print ("%c %s\n", direction, e_ews_debug_redact_headers (direction, data));
+}
diff --git a/src/EWS/common/e-ews-debug.h b/src/EWS/common/e-ews-debug.h
index 46daf228..d2b50862 100644
--- a/src/EWS/common/e-ews-debug.h
+++ b/src/EWS/common/e-ews-debug.h
@@ -22,6 +22,13 @@ void         e_ews_debug_dump_raw_soup_request
                                                        (SoupMessage *msg);
 void           e_ews_debug_dump_raw_soup_response
                                                        (SoupMessage *msg);
+const gchar *  e_ews_debug_redact_headers              (gchar direction,
+                                                        const gchar *data);
+void           e_ews_debug_soup_log_printer_stdout     (SoupLogger *logger,
+                                                        SoupLoggerLogLevel level,
+                                                        char direction,
+                                                        const gchar *data,
+                                                        gpointer user_data);
 G_END_DECLS
 
 #endif /* E_EWS_DEBUG_H */
diff --git a/src/EWS/common/e-ews-notification.c b/src/EWS/common/e-ews-notification.c
index af72893f..28325298 100644
--- a/src/EWS/common/e-ews-notification.c
+++ b/src/EWS/common/e-ews-notification.c
@@ -258,6 +258,9 @@ e_ews_notification_init (EEwsNotification *notification)
                SoupLogger *logger;
                logger = soup_logger_new (SOUP_LOGGER_LOG_BODY, -1);
 
+               if (log_level == 2)
+                       soup_logger_set_printer (logger, e_ews_debug_soup_log_printer_stdout, NULL, NULL);
+
                soup_session_add_feature (notification->priv->soup_session, SOUP_SESSION_FEATURE (logger));
                g_object_unref (logger);
        }
@@ -356,7 +359,7 @@ e_ews_notification_subscribe_folder_sync (EEwsNotification *notification,
                return FALSE;
        }
 
-       if (log_level >= 1 && log_level < 3) {
+       if (log_level >= 1 && log_level != 2 && log_level < 4) {
                e_ews_debug_dump_raw_soup_request (SOUP_MESSAGE (msg));
        }
 
@@ -380,7 +383,7 @@ e_ews_notification_subscribe_folder_sync (EEwsNotification *notification,
 
        response = e_soap_response_new_from_xmldoc (doc);
 
-       if (log_level >= 1 && log_level < 3) {
+       if (log_level >= 1 && log_level != 2 && log_level < 4) {
                e_ews_debug_dump_raw_soup_response (SOUP_MESSAGE (msg));
        }
        g_object_unref (msg);
@@ -740,7 +743,7 @@ ews_notification_soup_got_chunk (SoupMessage *msg,
                if (response == NULL)
                        break;
 
-               if (log_level >= 1 && log_level < 3) {
+               if (log_level >= 1 && log_level != 2 && log_level < 4) {
                        e_ews_debug_dump_raw_soup_response (msg);
                        e_soap_response_dump_response (response, stdout);
                }
@@ -871,7 +874,7 @@ e_ews_notification_get_events_sync (EEwsNotification *notification,
 
        e_ews_message_write_footer (msg); /* Complete the footer and print the request */
 
-       if (e_ews_debug_get_log_level () <= 2)
+       if (e_ews_debug_get_log_level () <= 3)
                soup_message_body_set_accumulate (SOUP_MESSAGE (msg)->response_body, FALSE);
 
        handler_id = g_signal_connect (
diff --git a/src/EWS/common/e-soap-message.c b/src/EWS/common/e-soap-message.c
index 434c19af..b845c125 100644
--- a/src/EWS/common/e-soap-message.c
+++ b/src/EWS/common/e-soap-message.c
@@ -330,7 +330,7 @@ e_soap_message_new (const gchar *method,
         * Don't use streaming-based messages when we are loggin the traffic
         * to generate trace files for tests
         */
-       if (e_ews_debug_get_log_level () <= 2) {
+       if (e_ews_debug_get_log_level () <= 3) {
                /* Don't accumulate body data into a huge buffer.
                 * Instead, parse it as it arrives. */
                soup_message_body_set_accumulate (
diff --git a/tests/ews-test-common.c b/tests/ews-test-common.c
index f4e8392f..cd3adc29 100644
--- a/tests/ews-test-common.c
+++ b/tests/ews-test-common.c
@@ -211,7 +211,7 @@ ews_test_init (gint argc,
        g_test_init (&argc, &argv, NULL);
        g_test_bug_base ("http://bugzilla.gnome.org/show_bug.cgi?id=";);
 
-       g_setenv ("EWS_DEBUG", "3", TRUE);
+       g_setenv ("EWS_DEBUG", "4", TRUE);
        g_setenv ("EWS_INTERNAL_TEST", "1", TRUE);
 
        mock_server = uhm_server_new ();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]