forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.php
More file actions
460 lines (372 loc) · 13.7 KB
/
file.php
File metadata and controls
460 lines (372 loc) · 13.7 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
<?php
/**
* @group file
*/
class Tests_File extends WP_UnitTestCase {
const BADCHARS = '"\'[]*&?$';
private $dir;
public function set_up() {
parent::set_up();
$this->dir = untrailingslashit( get_temp_dir() );
}
/**
* @group plugins
* @group themes
*/
public function test_get_file_data() {
$theme_headers = array(
'Name' => 'Theme Name',
'ThemeURI' => 'Theme URI',
'Description' => 'Description',
'Version' => 'Version',
'Author' => 'Author',
'AuthorURI' => 'Author URI',
);
$actual = get_file_data( DIR_TESTDATA . '/themedir1/default/style.css', $theme_headers );
$expected = array(
'Name' => 'WordPress Default',
'ThemeURI' => 'http://wordpress.org/',
'Description' => 'The default WordPress theme based on the famous <a href="http://binarybonsai.com/kubrick/">Kubrick</a>.',
'Version' => '1.6',
'Author' => 'Michael Heilemann',
'AuthorURI' => 'http://binarybonsai.com/',
);
foreach ( $actual as $header => $value ) {
$this->assertSame( $expected[ $header ], $value, $header );
}
}
/**
* @ticket 19854
* @group plugins
* @group themes
*/
public function test_get_file_data_with_cr_line_endings() {
$headers = array(
'SomeHeader' => 'Some Header',
'Description' => 'Description',
'Author' => 'Author',
);
$actual = get_file_data( DIR_TESTDATA . '/formatting/file-header-cr-line-endings.php', $headers );
$expected = array(
'SomeHeader' => 'Some header value!',
'Description' => 'This file is using CR line endings for a testcase.',
'Author' => 'A Very Old Mac',
);
foreach ( $actual as $header => $value ) {
$this->assertSame( $expected[ $header ], $value, $header );
}
}
/**
* @ticket 47186
* @group plugins
* @group themes
*/
public function test_get_file_data_with_php_open_tag_prefix() {
$headers = array(
'TemplateName' => 'Template Name',
);
$actual = get_file_data( DIR_TESTDATA . '/formatting/file-header-php-open-tag-prefix.php', $headers );
$expected = array(
'TemplateName' => 'Something',
);
foreach ( $actual as $header => $value ) {
$this->assertSame( $expected[ $header ], $value, $header );
}
}
private function is_unique_writable_file( $path, $filename ) {
$fullpath = $path . DIRECTORY_SEPARATOR . $filename;
$fp = fopen( $fullpath, 'x' );
// File already exists?
if ( ! $fp ) {
return false;
}
// Write some contents.
$c = 'foo';
fwrite( $fp, $c );
fclose( $fp );
if ( file_get_contents( $fullpath ) === $c ) {
$result = true;
} else {
$result = false;
}
return $result;
}
public function test_unique_filename_is_valid() {
// Make sure it produces a valid, writable, unique filename.
$filename = wp_unique_filename( $this->dir, __FUNCTION__ . '.txt' );
$this->assertTrue( $this->is_unique_writable_file( $this->dir, $filename ) );
unlink( $this->dir . DIRECTORY_SEPARATOR . $filename );
}
public function test_unique_filename_is_unique() {
// Make sure it produces two unique filenames.
$name = __FUNCTION__;
$filename1 = wp_unique_filename( $this->dir, $name . '.txt' );
$this->assertTrue( $this->is_unique_writable_file( $this->dir, $filename1 ) );
$filename2 = wp_unique_filename( $this->dir, $name . '.txt' );
$this->assertTrue( $this->is_unique_writable_file( $this->dir, $filename2 ) );
// The two should be different.
$this->assertNotEquals( $filename1, $filename2 );
unlink( $this->dir . DIRECTORY_SEPARATOR . $filename1 );
unlink( $this->dir . DIRECTORY_SEPARATOR . $filename2 );
}
public function test_unique_filename_is_sanitized() {
$name = __FUNCTION__;
$filename = wp_unique_filename( $this->dir, $name . self::BADCHARS . '.txt' );
// Make sure the bad characters were all stripped out.
$this->assertSame( $name . '.txt', $filename );
$this->assertTrue( $this->is_unique_writable_file( $this->dir, $filename ) );
unlink( $this->dir . DIRECTORY_SEPARATOR . $filename );
}
public function test_unique_filename_with_slashes() {
$name = __FUNCTION__;
// "foo/foo.txt"
$filename = wp_unique_filename( $this->dir, $name . '/' . $name . '.txt' );
// The slash should be removed, i.e. "foofoo.txt".
$this->assertSame( $name . $name . '.txt', $filename );
$this->assertTrue( $this->is_unique_writable_file( $this->dir, $filename ) );
unlink( $this->dir . DIRECTORY_SEPARATOR . $filename );
}
public function test_unique_filename_multiple_ext() {
$name = __FUNCTION__;
$filename = wp_unique_filename( $this->dir, $name . '.php.txt' );
// "foo.php.txt" becomes "foo.php_.txt".
$this->assertSame( $name . '.php_.txt', $filename );
$this->assertTrue( $this->is_unique_writable_file( $this->dir, $filename ) );
unlink( $this->dir . DIRECTORY_SEPARATOR . $filename );
}
public function test_unique_filename_no_ext() {
$name = __FUNCTION__;
$filename = wp_unique_filename( $this->dir, $name );
$this->assertSame( $name, $filename );
$this->assertTrue( $this->is_unique_writable_file( $this->dir, $filename ) );
unlink( $this->dir . DIRECTORY_SEPARATOR . $filename );
}
/**
* @dataProvider data_wp_tempnam_filenames
*/
public function test_wp_tempnam( $filename ) {
$file = wp_tempnam( $filename );
unlink( $file );
$this->assertNotEmpty( basename( basename( $file, '.tmp' ), '.zip' ) );
}
public function data_wp_tempnam_filenames() {
return array(
array( '0.zip' ),
array( '0.1.2.3.zip' ),
array( 'filename.zip' ),
array( 'directory/0.zip' ),
array( 'directory/filename.zip' ),
array( 'directory/0/0.zip' ),
);
}
/**
* Tests that `wp_tempnam()` limits the filename's length to 252 characters.
*
* @ticket 35755
*
* @covers ::wp_tempnam
*
* @dataProvider data_wp_tempnam_should_limit_filename_length_to_252_characters
*/
public function test_wp_tempnam_should_limit_filename_length_to_252_characters( $filename ) {
$file = wp_tempnam( $filename );
if ( file_exists( $file ) ) {
self::unlink( $file );
}
$this->assertLessThanOrEqual( 252, strlen( basename( $file ) ) );
}
/**
* Data provider.
*
* @return array[]
*/
public function data_wp_tempnam_should_limit_filename_length_to_252_characters() {
return array(
'the limit before adding characters for uniqueness' => array( 'filename' => str_pad( '', 241, 'filename' ) ),
'one more than the limit before adding characters for uniqueness' => array( 'filename' => str_pad( '', 242, 'filename' ) ),
'251 characters' => array( 'filename' => str_pad( '', 251, 'filename' ) ),
'252 characters' => array( 'filename' => str_pad( '', 252, 'filename' ) ),
'253 characters' => array( 'filename' => str_pad( '', 253, 'filename' ) ),
);
}
/**
* Tests that `wp_tempnam()` limits the filename's length to 252 characters
* when there is a name conflict.
*
* @ticket 35755
*
* @covers ::wp_tempnam
*/
public function test_wp_tempnam_should_limit_filename_length_to_252_characters_with_name_conflict() {
// Create a conflict by removing the randomness of the generated password.
add_filter(
'random_password',
static function () {
return '123456';
},
10,
0
);
// A filename at the limit.
$filename = str_pad( '', 252, 'filename' );
// Create the initial file.
$existing_file = wp_tempnam( $filename );
// Try creating a file with the same name.
$actual = wp_tempnam( basename( $existing_file ) );
self::unlink( $existing_file );
self::unlink( $actual );
$this->assertLessThanOrEqual( 252, strlen( basename( $actual ) ) );
}
/**
* Tests that `wp_tempnam()` limits the filename's length to 252 characters
* when a 'random_password' filter returns passwords longer than 6 characters.
*
* @ticket 35755
*
* @covers ::wp_tempnam
*/
public function test_wp_tempnam_should_limit_filename_length_to_252_characters_when_random_password_is_filtered() {
// Force random passwords to 12 characters.
add_filter(
'random_password',
static function () {
return '1a2b3c4d5e6f';
},
10,
0
);
// A filename at the limit.
$filename = str_pad( '', 252, 'filename' );
$actual = wp_tempnam( $filename );
self::unlink( $actual );
$this->assertLessThanOrEqual( 252, strlen( basename( $actual ) ) );
}
/**
* Tests that `wp_tempnam()` limits the filename's length to 252 characters
* when a 'wp_unique_filename' filter returns a filename longer than 252 characters.
*
* @ticket 35755
*
* @covers ::wp_tempnam
*/
public function test_wp_tempnam_should_limit_filename_length_to_252_characters_when_wp_unique_filename_is_filtered() {
// Determine the number of additional characters added by `wp_tempnam()`.
$temp_dir = get_temp_dir();
$additional_chars_filename = wp_unique_filename( $temp_dir, 'filename' );
$additional_chars_generated = wp_tempnam( $additional_chars_filename, $temp_dir );
$additional_chars_difference = strlen( basename( $additional_chars_generated ) ) - strlen( $additional_chars_filename );
$filenames_over_limit = 0;
// Make the filter send the filename over the limit.
add_filter(
'wp_unique_filename',
static function ( $filename ) use ( &$filenames_over_limit ) {
if ( strlen( $filename ) === 252 ) {
$filename .= '1';
++$filenames_over_limit;
}
return $filename;
},
10,
1
);
// A filename that will hit the limit when `wp_tempnam()` adds characters.
$filename = str_pad( '', 252 - $additional_chars_difference, 'filename' );
$actual = wp_tempnam( $filename );
self::unlink( $additional_chars_generated );
self::unlink( $actual );
$this->assertLessThanOrEqual( 252, strlen( basename( $actual ) ), 'The final filename was over the limit.' );
$this->assertSame( 1, $filenames_over_limit, 'One filename should have been over the limit.' );
}
/**
* Tests that `wp_tempnam()` limits the filename's length to 252 characters
* when both a 'random_password' filter and a 'wp_unique_filename' filter
* cause the filename to be greater than 252 characters.
*
* @ticket 35755
*
* @covers ::wp_tempnam
*/
public function test_wp_tempnam_should_limit_filename_length_to_252_characters_when_random_password_and_wp_unique_filename_are_filtered() {
// Force random passwords to 12 characters.
add_filter(
'random_password',
static function () {
return '1a2b3c4d5e6f';
},
10,
0
);
// Determine the number of additional characters added by `wp_tempnam()`.
$temp_dir = get_temp_dir();
$additional_chars_filename = wp_unique_filename( $temp_dir, 'filename' );
$additional_chars_generated = wp_tempnam( $additional_chars_filename, $temp_dir );
$additional_chars_difference = strlen( basename( $additional_chars_generated ) ) - strlen( $additional_chars_filename );
$filenames_over_limit = 0;
// Make the filter send the filename over the limit.
add_filter(
'wp_unique_filename',
static function ( $filename ) use ( &$filenames_over_limit ) {
if ( strlen( $filename ) === 252 ) {
$filename .= '1';
++$filenames_over_limit;
}
return $filename;
},
10,
1
);
// A filename that will hit the limit when `wp_tempnam()` adds characters.
$filename = str_pad( '', 252 - $additional_chars_difference, 'filename' );
$actual = wp_tempnam( $filename );
self::unlink( $additional_chars_generated );
self::unlink( $actual );
$this->assertLessThanOrEqual( 252, strlen( basename( $actual ) ), 'The final filename was over the limit.' );
$this->assertSame( 1, $filenames_over_limit, 'One filename should have been over the limit.' );
}
/**
* @ticket 47186
*/
public function test_file_signature_functions_as_expected() {
$file = wp_tempnam();
file_put_contents( $file, 'WordPress' );
// The signature of 'WordPress' after SHA384 hashing, for verification against the key within self::filter_trust_plus85Tq_key().
$expected_signature = 'PmNv0b1ziwJAsVhjdpjd4+PQZidZWSlBm5b+GbbwE9m9HVKDFhEyvyRTHkRYOLypB8P2YvbW7CoOMZqGh8mEAA==';
add_filter( 'wp_trusted_keys', array( $this, 'filter_trust_plus85Tq_key' ) );
// Measure how long the call takes.
$timer_start = microtime( 1 );
$verify = verify_file_signature( $file, $expected_signature, 'WordPress' );
$timer_end = microtime( 1 );
$time_taken = ( $timer_end - $timer_start );
unlink( $file );
remove_filter( 'wp_trusted_keys', array( $this, 'filter_trust_plus85Tq_key' ) );
// verify_file_signature() should intentionally never take more than 10s to run.
$this->assertLessThan( 10, $time_taken, 'verify_file_signature() took longer than 10 seconds.' );
// Check to see if the system parameters prevent signature verifications.
if ( is_wp_error( $verify ) && 'signature_verification_unsupported' === $verify->get_error_code() ) {
$this->markTestSkipped( 'This system does not support Signature Verification.' );
}
$this->assertNotWPError( $verify );
$this->assertTrue( $verify );
}
/**
* @ticket 47186
*/
public function test_file_signature_expected_failure() {
$file = wp_tempnam();
file_put_contents( $file, 'WordPress' );
// Test an invalid signature.
$expected_signature = base64_encode( str_repeat( 'A', SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES ) );
$verify = verify_file_signature( $file, $expected_signature, 'WordPress' );
unlink( $file );
if ( is_wp_error( $verify ) && 'signature_verification_unsupported' === $verify->get_error_code() ) {
$this->markTestSkipped( 'This system does not support Signature Verification.' );
}
$this->assertWPError( $verify );
$this->assertSame( 'signature_verification_failed', $verify->get_error_code() );
}
public function filter_trust_plus85Tq_key( $keys ) {
// A static once-off key used to verify verify_file_signature() works as expected.
$keys[] = '+85TqMhxQVAYVW4BSCVkJQvZH4q7z8I9lePbvngvf7A=';
return $keys;
}
}