$versions{'infosec.pl'} = '06.6.00.0000';

#######################################################################
#
# AgoraCart and all associated files, except where noted, are
# Copyright 2001 to Present jointly by K-Factor Technologies, Inc.
# and by C E Mayo (aka Mister Ed) at AgoraCart.com & K-Factor.net
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This copyright notice may not be removed or altered in any way.
#
#######################################################################

#
#
# Contains the security functions for InfoSec (information ssecurity)
# needs of the cart & related content management.
#
#

########################################################################
# For XSS clean up purposes.  XSS doesn't get in anyway, but cleans up any
# residual code thingies.  PERL taint and special security routines already block
# XSS stuff.  Added by Mister Ed at K-Factor Technologies, Inc. on Dec 12, 2006
########################################################################

sub xssKiller {
    my $temp = q{};
    for $xx ( keys %form_data ) {
        $temp =~ s/%([A-Fa-f0-9]{2})/pack("c",hex($form_data{$xx}))/ge;
        if (    ( $temp =~ /((<)(script|object|java|vbscript|img|embed|iframe|frameset))/i )
                || ( $temp =~ /script|src=|\.js|\.cgi|\.pl|document\.|\.cookie|\.jsp|\.asp|\.php|\.css|javascript|java|vbscript|eval\(|\`/i )
                || ( $form_data{$xx} =~ /((<)(script|object|java|vbscript|img|embed|iframe|frameset))/i )
                || ( $form_data{$xx} =~ /script|src=|\.js|\.cgi|\.pl|document\.|\.cookie|\.jsp|\.asp|\.php|\.css|javascript|java|vbscript|eval\(|\`/i )
            )
        {
            $form_data{$xx} = q{};
            delete( $form_data{$xx} );
        }
    }
}

#######################################################################

sub query_by_URL_cleaner {
      local ($data) = @_;
      $data =~ s/\-_\-/\+/g;
      $data =~ s/\-/ /g;
      $data =~ s/\+/\-/g;
      $data =~ s/\_/ /g; # legacy
      $data =~ s/\%20/ /g;
      $data =~ s/\&amp\;/\&/ig;
      $data =~ s/\%26/\&/ig;
      $data =~ s/\%3A/\:/ig;
      $data =~ s/\%2F/\//ig;
      $data =~ s/\%3B/\;/ig;
      $data =~ s/\%2C/\,/ig;
      $data =~ s/\%22/\"/ig; #"
      $data =~ s/\%27/\'/ig; #'
      return ($data);
}

#######################################################################

sub untaintFormData {

    # defaults for English: USA, UK, Australia, Canada
    my $patternString  = '[^ \$\w\-=\+\.\/,@#!_\\[\]\^\{\}\:&;|~\*\x00\(\)]+';
    my $patternString2 = '[ \$\w\-=\+\.\/,@#!_\\[\]\^\{\}\:&;|~\*\x00\(\)]+';

    if ( $sc_use_international_latin_characters =~ /yes/i ) {
        $patternString = '[^ \$\w\-=\+\.\/,@#!_\\[\]\^\{\}\:&;|\x8A\x8C\x8E\x9A\x9C\x9E\x9F~\xC0-\xD6\*\x00\(\xD8-\xF6\)\xF8-\xFF]+';
        $patternString2 = '[ \$\w\-=\+\.\/,@#!_\\[\]\^\{\}\:&;|\x8A\x8C\x8E\x9A\x9C\x9E\x9F~\xC0-\xD6\*\x00\(\xD8-\xF6\)\xF8-\xFF]+';
    }

    # prefilter everything for meta characters
    if ( !( $sc_debug_mode =~ /yes/i ) ) { delete( $form_data{'versions'} ); }
    $form_data{$xx} =~ s/<//g;
    $form_data{$xx} =~ s/>//g;
    for $xx ( keys %form_data ) {
        $form_data{$xx} =~ s/($patternString)//g;
        if ( $form_data{$xx} =~ /($patternString2)/ ) {
            $form_data{$xx} = $1;
        }
        else {
            $form_data{$xx} = q{};
        }
    }
}

#######################################################################

# disallows submitting items in browser address bar
sub browserBarSubmitPrevent {
    if ( $form_data{'option'} ne '' ) {
        $form_data{'add_to_cart_button'}   = q{};
        $form_data{'add_to_cart_button.x'} = q{};
        $sc_unique_cart_modifier_orig      = q{};
        $sc_unique_cart_modifier           = q{};
    }
}

#######################################################################

sub untaintCartID {

    # un-taint the value of $form_data{'cart_id'}
    # also pattern match it, in case the form has 2+ cart_id fields

    ( $form_data{'cart_id'}, $junk ) = split( /\*/, $form_data{'cart_id'}, 2 );
    $sc_unique_cart_modifier_orig = $junk;
    $sc_unique_cart_modifier = substr( $sc_unique_cart_modifier_orig, 0, 6 );
    if ( $form_data{'cart_id'} =~ /^([\w\-\=\+\/]+)\.(\w+)/ ) {
        $cart_id = "$1.$2";
        if ( $cart_id eq '.' ) { $cart_id = ''; }
        if ( $form_data{'cart_id'} ne $cart_id ) { $cart_id = q{}; }
        $form_data{'cart_id'} = $cart_id;
    }
    else {
        ( $cart_id, $form_data{'cart_id'} ) = q{};
        $sc_unique_cart_modifier_orig = q{};
        $sc_unique_cart_modifier = q{};
    }

}

#######################################################################

sub untaintFormCookieCombo {

    # un-taint the value of $form_data{'keynamehere'} and the $cookie hash equiv.

    my $value = shift;
    my $temp = '';

    if ( $cookie{$value} ) {
        if ( $cookie{$value} =~ /^([\w\-\=\+\/]+)/ ) {
            $temp = "$1";
            if ( $cookie{$value} ne $temp ) { $cookie{$value} = q{}; }
            else { $cookie{$value} = $temp; }
        }
    }
    if ( $form_data{$value} ) {
        if ( $form_data{$value} =~ /^([\w\-\=\+\/]+)/ ) {
            $temp = "$1";
            if ( $form_data{$value} ne $temp ) { $form_data{$value} = q{}; }
            else { $form_data{$value} = $temp; }
        }
    }
}

#######################################################################

sub untaintCartIDCookie {

    #Untaint the cartID pulled from cookies

    if ( $cookie{'cart_id'} =~ /(^[\w\-\=\+\/]+)\.(\w+)/ ) {
        $cookie{'cart_id'} = "$1.$2";
        if ( $cookie{'cart_id'} eq '.' ) {
            $cookie{'cart_id'} = '';
        }
    }
    else {
        $cookie{'cart_id'} = '';
    }
}

#######################################################################

sub make_random_chars {
    # name says it all
    my ( $part1, $part2, $valid_chars, $chars, $inx );
    $part1 = 'abcdefghijklmnopqrstuvwxyz';
    $part2 = $part1;
    $part2 =~ tr/a-z/A-Z/;
    $valid_chars = $part1 . $part2 . '0123456789_-';
    $chars       = q{};
    for ( $inx = 0 ; ( $inx < 2 ) ; $inx++ ) {
        $chars .= substr( $valid_chars, rand( length($valid_chars) ), 1 );
    }
    $chars .= substr( rand(9), 0, 1 );
    for ( $inx = 0 ; ( $inx < 2 ) ; $inx++ ) {
        $chars .= substr( $valid_chars, rand( length($valid_chars) ), 1 );
    }
    $chars .= substr( rand(9), 0, 1 );
    return $chars;
}

#######################################################################

1;
