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

1.7       paf         1: /** @file
1.8       paf         2:        Parser: String class part: untaint mechanizm.
                      3: 
1.13      paf         4:        Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com)
1.63      parser      5:        Author: Alexander Petrosyan <paf@design.ru>(http://design.ru/paf)
1.8       paf         6: 
1.70    ! paf         7:        $Id: untaint.C,v 1.69 2001/10/19 12:43:30 parser Exp $
1.1       paf         8: */
                      9: 
                     10: #include "pa_pool.h"
                     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.34      paf        16: #include "pa_sql_connection.h"
1.58      parser     17: #include "pa_dictionary.h"
1.66      parser     18: #include "pa_common.h"
1.1       paf        19: 
1.18      paf        20: #define escape(action) \
1.1       paf        21:        { \
1.13      paf        22:                const char *src=row->item.ptr; \
                     23:                for(int size=row->item.size; size--; src++) \
1.18      paf        24:                        action \
1.1       paf        25:        }
1.13      paf        26: #define _default  default: *dest++=*src; break
                     27: #define encode(need_encode_func, prefix)  \
1.5       paf        28:                default: \
1.13      paf        29:                        if(need_encode_func(*src)) { \
1.5       paf        30:                                static const char *hex="0123456789ABCDEF"; \
1.9       paf        31:                                char chunk[3]={prefix}; \
1.13      paf        32:                                chunk[1]=hex[((unsigned char)*src)/0x10]; \
                     33:                                chunk[2]=hex[((unsigned char)*src)%0x10]; \
1.60      parser     34:                                memcpy(dest, chunk, 3);  dest+=3; \
1.5       paf        35:                        } else \
1.13      paf        36:                                *dest++=*src; \
1.5       paf        37:                        break
1.18      paf        38: #define to_char(c)  *dest++=c
                     39: #define to_string(b, bsize)  \
1.60      parser     40:                memcpy(dest, b, bsize); \
1.18      paf        41:                dest+=bsize; \
1.4       paf        42: 
1.9       paf        43: inline bool need_file_encode(unsigned char c){
1.13      paf        44:     if((c>='0') &&(c<='9') ||(c>='A') &&(c<='Z') ||(c>='a') &&(c<='z')) 
1.9       paf        45:                return false;
                     46: 
1.31      paf        47:     return !strchr(
                     48: #ifdef WIN32
1.37      paf        49:                ":\\~"
1.31      paf        50: #endif
1.39      paf        51:                "./()_-", c);
1.9       paf        52: }
1.5       paf        53: inline bool need_uri_encode(unsigned char c){
1.13      paf        54:     if((c>='0') &&(c<='9') ||(c>='A') &&(c<='Z') ||(c>='a') &&(c<='z')) 
1.4       paf        55:                return false;
                     56: 
1.5       paf        57:     return !strchr("_-./", c);
                     58: }
1.36      paf        59: inline bool need_http_header_encode(unsigned char c){
1.18      paf        60:     if(strchr(" , :", c))
1.5       paf        61:                return false;
                     62: 
                     63:        return need_uri_encode(c);
1.4       paf        64: }
1.1       paf        65: 
1.56      parser     66: //
                     67: 
                     68: static const char * String_Untaint_lang_name[]={
                     69:        "U", ///< zero value handy for hash lookup @see untaint_lang_name2enum
                     70:        "C", ///< clean
                     71:        "T",  ///< tainted, untaint language as assigned later 
                     72:        // untaint languages. assigned by ^untaint[lang]{...}
                     73:        "P",
                     74:                /**<
                     75:                        leave language built into string being appended.
                     76:                        just a flag, that value not stored
                     77:                */
                     78:        "A",     ///< leave all characters intact
1.68      parser     79:        "F", ///< file specification
                     80:        "H",    ///< ext in HTTP response header
1.56      parser     81:        "M",    ///< text in mail header
                     82:        "URI",       ///< text in uri
                     83:        "T",     ///< ^table:set body
                     84:        "SQL",       ///< ^table:sql body
                     85:        "JS",        ///< JavaScript code
1.68      parser     86:        "XML",          ///< ^dom:set xml
1.56      parser     87:        "HTML",      ///< HTML code (for editing)
                     88:        "UHTML", ///< HTML code with USER chars
                     89: };
                     90: 
                     91: 
1.1       paf        92: // String
                     93: 
1.13      paf        94: static bool typo_present(Array::Item *value, const void *info) {
                     95:        Array *row=static_cast<Array *>(value);
                     96:        const char *src=static_cast<const char *>(info);
                     97: 
                     98:        int partial;
1.28      paf        99:        row->get_string(0)->cmp(partial, src);
1.14      paf       100:        return 
                    101:                partial==0 || // full match
                    102:                partial==1; // typo left column starts 'src'
1.13      paf       103: }
                    104: 
1.41      paf       105: /*
                    106: 
                    107: HTTP-header    = field-name ":" [ field-value ] CRLF
                    108: 
                    109:        field-name     = token
                    110:        field-value    = *( field-content | LWS )
                    111: 
                    112:        field-content  = <the OCTETs making up the field-value
                    113:                         and consisting of either *TEXT or combinations
                    114:                         of token, tspecials, and quoted-string>
                    115: 
                    116: 
                    117: 
                    118: word           = token | quoted-string
                    119: 
                    120: token          = 1*<any CHAR except CTLs or tspecials>
                    121: 
                    122: 
                    123: 
                    124: tspecials      = "(" | ")" | "<" | ">" | "@"
                    125:                       | "," | ";" | ":" | "\" | <">
                    126:                       | "/" | "[" | "]" | "?" | "="
                    127:                       | "{" | "}" | SP | HT
                    128: 
                    129: SP             = <US-ASCII SP, space (32)>
                    130: HT             = <US-ASCII HT, horizontal-tab (9)>
                    131: 
                    132: LWS            = [CRLF] 1*( SP | HT )
                    133: TEXT           = <any OCTET except CTLs,
                    134:                         but including LWS>
                    135: 
                    136: quoted-pair    = "\" CHAR
                    137: 
                    138:   if(strchr("()<>@,;:\\\"/[]?={} \t", *ptr))
                    139: */
                    140: inline bool need_quote_http_header(const char *ptr, size_t size) {
                    141:        for(; size--; ptr++)
1.42      paf       142:                if(strchr(";\\\"= \t" /* excluded ()<>@, :/ ? []{} */, *ptr))
1.41      paf       143:                        return true;
                    144:        return false;
                    145: }
                    146: 
1.55      parser    147: /** @todo maybe additional check "are all pieces are clean?" would be profitable?
                    148:        @todo fix potential forigins_mode buf overrun
                    149: */
1.51      parser    150: size_t String::cstr_bufsize(Untaint_lang lang) const {
1.70    ! paf       151:        return  (
        !           152:                        lang==UL_AS_IS?
        !           153:                                size()
        !           154:                        :
        !           155:                                size()
        !           156:                                *UNTAINT_TIMES_BIGGER
        !           157:                                *(forigins_mode?10:1)
        !           158:                ) 
        !           159:                +1;
1.51      parser    160: }
                    161: 
1.54      parser    162: /** @todo fix theoretical \n mem overrun in TYPO replacements
                    163: */
1.43      paf       164: char *String::store_to(char *dest, Untaint_lang lang, 
                    165:                                           SQL_Connection *connection,
                    166:                                           const char *charset) const {
1.13      paf       167:        // $MAIN:html-typo table
1.58      parser    168:        Dictionary *user_typo_dict=static_cast<Dictionary *>(pool().tag());
                    169:        Dictionary *typo_dict=user_typo_dict?user_typo_dict:default_typo_dict;
1.1       paf       170: 
1.44      paf       171:        bool whitespace=true;
1.1       paf       172:        const Chunk *chunk=&head; 
                    173:        do {
                    174:                const Chunk::Row *row=chunk->rows;
1.28      paf       175:                for(size_t i=0; i<chunk->count; i++, row++) {
1.1       paf       176:                        if(row==append_here)
                    177:                                goto break2;
                    178: 
1.55      parser    179:                        Untaint_lang to_lang=lang==UL_UNSPECIFIED?row->item.lang:lang;
                    180: 
                    181:                        char *dest_before_origins=dest;
                    182: 
                    183:                        if(forigins_mode) {
                    184: #ifndef NO_STRING_ORIGIN
                    185:                                if(row->item.origin.file)
                    186:                                        dest+=sprintf(dest, "%s(%d)",
                    187:                                                row->item.origin.file,
                    188:                                                1+row->item.origin.line);
                    189:                                else
                    190:                                        dest+=sprintf(dest, "unknown");
                    191: #endif
1.56      parser    192:                                dest+=sprintf(dest, "#%s: ",
                    193:                                        String_Untaint_lang_name[to_lang]);
1.55      parser    194:                        }
                    195:                        char *dest_after_origins=dest;
                    196: 
1.1       paf       197:                        // WARNING:
                    198:                        //      string can grow only UNTAINT_TIMES_BIGGER
1.55      parser    199:                        switch(to_lang) {
1.29      paf       200:                        case UL_CLEAN:
1.1       paf       201:                                // clean piece
1.44      paf       202:                                { // optimizing whitespace
                    203:                                        const char *src=row->item.ptr; 
                    204:                                        for(int size=row->item.size; size--; src++)
                    205:                                                switch(*src) {
1.70    ! paf       206:                                                /***case ' ': case '\n': case '\t':
1.44      paf       207:                                                        if(!whitespace) {
                    208:                                                                *dest++=*src;
                    209:                                                                whitespace=true;
                    210:                                                        }
1.70    ! paf       211:                                                        break;*/
1.44      paf       212:                                                default:
                    213:                                                        whitespace=false;
                    214:                                                        *dest++=*src;
                    215:                                                        break;
                    216:                                                }
                    217:                                }
                    218:                                break;
1.29      paf       219:                        case UL_TAINTED:
1.1       paf       220:                                // tainted piece, but undefined untaint language
1.23      paf       221:                                // for VString.as_double of tainted values
1.1       paf       222:                                // for ^process{body} evaluation
1.11      paf       223:                        case UL_AS_IS:
1.1       paf       224:                                // tainted, untaint language: as-is
1.13      paf       225:                                memcpy(dest, row->item.ptr, row->item.size); 
                    226:                                dest+=row->item.size;
1.1       paf       227:                                break;
1.62      parser    228:                        case UL_FILE_SPEC:
1.9       paf       229:                                // tainted, untaint language: file [name]
1.18      paf       230:                                escape(switch(*src) {
                    231:                                        case ' ': to_char('_');  break;
1.39      paf       232:                                        encode(need_file_encode, '+');
1.18      paf       233:                                });
1.9       paf       234:                                break;
1.11      paf       235:                        case UL_URI:
1.4       paf       236:                                // tainted, untaint language: uri
1.18      paf       237:                                escape(switch(*src) {
                    238:                                        case ' ': to_char('+');  break;
1.13      paf       239:                                        encode(need_uri_encode, '%');
1.18      paf       240:                                });
1.5       paf       241:                                break;
1.36      paf       242:                        case UL_HTTP_HEADER:
1.67      parser    243:                                // tainted, untaint language: http-field-content-text
                    244:                                escape(switch(*src) {
                    245:                                        case ' ': to_char('+');  break;
                    246:                                        encode(need_uri_encode, '%');
                    247:                                });
1.36      paf       248:                                break;
                    249:                        case UL_MAIL_HEADER:
                    250:                                // tainted, untaint language: mail-header
1.46      paf       251:                                if(charset) {
1.43      paf       252:                                        // Subject: Re: parser3: =?koi8-r?Q?=D3=C5=CD=C9=CE=C1=D2?=
                    253:                                        const char *src=row->item.ptr; 
1.59      parser    254:                                        bool to_quoted_printable=false;
1.43      paf       255:                                        for(int size=row->item.size; size--; src++) {
                    256:                                                if(*src & 0x80) {
1.59      parser    257:                                                        if(!to_quoted_printable) {
1.43      paf       258:                                                                dest+=sprintf(dest, "=?%.15s?Q?", charset);
1.59      parser    259:                                                                to_quoted_printable=true;
1.43      paf       260:                                                        }
1.45      paf       261:                                                        dest+=sprintf(dest, "=%02X", *src & 0xFF);
1.43      paf       262:                                                } else {
                    263:                                                        *dest++=*src;                                           
                    264:                                                }
                    265:                                        }
1.59      parser    266:                                        if(to_quoted_printable) // close
1.43      paf       267:                                                dest+=sprintf(dest, "?=");
1.46      paf       268:                                } else {
                    269:                                        memcpy(dest, row->item.ptr, row->item.size); 
                    270:                                        dest+=row->item.size;
1.43      paf       271:                                }
1.4       paf       272:                                break;
1.11      paf       273:                        case UL_TABLE: 
1.15      paf       274:                                // tainted, untaint language: table
1.18      paf       275:                                escape(switch(*src) {
                    276:                                        case '\t': to_char(' ');  break;
                    277:                                        case '\n': to_char(' ');  break;
1.13      paf       278:                                        _default;
1.18      paf       279:                                });
1.1       paf       280:                                break;
1.11      paf       281:                        case UL_SQL:
1.1       paf       282:                                // tainted, untaint language: sql
1.34      paf       283:                                if(connection)
                    284:                                        dest+=connection->quote(dest, row->item.ptr, row->item.size);
                    285:                                else
1.69      parser    286:                                        throw Exception(0, 0,
1.34      paf       287:                                                this,
                    288:                                                "untaint in SQL language failed - no connection specified");
1.1       paf       289:                                break;
1.11      paf       290:                        case UL_JS:
1.18      paf       291:                                escape(switch(*src) {
                    292:                                        case '"': to_string("\\\"", 2);  break;
                    293:                                        case '\'': to_string("\\'", 2);  break;
                    294:                                        case '\n': to_string("\\n", 2);  break;
                    295:                                        case '\\': to_string("\\\\", 2);  break;
                    296:                                        case '\xFF': to_string("\\\xFF", 2);  break;
1.13      paf       297:                                        _default;
1.18      paf       298:                                });
1.1       paf       299:                                break;
1.61      parser    300:                        case UL_XML:
                    301:                                escape(switch(*src) {
                    302:                                        case '&': to_string("&amp;", 5);  break;
                    303:                                        case '>': to_string("&gt;", 4);  break;
                    304:                                        case '<': to_string("&lt;", 4);  break;
                    305:                                        case '"': to_string("&quot;", 6);  break;
                    306:                                        case '\'': to_string("&apos;", 6);  break;
                    307:                                        _default;
                    308:                                });
                    309:                                break;
1.11      paf       310:                        case UL_HTML:
1.18      paf       311:                                escape(switch(*src) {
                    312:                                        case '&': to_string("&amp;", 5);  break;
                    313:                                        case '>': to_string("&gt;", 4);  break;
                    314:                                        case '<': to_string("&lt;", 4);  break;
                    315:                                        case '"': to_string("&quot;", 6);  break;
1.13      paf       316:                                        _default;
1.18      paf       317:                                });
1.1       paf       318:                                break;
1.47      paf       319:                        case UL_USER_HTML: {
1.1       paf       320:                                // tainted, untaint language: html-typo
1.58      parser    321:                                if(!typo_dict) // never, always has default
1.69      parser    322:                                        throw Exception(0, 0,
1.57      parser    323:                                                this,
                    324:                                                "untaint to user-html lang failed, no typo table");
                    325: 
1.50      parser    326:                                char *html_for_typo=
1.70    ! paf       327:                                        (char *)malloc(row->item.size*2/* '\n' -> '\' 'n' */+1,16);
1.19      paf       328:                                // note:
                    329:                                //   there still is a possibility that user 
                    330:                                //   would not replace \n as she supposed to
                    331:                                //   and rather replace \ and n into huge strings
                    332:                                //   thus causing memory overrun
                    333:                                //   this can be dealed by allocating *2 memory, but that's too expensive
1.18      paf       334:                                size_t html_for_typo_size;
1.13      paf       335:                                { // local dest
1.18      paf       336:                                        char *dest=html_for_typo;
                    337:                                        escape(switch(*src) {
1.16      paf       338:                                                // convinient name for typo match "\n"
1.18      paf       339:                                                case '\n': 
1.57      parser    340:                                                        to_string("\\n", 2);
1.16      paf       341:                                                        break;
1.13      paf       342:                                                _default;
1.18      paf       343:                                        });
1.13      paf       344:                                        *dest=0;
1.18      paf       345:                                        html_for_typo_size=dest-html_for_typo;
1.13      paf       346:                                }
                    347:                                // typo table replacements
1.21      paf       348:                                const char *src=html_for_typo;
                    349:                                do {
                    350:                                        // there is a row where first column starts 'src'
1.58      parser    351:                                        if(Table::Item *item=typo_dict->first_that_starts(src)) {
1.21      paf       352:                                                // get a=>b values
                    353:                                                const String& a=*static_cast<Array *>(item)->get_string(0);
                    354:                                                const String& b=*static_cast<Array *>(item)->get_string(1);
                    355:                                                // overflow check:
                    356:                                                //   b allowed to be max UNTAINT_TIMES_BIGGER then a
                    357:                                                if(b.size()>UNTAINT_TIMES_BIGGER*a.size()) {
1.58      parser    358:                                                        pool().set_tag(0); // avoid recursion
1.69      parser    359:                                                        throw Exception(0, 0, 
1.21      paf       360:                                                                &b, 
                    361:                                                                "is %g times longer then '%s', "
                    362:                                                                "while maximum, handled by Parser, is %d", 
1.50      parser    363:                                                                        ((double)b.size())/a.size(), 
                    364:                                                                        a.cstr(), 
                    365:                                                                        UNTAINT_TIMES_BIGGER);
1.21      paf       366:                                                }
                    367:                                                
                    368:                                                // skip 'a' in 'src'
                    369:                                                src+=a.size();
                    370:                                                // write 'b' to 'dest'
                    371:                                                b.store_to(dest);
1.59      parser    372:                                                // skip 'b' in 'dest'
1.21      paf       373:                                                dest+=b.size();
                    374:                                        } else
                    375:                                                *dest++=*src++;
                    376:                                } while(*src);
1.1       paf       377:                                break;
1.13      paf       378:                                }
1.1       paf       379:                        default:
1.69      parser    380:                                throw Exception(0, 0, 
1.18      paf       381:                                        this, 
1.1       paf       382:                                        "unknown untaint language #%d of %d piece", 
1.18      paf       383:                                                static_cast<int>(row->item.lang), 
1.38      paf       384:                                                i); // never
1.48      parser    385:                                break; // never
1.1       paf       386:                        }
1.44      paf       387: 
                    388:                        if((lang==UL_UNSPECIFIED?row->item.lang:lang)!=UL_CLEAN)
                    389:                                whitespace=false;
1.55      parser    390: 
                    391:                        if(forigins_mode)
                    392:                                if(dest==dest_after_origins) // never moved==optimized space
                    393:                                        dest=dest_before_origins;
                    394:                                else {
1.66      parser    395:                                        remove_crlf(dest_after_origins, dest);
1.55      parser    396: 
                    397:                                        to_char('\n');
                    398:                                }
1.1       paf       399:                }
                    400:                chunk=row->link;
                    401:        } while(chunk);
1.64      parser    402: 
1.1       paf       403: break2:
1.13      paf       404:        return dest;
1.1       paf       405: }

E-mail: