php-src icon indicating copy to clipboard operation
php-src copied to clipboard

For php://input fread() writes temp files

Open raubv0gel opened this issue 4 years ago • 6 comments

Description

The following code:

<?php
// index.php

$postBodyResource = fopen("php://input", 'rb');
while (!feof($postBodyResource)) {
    $data = fread($postBodyResource, 5 * 1024);
}
fclose($postBodyResource);

and Apache 2.4 config

Alias "/api" "${SRVROOT}/htdocs/Api"
<Directory "${SRVROOT}/htdocs/Api">
    RewriteEngine On 
    RewriteCond %{REQUEST_FILEaNAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ index.php [QSA,L]

    Script POST "index.php"
    Script PUT "index.php"

    php_value enable_post_data_reading Off
</Directory>

Resulted in this output: If I do a HTTP POST (Content-Type: application/octet-stream) at http://localhost/api with a body larger than 16 kB, PHP creates a temp file at sys_temp_dir or upload_dir that stores the whole content of the stream php://input while doing fread().

But I expected this output instead: At least there should be a configuration option to disable writing the temp files.

See temp file writing so caused by php_fopen_wrapper.c#L231.

Please, see Stackoverflow for some discussion.

PHP Version

PHP 7.4+

Operating System

Windows 10 21H2

raubv0gel avatar Mar 22 '22 12:03 raubv0gel

To be able to apply stream operations, there needs to be a stream. Always forcing a memory stream could deploy the available memory on the system, but maybe we should think about increasing the threshold; the current 0x4000 might be anachronistic. And yes, we also could introduce an INI setting for this. The current behavior doesn't look like a bug to me, though, so I'm changing to feature request. And while I don't think this needs an RFC, at least some discussion on the internals mailing list appears to be in order.

cmb69 avatar Mar 23 '22 10:03 cmb69

@cmb69, thank you! The point is: There exists cases for which there is no need to back the entire stream data (in memory, in temp files, …) One of the advantages to uses streams is that you do not need to know all data at a certain point of time. In my case a hash function that supports streaming only needs to know some part (e. g. 1024 bytes) of the data at a certain point of time (the “incremented” final hash value is calculated at end of stream). Therefore there should at least be an setting to disable backing the php://input stream.

Or am I missing something here?

raubv0gel avatar Mar 23 '22 10:03 raubv0gel

To be able to apply stream operations, there needs to be a stream.

It might be possible to introduce new stream wrappers which allow to avoid copying the input to the memory stream, but it might not be possible to support all operations which are currently supported by php://input for all SAPIs (what would introduce a BC break).

cmb69 avatar Mar 23 '22 12:03 cmb69

To clarify: There has to be a HTTP input stream from a HTTP request. This is a network stream. This stream can be consumed by PHP user code. The stream should not be read by PHP internal code to store the data in memory or temp files or whatever – but that’s the case for php://input. The user code partially reads from the stream (a number of bytes) and does some processing with the partial data (e. g. storing on other streams [memory, temp files, …], partial hash value calculations, …). After, the read data is dropped from the stream and the user code continues forward reading from the stream until the stream ends. (Of course, internally, there should be some small memory buffer to manage receiving and reading the network data.) The HTTP input stream is a forward only and read only.

That’s a common way in Java (e. g. HttpExchange.getRequestBody()) and C# (e. g. https://docs.microsoft.com/en-us/dotnet/framework/network-programming/using-streams-on-the-network).

raubv0gel avatar Mar 23 '22 21:03 raubv0gel

Note: Reading from php://input writes temp files, there will be an error if the temp directory storage space is exceeded. This should not happen when reading from network streams.

raubv0gel avatar Mar 24 '22 21:03 raubv0gel

I just found around 80 GB "phpXXXXX" files in /tmp, and it looks like it's due to this. Had no idea reading php://input would write a file, seems counter-intuitive. Any updates on this? Would like to turn it off.

fredsted avatar Feb 21 '24 20:02 fredsted