Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions ext/fileinfo/fileinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -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);

Expand Down
26 changes: 26 additions & 0 deletions ext/fileinfo/tests/finfo_file_remote_url.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
GH-20679 (finfo_file() doesn't work on remote resources)
--EXTENSIONS--
fileinfo
--INI--
allow_url_fopen=1
--SKIPIF--
<?php
if (@!include "./ext/standard/tests/http/server.inc") die('skip server.inc not available');
http_server_skipif();
?>
--FILE--
<?php
require "./ext/standard/tests/http/server.inc";

['pid' => $pid, 'uri' => $uri] = http_server([
"data://text/plain,HTTP/1.0 200 Ok\r\n\r\n<html>foo",
], $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"
72 changes: 72 additions & 0 deletions test_finfo_remote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Test script for finfo_file() with remote URLs
* This tests the fix for the bug where finfo_file() returned false for remote URLs
*/

echo "Testing finfo_file() with remote URLs\n";
echo str_repeat("=", 50) . "\n\n";

$finfo = finfo_open();

// Check if HTTPS wrapper is available
$wrappers = stream_get_wrappers();
$hasHttps = in_array('https', $wrappers);

// Test 1: Remote URL (the original failing case)
echo "Test 1: Remote URL (https://example.com)\n";
if (!$hasHttps) {
echo "⚠ SKIPPED: HTTPS wrapper not available (OpenSSL support required)\n";
echo " To enable HTTPS support, rebuild PHP with: ./configure --with-openssl\n\n";
} else {
$result = finfo_file($finfo, "https://example.com");
if ($result === false) {
echo "❌ FAILED: finfo_file() returned false (this is the bug we're fixing)\n";
} else {
echo "✅ PASSED: finfo_file() returned: " . var_export($result, true) . "\n";
// Expected something like: "HTML document, ASCII text, with very long lines (512)"
if (stripos($result, "HTML") !== false || stripos($result, "text") !== false) {
echo " ✓ Result contains expected content type indicators\n";
}
}
}
echo "\n";

// Test 2: Local file (should still work)
echo "Test 2: Local file (this script itself)\n";
$result = finfo_file($finfo, __FILE__);
if ($result === false) {
echo "❌ FAILED: finfo_file() returned false for local file\n";
} else {
echo "✅ PASSED: finfo_file() returned: " . var_export($result, true) . "\n";
}
echo "\n";

// Test 3: Directory (should still work and return "directory")
echo "Test 3: Directory\n";
$result = finfo_file($finfo, __DIR__);
if ($result === false) {
echo "❌ FAILED: finfo_file() returned false for directory\n";
} else {
echo "✅ PASSED: finfo_file() returned: " . var_export($result, true) . "\n";
if ($result === "directory") {
echo " ✓ Correctly identified as directory\n";
} else {
echo " ⚠ WARNING: Expected 'directory', got something else\n";
}
}
echo "\n";

// Test 4: Another remote URL (different domain)
echo "Test 4: Another remote URL (http://httpbin.org/html)\n";
$result = finfo_file($finfo, "http://httpbin.org/html");
if ($result === false) {
echo "❌ FAILED: finfo_file() returned false for remote URL\n";
} else {
echo "✅ PASSED: finfo_file() returned: " . var_export($result, true) . "\n";
}
echo "\n";

echo str_repeat("=", 50) . "\n";
echo "Test completed!\n";