Re: Apache2::Filter Intermittently Missing Injected String

"Hendrik Schumacher" <[email protected]>
Newsgroups gmane.comp.apache.mod-perl
Message-ID <[email protected]>
Am Do, 31.03.2011, 06:30 schrieb Chris Datfung:
> On Wed, Mar 30, 2011 at 12:36 PM, Hendrik Schumacher
> <[email protected]>wrote:
>
>> Am Mi, 30.03.2011, 12:17 schrieb Chris Datfung:
>>
>> I had a similar problem with a http proxy that injected a string into
>> the
>> HTML body. If the response is passed to the filter in multiple parts
>> there
>> is a certain probability that the response is split on the string
>> position
>> you are looking for (for example part 2 ends with "</bo" and part 3
>> starts
>> with "dy>"). I had to buffer the last bytes of each response part and
>> take
>> them into account
>
>
> Hi Hendrik,
>
> That is exactly the problem. How did you buffer the last bytes of each
> response. Don't you just set the BUFF_LEN and thats the number of
> characters
> you get?
>
> Chris
>

You have to handle the "last bytes buffer" yourself. If you use the
f->read approach of Apache2::Filter, you could use the following (untested
and probably not very efficient):

my $lastbytes = undef;
my $done = undef;
while ($filter->read(my $buffer, $wanted)) {
{
  if ($lastbytes)
  {
    $buffer = $lastbytes.$buffer;
    $lastbytes = undef;
  }
  if (not $done)
  {
    if ($buffer =~ s/<\/body>/$injection<\/body>/)
    {
      $done = 1;
    }
    else
    {
      $lastbytes = substr ($buffer, -6); # length of string to search - 1
      $buffer = substr ($buffer, 0, -6);
    }
  }
  $filter->print($buffer);
}
if ($filter->seen_eos && $lastbytes) {
  $filter->print($lastbytes);
}

If you are using the callback approach, you would have to store $lastbytes
somewhere (eg in $filter->ctx) and make sure to flush $lastbytes on eos.

Hendrik
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.