Skip to content

Commit 971728f

Browse files
authored
Fix GH-20679: finfo_file() doesn't work on remote resources (#20700)
The remote resources don't work because remote streams don't have a stat method. Since the check is only here for a best-effort check to return "directory" instead of "empty", we can try the stat and still execute the magic_stream() code even if it failed. Unfortunately we can't distinguish between a failed stat and an unimplemented stat. If we could, then this code could be even more robust.
1 parent 6103618 commit 971728f

4 files changed

Lines changed: 39 additions & 5 deletions

File tree

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ PHP NEWS
99
needing to be present beforehand. (ndossche)
1010
. Added `clamp()`. (kylekatarnls, thinkverse)
1111

12+
- Fileinfo:
13+
. Fixed bug GH-20679 (finfo_file() doesn't work on remote resources).
14+
(ndossche)
15+
1216
- Hash:
1317
. Upgrade xxHash to 0.8.2. (timwolla)
1418

UPGRADING

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ PHP 8.6 UPGRADE NOTES
3131
. It is now possible to use reference assign on WeakMap without the key
3232
needing to be present beforehand.
3333

34+
- Fileinfo:
35+
. finfo_file() now works with remote streams.
36+
3437
- Intl:
3538
. Added IntlNumberRangeFormatter class to format an interval of two numbers with a given skeleton, locale, IntlNumberRangeFormatter::COLLAPSE_AUTO, IntlNumberRangeFormatter::COLLAPSE_NONE, IntlNumberRangeFormatter::COLLAPSE_UNIT, IntlNumberRangeFormatter::COLLAPSE_ALL collapse and
3639
IntlNumberRangeFormatter::IDENTITY_FALLBACK_SINGLE_VALUE, IntlNumberRangeFormatter::IDENTITY_FALLBACK_APPROXIMATELY_OR_SINGLE_VALUE, IntlNumberRangeFormatter::IDENTITY_FALLBACK_APPROXIMATELY and

ext/fileinfo/fileinfo.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,12 @@ static const char* php_fileinfo_from_path(struct magic_set *magic, const zend_st
268268
if (php_stream_stat(stream, &ssb) == SUCCESS) {
269269
if (ssb.sb.st_mode & S_IFDIR) {
270270
ret_val = "directory";
271-
} else {
272-
ret_val = magic_stream(magic, stream);
273-
if (UNEXPECTED(ret_val == NULL)) {
274-
php_error_docref(NULL, E_WARNING, "Failed identify data %d:%s", magic_errno(magic), magic_error(magic));
275-
}
271+
}
272+
}
273+
if (!ret_val) {
274+
ret_val = magic_stream(magic, stream);
275+
if (UNEXPECTED(ret_val == NULL)) {
276+
php_error_docref(NULL, E_WARNING, "Failed identify data %d:%s", magic_errno(magic), magic_error(magic));
276277
}
277278
}
278279

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
GH-20679 (finfo_file() doesn't work on remote resources)
3+
--EXTENSIONS--
4+
fileinfo
5+
--INI--
6+
allow_url_fopen=1
7+
--SKIPIF--
8+
<?php
9+
if (@!include "./ext/standard/tests/http/server.inc") die('skip server.inc not available');
10+
http_server_skipif();
11+
?>
12+
--FILE--
13+
<?php
14+
require "./ext/standard/tests/http/server.inc";
15+
16+
['pid' => $pid, 'uri' => $uri] = http_server([
17+
"data://text/plain,HTTP/1.0 200 Ok\r\n\r\n<html>foo",
18+
], $output);
19+
20+
$f = finfo_open();
21+
var_dump(finfo_file($f, $uri));
22+
23+
http_server_kill($pid);
24+
?>
25+
--EXPECT--
26+
string(51) "HTML document, ASCII text, with no line terminators"

0 commit comments

Comments
 (0)