From baa97fbd92d0ef7081d278f3a63130964a7c9a18 Mon Sep 17 00:00:00 2001 From: Mdsujansarkar Date: Mon, 22 Dec 2025 16:00:34 +0600 Subject: [PATCH 1/2] Fix: finfo_file() issue --- ext/fileinfo/fileinfo.c | 19 ++--- ext/fileinfo/tests/finfo_file_remote_url.phpt | 26 +++++++ test_finfo_remote.php | 72 +++++++++++++++++++ 3 files changed, 109 insertions(+), 8 deletions(-) create mode 100644 ext/fileinfo/tests/finfo_file_remote_url.phpt create mode 100644 test_finfo_remote.php diff --git a/ext/fileinfo/fileinfo.c b/ext/fileinfo/fileinfo.c index baae757154950..b4c824a8aad51 100644 --- a/ext/fileinfo/fileinfo.c +++ b/ext/fileinfo/fileinfo.c @@ -268,11 +268,12 @@ static const char* php_fileinfo_from_path(struct magic_set *magic, const zend_st if (php_stream_stat(stream, &ssb) == SUCCESS) { if (ssb.sb.st_mode & S_IFDIR) { ret_val = "directory"; - } else { - ret_val = magic_stream(magic, stream); - if (UNEXPECTED(ret_val == NULL)) { - php_error_docref(NULL, E_WARNING, "Failed identify data %d:%s", magic_errno(magic), magic_error(magic)); - } + } + } + if (!ret_val) { + ret_val = magic_stream(magic, stream); + if (UNEXPECTED(ret_val == NULL)) { + php_error_docref(NULL, E_WARNING, "Failed identify data %d:%s", magic_errno(magic), magic_error(magic)); } } @@ -430,8 +431,10 @@ PHP_FUNCTION(mime_content_type) if (path) { php_stream_context *context = php_stream_context_get_default(false); ret_val = php_fileinfo_from_path(magic, path, context); - } else { - /* remember stream position for restoration */ + } + + if (!path) { + /* Remember stream position for restoration */ zend_off_t current_stream_pos = php_stream_tell(stream); php_stream_seek(stream, 0, SEEK_SET); @@ -440,7 +443,7 @@ PHP_FUNCTION(mime_content_type) php_error_docref(NULL, E_WARNING, "Failed identify data %d:%s", magic_errno(magic), magic_error(magic)); } - php_stream_seek(stream, current_stream_pos, SEEK_SET); + php_stream_seek(stream, current_stream_pos, SEEK_SET); } if (UNEXPECTED(ret_val == NULL)) { diff --git a/ext/fileinfo/tests/finfo_file_remote_url.phpt b/ext/fileinfo/tests/finfo_file_remote_url.phpt new file mode 100644 index 0000000000000..3301ffaa78e71 --- /dev/null +++ b/ext/fileinfo/tests/finfo_file_remote_url.phpt @@ -0,0 +1,26 @@ +--TEST-- +GH-20679 (finfo_file() doesn't work on remote resources) +--EXTENSIONS-- +fileinfo +--INI-- +allow_url_fopen=1 +--SKIPIF-- + +--FILE-- + $pid, 'uri' => $uri] = http_server([ + "data://text/plain,HTTP/1.0 200 Ok\r\n\r\nfoo", +], $output); + +$f = finfo_open(); +var_dump(finfo_file($f, $uri)); + +http_server_kill($pid); +?> +--EXPECT-- +string(51) "HTML document, ASCII text, with no line terminators" \ No newline at end of file diff --git a/test_finfo_remote.php b/test_finfo_remote.php new file mode 100644 index 0000000000000..6d7801851a71c --- /dev/null +++ b/test_finfo_remote.php @@ -0,0 +1,72 @@ + Date: Mon, 22 Dec 2025 16:03:41 +0600 Subject: [PATCH 2/2] Extra space remove --- ext/fileinfo/fileinfo.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/fileinfo/fileinfo.c b/ext/fileinfo/fileinfo.c index b4c824a8aad51..9c4dc2c5345c8 100644 --- a/ext/fileinfo/fileinfo.c +++ b/ext/fileinfo/fileinfo.c @@ -443,7 +443,7 @@ PHP_FUNCTION(mime_content_type) php_error_docref(NULL, E_WARNING, "Failed identify data %d:%s", magic_errno(magic), magic_error(magic)); } - php_stream_seek(stream, current_stream_pos, SEEK_SET); + php_stream_seek(stream, current_stream_pos, SEEK_SET); } if (UNEXPECTED(ret_val == NULL)) {