Skip to content

Commit

Permalink
Changes to not catch exception related to coding errors
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimmetz committed Feb 14, 2021
1 parent 7b2a4e8 commit 2425e32
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 3 deletions.
5 changes: 5 additions & 0 deletions plaso/engine/single_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def _CacheFileSystem(self, path_spec):
self._file_system_cache.remove(file_system)
self._file_system_cache.append(file_system)

# pylint: disable=missing-raises-doc
def _ProcessPathSpec(self, extraction_worker, parser_mediator, path_spec):
"""Processes a path specification.
Expand All @@ -82,10 +83,14 @@ def _ProcessPathSpec(self, extraction_worker, parser_mediator, path_spec):
excluded_find_specs = (
self.collection_filters_helper.excluded_file_system_find_specs)

# pylint: disable=try-except-raise
try:
extraction_worker.ProcessPathSpec(
parser_mediator, path_spec, excluded_find_specs=excluded_find_specs)

except definitions.EXCEPTIONS_EXCLUDED_FROM_CATCH_ALL:
raise

except KeyboardInterrupt:
self._abort = True

Expand Down
4 changes: 4 additions & 0 deletions plaso/lib/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"""The definitions."""


# Exceptions (or errors) that should not be caught in a catch all try-catch.
EXCEPTIONS_EXCLUDED_FROM_CATCH_ALL = (
AttributeError, ImportError, NameError, TypeError, UnboundLocalError)

MICROSECONDS_PER_SECOND = 1000000
MICROSECONDS_PER_MINUTE = 60000000
NANOSECONDS_PER_SECOND = 1000000000
Expand Down
4 changes: 4 additions & 0 deletions plaso/multi_processing/analysis_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ def _Main(self):

storage_writer.WriteTaskStart()

# pylint: disable=try-except-raise
try:
logger.debug(
'{0!s} (PID: {1:d}) started monitoring event queue.'.format(
Expand Down Expand Up @@ -179,6 +180,9 @@ def _Main(self):

self._analysis_mediator.ProduceAnalysisReport(self._analysis_plugin)

except definitions.EXCEPTIONS_EXCLUDED_FROM_CATCH_ALL:
raise

# All exceptions need to be caught here to prevent the process
# from being killed by an uncaught exception.
except Exception as exception: # pylint: disable=broad-except
Expand Down
9 changes: 9 additions & 0 deletions plaso/multi_processing/worker_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ def _Main(self):

self._status = definitions.STATUS_INDICATOR_RUNNING

# pylint: disable=try-except-raise
try:
logger.debug('{0!s} (PID: {1:d}) started monitoring task queue.'.format(
self._name, self._pid))
Expand All @@ -217,6 +218,9 @@ def _Main(self):
logger.debug('{0!s} (PID: {1:d}) stopped monitoring task queue.'.format(
self._name, self._pid))

except definitions.EXCEPTIONS_EXCLUDED_FROM_CATCH_ALL:
raise

# All exceptions need to be caught here to prevent the process
# from being killed by an uncaught exception.
except Exception as exception: # pylint: disable=broad-except
Expand Down Expand Up @@ -261,6 +265,7 @@ def _Main(self):
except errors.QueueAlreadyClosed:
logger.error('Queue for {0:s} was already closed.'.format(self.name))

# pylint: disable=missing-raises-doc
def _ProcessPathSpec(self, extraction_worker, parser_mediator, path_spec):
"""Processes a path specification.
Expand All @@ -279,10 +284,14 @@ def _ProcessPathSpec(self, extraction_worker, parser_mediator, path_spec):
excluded_find_specs = (
self._collection_filters_helper.excluded_file_system_find_specs)

# pylint: disable=try-except-raise
try:
extraction_worker.ProcessPathSpec(
parser_mediator, path_spec, excluded_find_specs=excluded_find_specs)

except definitions.EXCEPTIONS_EXCLUDED_FROM_CATCH_ALL:
raise

except dfvfs_errors.CacheFullError:
# TODO: signal engine of failure.
self._abort = True
Expand Down
7 changes: 7 additions & 0 deletions plaso/parsers/esedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pyesedb

from plaso.lib import definitions
from plaso.lib import specification
from plaso.parsers import interface
from plaso.parsers import logger
Expand Down Expand Up @@ -97,6 +98,7 @@ def GetFormatSpecification(cls):
format_specification.AddNewSignature(b'\xef\xcd\xab\x89', offset=4)
return format_specification

# pylint: disable=missing-raises-doc
def ParseFileObject(self, parser_mediator, file_object):
"""Parses an ESE database file-like object.
Expand All @@ -116,6 +118,7 @@ def ParseFileObject(self, parser_mediator, file_object):

# Compare the list of available plugin objects.
cache = ESEDBCache()

try:
for plugin in self._plugins:
if parser_mediator.abort:
Expand All @@ -132,10 +135,14 @@ def ParseFileObject(self, parser_mediator, file_object):
logger.debug('Parsing file: {0:s} with plugin: {1:s}'.format(
display_name, plugin.NAME))

# pylint: disable=try-except-raise
try:
plugin.UpdateChainAndProcess(
parser_mediator, cache=cache, database=database)

except definitions.EXCEPTIONS_EXCLUDED_FROM_CATCH_ALL:
raise

except Exception as exception: # pylint: disable=broad-except
parser_mediator.ProduceExtractionWarning((
'plugin: {0:s} unable to parse ESE database with error: '
Expand Down
6 changes: 6 additions & 0 deletions plaso/parsers/esedb_plugins/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from dtfabric import errors as dtfabric_errors
from dtfabric.runtime import fabric as dtfabric_fabric

from plaso.lib import definitions
from plaso.lib import errors
from plaso.parsers import logger
from plaso.parsers import plugins
Expand Down Expand Up @@ -214,6 +215,7 @@ def _GetRecordValue(self, record, value_entry):
return long_value.get_data()
return record.get_value_data(value_entry)

# pylint: disable=missing-raises-doc
def _GetRecordValues(
self, parser_mediator, table_name, record, value_mappings=None):
"""Retrieves the values from the record.
Expand Down Expand Up @@ -254,10 +256,14 @@ def _GetRecordValues(
self.NAME, value_callback_method, column_name, table_name))

if value_callback:
# pylint: disable=try-except-raise
try:
value_data = record.get_value_data(value_entry)
value = value_callback(value_data)

except definitions.EXCEPTIONS_EXCLUDED_FROM_CATCH_ALL:
raise

except Exception as exception: # pylint: disable=broad-except
logger.error(exception)
value = None
Expand Down
11 changes: 11 additions & 0 deletions plaso/parsers/olecf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pyolecf

from plaso.lib import definitions
from plaso.lib import specification
from plaso.parsers import interface
from plaso.parsers import logger
Expand Down Expand Up @@ -40,6 +41,7 @@ def GetFormatSpecification(cls):

return format_specification

# pylint: disable=missing-raises-doc
def ParseFileObject(self, parser_mediator, file_object):
"""Parses an OLE Compound File (OLECF) file-like object.
Expand Down Expand Up @@ -88,19 +90,28 @@ def ParseFileObject(self, parser_mediator, file_object):
logger.debug('Parsing file: {0:s} with plugin: {1:s}'.format(
display_name, plugin.NAME))

# pylint: disable=try-except-raise
try:
plugin.UpdateChainAndProcess(parser_mediator, root_item=root_item)

except definitions.EXCEPTIONS_EXCLUDED_FROM_CATCH_ALL:
raise

except Exception as exception: # pylint: disable=broad-except
parser_mediator.ProduceExtractionWarning((
'plugin: {0:s} unable to parse OLECF file with error: '
'{1!s}').format(plugin.NAME, exception))

if self._default_plugin and not parser_mediator.abort:
# pylint: disable=try-except-raise
try:
self._default_plugin.UpdateChainAndProcess(
parser_mediator, root_item=root_item)

# Raise on coding errors.
except definitions.EXCEPTIONS_EXCLUDED_FROM_CATCH_ALL:
raise

except Exception as exception: # pylint: disable=broad-except
parser_mediator.ProduceExtractionWarning((
'plugin: {0:s} unable to parse OLECF file with error: '
Expand Down
5 changes: 5 additions & 0 deletions plaso/parsers/safari_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def _ParsePage(self, parser_mediator, file_offset, page_data):

self._ParseRecord(parser_mediator, page_data, record_offset)

# pylint: disable=missing-raises-doc
def _ParseRecord(self, parser_mediator, page_data, record_offset):
"""Parses a record from the page data.
Expand Down Expand Up @@ -174,11 +175,15 @@ def _ParseRecord(self, parser_mediator, page_data, record_offset):
if event_data.cookie_name != plugin.COOKIE_NAME:
continue

# pylint: disable=try-except-raise
try:
plugin.UpdateChainAndProcess(
parser_mediator, cookie_name=event_data.cookie_name,
cookie_data=event_data.cookie_value, url=event_data.url)

except definitions.EXCEPTIONS_EXCLUDED_FROM_CATCH_ALL:
raise

except Exception as exception: # pylint: disable=broad-except
parser_mediator.ProduceExtractionWarning(
'plugin: {0:s} unable to parse cookie with error: {1!s}'.format(
Expand Down
9 changes: 9 additions & 0 deletions plaso/parsers/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from dfvfs.path import factory as dfvfs_factory

from plaso.lib import definitions
from plaso.lib import specification
from plaso.parsers import interface
from plaso.parsers import logger
Expand Down Expand Up @@ -372,6 +373,7 @@ def GetFormatSpecification(cls):
format_specification.AddNewSignature(b'SQLite format 3', offset=0)
return format_specification

# pylint: disable=missing-raises-doc
def ParseFileEntry(self, parser_mediator, file_entry):
"""Parses a SQLite database file entry.
Expand Down Expand Up @@ -425,11 +427,15 @@ def ParseFileEntry(self, parser_mediator, file_entry):
parser_mediator.SetFileEntry(file_entry)
parser_mediator.AddEventAttribute('schema_match', schema_match)

# pylint: disable=try-except-raise
try:
plugin.UpdateChainAndProcess(
parser_mediator, cache=cache, database=database,
database_wal=database_wal, wal_file_entry=wal_file_entry)

except definitions.EXCEPTIONS_EXCLUDED_FROM_CATCH_ALL:
raise

except Exception as exception: # pylint: disable=broad-except
parser_mediator.ProduceExtractionWarning((
'plugin: {0:s} unable to parse SQLite database with error: '
Expand All @@ -451,6 +457,9 @@ def ParseFileEntry(self, parser_mediator, file_entry):
parser_mediator, cache=cache, database=database,
database_wal=database_wal, wal_file_entry=wal_file_entry)

except definitions.EXCEPTIONS_EXCLUDED_FROM_CATCH_ALL:
raise

except Exception as exception: # pylint: disable=broad-except
parser_mediator.ProduceExtractionWarning((
'plugin: {0:s} unable to parse SQLite database and WAL with '
Expand Down
5 changes: 5 additions & 0 deletions plaso/parsers/sqlite_plugins/chrome_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def __init__(self):
self._cookie_plugins = (
cookie_plugins_manager.CookiePluginsManager.GetPlugins())

# pylint: disable=missing-raises-doc
def ParseCookieRow(self, parser_mediator, query, row, **unused_kwargs):
"""Parses a cookie row.
Expand Down Expand Up @@ -132,11 +133,15 @@ def ParseCookieRow(self, parser_mediator, query, row, **unused_kwargs):
if cookie_name != plugin.COOKIE_NAME:
continue

# pylint: disable=try-except-raise
try:
plugin.UpdateChainAndProcess(
parser_mediator, cookie_data=cookie_data, cookie_name=cookie_name,
url=url)

except definitions.EXCEPTIONS_EXCLUDED_FROM_CATCH_ALL:
raise

except Exception as exception: # pylint: disable=broad-except
parser_mediator.ProduceExtractionWarning(
'plugin: {0:s} unable to parse cookie with error: {1!s}'.format(
Expand Down
6 changes: 6 additions & 0 deletions plaso/parsers/sqlite_plugins/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import sqlite3

from plaso.lib import definitions
from plaso.parsers import logger
from plaso.parsers import plugins

Expand Down Expand Up @@ -89,6 +90,7 @@ def _HashRow(cls, row):

return hash(' '.join(values))

# pylint: disable=missing-raises-doc
def _ParseQuery(self, parser_mediator, database, query, callback, cache):
"""Queries a database and parses the results.
Expand Down Expand Up @@ -118,9 +120,13 @@ def _ParseQuery(self, parser_mediator, database, query, callback, cache):
if row_hash in row_cache:
continue

# pylint: disable=try-except-raise
try:
callback(parser_mediator, query, row, cache=cache, database=database)

except definitions.EXCEPTIONS_EXCLUDED_FROM_CATCH_ALL:
raise

except Exception as exception: # pylint: disable=broad-except
parser_mediator.ProduceExtractionWarning((
'unable to parse row: {0:d} with callback: {1:s} on database '
Expand Down
3 changes: 0 additions & 3 deletions tests/multi_processing/worker_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,6 @@ def testProcessPathSpec(self):
self.assertEqual(parser_mediator._number_of_warnings, 0)
self.assertTrue(test_process._abort)

test_process._ProcessPathSpec(None, parser_mediator, path_spec)
self.assertEqual(parser_mediator._number_of_warnings, 1)

def testProcessTask(self):
"""Tests the _ProcessTask function."""
session = sessions.Session()
Expand Down

0 comments on commit 2425e32

Please sign in to comment.