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

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

E-mail: