44: Set a Limit for php://filter Chains
Overview
Section titled “Overview”Apply a known read filter with an explicit slot limit. PHP 8.6 deprecates a php://filter URL with more than 16 filters if no explicit limit is configured.
Prerequisites
Section titled “Prerequisites”- PHP 8.6 CLI. Confirm the version with
php --version. allow_url_fopen=1for the in-memory data URL used as the input.- Review the previous chapter if you’re following the series in order.
Allow about 10 minutes for the walkthrough and exercise.
What You’ll Build
Section titled “What You’ll Build”A runnable stream-filter-limit.php example.
Step 1: Run the example (~5 min)
Section titled “Step 1: Run the example (~5 min)”Run the following from the root of the Code with PHP repository.
php code/php-8-6/44-stream-filter-limit/stream-filter-limit.php<?php
declare(strict_types=1);
$context = stream_context_create(['filter' => ['max_filter_count' => 1]]);$stream = fopen('php://filter/read=string.toupper/resource=data://text/plain,order', 'r', context: $context);if ($stream === false) { throw new RuntimeException('Cannot open the filtered input');}try { echo stream_get_contents($stream), PHP_EOL;} finally { fclose($stream);}Expected Result
Section titled “Expected Result”ORDERWhy It Works
Section titled “Why It Works”Pass max_filter_count in the filter group of the stream context. The configured limit makes exceeding it fail the open. For code-defined chains, stream_filter_append() lets you attach filters directly to an opened stream.
Exercise
Section titled “Exercise”Set the limit to 0 and confirm that opening the stream with one filter now fails. Keep 1 to allow the filter used here.
Wrap-up
Section titled “Wrap-up”Continue with Audit PHP 8.6 Session Cookie Defaults.