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

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

E-mail: