--- parser3/src/main/untaint.C 2009/05/23 08:12:56 1.147 +++ parser3/src/main/untaint.C 2009/08/21 08:38:55 1.151 @@ -5,7 +5,7 @@ Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char * const IDENT_UNTAINT_C="$Date: 2009/05/23 08:12:56 $"; +static const char * const IDENT_UNTAINT_C="$Date: 2009/08/21 08:38:55 $"; #include "pa_string.h" @@ -99,6 +99,10 @@ inline bool need_regex_escape(unsigned c return strchr("\\^$.[]|()?*+{}-", c)!=0; } +inline bool need_parser_code_escape(unsigned char c){ + return strchr("^$;@()[]{}:#\"", c)!=0; +} + // String /* @@ -317,13 +321,13 @@ struct Cstr_to_string_body_block_info { 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 =%s=\n", fragment_lang, fragment_length, info->body->cstr()); + //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; @@ -365,25 +369,8 @@ int cstr_to_string_body_block(char alang } break; case String::L_URI: - // 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: // tainted, untaint language: http-field-content-text - // the same as L_URI BUT not transcoded into $response:charset before encoding escape_fragment( encode(need_uri_encode, '%', c); ); @@ -525,6 +512,14 @@ int cstr_to_string_body_block(char alang } break; + case String::L_PARSER_CODE: + // for auto-untaint in process + escape_fragment( + if(need_parser_code_escape(c)) + to_char('^'); + _default; + ); + break; default: SAPI::abort("unknown untaint language #%d", static_cast(to_lang)); // should never @@ -537,9 +532,50 @@ 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 { +String::Body String::cstr_to_string_body_taint(Language lang, SQL_Connection* connection, const Request_charsets *charsets) const { + if(is_empty()) + return String::Body(); + + Cstr_to_string_body_block_info info; + // input + info.lang=lang; + info.connection=connection; + info.charsets=charsets; + info.body=&body; + // output + CORD_ec_init(info.result); + // private + body.set_pos(info.pos, 0); + info.fragment_begin=0; + info.exception=0; + info.whitespace=true; + + cstr_to_string_body_block(lang, length(), &info); + + if(info.exception) + throw Exception(0, + 0, + info.exception); + + return String::Body(CORD_ec_to_cord(info.result)); +} + +int cstr_to_string_body_block_untaint(char alang, size_t fragment_length, Cstr_to_string_body_block_info* info){ + const String::Language lang=(String::Language)(unsigned char)alang; + // see append_fragment_* for explanation + if(info->lang&String::L_OPTIMIZE_BIT) + return cstr_to_string_body_block( + lang==String::L_TAINTED? + info->lang + :lang==String::L_CLEAN? + (String::Language)(String::L_CLEAN|String::L_OPTIMIZE_BIT) + :lang, + fragment_length, info); + else + return cstr_to_string_body_block(lang==String::L_TAINTED ? info->lang:lang, fragment_length, info); +} + +String::Body String::cstr_to_string_body_untaint(Language lang, SQL_Connection* connection, const Request_charsets *charsets) const { if(is_empty()) return String::Body(); @@ -557,12 +593,71 @@ String::Body String::cstr_to_string_body info.exception=0; info.whitespace=true; - langs.for_each(body, cstr_to_string_body_block, &info); - if(info.exception){ + langs.for_each(body, cstr_to_string_body_block_untaint, &info); + + if(info.exception) throw Exception(0, 0, info.exception); + + return String::Body(CORD_ec_to_cord(info.result)); +} + +const char* String::transcode_and_untaint_cstr(Language lang, const Request_charsets *charsets) const { + if(charsets && &charsets->source() != &charsets->client()){ + return cstr_to_string_body_transcode_and_untaint(lang, charsets).cstr(); + } else { + return cstr_to_string_body_untaint(lang, 0, charsets).cstr(); } +} + +int cstr_to_string_body_block_transcode_and_untaint(char alang, size_t fragment_length, Cstr_to_string_body_block_info* info){ + String::C output_c=Charset::transcode( + String::C(info->body->mid(info->fragment_begin, fragment_length).cstr(), fragment_length), + info->charsets->source(), + info->charsets->client() + ); + String::Body output_body=String::Body(output_c); + + size_t fragment_end=info->fragment_begin+fragment_length; + const String::Body* info_body=info->body; + + info->fragment_begin=0; + info->body=&output_body; + info->body->set_pos(info->pos, 0); + + int result=cstr_to_string_body_block_untaint(alang, output_c.length, info); + + info->fragment_begin=fragment_end; + info->body=info_body; + + return result; +} + +String::Body String::cstr_to_string_body_transcode_and_untaint(Language lang, const Request_charsets *charsets) const { + if(is_empty()) + return String::Body(); + + Cstr_to_string_body_block_info info; + // input + info.lang=lang; + info.connection=0; + info.charsets=charsets; + info.body=&body; + // output + CORD_ec_init(info.result); + // private + body.set_pos(info.pos, 0); + info.fragment_begin=0; + info.exception=0; + info.whitespace=true; + + langs.for_each(body, cstr_to_string_body_block_transcode_and_untaint, &info); + + if(info.exception) + throw Exception(0, + 0, + info.exception); return String::Body(CORD_ec_to_cord(info.result)); }