check_multi icon indicating copy to clipboard operation
check_multi copied to clipboard

Double escaping of html entities in xml format causes problems when used in a pipeline

Open scotsham opened this issue 7 years ago • 0 comments

Using check_multi in a pipeline to do multiple checks in a single call to a slow remote system

ssh remote "check_multi -f my.cmd -r 256" | check_multi -f - -r 8192+8+1
OK - 4 plugins checked, 4 ok [global_result_rating: parsing error (Evaluation error in 'COUNT(WARNING) > 0': syntax error at (eval 15) line 1, near "&gt"  ),global_result_rating: parsing error (Evaluation error in 'COUNT(CRITICAL) > 0': syntax error at (eval 16) line 1, near "&gt"  )]

The response causing problems is from the remote 'check_proc' command which returns the name of the process in single quotes.

Plaintext response
[ 1] proc_couchdb PROCS OK: 1 process with command name 'beam'

XML response
<output>PROCS OK: 1 process with command name &amp;#039;beam&amp;#039; </output>

This is my first post to github, so I am not sure of the etiquette, but I fixed the problem locally which is caused when the "&" character returned in the xml is sometimes double escaped due to the variable sorting of keys in the perl hash which is used to transform the response.

OLD

#---
#--- helper routine which encodes several XML specific characters
#---
sub xml_encode {
        my $input=shift;
        my %transtab=(
                '\''    => '&#039;',
                '&'     => '&amp;',
                '<'     => '&lt;',
                '>'     => '&gt;',
                '\|'    => '&#124;',
        );
        for (keys(%transtab)) {
                $input=~s/$_/$transtab{$_}/g;
        }
        $input;
}

NEW working solution

#---
#--- helper routine which encodes several XML specific characters
#---
sub xml_encode {
        my $input=shift;
        my %transtab=(
                '\''    => '&#039;',
                '<'     => '&lt;',
                '>'     => '&gt;',
                '\|'    => '&#124;',
        );
        # must encode '&' first, otherwise double encoding can happen
        # hash keys() doesn't guarantee order.
        $input=~s/&/&amp;/g;
        for (keys(%transtab)) {
                $input=~s/$_/$transtab{$_}/g;
        }
        $input;
}

scotsham avatar Jun 20 '17 10:06 scotsham