|
|
| version 1.146, 2009/04/18 22:49:47 | version 1.154, 2009/09/10 12:31:43 |
|---|---|
| Line 82 inline bool need_file_encode(unsigned ch | Line 82 inline bool need_file_encode(unsigned ch |
| } | } |
| inline bool need_uri_encode(unsigned char c){ | inline bool need_uri_encode(unsigned char c){ |
| if((c>='0') &&(c<='9') ||(c>='A') &&(c<='Z') ||(c>='a') &&(c<='z')) | if((c>='0') && (c<='9') || (c>='A') && (c<='Z') || (c>='a') && (c<='z')) |
| return false; | return false; |
| return !strchr("_-./", c); | return !strchr("_-./", c); |
| Line 99 inline bool need_regex_escape(unsigned c | Line 99 inline bool need_regex_escape(unsigned c |
| return strchr("\\^$.[]|()?*+{}-", c)!=0; | return strchr("\\^$.[]|()?*+{}-", c)!=0; |
| } | } |
| inline bool need_parser_code_escape(unsigned char c){ | |
| return strchr("^$;@()[]{}:#\"", c)!=0; | |
| } | |
| // String | // String |
| /* | /* |
| HTTP-header = field-name ":" [ field-value ] CRLF | HTTP-header = field-name ":" [ field-value ] CRLF |
| field-name = token | field-name = token |
| field-value = *( field-content | LWS ) | field-value = *( field-content | LWS ) |
| field-content = <the OCTETs making up the field-value | field-content = <the OCTETs making up the field-value |
| and consisting of either *TEXT or combinations | and consisting of either *TEXT or combinations |
| of token, tspecials, and quoted-string> | of token, tspecials, and quoted-string> |
| word = token | quoted-string | |
| token = 1*<any CHAR except CTLs or tspecials> | token = 1*<any CHAR except CTLs or tspecials> |
| word = token | quoted-string | |
| quoted-string = ( <"> *(qdtext | quoted-pair ) <"> ) | |
| qdtext = <any TEXT except <">> | |
| quoted-pair = "\" CHAR | |
| OCTET = <any 8-bit sequence of data> | |
| CHAR = <any US-ASCII character (octets 0 - 127)> | |
| tspecials = "(" | ")" | "<" | ">" | "@" | tspecials = "(" | ")" | "<" | ">" | "@" |
| | "," | ";" | ":" | "\" | <"> | | "," | ";" | ":" | "\" | <"> |
| | "/" | "[" | "]" | "?" | "=" | | "/" | "[" | "]" | "?" | "=" |
| | "{" | "}" | SP | HT | | "{" | "}" | SP | HT |
| SP = <US-ASCII SP, space (32)> | SP = <US-ASCII SP, space (32)> |
| HT = <US-ASCII HT, horizontal-tab (9)> | HT = <US-ASCII HT, horizontal-tab (9)> |
| LWS = [CRLF] 1*( SP | HT ) | LWS = [CRLF] 1*( SP | HT ) |
| TEXT = <any OCTET except CTLs, | TEXT = <any OCTET except CTLs, but including LWS> |
| but including LWS> | CTL = <any US-ASCII control character (octets 0 - 31) and DEL (127)> |
| quoted-pair = "\" CHAR | |
| if(strchr("()<>@,;:\\\"/[]?={} \t", *ptr)) | if(strchr("()<>@,;:\\\"/[]?={} \t", *ptr)) |
| */ | */ |
| Line 185 int append_fragment_nonoptimizing(char a | Line 190 int append_fragment_nonoptimizing(char a |
| or marking ALL pieces of it with a @a lang when @a forced to, | or marking ALL pieces of it with a @a lang when @a forced to, |
| and propagating OPTIMIZE language bit. | and propagating OPTIMIZE language bit. |
| */ | */ |
| String& String::append_to(String& dest, Language lang, bool forced) const { | String& String::append_to(String& dest, Language ilang, bool forced) const { |
| if(is_empty()) | if(is_empty()) |
| return dest; | return dest; |
| // first: fragment infos | // first: fragment infos |
| if(lang==L_PASS_APPENDED) // without language-change? | if(ilang==L_PASS_APPENDED) // without language-change? |
| dest.langs.appendHelper(dest.body, body, langs); | dest.langs.appendHelper(dest.body, langs, body); |
| else if(forced) //forcing passed lang? | else if(forced) //forcing passed lang? |
| dest.langs.append(dest.body, lang, length()); | dest.langs.appendHelper(dest.body, ilang, body); |
| else { | else { |
| Append_fragment_info info={lang, &dest.langs, dest.body.length()}; | if(langs.opt.is_not_just_lang){ |
| langs.for_each(body, lang&L_OPTIMIZE_BIT? | Append_fragment_info info={ilang, &dest.langs, dest.body.length()}; |
| append_fragment_optimizing | langs.for_each(body, ilang&L_OPTIMIZE_BIT? |
| :append_fragment_nonoptimizing, &info); | append_fragment_optimizing |
| :append_fragment_nonoptimizing, &info); | |
| } else { | |
| Language lang=langs.opt.lang; | |
| // see append_fragment_* for explanation | |
| if(ilang&L_OPTIMIZE_BIT){ | |
| dest.langs.appendHelper(dest.body, | |
| lang==String::L_TAINTED? | |
| ilang | |
| :lang==String::L_CLEAN? | |
| (String::Language)(String::L_CLEAN|String::L_OPTIMIZE_BIT) | |
| :lang, | |
| body); | |
| } else { | |
| dest.langs.appendHelper(dest.body, lang==String::L_TAINTED ? ilang:lang, body); | |
| } | |
| } | |
| } | } |
| // next: letters | // next: letters |
| Line 301 struct Cstr_to_string_body_block_info { | Line 322 struct Cstr_to_string_body_block_info { |
| const char* exception; | const char* exception; |
| }; | }; |
| #endif | #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; | bool& whitespace=info->whitespace; |
| size_t fragment_end=info->fragment_begin+fragment_length; | 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; | bool optimize=(to_lang & String::L_OPTIMIZE_BIT)!=0; |
| if(!optimize) | if(!optimize) |
| whitespace=false; | whitespace=false; |
| Line 328 int cstr_to_string_body_block(char alang | Line 349 int cstr_to_string_body_block(char alang |
| case String::L_FILE_SPEC: | case String::L_FILE_SPEC: |
| // tainted, untaint language: file [name] | // tainted, untaint language: file [name] |
| { | { |
| bool is1251=(info->charsets && info->charsets->source().NAME()=="WINDOWS-1251"); | |
| escape_fragment( | escape_fragment( |
| // Macintosh has problems with small Russian letter 'r' | encode(need_file_encode, '_', c); |
| if( is1251 && c=='\xF0' ) { | |
| // fixing that letter for most common charset | |
| to_char('p'); | |
| } else // fallback to default | |
| encode(need_file_encode, '_', c); | |
| ); | ); |
| } | } |
| break; | break; |
| Line 349 int cstr_to_string_body_block(char alang | Line 364 int cstr_to_string_body_block(char alang |
| } | } |
| break; | break; |
| case String::L_URI: | 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: | case String::L_HTTP_HEADER: |
| // tainted, untaint language: http-field-content-text | // tainted, untaint language: http-field-content-text |
| // the same as L_URI BUT not transcoded into $response:charset before encoding | |
| escape_fragment( | escape_fragment( |
| encode(need_uri_encode, '%', c); | encode(need_uri_encode, '%', c); |
| ); | ); |
| Line 503 int cstr_to_string_body_block(char alang | Line 501 int cstr_to_string_body_block(char alang |
| pa_CORD_pos_advance(info->pos, fragment_length); | pa_CORD_pos_advance(info->pos, fragment_length); |
| String::C output(fragment_str, fragment_length); | String::C output(fragment_str, fragment_length); |
| output=Charset::escape(output, info->charsets->source()); | output=Charset::escape(output, info->charsets->client()); |
| //throw Exception(0, 0, output); | //throw Exception(0, 0, output); |
| to_string(output); | to_string(output); |
| } | } |
| break; | break; |
| case String::L_PARSER_CODE: | |
| // for auto-untaint in process | |
| escape_fragment( | |
| if(need_parser_code_escape(c)) | |
| to_char('^'); | |
| _default; | |
| ); | |
| break; | |
| default: | default: |
| SAPI::abort("unknown untaint language #%d", | SAPI::abort("unknown untaint language #%d", |
| static_cast<int>(to_lang)); // should never | static_cast<int>(to_lang)); // should never |
| Line 521 int cstr_to_string_body_block(char alang | Line 527 int cstr_to_string_body_block(char alang |
| } | } |
| String::Body String::cstr_to_string_body(Language lang, | String::Body String::cstr_to_string_body_taint(Language lang, SQL_Connection* connection, const Request_charsets *charsets) const { |
| SQL_Connection* connection, | if(is_empty()) |
| const Request_charsets *charsets) const { | 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()) | if(is_empty()) |
| return String::Body(); | return String::Body(); |
| Line 541 String::Body String::cstr_to_string_body | Line 588 String::Body String::cstr_to_string_body |
| info.exception=0; | info.exception=0; |
| info.whitespace=true; | info.whitespace=true; |
| langs.for_each(body, cstr_to_string_body_block, &info); | langs.for_each(body, cstr_to_string_body_block_untaint, &info); |
| if(info.exception){ | |
| if(info.exception) | |
| throw Exception(0, | throw Exception(0, |
| 0, | 0, |
| info.exception); | 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)); | return String::Body(CORD_ec_to_cord(info.result)); |
| } | } |