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

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.8       paf         5: 
1.13      paf         6:        Author: Alexander Petrosyan <paf@design.ru>(http://design.ru/paf)
1.1       paf         7: 
1.51    ! parser      8:        $Id: untaint.C,v 1.50 2001/05/19 19:10:20 parser Exp $
1.1       paf         9: */
                     10: 
                     11: #include "pa_pool.h"
                     12: #include "pa_string.h"
                     13: #include "pa_hash.h"
                     14: #include "pa_exception.h"
1.13      paf        15: #include "pa_table.h"
1.32      paf        16: #include "pa_globals.h"
1.34      paf        17: #include "pa_sql_connection.h"
1.1       paf        18: 
1.18      paf        19: #define escape(action) \
1.1       paf        20:        { \
1.13      paf        21:                const char *src=row->item.ptr; \
                     22:                for(int size=row->item.size; size--; src++) \
1.18      paf        23:                        action \
1.1       paf        24:        }
1.13      paf        25: #define _default  default: *dest++=*src; break
                     26: #define encode(need_encode_func, prefix)  \
1.5       paf        27:                default: \
1.13      paf        28:                        if(need_encode_func(*src)) { \
1.5       paf        29:                                static const char *hex="0123456789ABCDEF"; \
1.9       paf        30:                                char chunk[3]={prefix}; \
1.13      paf        31:                                chunk[1]=hex[((unsigned char)*src)/0x10]; \
                     32:                                chunk[2]=hex[((unsigned char)*src)%0x10]; \
                     33:                                strncpy(dest, chunk, 3);  dest+=3; \
1.5       paf        34:                        } else \
1.13      paf        35:                                *dest++=*src; \
1.5       paf        36:                        break
1.18      paf        37: #define to_char(c)  *dest++=c
                     38: #define to_string(b, bsize)  \
                     39:                strncpy(dest, b, bsize); \
                     40:                dest+=bsize; \
1.4       paf        41: 
1.9       paf        42: inline bool need_file_encode(unsigned char c){
1.13      paf        43:     if((c>='0') &&(c<='9') ||(c>='A') &&(c<='Z') ||(c>='a') &&(c<='z')) 
1.9       paf        44:                return false;
                     45: 
1.31      paf        46:     return !strchr(
                     47: #ifdef WIN32
1.37      paf        48:                ":\\~"
1.31      paf        49: #endif
1.39      paf        50:                "./()_-", c);
1.9       paf        51: }
1.5       paf        52: inline bool need_uri_encode(unsigned char c){
1.13      paf        53:     if((c>='0') &&(c<='9') ||(c>='A') &&(c<='Z') ||(c>='a') &&(c<='z')) 
1.4       paf        54:                return false;
                     55: 
1.5       paf        56:     return !strchr("_-./", c);
                     57: }
1.36      paf        58: inline bool need_http_header_encode(unsigned char c){
1.18      paf        59:     if(strchr(" , :", c))
1.5       paf        60:                return false;
                     61: 
                     62:        return need_uri_encode(c);
1.4       paf        63: }
1.1       paf        64: 
                     65: // String
                     66: 
1.13      paf        67: static bool typo_present(Array::Item *value, const void *info) {
                     68:        Array *row=static_cast<Array *>(value);
                     69:        const char *src=static_cast<const char *>(info);
                     70: 
                     71:        int partial;
1.28      paf        72:        row->get_string(0)->cmp(partial, src);
1.14      paf        73:        return 
                     74:                partial==0 || // full match
                     75:                partial==1; // typo left column starts 'src'
1.13      paf        76: }
                     77: 
1.41      paf        78: /*
                     79: 
                     80: HTTP-header    = field-name ":" [ field-value ] CRLF
                     81: 
                     82:        field-name     = token
                     83:        field-value    = *( field-content | LWS )
                     84: 
                     85:        field-content  = <the OCTETs making up the field-value
                     86:                         and consisting of either *TEXT or combinations
                     87:                         of token, tspecials, and quoted-string>
                     88: 
                     89: 
                     90: 
                     91: word           = token | quoted-string
                     92: 
                     93: token          = 1*<any CHAR except CTLs or tspecials>
                     94: 
                     95: 
                     96: 
                     97: tspecials      = "(" | ")" | "<" | ">" | "@"
                     98:                       | "," | ";" | ":" | "\" | <">
                     99:                       | "/" | "[" | "]" | "?" | "="
                    100:                       | "{" | "}" | SP | HT
                    101: 
                    102: SP             = <US-ASCII SP, space (32)>
                    103: HT             = <US-ASCII HT, horizontal-tab (9)>
                    104: 
                    105: LWS            = [CRLF] 1*( SP | HT )
                    106: TEXT           = <any OCTET except CTLs,
                    107:                         but including LWS>
                    108: 
                    109: quoted-pair    = "\" CHAR
                    110: 
                    111:   if(strchr("()<>@,;:\\\"/[]?={} \t", *ptr))
                    112: */
                    113: inline bool need_quote_http_header(const char *ptr, size_t size) {
                    114:        for(; size--; ptr++)
1.42      paf       115:                if(strchr(";\\\"= \t" /* excluded ()<>@, :/ ? []{} */, *ptr))
1.41      paf       116:                        return true;
                    117:        return false;
                    118: }
                    119: 
1.51    ! parser    120: /// @todo maybe additional check "are all pieces are clean?" would be profitable?
        !           121: size_t String::cstr_bufsize(Untaint_lang lang) const {
        !           122:        return (lang==UL_AS_IS?size():size()*UNTAINT_TIMES_BIGGER) +1;
        !           123: }
        !           124: 
        !           125: ///    @todo fix theoretical \n mem overrun in TYPO replacements
1.43      paf       126: char *String::store_to(char *dest, Untaint_lang lang, 
                    127:                                           SQL_Connection *connection,
                    128:                                           const char *charset) const {
1.13      paf       129:        // $MAIN:html-typo table
1.26      paf       130:        Table *user_typo_table=static_cast<Table *>(pool().tag());
                    131:        Table *typo_table=user_typo_table?user_typo_table:default_typo_table;
1.1       paf       132: 
1.44      paf       133:        bool whitespace=true;
1.1       paf       134:        const Chunk *chunk=&head; 
                    135:        do {
                    136:                const Chunk::Row *row=chunk->rows;
1.28      paf       137:                for(size_t i=0; i<chunk->count; i++, row++) {
1.1       paf       138:                        if(row==append_here)
                    139:                                goto break2;
                    140: 
                    141:                        // WARNING:
                    142:                        //      string can grow only UNTAINT_TIMES_BIGGER
1.35      paf       143:                        switch(lang==UL_UNSPECIFIED?row->item.lang:lang) {
1.29      paf       144:                        case UL_CLEAN:
1.1       paf       145:                                // clean piece
1.44      paf       146:                                { // optimizing whitespace
                    147:                                        const char *src=row->item.ptr; 
                    148:                                        for(int size=row->item.size; size--; src++)
                    149:                                                switch(*src) {
                    150:                                                case ' ': case '\n': case '\r': case '\t':
                    151:                                                        if(!whitespace) {
                    152:                                                                *dest++=*src;
                    153:                                                                whitespace=true;
                    154:                                                        }
                    155:                                                        break;
                    156:                                                default:
                    157:                                                        whitespace=false;
                    158:                                                        *dest++=*src;
                    159:                                                        break;
                    160:                                                }
                    161:                                }
                    162:                                break;
1.29      paf       163:                        case UL_TAINTED:
1.1       paf       164:                                // tainted piece, but undefined untaint language
1.23      paf       165:                                // for VString.as_double of tainted values
1.1       paf       166:                                // for ^process{body} evaluation
1.11      paf       167:                        case UL_AS_IS:
1.1       paf       168:                                // tainted, untaint language: as-is
1.13      paf       169:                                memcpy(dest, row->item.ptr, row->item.size); 
                    170:                                dest+=row->item.size;
1.1       paf       171:                                break;
1.11      paf       172:                        case UL_FILE_NAME:
1.9       paf       173:                                // tainted, untaint language: file [name]
1.18      paf       174:                                escape(switch(*src) {
                    175:                                        case ' ': to_char('_');  break;
1.39      paf       176:                                        encode(need_file_encode, '+');
1.18      paf       177:                                });
1.9       paf       178:                                break;
1.11      paf       179:                        case UL_URI:
1.4       paf       180:                                // tainted, untaint language: uri
1.18      paf       181:                                escape(switch(*src) {
                    182:                                        case ' ': to_char('+');  break;
1.13      paf       183:                                        encode(need_uri_encode, '%');
1.18      paf       184:                                });
1.5       paf       185:                                break;
1.36      paf       186:                        case UL_HTTP_HEADER:
                    187:                                // tainted, untaint language: http-header
1.41      paf       188:                                if(need_quote_http_header(row->item.ptr, row->item.size)) {
                    189:                                        *dest++='\"';
                    190:                                        escape(switch(*src) {
                    191:                                                case '\"': to_string("\\\"", 2);  break;
                    192:                                                _default;
                    193:                                        });
                    194:                                        *dest++='\"';
                    195:                                } else {
                    196:                                        memcpy(dest, row->item.ptr, row->item.size); 
                    197:                                        dest+=row->item.size;
                    198:                                }
1.36      paf       199:                                break;
                    200:                        case UL_MAIL_HEADER:
                    201:                                // tainted, untaint language: mail-header
1.46      paf       202:                                if(charset) {
1.43      paf       203:                                        // Subject: Re: parser3: =?koi8-r?Q?=D3=C5=CD=C9=CE=C1=D2?=
                    204:                                        const char *src=row->item.ptr; 
1.45      paf       205:                                        bool to_base_64=false;
1.43      paf       206:                                        for(int size=row->item.size; size--; src++) {
                    207:                                                if(*src & 0x80) {
1.45      paf       208:                                                        if(!to_base_64) {
1.43      paf       209:                                                                dest+=sprintf(dest, "=?%.15s?Q?", charset);
1.45      paf       210:                                                                to_base_64=true;
1.43      paf       211:                                                        }
1.45      paf       212:                                                        dest+=sprintf(dest, "=%02X", *src & 0xFF);
1.43      paf       213:                                                } else {
                    214:                                                        *dest++=*src;                                           
                    215:                                                }
                    216:                                        }
1.45      paf       217:                                        if(to_base_64) // close
1.43      paf       218:                                                dest+=sprintf(dest, "?=");
1.46      paf       219:                                } else {
                    220:                                        memcpy(dest, row->item.ptr, row->item.size); 
                    221:                                        dest+=row->item.size;
1.43      paf       222:                                }
1.4       paf       223:                                break;
1.11      paf       224:                        case UL_TABLE: 
1.15      paf       225:                                // tainted, untaint language: table
1.18      paf       226:                                escape(switch(*src) {
                    227:                                        case '\t': to_char(' ');  break;
                    228:                                        case '\n': to_char(' ');  break;
1.13      paf       229:                                        _default;
1.18      paf       230:                                });
1.1       paf       231:                                break;
1.11      paf       232:                        case UL_SQL:
1.1       paf       233:                                // tainted, untaint language: sql
1.34      paf       234:                                if(connection)
                    235:                                        dest+=connection->quote(dest, row->item.ptr, row->item.size);
                    236:                                else
                    237:                                        THROW(0, 0,
                    238:                                                this,
                    239:                                                "untaint in SQL language failed - no connection specified");
1.1       paf       240:                                break;
1.11      paf       241:                        case UL_JS:
1.18      paf       242:                                escape(switch(*src) {
                    243:                                        case '"': to_string("\\\"", 2);  break;
                    244:                                        case '\'': to_string("\\'", 2);  break;
                    245:                                        case '\n': to_string("\\n", 2);  break;
                    246:                                        case '\\': to_string("\\\\", 2);  break;
                    247:                                        case '\xFF': to_string("\\\xFF", 2);  break;
1.13      paf       248:                                        _default;
1.18      paf       249:                                });
1.1       paf       250:                                break;
1.11      paf       251:                        case UL_HTML:
1.18      paf       252:                                escape(switch(*src) {
                    253:                                        case '&': to_string("&amp;", 5);  break;
                    254:                                        case '>': to_string("&gt;", 4);  break;
                    255:                                        case '<': to_string("&lt;", 4);  break;
                    256:                                        case '"': to_string("&quot;", 6);  break;
1.19      paf       257:                                        //TODO: XSLT case '\'': to_string("&apos;", 6);  break;
1.13      paf       258:                                        _default;
1.18      paf       259:                                });
1.1       paf       260:                                break;
1.47      paf       261:                        case UL_USER_HTML: {
1.1       paf       262:                                // tainted, untaint language: html-typo
1.50      parser    263:                                char *html_for_typo=
                    264:                                        (char *)malloc(row->item.size*2/* '\n' -> '\' 'n' */+1);
1.19      paf       265:                                // note:
                    266:                                //   there still is a possibility that user 
                    267:                                //   would not replace \n as she supposed to
                    268:                                //   and rather replace \ and n into huge strings
                    269:                                //   thus causing memory overrun
                    270:                                //   this can be dealed by allocating *2 memory, but that's too expensive
1.18      paf       271:                                size_t html_for_typo_size;
1.13      paf       272:                                { // local dest
1.18      paf       273:                                        char *dest=html_for_typo;
                    274:                                        escape(switch(*src) {
1.16      paf       275:                                                // convinient name for typo match "\n"
                    276:                                                case '\r': 
1.18      paf       277:                                                        if(typo_table) {
                    278:                                                                *dest++='\\';  *dest++='n'; // \r -> \n
1.24      paf       279:                                                                if(src[1]=='\n') { // \r\n -> remove \n
                    280:                                                                        size--; src++;
                    281:                                                                }
1.18      paf       282:                                                        }
                    283:                                                        break;
                    284:                                                case '\n': 
                    285:                                                        if(typo_table)
                    286:                                                                to_string("\\n", 2);
1.16      paf       287:                                                        break;
1.19      paf       288:                                                //TODO: XSLT case '\'': to_string("&apos;", 6);  break;
1.13      paf       289:                                                _default;
1.18      paf       290:                                        });
1.13      paf       291:                                        *dest=0;
1.18      paf       292:                                        html_for_typo_size=dest-html_for_typo;
1.13      paf       293:                                }
                    294:                                // typo table replacements
1.21      paf       295:                                const char *src=html_for_typo;
                    296:                                do {
                    297:                                        // there is a row where first column starts 'src'
                    298:                                        if(Table::Item *item=typo_table->first_that(typo_present, src)) {
                    299:                                                // get a=>b values
                    300:                                                const String& a=*static_cast<Array *>(item)->get_string(0);
                    301:                                                const String& b=*static_cast<Array *>(item)->get_string(1);
                    302:                                                // empty 'a' | 'b' checks
                    303:                                                if(a.size()==0 || b.size()==0) {
1.26      paf       304:                                                        pool().set_tag(default_typo_table); // avoid recursion
1.21      paf       305:                                                        THROW(0, 0, 
                    306:                                                                typo_table->origin_string(), 
                    307:                                                                "typo table column elements must not be empty");
                    308:                                                }
                    309:                                                // overflow check:
                    310:                                                //   b allowed to be max UNTAINT_TIMES_BIGGER then a
                    311:                                                if(b.size()>UNTAINT_TIMES_BIGGER*a.size()) {
1.26      paf       312:                                                        pool().set_tag(default_typo_table); // avoid recursion
1.21      paf       313:                                                        THROW(0, 0, 
                    314:                                                                &b, 
                    315:                                                                "is %g times longer then '%s', "
                    316:                                                                "while maximum, handled by Parser, is %d", 
1.50      parser    317:                                                                        ((double)b.size())/a.size(), 
                    318:                                                                        a.cstr(), 
                    319:                                                                        UNTAINT_TIMES_BIGGER);
1.21      paf       320:                                                }
                    321:                                                
                    322:                                                // skip 'a' in 'src'
                    323:                                                src+=a.size();
                    324:                                                // write 'b' to 'dest'
                    325:                                                b.store_to(dest);
                    326:                                                dest+=b.size();
                    327:                                        } else
                    328:                                                *dest++=*src++;
                    329:                                } while(*src);
1.1       paf       330:                                break;
1.13      paf       331:                                }
1.1       paf       332:                        default:
1.18      paf       333:                                THROW(0, 0, 
                    334:                                        this, 
1.1       paf       335:                                        "unknown untaint language #%d of %d piece", 
1.18      paf       336:                                                static_cast<int>(row->item.lang), 
1.38      paf       337:                                                i); // never
1.48      parser    338:                                break; // never
1.1       paf       339:                        }
1.44      paf       340: 
                    341:                        if((lang==UL_UNSPECIFIED?row->item.lang:lang)!=UL_CLEAN)
                    342:                                whitespace=false;
1.1       paf       343:                }
                    344:                chunk=row->link;
                    345:        } while(chunk);
                    346: break2:
1.13      paf       347:        return dest;
1.1       paf       348: }

E-mail: