Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bugzilla): handle invalid attachment file names #475

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/grizzly/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def __init__(self) -> None:
"--log-level",
choices=sorted(self.LEVEL_MAP),
default="INFO",
help="Configure console logging (default: %(default)s)",
help="Configure console output. (default: %(default)s)",
)

# build 'asset' help string formatted as:
Expand Down
14 changes: 12 additions & 2 deletions src/grizzly/common/bugzilla.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ class BugzillaBug:

def __init__(self, bug: Bug, ignore: Iterable[str] = IGNORE_EXTS) -> None:
self._bug = bug
self._data = Path(mkdtemp(prefix=f"bug{bug.id}-", dir=grz_tmp("bugzilla")))
self._data = Path(
mkdtemp(prefix=f"bug{bug.id}-", dir=grz_tmp("bugzilla"))
).resolve()
self._fetch_attachments(ignore)

def __enter__(self) -> BugzillaBug:
Expand Down Expand Up @@ -82,14 +84,22 @@ def _fetch_attachments(
or attachment.file_name.split(".")[-1] in ignore
):
continue
target = (self._data / attachment.file_name).resolve()
# check file name
if target.is_dir() or not target.is_relative_to(self._data):
LOG.debug(
"bug %d: skipping attachment %r", self._bug.id, attachment.file_name
)
continue
# decode data
try:
data = b64decode(attachment.data or "")
except binascii.Error as exc:
LOG.warning(
"Failed to decode attachment: %r (%s)", attachment.file_name, exc
)
continue
(self._data / attachment.file_name).write_bytes(data)
target.write_bytes(data)

def _unpack_archives(self) -> None:
"""Unpack and remove archives.
Expand Down
32 changes: 31 additions & 1 deletion src/grizzly/common/test_bugzilla.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,28 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# pylint: disable=protected-access
from base64 import b64encode
from pathlib import Path
from zipfile import ZipFile

from bugsy import Attachment, Bug, BugsyException
from pytest import mark
from pytest import fixture, mark
from requests.exceptions import ConnectionError as RequestsConnectionError

from .bugzilla import BugzillaBug


@fixture(autouse=True)
def tmp_path_grz_tmp(tmp_path, mocker):
"""Provide an alternate working directory for testing."""

def _grz_tmp(*subdir):
path = Path(tmp_path, "grizzly", *subdir)
path.mkdir(parents=True, exist_ok=True)
return path

mocker.patch("grizzly.common.bugzilla.grz_tmp", _grz_tmp)


def test_bugzilla_01(mocker):
"""test BugzillaBug._fetch_attachments()"""
bug = mocker.Mock(spec=Bug, id=123)
Expand Down Expand Up @@ -47,11 +60,28 @@ def test_bugzilla_01(mocker):
file_name="broken.html",
data=b"bad-b64",
),
# invalid file name
mocker.Mock(
spec=Attachment,
is_obsolete=False,
content_type="text/html",
file_name=".",
data=b64encode(b"foo"),
),
# invalid file name
mocker.Mock(
spec=Attachment,
is_obsolete=False,
content_type="text/html",
file_name="../escape.html",
data=b64encode(b"foo"),
),
)
with BugzillaBug(bug) as bz_bug:
assert len(tuple(bz_bug.path.iterdir())) == 1
assert (bz_bug.path / "test.html").is_file()
assert (bz_bug.path / "test.html").read_text() == "foo"
assert not (bz_bug.path / ".." / "escape.html").is_file()


@mark.parametrize(
Expand Down