Diff for /parser3/src/main/untaint.C between versions 1.122.2.2 and 1.133

version 1.122.2.2, 2004/01/22 08:28:20 version 1.133, 2004/07/01 07:20:10
Line 1 Line 1
 /** @file  /** @file
         Parser: String class part: untaint mechanizm.          Parser: String class part: untaint mechanizm.
   
         Copyright(c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)          Copyright(c) 2001-2004 ArtLebedev Group (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* IDENT_UNTAINT_C="$Date$";  static const char * const IDENT_UNTAINT_C="$Date$";
   
   
 #include "pa_string.h"  #include "pa_string.h"
Line 23  extern "C" { // author forgot to do that Line 23  extern "C" { // author forgot to do that
 #include "ec.h"  #include "ec.h"
 }  }
   
 #define PA_SQL  
   
 #ifdef PA_SQL  
 #include "pa_sql_connection.h"  #include "pa_sql_connection.h"
 #endif  
   
 // defines  // defines
   
   
 #undef CORD_ec_append  #undef CORD_ec_append
 // redefining to intercept flushes and implement whitespace optimization  // redefining to intercept flushes and implement whitespace optimization
 // of all consequent white space chars leaving only first one  // of all consequent white space chars leaving only first one
Line 39  extern "C" { // author forgot to do that Line 34  extern "C" { // author forgot to do that
     {  \      {  \
         bool skip=false; \          bool skip=false; \
         if(optimize) switch(c) { \          if(optimize) switch(c) { \
         case ' ': case '\r': case '\n': case '\t': \          case ' ': case '\n': case '\t': \
                 if(whitespace) \                  if(whitespace) \
                         skip=true; /*skipping subsequent*/ \                          skip=true; /*skipping subsequent*/ \
                 else \                  else \
Line 63  extern "C" { // author forgot to do that Line 58  extern "C" { // author forgot to do that
                 char c=CORD_pos_fetch(info->pos); \                  char c=CORD_pos_fetch(info->pos); \
                 action \                  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)  \  #define encode(need_encode_func, prefix, otherwise)  \
         if(need_encode_func(c)) { \          if(need_encode_func(c)) { \
                 static const char* hex="0123456789ABCDEF"; \                  static const char* hex="0123456789ABCDEF"; \
Line 98  inline bool need_http_header_encode(unsi Line 93  inline bool need_http_header_encode(unsi
         return need_uri_encode(c);          return need_uri_encode(c);
 }  }
   
 //  
   
 static const char*  String_Untaint_lang_name[]={  
         "U", ///< zero value handy for hash lookup @see untaint_lang_name2enum  
         "C", ///< clean  
         "T",  ///< tainted, untaint language as assigned later   
         // untaint languages. assigned by ^untaint[lang]{...}  
         "P",  
                 /**<  
                         leave language built into string being appended.  
                         just a flag, that value not stored  
                 */  
         "A",     ///< leave all characters intact  
         "F", ///< file specification  
         "H",    ///< ext in HTTP response header  
         "M",    ///< text in mail header  
         "URI",       ///< text in uri  
         "T",     ///< ^table:set body  
         "SQL",       ///< ^table:sql body  
         "JS",        ///< JavaScript code  
         "XML",          ///< ^dom:set xml  
         "HTML"      ///< HTML code (for editing)  
 };  
   
   
 // String  // String
   
 /*  /*
   
 HTTP-header    = field-name ":" [ field-value ] CRLF  HTTP-header    = field-name ":" [ field-value ] CRLF
   
        field-name     = token         field-name     = token
Line 268  inline bool mail_header_char_valid_withi Line 237  inline bool mail_header_char_valid_withi
                 || strchr("!*+-/", c);                  || 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++)
                 if(c=='<')                  if(c=='<')
                         return true;                          return true;
                 else if(!(c==' ' || c=='\t'))                  else if(!(c==' ' || c=='\t'))
Line 284  inline bool addr_spec_soon(const char *s Line 253  inline bool addr_spec_soon(const char *s
 inline bool mail_header_nonspace_char(char c) {  inline bool mail_header_nonspace_char(char c) {
         return c != 0x20;          return c != 0x20;
 }  }
   
 inline void ec_append(CORD_ec& result, bool& optimize, bool& whitespace, CORD_pos pos, size_t size) {  inline void ec_append(CORD_ec& result, bool& optimize, bool& whitespace, CORD_pos pos, size_t size) {
         while(size--) {          while(size--) {
                 CORD_ec_append(result, CORD_pos_fetch(pos));                  CORD_ec_append(result, CORD_pos_fetch(pos));
Line 292  inline void ec_append(CORD_ec& result, b Line 262  inline void ec_append(CORD_ec& result, b
 }  }
 inline void pa_CORD_pos_advance(CORD_pos pos, size_t n) {  inline void pa_CORD_pos_advance(CORD_pos pos, size_t n) {
         while(true) {          while(true) {
                 size_t avail=CORD_pos_chars_left(pos);                  long avail=CORD_pos_chars_left(pos);
                 if(avail==0) {                  if(avail<=0) {
                         CORD_next(pos);                          CORD_next(pos);
                         if(!--n)                          if(!--n)
                                 break;                                  break;
                 } else if(avail<n) {                  } else if((size_t)avail<n) {
                         CORD_pos_advance(pos, avail);                          CORD_pos_advance(pos, avail);
                         n-=avail;                          n-=avail;
                 } else { // avail>=n                  } else { // avail>=n
Line 373  int cstr_to_string_body_block(char alang Line 343  int cstr_to_string_body_block(char alang
                                         info->charsets->client());                                          info->charsets->client());
   
                         char c;                          char c;
                         for(const char* src=output.str; c=*src++; )                           for(const char* src=output.str; (c=*src++); ) 
                                 encode(need_uri_encode, '%', c);                                  encode(need_uri_encode, '%', c);
                 }                  }
                 break;                  break;
Line 400  int cstr_to_string_body_block(char alang Line 370  int cstr_to_string_body_block(char alang
   
                         bool email=false;                          bool email=false;
                         uchar c;                          uchar c;
                         for(const char* src=mail_ptr; c=(uchar)*src++; ) {                          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==',' || c == '"' || addr_spec_soon(src-1/*position to 'c'*/))) {
                                 if(to_quoted_printable && (c==',' || addr_spec_soon(src) || c == '"')) {  
                                         email=c=='<';                                          email=c=='<';
                                         to_string("?=");                                          to_string("?=");
                                         to_quoted_printable=false;                                          to_quoted_printable=false;
                                 }                                  }
                                   //RFC   + An 'encoded-word' MUST NOT appear in any portion of an 'addr-spec'.
                                 if(!email && (                                  if(!email && (
                                         !to_quoted_printable && (c & 0x80)  // starting quote-printable-encoding on first 8bit char                                          !to_quoted_printable && (c & 0x80)  // starting quote-printable-encoding on first 8bit char
                                         || to_quoted_printable && !mail_header_char_valid_within_Qencoded(c)                                          || to_quoted_printable && !mail_header_char_valid_within_Qencoded(c)
Line 429  int cstr_to_string_body_block(char alang Line 399  int cstr_to_string_body_block(char alang
                 } else                  } else
                         ec_append(info->result, optimize, whitespace, info->pos, fragment_length);                          ec_append(info->result, optimize, whitespace, info->pos, fragment_length);
                 break;                  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:          case String::L_SQL:
                 // tainted, untaint language: sql                  // tainted, untaint language: sql
                 if(info->connection) {                  if(info->connection) {
Line 451  int cstr_to_string_body_block(char alang Line 412  int cstr_to_string_body_block(char alang
                                 0,                                  0,
                                 "untaint in SQL language failed - no connection specified");                                  "untaint in SQL language failed - no connection specified");
                 break;                  break;
 #endif  
         case String::L_JS:          case String::L_JS:
                 escape(switch(c) {                  escape(switch(c) {
                         case '"': to_string("\\\"");  break;                          case '"': to_string("\\\"");  break;
Line 459  int cstr_to_string_body_block(char alang Line 419  int cstr_to_string_body_block(char alang
                         case '\n': to_string("\\n");  break;                          case '\n': to_string("\\n");  break;
                         case '\\': to_string("\\\\");  break;                          case '\\': to_string("\\\\");  break;
                         case '\xFF': to_string("\\\xFF");  break;                          case '\xFF': to_string("\\\xFF");  break;
                         _default;                          default: _default; break;
                 });                  });
                 break;                  break;
         case String::L_XML:          case String::L_XML:
                   // [2]    Char    ::=    #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] 
                 escape(switch(c) {                  escape(switch(c) {
                           case '\x9': 
                           case '\xA': 
                           case '\xD': // this is usually removed on input
                                   _default; 
                                   break;
                         case '&': to_string("&amp;");  break;                          case '&': to_string("&amp;");  break;
                         case '>': to_string("&gt;");  break;                          case '>': to_string("&gt;");  break;
                         case '<': to_string("&lt;");  break;                          case '<': to_string("&lt;");  break;
                         case '"': to_string("&quot;");  break;                          case '"': to_string("&quot;");  break;
                         case '\'': to_string("&apos;");  break;                          case '\'': to_string("&apos;");  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;                  break;
         case String::L_HTML:          case String::L_HTML:
Line 478  int cstr_to_string_body_block(char alang Line 460  int cstr_to_string_body_block(char alang
                         case '>': to_string("&gt;");  break;                          case '>': to_string("&gt;");  break;
                         case '<': to_string("&lt;");  break;                          case '<': to_string("&lt;");  break;
                         case '"': to_string("&quot;");  break;                          case '"': to_string("&quot;");  break;
                         _default;                          default: _default; break;
                 });                  });
                 break;                  break;
         default:          default:
                 assert(!"should never");                  SAPI::abort("unknown untaint language #%d", 
                 SAPI::die("unknown untaint language #%d",   
                         static_cast<int>(to_lang)); // should never                          static_cast<int>(to_lang)); // should never
                 break; // never                  break; // never
         }          }

Removed from v.1.122.2.2  
changed lines
  Added in v.1.133


E-mail: