--- parser3/src/main/untaint.C 2001/11/21 10:02:13 1.80 +++ parser3/src/main/untaint.C 2001/12/15 21:37:43 1.85 @@ -4,7 +4,7 @@ Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com) Author: Alexander Petrosyan (http://paf.design.ru) - $Id: untaint.C,v 1.80 2001/11/21 10:02:13 paf Exp $ + $Id: untaint.C,v 1.85 2001/12/15 21:37:43 paf Exp $ */ #include "pa_pool.h" @@ -16,6 +16,7 @@ #include "pa_sql_connection.h" #include "pa_dictionary.h" #include "pa_common.h" +#include "pa_charset.h" #define escape(action) \ { \ @@ -25,7 +26,6 @@ } #define _default default: *dest++=*src; break #define encode(need_encode_func, prefix) \ - default: \ if(need_encode_func(*src)) { \ static const char *hex="0123456789ABCDEF"; \ char chunk[3]={prefix}; \ @@ -41,14 +41,20 @@ dest+=bsize; \ inline bool need_file_encode(unsigned char c){ + // theoretical problem with, for instance, "_2B" and "." fragments, + // they would yield the same + // because need_file_encode('_')=false + // but we need to delete such files somehow, getting names from ^index + if((c>='0') &&(c<='9') ||(c>='A') &&(c<='Z') ||(c>='a') &&(c<='z')) return false; return !strchr( + "_./()-" #ifdef WIN32 ":\\~" #endif - "./()_-", c); + , c); } inline bool need_uri_encode(unsigned char c){ if((c>='0') &&(c<='9') ||(c>='A') &&(c<='Z') ||(c>='a') &&(c<='z')) @@ -84,8 +90,7 @@ static const char * String_Untaint_lang_ "SQL", ///< ^table:sql body "JS", ///< JavaScript code "XML", ///< ^dom:set xml - "HTML", ///< HTML code (for editing) - "UHTML", ///< HTML code with USER chars + "HTML" ///< HTML code (for editing) }; @@ -195,7 +200,7 @@ size_t String::cstr_bufsize(Untaint_lang break; case UL_URI: // tainted, untaint language: uri - dest+=row->item.size*3/* worst: Z->%XX */; + dest+=row->item.size*6*3/* worst utf8 x worst Z->%XX */; break; case UL_HTTP_HEADER: // tainted, untaint language: http-field-content-text @@ -286,23 +291,31 @@ char *String::store_to(char *dest, Untai break; case UL_FILE_SPEC: // tainted, untaint language: file [name] - escape(switch(*src) { - case ' ': to_char('_'); break; - encode(need_file_encode, '+'); - }); + escape( + encode(need_file_encode, '_'); + ); break; case UL_URI: // tainted, untaint language: uri - escape(switch(*src) { - case ' ': to_char('+'); break; - encode(need_uri_encode, '%'); - }); + const void *client_ptr; + size_t client_size; + Charset::transcode(pool(), + pool().get_source_charset(), row->item.ptr, row->item.size, + pool().get_client_charset(), client_ptr, client_size); + { + const char *src=(const char *)client_ptr; + for(int size=client_size; size--; src++) + switch(*src) { + case ' ': to_char('+'); break; + default: encode(need_uri_encode, '%'); + }; + } break; case UL_HTTP_HEADER: // tainted, untaint language: http-field-content-text escape(switch(*src) { case ' ': to_char('+'); break; - encode(need_uri_encode, '%'); + default: encode(need_uri_encode, '%'); }); break; case UL_MAIL_HEADER: @@ -378,9 +391,8 @@ char *String::store_to(char *dest, Untai default: throw Exception(0, 0, this, - "unknown untaint language #%d of %d piece", - static_cast(row->item.lang), - i); // never + "unknown untaint language #%d", + static_cast(row->item.lang)); // sould never break; // never } @@ -413,6 +425,7 @@ break2: } char *String::cstr_debug_origins() const { + //_asm int 3; char *result=(char *)malloc(size()+used_rows()*MAX_STRING*2); char *dest=result; @@ -431,8 +444,16 @@ char *String::cstr_debug_origins() const else dest+=sprintf(dest, ""); #endif - dest+=sprintf(dest, "#%s: ", - String_Untaint_lang_name[row->item.lang]); + uchar show_lang=row->item.lang & ~UL_OPTIMIZE_BIT; + if(show_lang>=sizeof(String_Untaint_lang_name)/sizeof(String_Untaint_lang_name[0])) + throw Exception(0, 0, + this, + "unknown untaint language #%d", + static_cast(show_lang)); // sould never + + dest+=sprintf(dest, "#%s%s: ", + String_Untaint_lang_name[show_lang], + row->item.lang & UL_OPTIMIZE_BIT?".O":""); char *dest_after_origins=dest; memcpy(dest, row->item.ptr, row->item.size);