-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathExtractionService.php
More file actions
203 lines (180 loc) · 5.73 KB
/
Copy pathExtractionService.php
File metadata and controls
203 lines (180 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
/**
* SPDX-FileCopyrightText: 2012-2022 Paul Lereverend <paulereverend@gmail.com>
* SPDX-FileCopyrightText: 2022 Claus-Justus Heine <himself@claus-justus-heine.de>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Extract\Service;
use OCP\IL10N;
use Psr\Log\LoggerInterface;
use ZipArchive;
final class ExtractionService {
public function __construct(
private IL10N $l,
private LoggerInterface $logger,
) {
}
/**
* Reject absolute paths and any ".." path component. Modern libzip
* blocks traversal in ZipArchive::extractTo, but neither unrar nor
* 7za do — a crafted archive can write outside $extractTo.
*/
private function isUnsafePath(string $entry): bool {
$norm = str_replace('\\', '/', $entry);
if ($norm === '' || $norm === '.' || $norm === './') {
return false;
}
if (str_starts_with($norm, '/')) {
return true;
}
return (bool)preg_match('#(^|/)\.\.($|/)#', $norm);
}
/** @return null|array{code: 0, desc: string} */
private function rejectUnsafe(string $entry): ?array {
if (!$this->isUnsafePath($entry)) {
return null;
}
$this->logger->warning('Refusing archive with unsafe entry: ' . $entry);
return [
'code' => 0,
'desc' => $this->l->t('Archive contains an unsafe path and was not extracted'),
];
}
/** Validate every ZIP entry via libzip indices. @return null|array{code: 0, desc: string} */
private function assertSafeZip(ZipArchive $zip): ?array {
for ($i = 0; $i < $zip->numFiles; $i++) {
$name = $zip->getNameIndex($i);
if ($name === false) {
return ['code' => 0, 'desc' => $this->l->t('Failed to read archive index')];
}
if ($err = $this->rejectUnsafe($name)) {
return $err;
}
}
return null;
}
/** Validate every RAR entry via `unrar lb` (bare list). @return null|array{code: 0, desc: string} */
private function assertSafeRarShell(string $file): ?array {
$output = [];
$return = 0;
exec('unrar lb ' . escapeshellarg($file) . ' 2>&1', $output, $return);
if ($return !== 0) {
$this->logger->error('unrar list failed (rc=' . $return . '): ' . implode("\n", $output));
return ['code' => 0, 'desc' => $this->l->t('Failed to inspect RAR contents')];
}
foreach ($output as $line) {
$line = trim($line);
if ($line === '') {
continue;
}
if ($err = $this->rejectUnsafe($line)) {
return $err;
}
}
return null;
}
/** Validate every entry via `7za l -ba -slt`. @return null|array{code: 0, desc: string} */
private function assertSafe7z(string $file): ?array {
$output = [];
$return = 0;
exec('7za l -ba -slt ' . escapeshellarg($file) . ' 2>&1', $output, $return);
if ($return !== 0) {
$this->logger->error('7za list failed (rc=' . $return . '): ' . implode("\n", $output));
return ['code' => 0, 'desc' => $this->l->t('Failed to inspect archive contents')];
}
foreach ($output as $line) {
if (!str_starts_with($line, 'Path = ')) {
continue;
}
$entry = substr($line, 7);
if ($err = $this->rejectUnsafe($entry)) {
return $err;
}
}
return null;
}
/**
* @return (bool|int|mixed)[]
*
* @psalm-return array{code: 0|1, desc?: string}
*/
public function extractZip(string $file, string $extractTo): array {
$response = [];
if (!extension_loaded('zip')) {
$response = array_merge($response, ['code' => 0, 'desc' => $this->l->t('Zip extension is not available')]);
return $response;
}
$zip = new ZipArchive();
if ($zip->open($file) !== true) {
$response = array_merge($response, ['code' => 0, 'desc' => $this->l->t('Cannot open Zip file')]);
return $response;
}
if ($err = $this->assertSafeZip($zip)) {
$zip->close();
return $err;
}
$success = $zip->extractTo($extractTo);
$zip->close();
$response = array_merge($response, ['code' => $success ? 1 : 0]);
return $response;
}
/**
* @return (int|mixed)[]
*
* @psalm-return array{code: 0|1, desc?: string}
*/
public function extractRar(string $file, string $extractTo): array {
$response = [];
if (!extension_loaded('rar')) {
if ($err = $this->assertSafeRarShell($file)) {
return $err;
}
$output = [];
$return = 0;
exec('unrar x ' . escapeshellarg($file) . ' -R ' . escapeshellarg($extractTo) . '/ -o+ 2>&1', $output, $return);
if ($return !== 0) {
$this->logger->error('unrar extract failed (rc=' . $return . '): ' . implode("\n", $output));
$response = array_merge($response, ['code' => 0, 'desc' => $this->l->t('Failed to extract RAR archive (is the rar extension or unrar installed?)')]);
return $response;
}
} else {
$rar_file = rar_open($file);
$list = rar_list($rar_file);
// Pre-validate every entry before extracting any of them.
foreach ($list as $archive_file) {
if ($err = $this->rejectUnsafe($archive_file->getName())) {
rar_close($rar_file);
return $err;
}
}
foreach ($list as $archive_file) {
$entry = rar_entry_get($rar_file, $archive_file->getName());
$entry->extract($extractTo);
}
rar_close($rar_file);
}
$response = array_merge($response, ['code' => 1]);
return $response;
}
/**
* @return (int|mixed)[]
*
* @psalm-return array{code: 0|1, desc?: string}
*/
public function extractOther(string $file, string $extractTo): array {
$response = [];
if ($err = $this->assertSafe7z($file)) {
return $err;
}
$output = [];
$return = 0;
exec('7za -y x ' . escapeshellarg($file) . ' -o' . escapeshellarg($extractTo) . ' 2>&1', $output, $return);
if ($return !== 0) {
$this->logger->error('7za extract failed (rc=' . $return . '): ' . implode("\n", $output));
$response = array_merge($response, ['code' => 0, 'desc' => $this->l->t('Failed to extract archive (is 7-Zip installed?)')]);
return $response;
}
$response = array_merge($response, ['code' => 1]);
return $response;
}
}