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

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

E-mail: