--- parser3/src/main/untaint.C 2005/11/22 15:09:10 1.136 +++ 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: 2005/11/22 15:09:10 $"; +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,48 +51,68 @@ extern "C" { // author forgot to do that } \ } - -#define escape(action) \ +#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; + , 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_uri_encode(unsigned char c){ + return !(pa_isalnum(c) || strchr("_-./*", c)); } -inline bool need_http_header_encode(unsigned char c){ - if(strchr(" , :", c)) - return false; - return need_uri_encode(c); -} inline bool need_regex_escape(unsigned char c){ - return strchr("\\^$.[]|()?*+{}", c)!=0; + return strchr("\\^$.[]|()?*+{}-", c)!=0; +} + +inline bool need_parser_code_escape(unsigned char c){ + return strchr("^$;@()[]{}:#\"", c)!=0; } // String @@ -101,34 +120,35 @@ inline bool need_regex_escape(unsigned c /* 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)) */ @@ -176,27 +196,60 @@ 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++) @@ -295,16 +345,16 @@ struct Cstr_to_string_body_block_info { CORD_pos pos; size_t fragment_begin; bool whitespace; + const char* exception; }; #endif -int cstr_to_string_body_block(char alang, size_t fragment_length, Cstr_to_string_body_block_info* info) { - const String::Language fragment_lang=(String::Language)(unsigned char)alang; + +// @todo: replace info->body->mid with something that uses info->pos +int cstr_to_string_body_block(String::Language to_lang, size_t fragment_length, Cstr_to_string_body_block_info* info) { bool& whitespace=info->whitespace; size_t fragment_end=info->fragment_begin+fragment_length; - //fprintf(stderr, "%d, %d\n", fragment.lang, fragment.length); - + //fprintf(stderr, "%d, %d =%s=\n", to_lang, fragment_length, info->body->cstr()); - String::Language to_lang=info->lang==String::L_UNSPECIFIED?fragment_lang:info->lang; bool optimize=(to_lang & String::L_OPTIMIZE_BIT)!=0; if(!optimize) whitespace=false; @@ -324,14 +374,11 @@ int cstr_to_string_body_block(char alang break; case String::L_FILE_SPEC: // tainted, untaint language: file [name] - escape( - // Macintosh has problems with small Russian letter 'r' - if( c=='\xF0' && info->charsets && info->charsets->source().NAME()=="WINDOWS-1251" ) { - // fixing that letter for most common charset - to_char('p'); - } else // fallback to default + { + escape_fragment( encode(need_file_encode, '_', c); - ); + ); + } break; case String::L_URI: // tainted, untaint language: uri @@ -340,21 +387,21 @@ int cstr_to_string_body_block(char alang // 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()); + if(info->charsets) + output=Charset::transcode(output, info->charsets->source(), info->charsets->client()); char c; - for(const char* src=output.str; (c=*src++); ) + for(const char* src=output.str; (c=*src++); ) encode(need_uri_encode, '%', c); } break; case String::L_HTTP_HEADER: // tainted, untaint language: http-field-content-text - escape( - 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 @@ -373,16 +420,18 @@ int cstr_to_string_body_block(char alang bool email=false; uchar c; - for(const char* src=mail_ptr; (c=(uchar)*src++); ) { - if(to_quoted_printable && (c==',' || c == '"' || addr_spec_soon(src-1/*position to 'c'*/))) { + 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))) { 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("=?"); @@ -410,26 +459,34 @@ int cstr_to_string_body_block(char alang pa_CORD_pos_advance(info->pos, fragment_length); to_string(info->connection->quote(fragment_str, fragment_length)); - } else - throw Exception(0, - 0, - "untaint in SQL language failed - no connection specified"); + } else { + info->exception="untaint in SQL language failed - no connection specified"; + info->fragment_begin=fragment_end; + return 1; // stop processing. can't throw exception here + } break; case String::L_JS: - escape(switch(c) { - case '"': to_string("\\\""); break; - case '\'': to_string("\\'"); break; + // quotes, < > & as \xXX: safe both inside