Repository navigation
Expand file tree
/
Copy pathclass-zipdecoder.php
More file actions
210 lines (178 loc) · 7.58 KB
/
Copy pathclass-zipdecoder.php
File metadata and controls
210 lines (178 loc) · 7.58 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
204
205
206
207
208
209
210
<?php
namespace WordPress\Zip;
use WordPress\ByteStream\ByteStreamException;
use WordPress\ByteStream\NotEnoughDataException;
use WordPress\ByteStream\ReadStream\ByteReadStream;
use WordPress\ByteStream\ReadStream\InflateReadStream;
use WordPress\ByteStream\ReadStream\LimitedByteReadStream;
class ZipDecoder {
const COMPRESSION_DEFLATE = 8;
const COMPRESSION_NONE = 0;
const STATE_SCAN = 'scan';
const STATE_FILE_ENTRY = 'file-entry';
const STATE_CENTRAL_DIRECTORY_ENTRY_READING = 'central-directory-entry-reading';
const STATE_END_CENTRAL_DIRECTORY_ENTRY_READING = 'end-central-directory-entry-reading';
const STATE_OBJECT_READY = 'object-ready';
const STATE_COMPLETE = 'complete';
private $state = self::STATE_SCAN;
private $object = null;
private $byte_reader;
public function __construct( ByteReadStream $byte_reader ) {
$this->byte_reader = $byte_reader;
}
public function reached_end_of_data(): bool {
return self::STATE_COMPLETE === $this->state;
}
public function next_object(): bool {
// If we're calling next_object() when an object is ready,
// it means we want to scan for the next object. Let's clear
// the state and start scanning again.
if ( self::STATE_OBJECT_READY === $this->state ) {
$this->after_record();
}
while ( true ) {
switch ( $this->state ) {
case self::STATE_SCAN:
try {
$this->byte_reader->pull( 4, ByteReadStream::PULL_EXACTLY );
} catch ( NotEnoughDataException $e ) {
$this->state = self::STATE_COMPLETE;
return false;
}
$signature = $this->byte_reader->consume( 4 );
$signature = unpack( 'V', $signature )[1];
switch ( $signature ) {
case FileEntry::SIGNATURE:
$this->state = self::STATE_FILE_ENTRY;
break;
case CentralDirectoryEntry::SIGNATURE:
$this->state = self::STATE_CENTRAL_DIRECTORY_ENTRY_READING;
break;
case EndCentralDirectoryEntry::SIGNATURE:
$this->state = self::STATE_END_CENTRAL_DIRECTORY_ENTRY_READING;
break;
default:
throw new ByteStreamException(
sprintf( 'Invalid ZIP object signature %d', $signature )
);
}
break;
case self::STATE_FILE_ENTRY:
$this->read_file_entry();
break;
case self::STATE_CENTRAL_DIRECTORY_ENTRY_READING:
$this->read_central_directory_entry();
break;
case self::STATE_END_CENTRAL_DIRECTORY_ENTRY_READING:
$this->read_end_central_directory_entry();
break;
case self::STATE_OBJECT_READY:
return true;
default:
return false;
}
}
}
public function get_object() {
return $this->object;
}
public function seek_to_record( $record_offset ) {
$this->after_record();
$this->byte_reader->seek( $record_offset );
}
private function read_file_entry() {
$this->byte_reader->pull( FileEntry::HEADER_SIZE, ByteReadStream::PULL_EXACTLY );
$data = $this->byte_reader->consume( FileEntry::HEADER_SIZE );
$header_fields = unpack(
'vversion/vgeneral_purpose/vcompression_method/vlast_modified_time/vlast_modified_date/Vcrc/Vcompressed_size/Vuncompressed_size/vpath_length/vextra_length',
$data
);
$this->object = new FileEntry( $header_fields );
$this->byte_reader->pull( $this->object->path_length, ByteReadStream::PULL_EXACTLY );
$path = $this->byte_reader->consume( $this->object->path_length );
$this->object->path = self::sanitize_path( $path );
$this->byte_reader->pull( $this->object->extra_length, ByteReadStream::PULL_EXACTLY );
$extra = $this->byte_reader->consume( $this->object->extra_length );
$this->object->extra = $extra;
$limit_reader = new LimitedByteReadStream(
$this->byte_reader,
$this->object->compressed_size
);
$is_compressed = self::COMPRESSION_DEFLATE === $this->object->compression_method;
if ( $is_compressed ) {
$this->object->body_reader = new InflateReadStream( $limit_reader, ZLIB_ENCODING_RAW );
} else {
$this->object->body_reader = $limit_reader;
}
$this->state = self::STATE_OBJECT_READY;
}
private function read_central_directory_entry() {
$this->byte_reader->pull( CentralDirectoryEntry::HEADER_SIZE, ByteReadStream::PULL_EXACTLY );
$data = $this->byte_reader->consume( CentralDirectoryEntry::HEADER_SIZE );
$header_fields = unpack(
'vversion_created/vversion_needed/vgeneral_purpose/vcompression_method/vlast_modified_time/vlast_modified_date/Vcrc/Vcompressed_size/Vuncompressed_size/vpath_length/vextra_length/vfile_comment_length/vdisk_number/vinternal_attributes/Vexternal_attributes/Vfirst_byte_at',
$data
);
$this->object = new CentralDirectoryEntry( $header_fields );
$this->byte_reader->pull( $this->object->path_length, ByteReadStream::PULL_EXACTLY );
$path_bytes = $this->byte_reader->consume( $this->object->path_length );
$this->object->path = self::sanitize_path( $path_bytes );
$this->byte_reader->pull( $this->object->extra_length, ByteReadStream::PULL_EXACTLY );
$extra_bytes = $this->byte_reader->consume( $this->object->extra_length );
$this->object->extra = $extra_bytes;
$this->byte_reader->pull( $this->object->file_comment_length, ByteReadStream::PULL_EXACTLY );
$file_comment_bytes = $this->byte_reader->consume( $this->object->file_comment_length );
$this->object->file_comment = $file_comment_bytes;
$this->state = self::STATE_OBJECT_READY;
}
private function read_end_central_directory_entry() {
$this->byte_reader->pull( EndCentralDirectoryEntry::HEADER_SIZE, ByteReadStream::PULL_EXACTLY );
$data = $this->byte_reader->consume( EndCentralDirectoryEntry::HEADER_SIZE );
$header_fields = unpack(
'vdisk_number/vcentral_directory_start_disk/vnumber_central_directory_records_on_this_disk/vnumber_central_directory_records/Vcentral_directory_size/Vcentral_directory_offset/vcomment_length',
$data
);
$this->object = new EndCentralDirectoryEntry(
$header_fields
);
$this->byte_reader->pull( $this->object->comment_length, ByteReadStream::PULL_EXACTLY );
$comment_bytes = $this->byte_reader->consume( $this->object->comment_length );
$this->object->comment = $comment_bytes;
$this->state = self::STATE_OBJECT_READY;
}
private function after_record() {
if ( $this->object instanceof FileEntry ) {
// Skip past the file bytes.
$this->object->body_reader->consume_all();
$this->object->body_reader->close_reading();
}
$this->state = self::STATE_SCAN;
$this->object = null;
}
/**
* Normalizes the parsed path to prevent directory traversal,
* a.k.a zip slip attacks.
*
* In ZIP, paths are arbitrary byte sequences. Nothing prevents
* a ZIP file from containing a path such as /etc/passwd or
* ../../../../etc/passwd.
*
* This function normalizes paths found in the ZIP file.
*
* @TODO: Scrutinize the implementation of this function. Consider
* unicode characters in the path, including ones that are
* just embelishments of the following character. Consider
* the impact of **all** seemingly "invalid" byte sequences,
* e.g. spaces, ASCII control characters, etc. What will the
* OS do when it receives a path containing .{null byte}./etc/passwd?
*/
public static function sanitize_path( $path ) {
// Replace multiple slashes with a single slash.
$path = preg_replace( '#/+#', '/', $path );
// Remove all the leading ../ segments.
$path = preg_replace( '#^(\.\./)+#', '', $path );
// Remove all the /./ and /../ segments.
$path = preg_replace( '#/\.\.?/#', '/', $path );
return $path;
}
}