From 7023ec8b3c793311d9561aa5c66e67ea4cd9834a Mon Sep 17 00:00:00 2001 From: emrekultursay Date: Mon, 21 Aug 2017 17:58:31 -0700 Subject: [PATCH] Sanitize the breakpoint location path, ignore whitespace and absolute path. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=166001966 --- src/googleclouddebugger/python_breakpoint.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/googleclouddebugger/python_breakpoint.py b/src/googleclouddebugger/python_breakpoint.py index 8e97e2e..fac23c3 100644 --- a/src/googleclouddebugger/python_breakpoint.py +++ b/src/googleclouddebugger/python_breakpoint.py @@ -87,10 +87,6 @@ datetime.strptime('2017-01-01', '%Y-%m-%d') -def _IsRootInitPy(path): - return path.lstrip(os.sep) == '__init__.py' - - def _StripCommonPathPrefix(paths): """Removes path common prefix from a list of path strings.""" # Find the longest common prefix in terms of characters. @@ -124,6 +120,11 @@ def _MultipleModulesFoundError(path, candidates): return fmt, params +def _SanitizePath(path): + """Removes leading/trailing whitespace, and leading path separator.""" + return path.strip().lstrip(os.sep) + + class PythonBreakpoint(object): """Handles a single Python breakpoint. @@ -166,9 +167,9 @@ def __init__(self, definition, hub_client, breakpoints_manager, if self.definition.get('action') == 'LOG': self._collector = capture_collector.LogCollector(self.definition) - # TODO(erezh): Ensure we handle whitespace in paths correctly. - # including, extension, basename, location_path - path = self.definition['location']['path'] + path = _SanitizePath(self.definition['location']['path']) + + # Only accept .py extension. if os.path.splitext(path)[1] != '.py': self._CompleteBreakpoint({ 'status': { @@ -177,7 +178,8 @@ def __init__(self, definition, hub_client, breakpoints_manager, 'description': {'format': ERROR_LOCATION_FILE_EXTENSION_0}}}) return - if _IsRootInitPy(path): + # A flat init file is too generic; path must include package name. + if path == '__init__.py': self._CompleteBreakpoint({ 'status': { 'isError': True, @@ -429,3 +431,4 @@ def _BreakpointEvent(self, event, frame): collector.Collect(frame) self._CompleteBreakpoint(collector.breakpoint, is_incremental=False) +