|
|
| version 1.156, 2010/08/27 02:53:24 | version 1.165, 2013/10/14 21:17:38 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: String class part: untaint mechanizm. | Parser: String class part: untaint mechanizm. |
| Copyright(c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| */ | */ |
| static const char * const IDENT_UNTAINT_C="$Date$"; | volatile const char * IDENT_UNTAINT_C="$Id$"; |
| #include "pa_string.h" | #include "pa_string.h" |
| Line 61 extern "C" { // author forgot to do that | Line 61 extern "C" { // author forgot to do that |
| #define _default CORD_ec_append(info->result, c) | #define _default CORD_ec_append(info->result, c) |
| #define encode(need_encode_func, prefix, otherwise) \ | #define encode(need_encode_func, prefix, otherwise) \ |
| if(need_encode_func(c)) { \ | if(need_encode_func(c)) { \ |
| static const char* hex="0123456789ABCDEF"; \ | |
| CORD_ec_append(info->result, prefix); \ | CORD_ec_append(info->result, prefix); \ |
| CORD_ec_append(info->result, hex[((unsigned char)c)/0x10]); \ | CORD_ec_append(info->result, hex_digits[((unsigned char)c) >> 4]); \ |
| CORD_ec_append(info->result, hex[((unsigned char)c)%0x10]); \ | CORD_ec_append(info->result, hex_digits[((unsigned char)c) & 0x0F]); \ |
| } else \ | } else \ |
| CORD_ec_append(info->result, otherwise); | CORD_ec_append(info->result, otherwise); |
| #define to_char(c) { CORD_ec_append(info->result, c); whitespace=false; } | #define to_char(c) { CORD_ec_append(info->result, c); whitespace=false; } |
| Line 74 inline bool need_file_encode(unsigned ch | Line 73 inline bool need_file_encode(unsigned ch |
| // russian letters and space ENABLED | // russian letters and space ENABLED |
| // encoding only these... | // encoding only these... |
| return strchr( | return strchr( |
| "*?'\"<>|" | "*?\"<>|" |
| #ifndef WIN32 | #ifndef WIN32 |
| ":\\" | ":\\" |
| #endif | #endif |
| Line 82 inline bool need_file_encode(unsigned ch | Line 81 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')) | return !(pa_isalnum(c) || strchr("_-./*", c)); |
| return false; | |
| return !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){ | inline bool need_regex_escape(unsigned char c){ |
| Line 258 RFC | Line 247 RFC |
| and without "_", or in would mean 0x20 | and without "_", or in would mean 0x20 |
| */ | */ |
| inline bool mail_header_char_valid_within_Qencoded(char c) { | inline bool mail_header_char_valid_within_Qencoded(char c) { |
| return c>='A' && c<='Z' | return (pa_isalnum((unsigned char)c) || strchr("!*+-/", c)); |
| || c>='a' && c<='Z' | |
| || c>='0' && c<='9' | |
| || strchr("!*+-/", c); | |
| } | } |
| inline bool addr_spec_soon(const char *src) { | inline bool addr_spec_soon(const char *src) { |
| for(char c; (c=*src); src++) | for(char c; (c=*src); src++) |
| Line 354 int cstr_to_string_body_block(String::La | Line 340 int cstr_to_string_body_block(String::La |
| ); | ); |
| } | } |
| break; | break; |
| case String::L_FILE_POST: | |
| { | |
| escape_fragment(switch(c) { | |
| case '\0': to_string("\\0"); break; | |
| case '\\': to_string("\\\\"); break; | |
| default: _default; break; | |
| }); | |
| } | |
| break; | |
| case String::L_URI: | case String::L_URI: |
| // tainted, untaint language: uri | // tainted, untaint language: uri |
| { | { |
| Line 382 int cstr_to_string_body_block(String::La | Line 359 int cstr_to_string_body_block(String::La |
| break; | 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 |
| escape_fragment( | escape_fragment(switch(c) { |
| encode(need_uri_encode, '%', c); | case '\n': |
| ); | case '\r': to_string(" "); break; |
| default: _default; break; | |
| }); | |
| break; | break; |
| case String::L_MAIL_HEADER: | case String::L_MAIL_HEADER: |
| // tainted, untaint language: mail-header | // tainted, untaint language: mail-header |
| Line 513 int cstr_to_string_body_block(String::La | Line 492 int cstr_to_string_body_block(String::La |
| // tainted, untaint language: json <http://json.org/> | // tainted, untaint language: json <http://json.org/> |
| // escape '"' '\' '/' '\n' '\t' '\r' '\b' '\f' chars and escape chars as \uXXXX if output charset != UTF-8 | // escape '"' '\' '/' '\n' '\t' '\r' '\b' '\f' chars and escape chars as \uXXXX if output charset != UTF-8 |
| { | { |
| if(info->charsets->client().isUTF8()){ | if(info->charsets==NULL || info->charsets->client().isUTF8()){ |
| // escaping to \uXXXX is not needed | // escaping to \uXXXX is not needed |
| escape_fragment(switch(c) { | escape_fragment(switch(c) { |
| case '\n': to_string("\\n"); break; | case '\n': to_string("\\n"); break; |
| Line 524 int cstr_to_string_body_block(String::La | Line 503 int cstr_to_string_body_block(String::La |
| case '\r': to_string("\\r"); break; | case '\r': to_string("\\r"); break; |
| case '\b': to_string("\\b"); break; | case '\b': to_string("\\b"); break; |
| case '\f': to_string("\\f"); break; | case '\f': to_string("\\f"); break; |
| default : _default; break; | default: |
| if((unsigned char)c < 0x20){ | |
| to_string("\\u00"); | |
| to_char(hex_digits[((unsigned char)c) >> 4]); | |
| to_char(hex_digits[((unsigned char)c) & 0x0F]); | |
| } else { | |
| _default; | |
| } | |
| break; | |
| }); | }); |
| } else { | } else { |
| const char *fragment_str=info->body->mid(info->fragment_begin, fragment_length).cstr(); | const char *fragment_str=info->body->mid(info->fragment_begin, fragment_length).cstr(); |
| Line 539 int cstr_to_string_body_block(String::La | Line 526 int cstr_to_string_body_block(String::La |
| break; | break; |
| case String::L_HTTP_COOKIE: | case String::L_HTTP_COOKIE: |
| // tainted, untaint language: cookie (3.3.0 and higher: %uXXXX in UTF-8) | // tainted, untaint language: cookie (3.3.0 and higher: %uXXXX in UTF-8) |
| { | if(info->charsets) { |
| const char *fragment_str=info->body->mid(info->fragment_begin, fragment_length).cstr(); | const char *fragment_str=info->body->mid(info->fragment_begin, fragment_length).cstr(); |
| // skip source [we use recoded version] | // skip source [we use recoded version] |
| 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->source()); |
| //throw Exception(0, 0, output); | |
| to_string(output); | to_string(output); |
| } else | |
| } | ec_append(info->result, optimize, whitespace, info->pos, fragment_length); |
| break; | break; |
| case String::L_PARSER_CODE: | case String::L_PARSER_CODE: |
| // for auto-untaint in process | // for auto-untaint in process |
| Line 596 String::Body String::cstr_to_string_body | Line 582 String::Body String::cstr_to_string_body |
| 0, | 0, |
| info.exception); | info.exception); |
| return String::Body(CORD_ec_to_cord(info.result), info.fragment_begin); | 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){ | int cstr_to_string_body_block_untaint(char alang, size_t fragment_length, Cstr_to_string_body_block_info* info){ |
| Line 639 String::Body String::cstr_to_string_body | Line 625 String::Body String::cstr_to_string_body |
| 0, | 0, |
| info.exception); | info.exception); |
| return String::Body(CORD_ec_to_cord(info.result), info.fragment_begin); | return String::Body(CORD_ec_to_cord(info.result)); |
| } | } |
| const char* String::untaint_and_transcode_cstr(Language lang, const Request_charsets *charsets) const { | const char* String::untaint_and_transcode_cstr(Language lang, const Request_charsets *charsets) const { |