Annotation of parser3/src/main/untaint.C, revision 1.168

1.7       paf         1: /** @file
1.8       paf         2:        Parser: String class part: untaint mechanizm.
                      3: 
1.163     moko        4:        Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com)
1.89      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.103     paf         6: */
1.8       paf         7: 
1.168   ! moko        8: volatile const char * IDENT_UNTAINT_C="$Id: untaint.C,v 1.167 2015/10/03 00:03:49 moko Exp $";
1.117     paf         9: 
1.1       paf        10: 
                     11: #include "pa_string.h"
                     12: #include "pa_hash.h"
                     13: #include "pa_exception.h"
1.13      paf        14: #include "pa_table.h"
1.32      paf        15: #include "pa_globals.h"
1.58      parser     16: #include "pa_dictionary.h"
1.66      parser     17: #include "pa_common.h"
1.85      paf        18: #include "pa_charset.h"
1.117     paf        19: #include "pa_request_charsets.h"
                     20: #include "pa_sapi.h"
1.1       paf        21: 
1.117     paf        22: extern "C" { // author forgot to do that
                     23: #include "ec.h"
                     24: }
1.91      paf        25: 
1.117     paf        26: #include "pa_sql_connection.h"
1.91      paf        27: 
1.117     paf        28: // defines
                     29: 
                     30: #undef CORD_ec_append
                     31: // redefining to intercept flushes and implement whitespace optimization
                     32: // of all consequent white space chars leaving only first one
                     33: #define CORD_ec_append(x, c) \
                     34:     {  \
                     35:        bool skip=false; \
                     36:        if(optimize) switch(c) { \
1.131     paf        37:        case ' ': case '\n': case '\t': \
1.117     paf        38:                if(whitespace) \
                     39:                        skip=true; /*skipping subsequent*/ \
                     40:                else \
                     41:                        whitespace=true; \
                     42:                break; \
                     43:        default: \
                     44:                whitespace=false; \
                     45:                break; \
                     46:        } \
                     47:        if(!skip) { \
                     48:                if ((x)[0].ec_bufptr == (x)[0].ec_buf + CORD_BUFSZ) { \
                     49:                        CORD_ec_flush_buf(x); \
                     50:                } \
                     51:                *((x)[0].ec_bufptr)++ = (c); \
                     52:        } \
                     53:     }
                     54: 
1.140     misha      55: #define escape_fragment(action) \
1.119     paf        56:        for(; fragment_length--; CORD_next(info->pos)) { \
                     57:                char c=CORD_pos_fetch(info->pos); \
1.117     paf        58:                action \
1.1       paf        59:        }
1.167     moko       60: 
1.117     paf        61: #define encode(need_encode_func, prefix, otherwise)  \
                     62:        if(need_encode_func(c)) { \
1.119     paf        63:                CORD_ec_append(info->result, prefix); \
1.167     moko       64:                to_hex(c); \
                     65:        } else \
                     66:                CORD_ec_append(info->result, otherwise);
                     67: 
                     68: #define to_hex(c) \
                     69:        { \
1.165     moko       70:                CORD_ec_append(info->result, hex_digits[((unsigned char)c) >> 4]); \
                     71:                CORD_ec_append(info->result, hex_digits[((unsigned char)c) & 0x0F]); \
1.167     moko       72:        }
                     73: 
1.126     paf        74: #define to_char(c)  { CORD_ec_append(info->result, c); whitespace=false; }
1.168   ! moko       75: #define to_string(s)  { CORD_ec_append_cord(info->result, (CORD)(s)); whitespace=false; }
1.167     moko       76: #define _default CORD_ec_append(info->result, c)
1.4       paf        77: 
1.9       paf        78: inline bool need_file_encode(unsigned char c){
1.108     paf        79:        // russian letters and space ENABLED
                     80:        // encoding only these...
                     81:        return strchr(
1.164     misha      82:                        "*?\"<>|"
1.108     paf        83: #ifndef WIN32
1.143     misha      84:                        ":\\"
1.31      paf        85: #endif
1.143     misha      86:                        , c)!=0;
1.9       paf        87: }
1.143     misha      88: 
1.5       paf        89: inline bool need_uri_encode(unsigned char c){
1.162     misha      90:        return !(pa_isalnum(c) || strchr("_-./*", c));
1.4       paf        91: }
1.143     misha      92: 
1.136     paf        93: inline bool need_regex_escape(unsigned char c){
1.142     misha      94:        return strchr("\\^$.[]|()?*+{}-", c)!=0;
1.136     paf        95: }
1.1       paf        96: 
1.150     misha      97: inline bool need_parser_code_escape(unsigned char c){
                     98:        return strchr("^$;@()[]{}:#\"", c)!=0;
                     99: }
                    100: 
1.1       paf       101: // String
                    102: 
1.41      paf       103: /*
                    104: HTTP-header    = field-name ":" [ field-value ] CRLF
                    105: 
1.152     misha     106: field-name     = token
                    107: field-value    = *( field-content | LWS )
1.41      paf       108: 
1.152     misha     109: field-content  = <the OCTETs making up the field-value
1.41      paf       110:                         and consisting of either *TEXT or combinations
                    111:                         of token, tspecials, and quoted-string>
                    112: 
                    113: 
1.152     misha     114: token          = 1*<any CHAR except CTLs or tspecials>
1.41      paf       115: word           = token | quoted-string
1.152     misha     116: quoted-string  = ( <"> *(qdtext | quoted-pair ) <"> )
                    117: qdtext         = <any TEXT except <">>
                    118: quoted-pair    = "\" CHAR
1.41      paf       119: 
1.152     misha     120: OCTET          = <any 8-bit sequence of data>
                    121: CHAR           = <any US-ASCII character (octets 0 - 127)>
1.41      paf       122: 
                    123: tspecials      = "(" | ")" | "<" | ">" | "@"
1.152     misha     124:                | "," | ";" | ":" | "\" | <">
                    125:                | "/" | "[" | "]" | "?" | "="
                    126:                | "{" | "}" | SP | HT
1.41      paf       127: 
                    128: SP             = <US-ASCII SP, space (32)>
                    129: HT             = <US-ASCII HT, horizontal-tab (9)>
                    130: 
                    131: LWS            = [CRLF] 1*( SP | HT )
1.152     misha     132: TEXT           = <any OCTET except CTLs, but including LWS>
                    133: CTL            = <any US-ASCII control character (octets 0 - 31) and DEL (127)>
1.41      paf       134: 
                    135: 
                    136:   if(strchr("()<>@,;:\\\"/[]?={} \t", *ptr))
                    137: */
1.117     paf       138: inline bool need_quote_http_header(const char* ptr, size_t size) {
1.41      paf       139:        for(; size--; ptr++)
1.42      paf       140:                if(strchr(";\\\"= \t" /* excluded ()<>@, :/ ? []{} */, *ptr))
1.41      paf       141:                        return true;
                    142:        return false;
                    143: }
                    144: 
1.119     paf       145: #ifndef DOXYGEN
                    146: struct Append_fragment_info {
                    147:        String::Language lang;
                    148:        String::Languages* dest_languages;
                    149:        size_t dest_body_plan_length;
                    150: };
                    151: #endif
                    152: int append_fragment_optimizing(char alang, size_t asize, Append_fragment_info* info) {
                    153:        const String::Language lang=(String::Language)(unsigned char)alang;
                    154:        // main idea here:
                    155:        // tainted piece would get OPTIMIZED bit from 'lang'
                    156:        // clean piece would be marked OPTIMIZED manually
                    157:        // pieces with determined languages [not tainted|clean] would retain theirs langs
                    158:        info->dest_languages->append(info->dest_body_plan_length, 
1.120     paf       159:                lang==String::L_TAINTED?
1.119     paf       160:                        info->lang
1.120     paf       161:                        :lang==String::L_CLEAN?
                    162:                                (String::Language)(String::L_CLEAN|String::L_OPTIMIZE_BIT) // ORing with OPTIMIZED flag
1.119     paf       163:                                :lang,
                    164:                asize);
                    165:        info->dest_body_plan_length+=asize;
                    166: 
                    167:        return 0; // 0=continue
                    168: }
                    169: int append_fragment_nonoptimizing(char alang, size_t asize, Append_fragment_info* info) {
                    170:        const String::Language lang=(String::Language)(unsigned char)alang;
                    171:        // The core idea: tainted pieces got marked with context's lang
                    172:        info->dest_languages->append(info->dest_body_plan_length, 
1.120     paf       173:                lang==String::L_TAINTED?
1.119     paf       174:                        info->lang
                    175:                        :lang,
                    176:                asize);
                    177:        info->dest_body_plan_length+=asize;
                    178: 
                    179:        return 0; // 0=continue
                    180: }
1.117     paf       181: 
1.91      paf       182: /** 
1.117     paf       183:        appends to other String,
1.91      paf       184:        marking all tainted pieces of it with @a lang.
1.92      paf       185:        or marking ALL pieces of it with a @a lang when @a forced to,
                    186:        and propagating OPTIMIZE language bit.
1.117     paf       187: */
1.147     misha     188: String& String::append_to(String& dest, Language ilang, bool forced) const {
1.117     paf       189:        if(is_empty())
                    190:                return dest;
1.91      paf       191: 
1.119     paf       192:        // first: fragment infos
1.117     paf       193:        
1.147     misha     194:        if(ilang==L_PASS_APPENDED) // without language-change?
                    195:                dest.langs.appendHelper(dest.body, langs, body);
1.117     paf       196:        else if(forced) //forcing passed lang?
1.147     misha     197:                dest.langs.appendHelper(dest.body, ilang, body);
                    198:        else {
                    199:                if(langs.opt.is_not_just_lang){
                    200:                        Append_fragment_info info={ilang, &dest.langs, dest.body.length()};
                    201:                        langs.for_each(body, ilang&L_OPTIMIZE_BIT?
                    202:                                append_fragment_optimizing
                    203:                                :append_fragment_nonoptimizing, &info);
                    204:                } else {
                    205:                        Language lang=langs.opt.lang;
                    206:                        // see append_fragment_* for explanation
                    207:                        if(ilang&L_OPTIMIZE_BIT){
                    208:                                dest.langs.appendHelper(dest.body,
                    209:                                        lang==String::L_TAINTED?
                    210:                                                ilang
                    211:                                                :lang==String::L_CLEAN?
                    212:                                                        (String::Language)(String::L_CLEAN|String::L_OPTIMIZE_BIT)
                    213:                                                        :lang,
                    214:                                        body);
                    215:                        } else {
                    216:                                dest.langs.appendHelper(dest.body, lang==String::L_TAINTED ? ilang:lang, body);
                    217:                        }
                    218:                }
1.91      paf       219:        }
                    220: 
1.119     paf       221:        // next: letters
                    222:        dest.body<<body;
                    223: 
1.117     paf       224:        ASSERT_STRING_INVARIANT(dest);
1.75      paf       225:        return dest;
1.51      parser    226: }
                    227: 
1.109     paf       228: /** http://www.ietf.org/rfc/rfc2047.txt
                    229: RFC
                    230: (3) As a replacement for a 'word' entity within a 'phrase', for example,
                    231:     one that precedes an address in a From, To, or Cc header.  The ABNF
                    232:     definition for 'phrase' from RFC 822 thus becomes:
                    233: 
                    234:     phrase = 1*( encoded-word / word )
                    235: 
                    236:     In this case the set of characters that may be used in a "Q"-encoded
                    237:     'encoded-word' is restricted to: <upper and lower case ASCII
                    238:     letters, decimal digits, "!", "*", "+", "-", "/", "=", and "_"
                    239:     (underscore, ASCII 95.)>.  An 'encoded-word' that appears within a
                    240:     'phrase' MUST be separated from any adjacent 'word', 'text' or
                    241:     'special' by 'linear-white-space'.
                    242: ...
                    243:    (2) The 8-bit hexadecimal value 20 (e.g., ISO-8859-1 SPACE) may be
                    244:        represented as "_" (underscore, ASCII 95.).  (This character may
                    245:        not pass through some internetwork mail gateways, but its use
                    246:        will greatly enhance readability of "Q" encoded data with mail
                    247:        readers that do not support this encoding.)  Note that the "_"
                    248:        always represents hexadecimal 20, even if the SPACE character
                    249:        occupies a different code position in the character set in use.
                    250: 
                    251:        paf: obviously, 
                    252:                without "=", or one could not differ "=E0" and "russian letter a"
                    253:                and without "_", or in would mean 0x20
                    254: */
1.117     paf       255: inline bool mail_header_char_valid_within_Qencoded(char c) {
1.162     misha     256:        return (pa_isalnum((unsigned char)c) || strchr("!*+-/", c));
1.109     paf       257: }
1.118     paf       258: inline bool addr_spec_soon(const char *src) {
1.124     paf       259:        for(char c; (c=*src); src++)
1.118     paf       260:                if(c=='<')
                    261:                        return true;
                    262:                else if(!(c==' ' || c=='\t'))
                    263:                        return false;
                    264:        return false;
                    265: }
1.117     paf       266: /**
                    267:        RFC 
                    268:        Upper case should be used for hexadecimal digits "A" through "F"
                    269:        The 8-bit hexadecimal value 20 (e.g., ISO-8859-1 SPACE) 
                    270:        may be represented as "_" 
                    271: */
                    272: inline bool mail_header_nonspace_char(char c) {
                    273:        return c != 0x20;
                    274: }
1.125     paf       275: 
1.117     paf       276: inline void ec_append(CORD_ec& result, bool& optimize, bool& whitespace, CORD_pos pos, size_t size) {
                    277:        while(size--) {
                    278:                CORD_ec_append(result, CORD_pos_fetch(pos));
                    279:                CORD_next(pos);
                    280:        }
                    281: }
                    282: inline void pa_CORD_pos_advance(CORD_pos pos, size_t n) {
                    283:        while(true) {
1.125     paf       284:                long avail=CORD_pos_chars_left(pos);
                    285:                if(avail<=0) {
1.117     paf       286:                        CORD_next(pos);
                    287:                        if(!--n)
                    288:                                break;
1.125     paf       289:                } else if((size_t)avail<n) {
1.117     paf       290:                        CORD_pos_advance(pos, avail);
                    291:                        n-=avail;
                    292:                } else { // avail>=n
                    293:                        CORD_pos_advance(pos, n);
                    294:                        break;
                    295:                }
                    296:        }
                    297: }
                    298: 
1.119     paf       299: #ifndef DOXYGEN
                    300: struct Cstr_to_string_body_block_info {
                    301:        // input
                    302:        String::Language lang;
                    303:        SQL_Connection* connection;
                    304:        const Request_charsets* charsets;
                    305:        const String::Body* body;
                    306: 
                    307:        // output
                    308:        CORD_ec result;
                    309: 
                    310:        // private
                    311:        CORD_pos pos;
                    312:        size_t fragment_begin;
                    313:        bool whitespace;
1.138     misha     314:        const char* exception;
1.119     paf       315: };
                    316: #endif
1.151     misha     317: 
                    318: // @todo: replace info->body->mid with something that uses info->pos
1.148     misha     319: int cstr_to_string_body_block(String::Language to_lang, size_t fragment_length, Cstr_to_string_body_block_info* info) {
1.119     paf       320:        bool& whitespace=info->whitespace;
                    321:        size_t fragment_end=info->fragment_begin+fragment_length;
1.148     misha     322:        //fprintf(stderr, "%d, %d =%s=\n", to_lang, fragment_length, info->body->cstr());
1.119     paf       323:        
1.120     paf       324:        bool optimize=(to_lang & String::L_OPTIMIZE_BIT)!=0;
1.119     paf       325:        if(!optimize)
                    326:                whitespace=false;
                    327:                
1.120     paf       328:        switch(to_lang & ~String::L_OPTIMIZE_BIT) {
                    329:        case String::L_CLEAN:
                    330:        case String::L_TAINTED:
                    331:        case String::L_AS_IS:
1.119     paf       332:                // clean piece
                    333: 
                    334:                // tainted piece, but undefined untaint language
                    335:                // for VString.as_double of tainted values
                    336:                // for ^process{body} evaluation
                    337: 
                    338:                // tainted, untaint language: as-is
                    339:                ec_append(info->result, optimize, whitespace, info->pos, fragment_length);
                    340:                break;
1.120     paf       341:        case String::L_FILE_SPEC:
1.119     paf       342:                // tainted, untaint language: file [name]
1.140     misha     343:                {
                    344:                        escape_fragment(
1.154     misha     345:                                encode(need_file_encode, '_', c); 
1.140     misha     346:                        );
                    347:                }
1.119     paf       348:                break;
1.120     paf       349:        case String::L_URI:
1.155     misha     350:                // tainted, untaint language: uri        
                    351:                {        
                    352:                        const char *fragment_str=info->body->mid(info->fragment_begin, fragment_length).cstr();  
                    353:                        // skip source [we use recoded version]  
                    354:                        pa_CORD_pos_advance(info->pos, fragment_length);         
                    355:                        String::C output(fragment_str, fragment_length);         
                    356:                        if(info->charsets)       
                    357:                                output=Charset::transcode(output,        
                    358:                                        info->charsets->source(),        
                    359:                                        info->charsets->client());       
                    360: 
                    361:                        char c;  
                    362:                        for(const char* src=output.str; (c=*src++); )    
                    363:                                encode(need_uri_encode, '%', c);         
                    364:                }
                    365:                break;
1.120     paf       366:        case String::L_HTTP_HEADER:
1.119     paf       367:                // tainted, untaint language: http-field-content-text
1.161     moko      368:                escape_fragment(switch(c) {
                    369:                        case '\n': 
                    370:                        case '\r': to_string(" ");  break;
                    371:                        default: _default; break;
                    372:                });
1.119     paf       373:                break;
1.120     paf       374:        case String::L_MAIL_HEADER:
1.119     paf       375:                // tainted, untaint language: mail-header
                    376:                // http://www.ietf.org/rfc/rfc2047.txt
                    377:                if(info->charsets) {
                    378:                        size_t mail_size;
                    379:                        const char *mail_ptr=
                    380:                                info->body->mid(info->fragment_begin, mail_size=fragment_length).cstr();
                    381:                        // skip source [we use recoded version]
                    382:                        pa_CORD_pos_advance(info->pos, mail_size);
                    383: 
                    384:                        const char* charset_name=info->charsets->mail().NAME().cstr();
                    385: 
                    386:                        // Subject: Re: parser3: =?koi8-r?Q?=D3=C5=CD=C9=CE=C1=D2?=
                    387:                        bool to_quoted_printable=false;
                    388: 
                    389:                        bool email=false;
                    390:                        uchar c;
1.124     paf       391:                        for(const char* src=mail_ptr; (c=(uchar)*src++); ) {
1.137     paf       392:                                if(c=='\r' || c=='\n')
                    393:                                        c=' ';
1.130     paf       394:                                if(to_quoted_printable && (c==',' || c == '"' || addr_spec_soon(src-1/*position to 'c'*/))) {
1.119     paf       395:                                        email=c=='<';
                    396:                                        to_string("?=");
                    397:                                        to_quoted_printable=false;
                    398:                                }
1.130     paf       399:                                //RFC   + An 'encoded-word' MUST NOT appear in any portion of an 'addr-spec'.
1.119     paf       400:                                if(!email && (
1.166     moko      401:                                        ( !to_quoted_printable && (c & 0x80) )  // starting quote-printable-encoding on first 8bit char
                    402:                                        || ( to_quoted_printable && !mail_header_char_valid_within_Qencoded(c) )
1.119     paf       403:                                        )) {
                    404:                                        if(!to_quoted_printable) {
                    405:                                                to_string("=?");
                    406:                                                to_string(charset_name);
                    407:                                                to_string("?Q?");
                    408:                                                to_quoted_printable=true;
1.105     paf       409:                                        }
1.119     paf       410:                                        encode(mail_header_nonspace_char, '=', '_'); 
                    411:                                } else
                    412:                                        to_char(c);
                    413:                                if(c=='>')
                    414:                                        email=false;
                    415:                        }
                    416:                        if(to_quoted_printable) // close
                    417:                                to_string("?=");
                    418:                
                    419:                } else
                    420:                        ec_append(info->result, optimize, whitespace, info->pos, fragment_length);
                    421:                break;
1.120     paf       422:        case String::L_SQL:
1.119     paf       423:                // tainted, untaint language: sql
                    424:                if(info->connection) {
                    425:                        const char *fragment_str=info->body->mid(info->fragment_begin, fragment_length).cstr();
                    426:                        // skip source [we use recoded version]
                    427:                        pa_CORD_pos_advance(info->pos, fragment_length);
                    428: 
                    429:                        to_string(info->connection->quote(fragment_str, fragment_length));
1.138     misha     430:                } else {
                    431:                        info->exception="untaint in SQL language failed - no connection specified";
                    432:                        info->fragment_begin=fragment_end;
                    433:                        return 1; // stop processing. can't throw exception here
                    434:                }
1.119     paf       435:                break;
1.120     paf       436:        case String::L_JS:
1.140     misha     437:                escape_fragment(switch(c) {
                    438:                        case '\n': to_string("\\n");  break;
1.119     paf       439:                        case '"': to_string("\\\"");  break;
                    440:                        case '\'': to_string("\\'");  break;
                    441:                        case '\\': to_string("\\\\");  break;
                    442:                        case '\xFF': to_string("\\\xFF");  break;
1.146     misha     443:                        case '\r': to_string("\\r");  break;
1.133     paf       444:                        default: _default; break;
1.119     paf       445:                });
                    446:                break;
1.120     paf       447:        case String::L_XML:
1.133     paf       448:                // [2]    Char    ::=    #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] 
1.140     misha     449:                escape_fragment(switch(c) {
                    450:                        case '\x20':
                    451:                        case '\x9':
                    452:                        case '\xA':
1.133     paf       453:                        case '\xD': // this is usually removed on input
                    454:                                _default; 
                    455:                                break;
1.119     paf       456:                        case '&': to_string("&amp;");  break;
                    457:                        case '>': to_string("&gt;");  break;
                    458:                        case '<': to_string("&lt;");  break;
                    459:                        case '"': to_string("&quot;");  break;
                    460:                        case '\'': to_string("&apos;");  break;
1.133     paf       461:                        default: 
                    462:                                if(((unsigned char)c)<0x20) {
                    463:                                        // fixing it, so that libxml would not result
                    464:                                        // in fatal error parsing text
                    465:                                        // though it really violates standard.
                    466:                                        // to indicate there were an error
                    467:                                        // replace bad char not to it's code, 
                    468:                                        // which we can do,
                    469:                                        // but rather to '!' to show that input were actually
                    470:                                        // invalid.
                    471:                                        // life: shows that MSIE can somehow garble form values
                    472:                                        // so that they contain these chars.
                    473:                                        to_char('!'); 
                    474:                                } else {
                    475:                                        _default; 
                    476:                                }
                    477:                                break;
1.119     paf       478:                });
                    479:                break;
1.120     paf       480:        case String::L_HTML:
1.140     misha     481:                escape_fragment(switch(c) {
1.119     paf       482:                        case '&': to_string("&amp;");  break;
                    483:                        case '>': to_string("&gt;");  break;
                    484:                        case '<': to_string("&lt;");  break;
                    485:                        case '"': to_string("&quot;");  break;
1.133     paf       486:                        default: _default; break;
1.119     paf       487:                });
                    488:                break;
1.136     paf       489:        case String::L_REGEX:
                    490:                // tainted, untaint language: regex
1.140     misha     491:                escape_fragment(
1.136     paf       492:                        if(need_regex_escape(c))
                    493:                                to_char('\\')
                    494:                        _default;
                    495:                );
                    496:                break;
1.156     misha     497:        case String::L_JSON:
                    498:                // tainted, untaint language: json <http://json.org/>
                    499:                // escape '"' '\' '/' '\n' '\t' '\r' '\b' '\f' chars and escape chars as \uXXXX if output charset != UTF-8
                    500:                {
1.159     moko      501:                        if(info->charsets==NULL || info->charsets->client().isUTF8()){
1.156     misha     502:                                // escaping to \uXXXX is not needed
1.167     moko      503:                                escape_fragment(switch((unsigned char)c) {
1.156     misha     504:                                        case '\n': to_string("\\n");  break;
                    505:                                        case '"' : to_string("\\\""); break;
                    506:                                        case '\\': to_string("\\\\"); break;
                    507:                                        case '/' : to_string("\\/");  break;
                    508:                                        case '\t': to_string("\\t");  break;
                    509:                                        case '\r': to_string("\\r");  break;
                    510:                                        case '\b': to_string("\\b");  break;
                    511:                                        case '\f': to_string("\\f");  break;
1.167     moko      512:                                        case 0xE2: // \u2028 and \u2029 (line/paragraph separators), check bug #1023
                    513:                                                if(info->charsets && info->charsets->source().isUTF8() && fragment_length>=2){
                    514:                                                        CORD_next(info->pos);
                    515:                                                        char c1=CORD_pos_fetch(info->pos);
                    516:                                                        CORD_next(info->pos);
                    517:                                                        char c2=CORD_pos_fetch(info->pos);
                    518:                                                        if((unsigned char)c1 == 0x80 && ((unsigned char)c2 >= 0xA8 && (unsigned char)c2 <= 0xAF)){
                    519:                                                                to_string("\\u20");
                    520:                                                                to_hex(((unsigned char)c2-0x80));
                    521:                                                        } else {
                    522:                                                                CORD_ec_append(info->result, c);
                    523:                                                                CORD_ec_append(info->result, c1);
                    524:                                                                CORD_ec_append(info->result, c2);
                    525:                                                        }
                    526:                                                        fragment_length-=2;
                    527:                                                } else {
                    528:                                                        _default;
                    529:                                                }
                    530:                                                break;
1.165     moko      531:                                        default: 
                    532:                                                if((unsigned char)c < 0x20){
                    533:                                                        to_string("\\u00");
1.167     moko      534:                                                        to_hex(c);
1.165     moko      535:                                                } else {
                    536:                                                        _default;
                    537:                                                }
                    538:                                                break;
1.156     misha     539:                                });
                    540:                        } else {
                    541:                                const char *fragment_str=info->body->mid(info->fragment_begin, fragment_length).cstr();
                    542:                                // skip source [we use recoded version]
                    543:                                pa_CORD_pos_advance(info->pos, fragment_length);
                    544:                                String::C output(fragment_str, fragment_length);
                    545: 
                    546:                                output=Charset::escape_JSON(output, info->charsets->source());
1.168   ! moko      547:                                to_string(output.str);
1.156     misha     548:                        }
                    549:                }
                    550:                break;
1.140     misha     551:        case String::L_HTTP_COOKIE:
                    552:                // tainted, untaint language: cookie (3.3.0 and higher: %uXXXX in UTF-8)
1.159     moko      553:                if(info->charsets) {
1.140     misha     554:                        const char *fragment_str=info->body->mid(info->fragment_begin, fragment_length).cstr();
                    555:                        // skip source [we use recoded version]
                    556:                        pa_CORD_pos_advance(info->pos, fragment_length);
                    557:                        String::C output(fragment_str, fragment_length);
                    558:                        
1.155     misha     559:                        output=Charset::escape(output, info->charsets->source());
1.168   ! moko      560:                        to_string(output.str);
1.159     moko      561:                } else
                    562:                        ec_append(info->result, optimize, whitespace, info->pos, fragment_length);
1.140     misha     563:                break;
1.150     misha     564:        case String::L_PARSER_CODE:
                    565:                // for auto-untaint in process
                    566:                escape_fragment(
                    567:                        if(need_parser_code_escape(c))
                    568:                                to_char('^');
                    569:                        _default;
                    570:                );
                    571:                break;
1.119     paf       572:        default:
1.127     paf       573:                SAPI::abort("unknown untaint language #%d", 
1.119     paf       574:                        static_cast<int>(to_lang)); // should never
                    575:                break; // never
1.117     paf       576:        }
1.55      parser    577: 
1.119     paf       578:        info->fragment_begin=fragment_end;
                    579: 
                    580:        return 0; // 0=continue
                    581: }
                    582: 
                    583: 
1.148     misha     584: String::Body String::cstr_to_string_body_taint(Language lang, SQL_Connection* connection, const Request_charsets *charsets) const {
1.134     paf       585:        if(is_empty())
                    586:                return String::Body();
1.119     paf       587: 
                    588:        Cstr_to_string_body_block_info info;
                    589:        // input
                    590:        info.lang=lang;
                    591:        info.connection=connection;
                    592:        info.charsets=charsets;
                    593:        info.body=&body;
                    594:        // output
                    595:        CORD_ec_init(info.result);
                    596:        // private
                    597:        body.set_pos(info.pos, 0);
                    598:        info.fragment_begin=0;
1.138     misha     599:        info.exception=0;
1.119     paf       600:        info.whitespace=true;
                    601: 
1.148     misha     602:        cstr_to_string_body_block(lang, length(), &info);
                    603: 
                    604:        if(info.exception)
                    605:                throw Exception(0,
                    606:                        0,
                    607:                        info.exception);
                    608: 
1.160     moko      609:        return String::Body(CORD_ec_to_cord(info.result));
1.148     misha     610: }
                    611: 
                    612: int cstr_to_string_body_block_untaint(char alang, size_t fragment_length, Cstr_to_string_body_block_info* info){
                    613:        const String::Language lang=(String::Language)(unsigned char)alang;
                    614:        // see append_fragment_* for explanation
                    615:        if(info->lang&String::L_OPTIMIZE_BIT)
                    616:                return cstr_to_string_body_block(
                    617:                        lang==String::L_TAINTED?
                    618:                                info->lang
                    619:                                :lang==String::L_CLEAN?
                    620:                                        (String::Language)(String::L_CLEAN|String::L_OPTIMIZE_BIT)
                    621:                                        :lang,
                    622:                        fragment_length, info);
                    623:        else
                    624:                return cstr_to_string_body_block(lang==String::L_TAINTED ? info->lang:lang, fragment_length, info);
                    625: }
                    626: 
                    627: String::Body String::cstr_to_string_body_untaint(Language lang, SQL_Connection* connection, const Request_charsets *charsets) const {
                    628:        if(is_empty())
                    629:                return String::Body();
                    630: 
                    631:        Cstr_to_string_body_block_info info;
                    632:        // input
                    633:        info.lang=lang;
                    634:        info.connection=connection;
                    635:        info.charsets=charsets;
                    636:        info.body=&body;
                    637:        // output
                    638:        CORD_ec_init(info.result);
                    639:        // private
                    640:        body.set_pos(info.pos, 0);
                    641:        info.fragment_begin=0;
                    642:        info.exception=0;
                    643:        info.whitespace=true;
                    644: 
                    645:        langs.for_each(body, cstr_to_string_body_block_untaint, &info);
1.151     misha     646: 
                    647:        if(info.exception)
                    648:                throw Exception(0,
                    649:                        0,
                    650:                        info.exception);
                    651: 
1.160     moko      652:        return String::Body(CORD_ec_to_cord(info.result));
1.151     misha     653: }
                    654: 
1.155     misha     655: const char* String::untaint_and_transcode_cstr(Language lang, const Request_charsets *charsets) const {
1.151     misha     656:        if(charsets && &charsets->source() != &charsets->client()){
1.155     misha     657:                // Note: L_URI is allready transcoded during untaint, but transcode does not affect %XX
                    658:                return Charset::transcode(cstr_to_string_body_untaint(lang, 0, charsets), charsets->source(), charsets->client()).cstr();
                    659:        } else
1.151     misha     660:                return cstr_to_string_body_untaint(lang, 0, charsets).cstr();
1.1       paf       661: }

E-mail: