--- parser3/src/main/untaint.C 2003/11/20 17:15:12 1.124 +++ parser3/src/main/untaint.C 2005/11/22 15:09:10 1.136 @@ -1,11 +1,11 @@ /** @file Parser: String class part: untaint mechanizm. - Copyright(c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com) + Copyright(c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char * const IDENT_UNTAINT_C="$Date: 2003/11/20 17:15:12 $"; +static const char * const IDENT_UNTAINT_C="$Date: 2005/11/22 15:09:10 $"; #include "pa_string.h" @@ -23,15 +23,10 @@ extern "C" { // author forgot to do that #include "ec.h" } -#define PA_SQL - -#ifdef PA_SQL #include "pa_sql_connection.h" -#endif // defines - #undef CORD_ec_append // redefining to intercept flushes and implement whitespace optimization // of all consequent white space chars leaving only first one @@ -39,7 +34,7 @@ extern "C" { // author forgot to do that { \ bool skip=false; \ if(optimize) switch(c) { \ - case ' ': case '\r': case '\n': case '\t': \ + case ' ': case '\n': case '\t': \ if(whitespace) \ skip=true; /*skipping subsequent*/ \ else \ @@ -63,7 +58,7 @@ extern "C" { // author forgot to do that char c=CORD_pos_fetch(info->pos); \ action \ } -#define _default default: CORD_ec_append(info->result, c); break +#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"; \ @@ -72,8 +67,8 @@ extern "C" { // author forgot to do that CORD_ec_append(info->result, hex[((unsigned char)c)%0x10]); \ } else \ CORD_ec_append(info->result, otherwise); -#define to_char(c) CORD_ec_append(info->result, c) -#define to_string(s) CORD_ec_append_cord(info->result, s) +#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; } inline bool need_file_encode(unsigned char c){ // russian letters and space ENABLED @@ -97,6 +92,9 @@ inline bool need_http_header_encode(unsi return need_uri_encode(c); } +inline bool need_regex_escape(unsigned char c){ + return strchr("\\^$.[]|()?*+{}", c)!=0; +} // String @@ -258,6 +256,7 @@ inline bool addr_spec_soon(const char *s inline bool mail_header_nonspace_char(char c) { return c != 0x20; } + inline void ec_append(CORD_ec& result, bool& optimize, bool& whitespace, CORD_pos pos, size_t size) { while(size--) { CORD_ec_append(result, CORD_pos_fetch(pos)); @@ -266,12 +265,12 @@ inline void ec_append(CORD_ec& result, b } inline void pa_CORD_pos_advance(CORD_pos pos, size_t n) { while(true) { - size_t avail=CORD_pos_chars_left(pos); - if(avail==0) { + long avail=CORD_pos_chars_left(pos); + if(avail<=0) { CORD_next(pos); if(!--n) break; - } else if(avail=n @@ -375,12 +374,12 @@ int cstr_to_string_body_block(char alang bool email=false; uchar c; for(const char* src=mail_ptr; (c=(uchar)*src++); ) { - //RFC + An 'encoded-word' MUST NOT appear in any portion of an 'addr-spec'. - if(to_quoted_printable && (c==',' || addr_spec_soon(src) || c == '"')) { + if(to_quoted_printable && (c==',' || c == '"' || addr_spec_soon(src-1/*position to 'c'*/))) { 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) @@ -403,15 +402,6 @@ int cstr_to_string_body_block(char alang } else ec_append(info->result, optimize, whitespace, info->pos, fragment_length); break; - case String::L_TABLE: - // tainted, untaint language: table - escape(switch(c) { - case '\t': to_char(' '); break; - case '\n': to_char(' '); break; - _default; - }); - break; -#ifdef PA_SQL case String::L_SQL: // tainted, untaint language: sql if(info->connection) { @@ -425,7 +415,6 @@ int cstr_to_string_body_block(char alang 0, "untaint in SQL language failed - no connection specified"); break; -#endif case String::L_JS: escape(switch(c) { case '"': to_string("\\\""); break; @@ -433,17 +422,39 @@ int cstr_to_string_body_block(char alang case '\n': to_string("\\n"); break; case '\\': to_string("\\\\"); break; case '\xFF': to_string("\\\xFF"); break; - _default; + default: _default; break; }); break; case String::L_XML: + // [2] Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] escape(switch(c) { + case '\x9': + case '\xA': + case '\xD': // this is usually removed on input + _default; + break; case '&': to_string("&"); break; case '>': to_string(">"); break; case '<': to_string("<"); break; case '"': to_string("""); break; case '\'': to_string("'"); break; - _default; + default: + if(((unsigned char)c)<0x20) { + // fixing it, so that libxml would not result + // in fatal error parsing text + // though it really violates standard. + // to indicate there were an error + // replace bad char not to it's code, + // which we can do, + // but rather to '!' to show that input were actually + // invalid. + // life: shows that MSIE can somehow garble form values + // so that they contain these chars. + to_char('!'); + } else { + _default; + } + break; }); break; case String::L_HTML: @@ -452,12 +463,19 @@ int cstr_to_string_body_block(char alang case '>': to_string(">"); break; case '<': to_string("<"); break; case '"': to_string("""); break; - _default; + default: _default; break; }); break; + case String::L_REGEX: + // tainted, untaint language: regex + escape( + if(need_regex_escape(c)) + to_char('\\') + _default; + ); + break; default: - assert(!"should never"); - SAPI::die("unknown untaint language #%d", + SAPI::abort("unknown untaint language #%d", static_cast(to_lang)); // should never break; // never } @@ -471,6 +489,8 @@ int cstr_to_string_body_block(char alang String::Body String::cstr_to_string_body(Language lang, SQL_Connection* connection, const Request_charsets *charsets) const { + if(is_empty()) + return String::Body(); Cstr_to_string_body_block_info info; // input @@ -485,8 +505,6 @@ String::Body String::cstr_to_string_body info.fragment_begin=0; info.whitespace=true; - if(!is_empty()) - langs.for_each(body, cstr_to_string_body_block, &info); - + langs.for_each(body, cstr_to_string_body_block, &info); return String::Body(CORD_ec_to_cord(info.result)); }