Skip to content

Commit

Permalink
feat(extractor): add a 12 hours timeout to the execution of external …
Browse files Browse the repository at this point in the history
…extractors.
  • Loading branch information
qkaiser committed Jun 26, 2023
1 parent ae8e1ea commit 2c9e9ac
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
18 changes: 17 additions & 1 deletion unblob/extractors/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@
from structlog import get_logger

from unblob.models import DirectoryExtractor, ExtractError, Extractor
from unblob.report import ExtractCommandFailedReport, ExtractorDependencyNotFoundReport
from unblob.report import (
ExtractCommandFailedReport,
ExtractorDependencyNotFoundReport,
ExtractorTimedOut,
)

if TYPE_CHECKING:
import io

# value that is high enough not to block long running execution such as extraction of large
# disk images, but small enough to make sure unblob finish its execution at some point.
COMMAND_TIMEOUT = 12 * 60 * 60

logger = get_logger()


Expand Down Expand Up @@ -45,6 +53,7 @@ def no_op():
cmd,
stdout=stdout_file,
stderr=subprocess.PIPE,
timeout=COMMAND_TIMEOUT,
)
if res.returncode != 0:
error_report = ExtractCommandFailedReport(
Expand All @@ -65,6 +74,13 @@ def no_op():
**error_report.asdict(),
)
raise ExtractError(error_report) from None
except subprocess.TimeoutExpired as e:
error_report = ExtractorTimedOut(cmd=e.cmd, timeout=e.timeout)
logger.error(
"Extract command timed out.",
**error_report.asdict(),
)
raise ExtractError(error_report) from None
finally:
cleanup()

Expand Down
9 changes: 9 additions & 0 deletions unblob/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ class ExtractorDependencyNotFoundReport(ErrorReport):
dependencies: List[str]


@attr.define(kw_only=True, frozen=True)
class ExtractorTimedOut(ErrorReport):
"""Describes an error when the extractor execution timed out."""

severity: Severity = Severity.ERROR
cmd: str
timeout: float


@attr.define(kw_only=True, frozen=True)
class MaliciousSymlinkRemoved(ErrorReport):
"""Describes an error when malicious symlinks have been removed from disk."""
Expand Down

0 comments on commit 2c9e9ac

Please sign in to comment.