742a38
From 0a71b956f2a778860cc35c83b051272f3f80cefc Mon Sep 17 00:00:00 2001
742a38
From: Matej Marusak <mmarusak@redhat.com>
742a38
Date: Thu, 5 Apr 2018 11:06:43 +0200
742a38
Subject: [PATCH] Anonymize paths in frames
742a38
742a38
Fixes abrt/libreport#523
742a38
742a38
Signed-off-by: Matej Marusak <mmarusak@redhat.com>
742a38
---
742a38
 include/utils.h                    |  3 +++
742a38
 lib/java_frame.c                   |  6 ++++++
742a38
 lib/js_frame.c                     |  1 +
742a38
 lib/normalize.c                    |  9 +++++++++
742a38
 lib/python_frame.c                 |  2 ++
742a38
 lib/ruby_frame.c                   |  2 ++
742a38
 lib/utils.c                        | 23 +++++++++++++++++++++++
742a38
 tests/java_frame.at                | 12 ++++++++++++
742a38
 tests/js_frame.at                  |  7 +++++++
742a38
 tests/python/python.py             |  8 ++++----
742a38
 tests/python_stacktraces/python-01 |  2 +-
742a38
 tests/ruby_frame.at                |  2 +-
742a38
 12 files changed, 71 insertions(+), 6 deletions(-)
742a38
742a38
diff --git a/include/utils.h b/include/utils.h
742a38
index 1c7984b..b36bc2c 100644
742a38
--- a/include/utils.h
742a38
+++ b/include/utils.h
742a38
@@ -406,6 +406,9 @@ sr_parse_os_release(const char *input,
742a38
                     void (*callback)(char*, char*, void*),
742a38
                     void *data);
742a38
 
742a38
+char*
742a38
+anonymize_path(char *file_name);
742a38
+
742a38
 /**
742a38
  * Demangles C++ symbol.
742a38
  * @returns
742a38
diff --git a/lib/java_frame.c b/lib/java_frame.c
742a38
index 8724b64..ea407bf 100644
742a38
--- a/lib/java_frame.c
742a38
+++ b/lib/java_frame.c
742a38
@@ -468,7 +468,10 @@ const char *sr_java_frame_parse_frame_url(struct sr_java_frame *frame, const cha
742a38
         sr_location_add(location, 0, sr_skip_char_cspan(&cursor, path_stop));
742a38
 
742a38
         if (mark != cursor)
742a38
+        {
742a38
             frame->class_path = sr_strndup(mark, cursor - mark);
742a38
+            frame->class_path = anonymize_path(frame->class_path);
742a38
+        }
742a38
     }
742a38
 
742a38
     if (*cursor != ']' && *cursor != '\n')
742a38
@@ -522,8 +525,11 @@ sr_java_frame_parse(const char **input,
742a38
             if (sr_java_frame_parse_is_native_method(mark))
742a38
                 frame->is_native = true;
742a38
             else if (!sr_java_frame_parse_is_unknown_source(mark))
742a38
+            {
742a38
                 /* DO NOT set file_name if input says that source isn't known */
742a38
                 frame->file_name = sr_strndup(mark, cursor - mark);
742a38
+                frame->file_name = anonymize_path(frame->file_name);
742a38
+            }
742a38
         }
742a38
 
742a38
         if (*cursor == ':')
742a38
diff --git a/lib/js_frame.c b/lib/js_frame.c
742a38
index cb29bd6..e9b6514 100644
742a38
--- a/lib/js_frame.c
742a38
+++ b/lib/js_frame.c
742a38
@@ -344,6 +344,7 @@ sr_js_frame_parse_v8(const char **input,
742a38
      * ^^^^^^^^^^^^^^^^^
742a38
      */
742a38
     frame->file_name = sr_strndup(local_input, token - local_input);
742a38
+    frame->file_name = anonymize_path(frame->file_name);
742a38
 
742a38
     location->column += sr_skip_char_cspan(&local_input, "\n");
742a38
 
742a38
diff --git a/lib/normalize.c b/lib/normalize.c
742a38
index d23e8f5..3973b3b 100644
742a38
--- a/lib/normalize.c
742a38
+++ b/lib/normalize.c
742a38
@@ -630,6 +630,15 @@ sr_normalize_core_thread(struct sr_core_thread *thread)
742a38
         frame = next_frame;
742a38
     }
742a38
 
742a38
+    /* Anonymize file_name if contains /home/<user>/...
742a38
+     */
742a38
+    frame = thread->frames;
742a38
+    while (frame)
742a38
+    {
742a38
+        frame->file_name = anonymize_path(frame->file_name);
742a38
+        frame = frame->next;
742a38
+    }
742a38
+
742a38
     /* If the first frame has address 0x0000 and its name is '??', it
742a38
      * is a dereferenced null, and we remove it. This frame is not
742a38
      * really invalid, but it affects stacktrace quality rating. See
742a38
diff --git a/lib/python_frame.c b/lib/python_frame.c
742a38
index 9287f3d..8453016 100644
742a38
--- a/lib/python_frame.c
742a38
+++ b/lib/python_frame.c
742a38
@@ -237,6 +237,8 @@ sr_python_frame_parse(const char **input,
742a38
         frame->file_name = inside;
742a38
     }
742a38
 
742a38
+    frame->file_name = anonymize_path(frame->file_name);
742a38
+
742a38
     location->column += strlen(frame->file_name);
742a38
 
742a38
     if (0 == sr_skip_string(&local_input, "\", line "))
742a38
diff --git a/lib/ruby_frame.c b/lib/ruby_frame.c
742a38
index 4926c63..76f17fe 100644
742a38
--- a/lib/ruby_frame.c
742a38
+++ b/lib/ruby_frame.c
742a38
@@ -258,6 +258,8 @@ sr_ruby_frame_parse(const char **input,
742a38
     /* Everything before the colon is the file name. */
742a38
     *p = '\0';
742a38
     frame->file_name = filename_lineno_in;
742a38
+    frame->file_name = anonymize_path(frame->file_name);
742a38
+
742a38
     filename_lineno_in = NULL;
742a38
 
742a38
     if(!sr_skip_char(&local_input, '`'))
742a38
diff --git a/lib/utils.c b/lib/utils.c
742a38
index 5bbbd19..415929c 100644
742a38
--- a/lib/utils.c
742a38
+++ b/lib/utils.c
742a38
@@ -31,6 +31,9 @@
742a38
 #include <fcntl.h>
742a38
 #include <ctype.h>
742a38
 
742a38
+#define ANONYMIZED_PATH "/home/anonymized"
742a38
+
742a38
+
742a38
 /* The prototype is in C++ header cxxabi.h, let's just copypaste it here
742a38
  * instead of fiddling with include directories */
742a38
 char* __cxa_demangle(const char* mangled_name, char* output_buffer,
742a38
@@ -849,3 +852,23 @@ sr_demangle_symbol(const char *sym)
742a38
 
742a38
     return demangled;
742a38
 }
742a38
+
742a38
+char*
742a38
+anonymize_path(char *orig_path)
742a38
+{
742a38
+    if (!orig_path)
742a38
+        return orig_path;
742a38
+    char* new_path = orig_path;
742a38
+    if (strncmp(orig_path, "/home/", strlen("/home/")) == 0)
742a38
+    {
742a38
+        new_path = strchr(new_path + strlen("/home/"), '/');
742a38
+        if (new_path)
742a38
+        {
742a38
+            // Join /home/anonymized/ and ^
742a38
+            new_path = sr_asprintf("%s%s", ANONYMIZED_PATH, new_path);
742a38
+            free(orig_path);
742a38
+            return new_path;
742a38
+        }
742a38
+    }
742a38
+    return orig_path;
742a38
+}
742a38
diff --git a/tests/java_frame.at b/tests/java_frame.at
742a38
index 00738be..2c6a7de 100644
742a38
--- a/tests/java_frame.at
742a38
+++ b/tests/java_frame.at
742a38
@@ -459,6 +459,18 @@ main(void)
742a38
   location.column = 0;
742a38
   check(c, &frame, c + strlen(c), &location);
742a38
 
742a38
+  /** next frame **/
742a38
+  sr_java_frame_init(&frame);
742a38
+  frame.name = sr_strdup("com.redhat.abrt.duke.nuke");
742a38
+  frame.file_name = sr_strdup("duke.java");
742a38
+  frame.class_path = sr_strdup("/home/anonymized/lib/java/foo.class");
742a38
+
742a38
+  c = "    at com.redhat.abrt.duke.nuke(duke.java:-1) [file:/home/user/lib/java/foo.class]\n";
742a38
+  sr_location_init(&location);
742a38
+  location.line = 2;
742a38
+  location.column = 0;
742a38
+  check(c, &frame, c + strlen(c), &location);
742a38
+
742a38
   /** next frame **/
742a38
   sr_java_frame_init(&frame);
742a38
   frame.name = sr_strdup("com.redhat.abrt.duke.nuke");
742a38
diff --git a/tests/js_frame.at b/tests/js_frame.at
742a38
index d17dd69..a3cc5d5 100644
742a38
--- a/tests/js_frame.at
742a38
+++ b/tests/js_frame.at
742a38
@@ -102,6 +102,13 @@ main(void)
742a38
                 33,
742a38
                 63);
742a38
 
742a38
+    check_valid("at    ContextifyScript.Script.runInThisContext    (/home/user/vm.js:25:33)",
742a38
+                "ContextifyScript.Script.runInThisContext",
742a38
+                "/home/anonymized/vm.js",
742a38
+                25,
742a38
+                33,
742a38
+                74);
742a38
+
742a38
     check_valid("at    ContextifyScript.Script.runInThisContext    (vm.js:25:33)  ",
742a38
                 "ContextifyScript.Script.runInThisContext",
742a38
                 "vm.js",
742a38
diff --git a/tests/python/python.py b/tests/python/python.py
742a38
index 9044200..77a9e59 100755
742a38
--- a/tests/python/python.py
742a38
+++ b/tests/python/python.py
742a38
@@ -6,7 +6,7 @@ from test_helpers import *
742a38
 path = '../python_stacktraces/python-01'
742a38
 contents = load_input_contents(path)
742a38
 frames_expected = 11
742a38
-expected_short_text = '''#1 _getPackage in /usr/share/PackageKit/helpers/yum/yumBackend.py:2534
742a38
+expected_short_text = '''#1 _getPackage in /home/anonymized/PackageKit/helpers/yum/yumBackend.py:2534
742a38
 #2 updateProgress in /usr/share/PackageKit/helpers/yum/yumBackend.py:2593
742a38
 #3 _do_start in /usr/share/PackageKit/helpers/yum/yumBackend.py:2551
742a38
 #4 start in /usr/lib/python2.6/site-packages/urlgrabber/progress.py:129
742a38
@@ -76,16 +76,16 @@ class TestPythonStacktrace(BindingsTestCase):
742a38
         self.assertEqual(self.trace.to_short_text(6), expected_short_text)
742a38
 
742a38
     def test_bthash(self):
742a38
-        self.assertEqual(self.trace.get_bthash(), 'fa0a7ff4b65f18661a6ce102eb787ff0d77ff12f')
742a38
+        self.assertEqual(self.trace.get_bthash(), 'eabeeae89433bb3b3d9eb8190659dcf057ab3cd1')
742a38
 
742a38
     def test_duphash(self):
742a38
         expected_plain = '''Thread
742a38
-/usr/share/PackageKit/helpers/yum/yumBackend.py:2534
742a38
+/home/anonymized/PackageKit/helpers/yum/yumBackend.py:2534
742a38
 /usr/share/PackageKit/helpers/yum/yumBackend.py:2593
742a38
 /usr/share/PackageKit/helpers/yum/yumBackend.py:2551
742a38
 '''
742a38
         self.assertEqual(self.trace.get_duphash(flags=satyr.DUPHASH_NOHASH, frames=3), expected_plain)
742a38
-        self.assertEqual(self.trace.get_duphash(), '2c8e509a33966a08df1dd8b2348e850d1bc5b776')
742a38
+        self.assertEqual(self.trace.get_duphash(), '8c8273cddf94e10fc0349284afcff8970056d9e5')
742a38
 
742a38
     def test_crash_thread(self):
742a38
         self.assertTrue(self.trace.crash_thread is self.trace)
742a38
diff --git a/tests/python_stacktraces/python-01 b/tests/python_stacktraces/python-01
742a38
index 58abbfc..ae5e72c 100644
742a38
--- a/tests/python_stacktraces/python-01
742a38
+++ b/tests/python_stacktraces/python-01
742a38
@@ -19,6 +19,6 @@ Traceback (most recent call last):
742a38
     self.updateProgress(name, 0.0, "", "")
742a38
   File "/usr/share/PackageKit/helpers/yum/yumBackend.py", line 2593, in updateProgress
742a38
     pkg = self._getPackage(name)
742a38
-  File "/usr/share/PackageKit/helpers/yum/yumBackend.py", line 2534, in _getPackage
742a38
+  File "/home/user/PackageKit/helpers/yum/yumBackend.py", line 2534, in _getPackage
742a38
     sections = name.rsplit('-', 2)
742a38
 AttributeError: 'NoneType' object has no attribute 'rsplit'
742a38
diff --git a/tests/ruby_frame.at b/tests/ruby_frame.at
742a38
index 1b1b9d0..cef5603 100644
742a38
--- a/tests/ruby_frame.at
742a38
+++ b/tests/ruby_frame.at
742a38
@@ -38,7 +38,7 @@ main(void)
742a38
         "/usr/share/ruby/vendor_ruby/will_crash.rb", 13, "func", false, 2, 1);
742a38
 
742a38
   check("/home/u/work/will:crash/will_crash.rb:30:in `block in <class:WillClass>'",
742a38
-        "/home/u/work/will:crash/will_crash.rb", 30, "class:WillClass", true, 1, 0);
742a38
+        "/home/anonymized/work/will:crash/will_crash.rb", 30, "class:WillClass", true, 1, 0);
742a38
 
742a38
   check("./will_ruby_raise:8:in `<main>'",
742a38
         "./will_ruby_raise", 8, "main", true, 0, 0);
742a38
-- 
742a38
2.17.1
742a38