--- parser3/src/main/untaint.C 2015/04/08 18:08:53 1.166 +++ parser3/src/main/untaint.C 2026/11/29 13:17:21 1.184 @@ -1,11 +1,11 @@ /** @file Parser: String class part: untaint mechanizm. - Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com) - Author: Alexandr Petrosian (http://paf.design.ru) + Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com) + Authors: Konstantin Morshnev , Alexandr Petrosian */ -volatile const char * IDENT_UNTAINT_C="$Id: untaint.C,v 1.166 2015/04/08 18:08:53 moko Exp $"; +volatile const char * IDENT_UNTAINT_C="$Id: untaint.C,v 1.184 2026/11/29 13:17:21 moko Exp $"; #include "pa_string.h" @@ -17,10 +17,9 @@ volatile const char * IDENT_UNTAINT_C="$ #include "pa_common.h" #include "pa_charset.h" #include "pa_request_charsets.h" -#include "pa_sapi.h" extern "C" { // author forgot to do that -#include "ec.h" +#include "../lib/cord/include/ec.h" } #include "pa_sql_connection.h" @@ -52,22 +51,46 @@ extern "C" { // author forgot to do that } \ } - #define escape_fragment(action) \ for(; fragment_length--; CORD_next(info->pos)) { \ char c=CORD_pos_fetch(info->pos); \ action \ } -#define _default CORD_ec_append(info->result, c) + #define encode(need_encode_func, prefix, otherwise) \ if(need_encode_func(c)) { \ CORD_ec_append(info->result, prefix); \ - CORD_ec_append(info->result, hex_digits[((unsigned char)c) >> 4]); \ - CORD_ec_append(info->result, hex_digits[((unsigned char)c) & 0x0F]); \ + to_hex(c); \ } else \ CORD_ec_append(info->result, otherwise); + +#define to_hex(c) \ + { \ + CORD_ec_append(info->result, hex_digits[((unsigned char)c) >> 4]); \ + CORD_ec_append(info->result, hex_digits[((unsigned char)c) & 0x0F]); \ + } + #define to_char(c) { CORD_ec_append(info->result, c); whitespace=false; } -#define to_string(s) { CORD_ec_append_cord(info->result, s); whitespace=false; } +#define to_string(s) { CORD_ec_append_cord(info->result, (CORD)(s)); whitespace=false; } +#define _default CORD_ec_append(info->result, c) + +/// \u2028 and \u2029 (line/paragraph separators) and the next ones in utf-8 as \u20XX: they end a js string, see bug #1023; +/// other bytes after 0xE2 are not taken here, so the loop escapes them as usual +#define escape_u20XX { \ + size_t index=CORD_pos_to_index(info->pos); \ + unsigned char c2; \ + if(info->charsets && info->charsets->source().isUTF8() && fragment_length>=2 \ + && (unsigned char)info->body->fetch(index+1)==0x80 \ + && (c2=(unsigned char)info->body->fetch(index+2))>=0xA8 && c2<=0xAF){ \ + to_string("\\u20"); \ + to_hex(c2-0x80); \ + CORD_next(info->pos); \ + CORD_next(info->pos); \ + fragment_length-=2; \ + } else { \ + _default; \ + } \ +} inline bool need_file_encode(unsigned char c){ // russian letters and space ENABLED @@ -75,7 +98,7 @@ inline bool need_file_encode(unsigned ch return strchr( "*?\"<>|" #ifndef WIN32 - ":\\" + ":" // not allowed in windows file names; '\' is a path separator everywhere since #1300 #endif , c)!=0; } @@ -173,6 +196,25 @@ int append_fragment_nonoptimizing(char a return 0; // 0=continue } + +/** + appends to other String without language change +*/ + +String& String::append_to(String& dest) const { + if(is_empty()) + return dest; + + // first: fragment infos + dest.langs.appendHelper(dest.body, langs, body); + + // next: letters + dest.body<body->mid(info->fragment_begin, fragment_length).cstr(); - // skip source [we use recoded version] - pa_CORD_pos_advance(info->pos, fragment_length); - String::C output(fragment_str, fragment_length); - if(info->charsets) - output=Charset::transcode(output, - info->charsets->source(), - info->charsets->client()); - - char c; - for(const char* src=output.str; (c=*src++); ) - encode(need_uri_encode, '%', c); + // tainted, untaint language: uri + { + const char *fragment_str=info->body->mid(info->fragment_begin, fragment_length).cstr(); + // skip source [we use recoded version] + pa_CORD_pos_advance(info->pos, fragment_length); + String::C output(fragment_str, fragment_length); + if(info->charsets) + output=Charset::transcode(output, info->charsets->source(), info->charsets->client()); + + char c; + for(const char* src=output.str; (c=*src++); ) + encode(need_uri_encode, '%', c); } break; case String::L_HTTP_HEADER: @@ -382,17 +420,17 @@ int cstr_to_string_body_block(String::La bool email=false; uchar c; - for(const char* src=mail_ptr; (c=(uchar)*src++); ) { + for(const char* src=mail_ptr; c=(uchar)*src; src++) { if(c=='\r' || c=='\n') c=' '; - if(to_quoted_printable && (c==',' || c == '"' || addr_spec_soon(src-1/*position to 'c'*/))) { + if(to_quoted_printable && (c==',' || c == '"' || addr_spec_soon(src))) { email=c=='<'; to_string("?="); to_quoted_printable=false; } //RFC + An 'encoded-word' MUST NOT appear in any portion of an 'addr-spec'. if(!email && ( - ( !to_quoted_printable && (c & 0x80) ) // starting quote-printable-encoding on first 8bit char + ( !to_quoted_printable && (c & 0x80 || (c == ' ' && src == mail_ptr) ) ) // starting quote-printable-encoding on first 8bit char or leading space (issue #123) || ( to_quoted_printable && !mail_header_char_valid_within_Qencoded(c) ) )) { if(!to_quoted_printable) { @@ -428,13 +466,18 @@ int cstr_to_string_body_block(String::La } break; case String::L_JS: + // quotes, < > & as \xXX: safe both inside