Skip to content

Commit edb4e5f

Browse files
e3krisztianqkaiser
authored andcommitted
refactor(ipkg): use FileSystem to report problems during extraction
1 parent 868da1b commit edb4e5f

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

unblob/handlers/archive/hp/ipkg.py

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,21 @@
55
from dissect.cstruct import Instance
66
from structlog import get_logger
77

8-
from unblob.extractor import carve_chunk_to_file, is_safe_path
9-
from unblob.file_utils import Endian, File, InvalidInputFormat, StructParser, snull
10-
from unblob.models import Chunk, Extractor, HexString, StructHandler, ValidChunk
8+
from unblob.file_utils import (
9+
Endian,
10+
File,
11+
FileSystem,
12+
InvalidInputFormat,
13+
StructParser,
14+
snull,
15+
)
16+
from unblob.models import (
17+
Extractor,
18+
ExtractResult,
19+
HexString,
20+
StructHandler,
21+
ValidChunk,
22+
)
1123

1224
logger = get_logger()
1325

@@ -54,6 +66,7 @@ def __init__(self):
5466

5567
def extract(self, inpath: Path, outdir: Path):
5668
entries = []
69+
fs = FileSystem(outdir)
5770
with File.from_path(inpath) as file:
5871
header = self._struct_parser.parse("ipkg_header_t", file, Endian.LITTLE)
5972
file.seek(header.toc_offset, io.SEEK_SET)
@@ -64,28 +77,18 @@ def extract(self, inpath: Path, outdir: Path):
6477
entry_path = Path(snull(entry.name).decode("utf-8"))
6578
if entry_path.parent.name:
6679
raise InvalidInputFormat("Entry name contains directories.")
67-
if not is_safe_path(outdir, entry_path):
68-
logger.warning(
69-
"Path traversal attempt, discarding.",
70-
outdir=outdir,
71-
)
72-
continue
7380
entries.append(
7481
(
75-
outdir.joinpath(outdir / entry_path.name),
76-
Chunk(
77-
start_offset=entry.offset,
78-
end_offset=entry.offset + entry.size,
79-
),
82+
Path(entry_path.name),
83+
entry.offset,
84+
entry.size,
8085
)
8186
)
8287

83-
for carve_path, chunk in entries:
84-
carve_chunk_to_file(
85-
file=file,
86-
chunk=chunk,
87-
carve_path=carve_path,
88-
)
88+
for carve_path, start_offset, size in entries:
89+
fs.carve(carve_path, file, start_offset, size)
90+
91+
return ExtractResult(reports=list(fs.problems))
8992

9093

9194
class HPIPKGHandler(StructHandler):

0 commit comments

Comments
 (0)