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

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

E-mail: