--- parser3/src/main/untaint.C 2009/08/21 08:38:55 1.151 +++ 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-2005 ArtLebedev Group (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 */ -static const char * const IDENT_UNTAINT_C="$Date: 2009/08/21 08:38:55 $"; +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 @@ static const char * const IDENT_UNTAINT_ #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,47 +51,60 @@ 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)) { \ - static const char* hex="0123456789ABCDEF"; \ CORD_ec_append(info->result, prefix); \ - CORD_ec_append(info->result, hex[((unsigned char)c)/0x10]); \ - CORD_ec_append(info->result, hex[((unsigned char)c)%0x10]); \ + 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 // encoding only these... return strchr( - "*?'\"<>|" + "*?\"<>|" #ifndef WIN32 - ":\\" + ":" // not allowed in windows file names; '\' is a path separator everywhere since #1300 #endif , c)!=0; } inline bool need_uri_encode(unsigned char c){ - if((c>='0') &&(c<='9') ||(c>='A') &&(c<='Z') ||(c>='a') &&(c<='z')) - return false; - - return !strchr("_-./", c); -} - -inline bool need_http_header_encode(unsigned char c){ - if(strchr(" , :", c)) - return false; - - return need_uri_encode(c); + return !(pa_isalnum(c) || strchr("_-./*", c)); } inline bool need_regex_escape(unsigned char c){ @@ -108,34 +120,35 @@ inline bool need_parser_code_escape(unsi /* HTTP-header = field-name ":" [ field-value ] CRLF - field-name = token - field-value = *( field-content | LWS ) +field-name = token +field-value = *( field-content | LWS ) - field-content = - -word = token | quoted-string - token = 1* +word = token | quoted-string +quoted-string = ( <"> *(qdtext | quoted-pair ) <"> ) +qdtext = > +quoted-pair = "\" CHAR - +OCTET = +CHAR = tspecials = "(" | ")" | "<" | ">" | "@" - | "," | ";" | ":" | "\" | <"> - | "/" | "[" | "]" | "?" | "=" - | "{" | "}" | SP | HT + | "," | ";" | ":" | "\" | <"> + | "/" | "[" | "]" | "?" | "=" + | "{" | "}" | SP | HT SP = HT = LWS = [CRLF] 1*( SP | HT ) -TEXT = +TEXT = +CTL = -quoted-pair = "\" CHAR if(strchr("()<>@,;:\\\"/[]?={} \t", *ptr)) */ @@ -183,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<='A' && c<='Z' - || c>='a' && c<='Z' - || c>='0' && c<='9' - || strchr("!*+-/", c); + return (pa_isalnum((unsigned char)c) || strchr("!*+-/", c)); } inline bool addr_spec_soon(const char *src) { for(char c; (c=*src); src++) @@ -348,32 +375,33 @@ int cstr_to_string_body_block(String::La case String::L_FILE_SPEC: // tainted, untaint language: file [name] { - bool is1251=(info->charsets && info->charsets->source().NAME()=="WINDOWS-1251"); escape_fragment( - // Macintosh has problems with small Russian letter 'r' - if( is1251 && c=='\xF0' ) { - // fixing that letter for most common charset - to_char('p'); - } else // fallback to default - encode(need_file_encode, '_', c); + encode(need_file_encode, '_', c); ); } break; - case String::L_FILE_POST: + case String::L_URI: + // tainted, untaint language: uri { - escape_fragment(switch(c) { - case '\0': to_string("\\0"); break; - case '\\': to_string("\\\\"); break; - default: _default; break; - }); + 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_URI: case String::L_HTTP_HEADER: // tainted, untaint language: http-field-content-text - escape_fragment( - encode(need_uri_encode, '%', c); - ); + escape_fragment(switch(c) { + case '\n': + case '\r': to_string(" "); break; + default: _default; break; + }); break; case String::L_MAIL_HEADER: // tainted, untaint language: mail-header @@ -392,18 +420,18 @@ 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 && !mail_header_char_valid_within_Qencoded(c) + ( !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) { to_string("=?"); @@ -438,13 +466,18 @@ int cstr_to_string_body_block(String::La } break; case String::L_JS: + // quotes, < > & as \xXX: safe both inside