Skip to content
This repository has been archived by the owner on Jan 23, 2024. It is now read-only.

Commit

Permalink
Sanitize the breakpoint location path, ignore whitespace and absolute…
Browse files Browse the repository at this point in the history
… path.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=166001966
  • Loading branch information
emrekultursay committed Aug 22, 2017
1 parent e992138 commit 7023ec8
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/googleclouddebugger/python_breakpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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': {
Expand All @@ -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,
Expand Down Expand Up @@ -429,3 +431,4 @@ def _BreakpointEvent(self, event, frame):
collector.Collect(frame)

self._CompleteBreakpoint(collector.breakpoint, is_incremental=False)

0 comments on commit 7023ec8

Please sign in to comment.