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

1.7       paf         1: /** @file
1.8       paf         2:        Parser: String class part: untaint mechanizm.
                      3: 
1.172     moko        4:        Copyright (c) 2001-2017 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.174   ! moko        8: volatile const char * IDENT_UNTAINT_C="$Id: untaint.C,v 1.173 2017/05/29 11:00:22 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
1.174   ! moko       23: #include "../lib/cord/include/ec.h"
1.117     paf        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.171     moko      182: 
                    183: /** 
                    184:        appends to other String without language change
                    185: */
                    186: 
                    187: String& String::append_to(String& dest) const {
                    188:        if(is_empty())
                    189:                return dest;
                    190: 
                    191:        // first: fragment infos
                    192:        dest.langs.appendHelper(dest.body, langs, body);
                    193: 
                    194:        // next: letters
                    195:        dest.body<<body;
                    196: 
                    197:        ASSERT_STRING_INVARIANT(dest);
                    198:        return dest;
                    199: }
                    200: 
1.91      paf       201: /** 
1.117     paf       202:        appends to other String,
1.91      paf       203:        marking all tainted pieces of it with @a lang.
1.92      paf       204:        or marking ALL pieces of it with a @a lang when @a forced to,
                    205:        and propagating OPTIMIZE language bit.
1.117     paf       206: */
1.147     misha     207: String& String::append_to(String& dest, Language ilang, bool forced) const {
1.117     paf       208:        if(is_empty())
                    209:                return dest;
1.91      paf       210: 
1.119     paf       211:        // first: fragment infos
1.117     paf       212:        
1.171     moko      213:        if(forced) //forcing passed lang?
1.147     misha     214:                dest.langs.appendHelper(dest.body, ilang, body);
                    215:        else {
                    216:                if(langs.opt.is_not_just_lang){
                    217:                        Append_fragment_info info={ilang, &dest.langs, dest.body.length()};
                    218:                        langs.for_each(body, ilang&L_OPTIMIZE_BIT?
                    219:                                append_fragment_optimizing
                    220:                                :append_fragment_nonoptimizing, &info);
                    221:                } else {
                    222:                        Language lang=langs.opt.lang;
                    223:                        // see append_fragment_* for explanation
                    224:                        if(ilang&L_OPTIMIZE_BIT){
                    225:                                dest.langs.appendHelper(dest.body,
                    226:                                        lang==String::L_TAINTED?
                    227:                                                ilang
                    228:                                                :lang==String::L_CLEAN?
                    229:                                                        (String::Language)(String::L_CLEAN|String::L_OPTIMIZE_BIT)
                    230:                                                        :lang,
                    231:                                        body);
                    232:                        } else {
                    233:                                dest.langs.appendHelper(dest.body, lang==String::L_TAINTED ? ilang:lang, body);
                    234:                        }
                    235:                }
1.91      paf       236:        }
                    237: 
1.119     paf       238:        // next: letters
                    239:        dest.body<<body;
                    240: 
1.117     paf       241:        ASSERT_STRING_INVARIANT(dest);
1.75      paf       242:        return dest;
1.51      parser    243: }
                    244: 
1.109     paf       245: /** http://www.ietf.org/rfc/rfc2047.txt
                    246: RFC
                    247: (3) As a replacement for a 'word' entity within a 'phrase', for example,
                    248:     one that precedes an address in a From, To, or Cc header.  The ABNF
                    249:     definition for 'phrase' from RFC 822 thus becomes:
                    250: 
                    251:     phrase = 1*( encoded-word / word )
                    252: 
                    253:     In this case the set of characters that may be used in a "Q"-encoded
                    254:     'encoded-word' is restricted to: <upper and lower case ASCII
                    255:     letters, decimal digits, "!", "*", "+", "-", "/", "=", and "_"
                    256:     (underscore, ASCII 95.)>.  An 'encoded-word' that appears within a
                    257:     'phrase' MUST be separated from any adjacent 'word', 'text' or
                    258:     'special' by 'linear-white-space'.
                    259: ...
                    260:    (2) The 8-bit hexadecimal value 20 (e.g., ISO-8859-1 SPACE) may be
                    261:        represented as "_" (underscore, ASCII 95.).  (This character may
                    262:        not pass through some internetwork mail gateways, but its use
                    263:        will greatly enhance readability of "Q" encoded data with mail
                    264:        readers that do not support this encoding.)  Note that the "_"
                    265:        always represents hexadecimal 20, even if the SPACE character
                    266:        occupies a different code position in the character set in use.
                    267: 
                    268:        paf: obviously, 
                    269:                without "=", or one could not differ "=E0" and "russian letter a"
                    270:                and without "_", or in would mean 0x20
                    271: */
1.117     paf       272: inline bool mail_header_char_valid_within_Qencoded(char c) {
1.162     misha     273:        return (pa_isalnum((unsigned char)c) || strchr("!*+-/", c));
1.109     paf       274: }
1.118     paf       275: inline bool addr_spec_soon(const char *src) {
1.124     paf       276:        for(char c; (c=*src); src++)
1.118     paf       277:                if(c=='<')
                    278:                        return true;
                    279:                else if(!(c==' ' || c=='\t'))
                    280:                        return false;
                    281:        return false;
                    282: }
1.117     paf       283: /**
                    284:        RFC 
                    285:        Upper case should be used for hexadecimal digits "A" through "F"
                    286:        The 8-bit hexadecimal value 20 (e.g., ISO-8859-1 SPACE) 
                    287:        may be represented as "_" 
                    288: */
                    289: inline bool mail_header_nonspace_char(char c) {
                    290:        return c != 0x20;
                    291: }
1.125     paf       292: 
1.117     paf       293: inline void ec_append(CORD_ec& result, bool& optimize, bool& whitespace, CORD_pos pos, size_t size) {
                    294:        while(size--) {
                    295:                CORD_ec_append(result, CORD_pos_fetch(pos));
                    296:                CORD_next(pos);
                    297:        }
                    298: }
                    299: inline void pa_CORD_pos_advance(CORD_pos pos, size_t n) {
                    300:        while(true) {
1.125     paf       301:                long avail=CORD_pos_chars_left(pos);
                    302:                if(avail<=0) {
1.117     paf       303:                        CORD_next(pos);
                    304:                        if(!--n)
                    305:                                break;
1.125     paf       306:                } else if((size_t)avail<n) {
1.117     paf       307:                        CORD_pos_advance(pos, avail);
                    308:                        n-=avail;
                    309:                } else { // avail>=n
                    310:                        CORD_pos_advance(pos, n);
                    311:                        break;
                    312:                }
                    313:        }
                    314: }
                    315: 
1.119     paf       316: #ifndef DOXYGEN
                    317: struct Cstr_to_string_body_block_info {
                    318:        // input
                    319:        String::Language lang;
                    320:        SQL_Connection* connection;
                    321:        const Request_charsets* charsets;
                    322:        const String::Body* body;
                    323: 
                    324:        // output
                    325:        CORD_ec result;
                    326: 
                    327:        // private
                    328:        CORD_pos pos;
                    329:        size_t fragment_begin;
                    330:        bool whitespace;
1.138     misha     331:        const char* exception;
1.119     paf       332: };
                    333: #endif
1.151     misha     334: 
                    335: // @todo: replace info->body->mid with something that uses info->pos
1.148     misha     336: int cstr_to_string_body_block(String::Language to_lang, size_t fragment_length, Cstr_to_string_body_block_info* info) {
1.119     paf       337:        bool& whitespace=info->whitespace;
                    338:        size_t fragment_end=info->fragment_begin+fragment_length;
1.148     misha     339:        //fprintf(stderr, "%d, %d =%s=\n", to_lang, fragment_length, info->body->cstr());
1.119     paf       340:        
1.120     paf       341:        bool optimize=(to_lang & String::L_OPTIMIZE_BIT)!=0;
1.119     paf       342:        if(!optimize)
                    343:                whitespace=false;
                    344:                
1.120     paf       345:        switch(to_lang & ~String::L_OPTIMIZE_BIT) {
                    346:        case String::L_CLEAN:
                    347:        case String::L_TAINTED:
                    348:        case String::L_AS_IS:
1.119     paf       349:                // clean piece
                    350: 
                    351:                // tainted piece, but undefined untaint language
                    352:                // for VString.as_double of tainted values
                    353:                // for ^process{body} evaluation
                    354: 
                    355:                // tainted, untaint language: as-is
                    356:                ec_append(info->result, optimize, whitespace, info->pos, fragment_length);
                    357:                break;
1.120     paf       358:        case String::L_FILE_SPEC:
1.119     paf       359:                // tainted, untaint language: file [name]
1.140     misha     360:                {
                    361:                        escape_fragment(
1.154     misha     362:                                encode(need_file_encode, '_', c); 
1.140     misha     363:                        );
                    364:                }
1.119     paf       365:                break;
1.120     paf       366:        case String::L_URI:
1.169     moko      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, info->charsets->source(), info->charsets->client());
                    375: 
                    376:                        char c;
                    377:                        for(const char* src=output.str; (c=*src++); )
                    378:                                encode(need_uri_encode, '%', c);
1.155     misha     379:                }
                    380:                break;
1.120     paf       381:        case String::L_HTTP_HEADER:
1.119     paf       382:                // tainted, untaint language: http-field-content-text
1.161     moko      383:                escape_fragment(switch(c) {
                    384:                        case '\n': 
                    385:                        case '\r': to_string(" ");  break;
                    386:                        default: _default; break;
                    387:                });
1.119     paf       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.173     moko      406:                        for(const char* src=mail_ptr; c=(uchar)*src; src++) {
1.137     paf       407:                                if(c=='\r' || c=='\n')
                    408:                                        c=' ';
1.173     moko      409:                                if(to_quoted_printable && (c==',' || c == '"' || addr_spec_soon(src))) {
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 && (
1.173     moko      416:                                        ( !to_quoted_printable && (c & 0x80 || (c == ' ' && src == mail_ptr) ) )  // starting quote-printable-encoding on first 8bit char or leading space (issue #123)
1.166     moko      417:                                        || ( to_quoted_printable && !mail_header_char_valid_within_Qencoded(c) )
1.119     paf       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:                {
1.159     moko      516:                        if(info->charsets==NULL || info->charsets->client().isUTF8()){
1.156     misha     517:                                // escaping to \uXXXX is not needed
1.167     moko      518:                                escape_fragment(switch((unsigned char)c) {
1.156     misha     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;
1.167     moko      527:                                        case 0xE2: // \u2028 and \u2029 (line/paragraph separators), check bug #1023
                    528:                                                if(info->charsets && info->charsets->source().isUTF8() && fragment_length>=2){
                    529:                                                        CORD_next(info->pos);
                    530:                                                        char c1=CORD_pos_fetch(info->pos);
                    531:                                                        CORD_next(info->pos);
                    532:                                                        char c2=CORD_pos_fetch(info->pos);
                    533:                                                        if((unsigned char)c1 == 0x80 && ((unsigned char)c2 >= 0xA8 && (unsigned char)c2 <= 0xAF)){
                    534:                                                                to_string("\\u20");
                    535:                                                                to_hex(((unsigned char)c2-0x80));
                    536:                                                        } else {
                    537:                                                                CORD_ec_append(info->result, c);
                    538:                                                                CORD_ec_append(info->result, c1);
                    539:                                                                CORD_ec_append(info->result, c2);
                    540:                                                        }
                    541:                                                        fragment_length-=2;
                    542:                                                } else {
                    543:                                                        _default;
                    544:                                                }
                    545:                                                break;
1.165     moko      546:                                        default: 
                    547:                                                if((unsigned char)c < 0x20){
                    548:                                                        to_string("\\u00");
1.167     moko      549:                                                        to_hex(c);
1.165     moko      550:                                                } else {
                    551:                                                        _default;
                    552:                                                }
                    553:                                                break;
1.156     misha     554:                                });
                    555:                        } else {
                    556:                                const char *fragment_str=info->body->mid(info->fragment_begin, fragment_length).cstr();
                    557:                                // skip source [we use recoded version]
                    558:                                pa_CORD_pos_advance(info->pos, fragment_length);
1.169     moko      559:                                to_string(Charset::escape_JSON(String::C(fragment_str, fragment_length), info->charsets->source()).str);
1.156     misha     560:                        }
                    561:                }
                    562:                break;
1.140     misha     563:        case String::L_HTTP_COOKIE:
                    564:                // tainted, untaint language: cookie (3.3.0 and higher: %uXXXX in UTF-8)
1.159     moko      565:                if(info->charsets) {
1.140     misha     566:                        const char *fragment_str=info->body->mid(info->fragment_begin, fragment_length).cstr();
                    567:                        // skip source [we use recoded version]
                    568:                        pa_CORD_pos_advance(info->pos, fragment_length);
1.169     moko      569:                        to_string(Charset::escape(String::C(fragment_str, fragment_length), info->charsets->source()).str);
1.159     moko      570:                } else
                    571:                        ec_append(info->result, optimize, whitespace, info->pos, fragment_length);
1.140     misha     572:                break;
1.150     misha     573:        case String::L_PARSER_CODE:
                    574:                // for auto-untaint in process
                    575:                escape_fragment(
                    576:                        if(need_parser_code_escape(c))
                    577:                                to_char('^');
                    578:                        _default;
                    579:                );
                    580:                break;
1.119     paf       581:        default:
1.127     paf       582:                SAPI::abort("unknown untaint language #%d", 
1.119     paf       583:                        static_cast<int>(to_lang)); // should never
                    584:                break; // never
1.117     paf       585:        }
1.55      parser    586: 
1.119     paf       587:        info->fragment_begin=fragment_end;
                    588: 
                    589:        return 0; // 0=continue
                    590: }
                    591: 
                    592: 
1.148     misha     593: String::Body String::cstr_to_string_body_taint(Language lang, SQL_Connection* connection, const Request_charsets *charsets) const {
1.134     paf       594:        if(is_empty())
                    595:                return String::Body();
1.119     paf       596: 
                    597:        Cstr_to_string_body_block_info info;
                    598:        // input
                    599:        info.lang=lang;
                    600:        info.connection=connection;
                    601:        info.charsets=charsets;
                    602:        info.body=&body;
                    603:        // output
                    604:        CORD_ec_init(info.result);
                    605:        // private
                    606:        body.set_pos(info.pos, 0);
                    607:        info.fragment_begin=0;
1.138     misha     608:        info.exception=0;
1.119     paf       609:        info.whitespace=true;
                    610: 
1.148     misha     611:        cstr_to_string_body_block(lang, length(), &info);
                    612: 
                    613:        if(info.exception)
                    614:                throw Exception(0,
                    615:                        0,
                    616:                        info.exception);
                    617: 
1.160     moko      618:        return String::Body(CORD_ec_to_cord(info.result));
1.148     misha     619: }
                    620: 
                    621: int cstr_to_string_body_block_untaint(char alang, size_t fragment_length, Cstr_to_string_body_block_info* info){
                    622:        const String::Language lang=(String::Language)(unsigned char)alang;
                    623:        // see append_fragment_* for explanation
                    624:        if(info->lang&String::L_OPTIMIZE_BIT)
                    625:                return cstr_to_string_body_block(
                    626:                        lang==String::L_TAINTED?
                    627:                                info->lang
                    628:                                :lang==String::L_CLEAN?
                    629:                                        (String::Language)(String::L_CLEAN|String::L_OPTIMIZE_BIT)
                    630:                                        :lang,
                    631:                        fragment_length, info);
                    632:        else
                    633:                return cstr_to_string_body_block(lang==String::L_TAINTED ? info->lang:lang, fragment_length, info);
                    634: }
                    635: 
                    636: String::Body String::cstr_to_string_body_untaint(Language lang, SQL_Connection* connection, const Request_charsets *charsets) const {
                    637:        if(is_empty())
                    638:                return String::Body();
                    639: 
                    640:        Cstr_to_string_body_block_info info;
                    641:        // input
                    642:        info.lang=lang;
                    643:        info.connection=connection;
                    644:        info.charsets=charsets;
                    645:        info.body=&body;
                    646:        // output
                    647:        CORD_ec_init(info.result);
                    648:        // private
                    649:        body.set_pos(info.pos, 0);
                    650:        info.fragment_begin=0;
                    651:        info.exception=0;
                    652:        info.whitespace=true;
                    653: 
                    654:        langs.for_each(body, cstr_to_string_body_block_untaint, &info);
1.151     misha     655: 
                    656:        if(info.exception)
                    657:                throw Exception(0,
                    658:                        0,
                    659:                        info.exception);
                    660: 
1.160     moko      661:        return String::Body(CORD_ec_to_cord(info.result));
1.151     misha     662: }
                    663: 
1.155     misha     664: const char* String::untaint_and_transcode_cstr(Language lang, const Request_charsets *charsets) const {
1.151     misha     665:        if(charsets && &charsets->source() != &charsets->client()){
1.155     misha     666:                // Note: L_URI is allready transcoded during untaint, but transcode does not affect %XX
                    667:                return Charset::transcode(cstr_to_string_body_untaint(lang, 0, charsets), charsets->source(), charsets->client()).cstr();
                    668:        } else
1.151     misha     669:                return cstr_to_string_body_untaint(lang, 0, charsets).cstr();
1.1       paf       670: }

E-mail: