Skip to content

Limit maximum number of filter chains - #22110

Open
Sjord wants to merge 14 commits into
php:masterfrom
Sjord:limit_filters
Open

Limit maximum number of filter chains#22110
Sjord wants to merge 14 commits into
php:masterfrom
Sjord:limit_filters

Conversation

@Sjord

@Sjord Sjord commented May 21, 2026

Copy link
Copy Markdown
Contributor

jvoisin and others added 2 commits May 13, 2026 15:21
Chaining filters is becoming an increasingly popular primitive to exploit PHP
applications. Limiting the usage of only a few of them at the time should,
if not close entirely, make it significantly less attractive.

This should close php#10453
Limit number of filters that can be chained in a php://filter URL.

Count number of filters already on the stream, instead of counting iterations on the loop. When filters are separated by slash instead of pipe, php_stream_apply_filter_list is called muliple times, so counting iterations won't work. Instead, count numbers of filters already on the chain.
Add more elaborate test that tests:
- file read
- file include
- no warning on stream_filter_append

Related to:
php#10453
php#16699
Sjoerd Langkemper added 2 commits May 22, 2026 11:46
Read the maximum number of filters from context stream. If it is set, it
doesn't make sense to raise a deprecation warning, because this stream
context option didn't exist earlier. We raise an error consistent with
current stream handling, as long as rfc/stream_errors has not landed.

If it is not set, we give a deprecation warning when the 17th filter is
added. This is unfortunately done in another place; to prevent showing
warnings multiple times, the counting is done within the loop and not at
the end.
We get a zend_long from the stream context, so it makes sense to
consistently use it for all counts of filters.
Sjoerd Langkemper added 3 commits June 8, 2026 13:28
Parallel to this work, the stream_errors RFC was accepted and merged. Use the new API php_stream_wrapper_log_warn to report the error of too many filters.
@Sjord

Sjord commented Jun 17, 2026

Copy link
Copy Markdown
Contributor Author

It's looking pretty good for the RFC, so it would be nice to get a review of this PR.

@Sjord

Sjord commented Jun 26, 2026

Copy link
Copy Markdown
Contributor Author

RFC is accepted. @bukka could you review this?

@bukka

bukka commented Jun 26, 2026

Copy link
Copy Markdown
Member

Will check this or next weekend hopefully.

@Sjord

Sjord commented Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

@bukka Could you review this please?

@Sjord

Sjord commented Jul 16, 2026

Copy link
Copy Markdown
Contributor Author

@LamentXU123 , @arnaud-lb Could one of you perhaps review this pull request?

@LamentXU123 LamentXU123 self-assigned this Jul 16, 2026

@LamentXU123 LamentXU123 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late response and thank you for your work.

I am not an expert in this topic so just my 2 cents

Comment thread ext/standard/php_fopen_wrapper.c Outdated
Comment thread ext/standard/php_fopen_wrapper.c Outdated
Sjord added 2 commits July 16, 2026 12:04
When both read_chain and write_chain are true, also count the number of
write filters and give a deprecation warning when that is exceeded.
Do the counting in php_stream_apply_filter_list and stop adding filters
when the limit is reached, instead of adding them all first.
@LamentXU123

Copy link
Copy Markdown
Member

@Sjord Thanks. Now please wait for others reviews on this topic because i am not an expert on this.Also keep my reviews unresolved because I want to hear from others opinions. This looks good to me :)

@LamentXU123
LamentXU123 requested a review from Girgias July 16, 2026 13:28
Comment thread ext/standard/tests/filters/max_filter_chain.phpt
Comment thread ext/standard/php_fopen_wrapper.c Outdated
Comment thread ext/standard/php_fopen_wrapper.c Outdated

@TimWolla TimWolla left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't actually look at the code, but these stood out. There might be more cases where you're using spaces instead of tabs.

Comment thread ext/standard/php_fopen_wrapper.c Outdated
Comment thread main/streams/filter.c Outdated
Sjord added 4 commits July 16, 2026 17:51
Instead of counting the number of items in the chain every time we add a
filter to the chain, keep an attribute `count` to determine the number
of filters already on the chain.
Negative values now disable the limit.
@Sjord
Sjord requested a review from arnaud-lb July 16, 2026 18:58

@arnaud-lb arnaud-lb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me otherwise!

Comment thread main/streams/php_stream_filter_api.h Outdated
Comment thread ext/standard/php_fopen_wrapper.c Outdated

@LamentXU123 LamentXU123 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awaiting Jakub for final review 👍

@LamentXU123 LamentXU123 assigned bukka and unassigned LamentXU123 Jul 19, 2026
@Sjord

Sjord commented Jul 27, 2026

Copy link
Copy Markdown
Contributor Author

@bukka Could you review/merge this?

@Sjord
Sjord requested a review from TimWolla July 27, 2026 09:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants