php / php/php-src

Dechunk incorrectly truncates string when it starts with a hex character

Đang mở
#21,983 10 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Extension: filter Feature Status: Requires RFC
Ngôn ngữ chính
C
Star
40.4k
Fork
8.1k
Merge trung bình
2 ngày 13 giờ
Pull request đã merge (30 ngày)
96

Mô tả

Description

The dechunk filter removes the first line if it starts with a hex character. This is incorrect and used in attacks. I propose it just passes the data through literally if the first line does not contain a valid length.

The dechunk filter is meant to decode HTTP chunked transfer encoding. It is used in the fopen URL wrapper. If the response contains "Transfer-Encoding: Chunked", it is passed through the dechunk filter.

It reads a length prefix and then so many bytes, and it works correctly:

var_dump(file_get_contents(
    "php://filter/dechunk/resource=data:text/plain,5\r\nhello\r\n"
));
// string(5) "hello"

If passed something that does not look chunked, it passes the data through literally:

var_dump(file_get_contents(
    "php://filter/dechunk/resource=data:text/plain,mango"
));
// string(5) "mango"

This makes a bit of sense. It is clearly not chunked encoding, so it is probably not encoded, and this is the best it can do.

However, if the string starts with a character which is valid hex character (0-9, a-f, A-F), it removes the content:

var_dump(file_get_contents(
    "php://filter/dechunk/resource=data:text/plain,apple"
));
// string(0) ""

This is a technical artifact of how the filter works. Besides that it is unexpected, it is also heavily misused in filter chain attacks. It creates an oracle that tells the attacker whether the string starts with a hex character.

I propose to change the filter so that it passes the data through literally if the first line is not a valid length.

var_dump(file_get_contents(
    "php://filter/dechunk/resource=data:text/plain,apple"
));
// string(0) "apple"

This would make its behavior more consistent, and make it harder for attackers to abuse this function.

I think this will make filter chain attacks slightly harder, but not impossible.

This change it unlikely to break anyones program:

  • dechunk is not documented;
  • the happy flow of actually decoding chunked data keeps working the same;
  • dechunk already returns the input unchanged for some inputs (not starting with hex characters).

Perhaps it should also raise a warning or notice when the encoding is incorrect, but that is not what this issue is about.

PHP Version
PHP 8.5.5 (cli) (built: Apr  7 2026 16:24:10) (NTS)
Copyright (c) The PHP Group
Built by Homebrew
Zend Engine v4.5.5, Copyright (c) Zend Technologies
    with Zend OPcache v8.5.5, Copyright (c), by Zend Technologies
Operating System

No response

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Tái hiện các ví dụ thông qua entry point php://filter/dechunk, sau đó tìm phần triển khai bằng C của bộ lọc dechunk và phạm vi kiểm thử hiện có của nó. Kiểm tra cách xử lý độ dài không hợp lệ ở dòng đầu tiên, đồng thời giữ nguyên việc giải mã chunk hợp lệ và truyền nguyên trạng các đầu vào như “apple”; hoàn thành khi các trường hợp được minh họa trả về các giá trị mong đợi.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
c, php
Lĩnh vực
backend, security
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Ít trao đổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
58/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.