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

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.32    ! paf         8:        $Id: untaint.C,v 1.31 2001/04/03 17:01:03 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.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]; \
                     34:                                strncpy(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)  \
                     40:                strncpy(dest, b, bsize); \
                     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
                     49:                ":\\"
                     50: #endif
                     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: }
                     59: inline bool need_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: 
                     66: // String
                     67: 
1.13      paf        68: static bool typo_present(Array::Item *value, const void *info) {
                     69:        Array *row=static_cast<Array *>(value);
                     70:        const char *src=static_cast<const char *>(info);
                     71: 
                     72:        int partial;
1.28      paf        73:        row->get_string(0)->cmp(partial, src);
1.14      paf        74:        return 
                     75:                partial==0 || // full match
                     76:                partial==1; // typo left column starts 'src'
1.13      paf        77: }
                     78: 
1.30      paf        79: /**
                     80:        @test optimize whitespaces for all but 'html'
                     81:        @todo fix theoretical \n mem overrun in TYPO replacements
                     82: */
1.31      paf        83: char *String::store_to(char *dest, Untaint_lang lang) const {
1.13      paf        84:        // $MAIN:html-typo table
1.26      paf        85:        Table *user_typo_table=static_cast<Table *>(pool().tag());
                     86:        Table *typo_table=user_typo_table?user_typo_table:default_typo_table;
1.1       paf        87: 
                     88:        const Chunk *chunk=&head; 
                     89:        do {
                     90:                const Chunk::Row *row=chunk->rows;
1.28      paf        91:                for(size_t i=0; i<chunk->count; i++, row++) {
1.1       paf        92:                        if(row==append_here)
                     93:                                goto break2;
                     94: 
                     95:                        // WARNING:
                     96:                        //      string can grow only UNTAINT_TIMES_BIGGER
1.31      paf        97:                        switch(lang==UL_UNKNOWN?row->item.lang:lang) {
1.29      paf        98:                        case UL_CLEAN:
1.1       paf        99:                                // clean piece
1.29      paf       100:                        case UL_TAINTED:
1.1       paf       101:                                // tainted piece, but undefined untaint language
1.23      paf       102:                                // for VString.as_double of tainted values
1.1       paf       103:                                // for ^process{body} evaluation
1.11      paf       104:                        case UL_AS_IS:
1.1       paf       105:                                // tainted, untaint language: as-is
1.13      paf       106:                                memcpy(dest, row->item.ptr, row->item.size); 
                    107:                                dest+=row->item.size;
1.1       paf       108:                                break;
1.11      paf       109:                        case UL_FILE_NAME:
1.9       paf       110:                                // tainted, untaint language: file [name]
1.18      paf       111:                                escape(switch(*src) {
                    112:                                        case ' ': to_char('_');  break;
1.13      paf       113:                                        encode(need_file_encode, '-');
1.18      paf       114:                                });
1.9       paf       115:                                break;
1.11      paf       116:                        case UL_URI:
1.4       paf       117:                                // tainted, untaint language: uri
1.18      paf       118:                                escape(switch(*src) {
                    119:                                        case ' ': to_char('+');  break;
1.13      paf       120:                                        encode(need_uri_encode, '%');
1.18      paf       121:                                });
1.5       paf       122:                                break;
1.11      paf       123:                        case UL_HEADER:
1.5       paf       124:                                // tainted, untaint language: header
1.18      paf       125:                                escape(switch(*src) {
1.13      paf       126:                                        encode(need_header_encode, '%');
1.18      paf       127:                                });
1.4       paf       128:                                break;
1.11      paf       129:                        case UL_TABLE: 
1.15      paf       130:                                // tainted, untaint language: table
1.18      paf       131:                                escape(switch(*src) {
                    132:                                        case '\t': to_char(' ');  break;
                    133:                                        case '\n': to_char(' ');  break;
1.13      paf       134:                                        _default;
1.18      paf       135:                                });
1.1       paf       136:                                break;
1.11      paf       137:                        case UL_SQL:
1.1       paf       138:                                // tainted, untaint language: sql
                    139:                                // TODO: зависимость от sql сервера
1.13      paf       140:                                memset(dest, '?', row->item.size); 
                    141:                                dest+=row->item.size;
1.1       paf       142:                                break;
1.11      paf       143:                        case UL_JS:
1.18      paf       144:                                escape(switch(*src) {
                    145:                                        case '"': to_string("\\\"", 2);  break;
                    146:                                        case '\'': to_string("\\'", 2);  break;
                    147:                                        case '\n': to_string("\\n", 2);  break;
                    148:                                        case '\\': to_string("\\\\", 2);  break;
                    149:                                        case '\xFF': to_string("\\\xFF", 2);  break;
1.13      paf       150:                                        _default;
1.18      paf       151:                                });
1.1       paf       152:                                break;
1.11      paf       153:                        case UL_HTML:
1.18      paf       154:                                escape(switch(*src) {
                    155:                                        case '&': to_string("&amp;", 5);  break;
                    156:                                        case '>': to_string("&gt;", 4);  break;
                    157:                                        case '<': to_string("&lt;", 4);  break;
                    158:                                        case '"': to_string("&quot;", 6);  break;
1.19      paf       159:                                        //TODO: XSLT case '\'': to_string("&apos;", 6);  break;
1.13      paf       160:                                        _default;
1.18      paf       161:                                });
1.1       paf       162:                                break;
1.13      paf       163:                        case UL_HTML_TYPO: {
1.1       paf       164:                                // tainted, untaint language: html-typo
1.19      paf       165:                                char *html_for_typo=(char *)malloc(size()*2/* '\n' -> '\' 'n' */+1);
                    166:                                // note:
                    167:                                //   there still is a possibility that user 
                    168:                                //   would not replace \n as she supposed to
                    169:                                //   and rather replace \ and n into huge strings
                    170:                                //   thus causing memory overrun
                    171:                                //   this can be dealed by allocating *2 memory, but that's too expensive
1.18      paf       172:                                size_t html_for_typo_size;
1.13      paf       173:                                { // local dest
1.18      paf       174:                                        char *dest=html_for_typo;
                    175:                                        escape(switch(*src) {
1.16      paf       176:                                                // convinient name for typo match "\n"
                    177:                                                case '\r': 
1.18      paf       178:                                                        if(typo_table) {
                    179:                                                                *dest++='\\';  *dest++='n'; // \r -> \n
1.24      paf       180:                                                                if(src[1]=='\n') { // \r\n -> remove \n
                    181:                                                                        size--; src++;
                    182:                                                                }
1.18      paf       183:                                                        }
                    184:                                                        break;
                    185:                                                case '\n': 
                    186:                                                        if(typo_table)
                    187:                                                                to_string("\\n", 2);
1.16      paf       188:                                                        break;
1.19      paf       189:                                                //TODO: XSLT case '\'': to_string("&apos;", 6);  break;
1.13      paf       190:                                                _default;
1.18      paf       191:                                        });
1.13      paf       192:                                        *dest=0;
1.18      paf       193:                                        html_for_typo_size=dest-html_for_typo;
1.13      paf       194:                                }
                    195:                                // typo table replacements
1.21      paf       196:                                const char *src=html_for_typo;
                    197:                                do {
                    198:                                        // there is a row where first column starts 'src'
                    199:                                        if(Table::Item *item=typo_table->first_that(typo_present, src)) {
                    200:                                                // get a=>b values
                    201:                                                const String& a=*static_cast<Array *>(item)->get_string(0);
                    202:                                                const String& b=*static_cast<Array *>(item)->get_string(1);
                    203:                                                // empty 'a' | 'b' checks
                    204:                                                if(a.size()==0 || b.size()==0) {
1.26      paf       205:                                                        pool().set_tag(default_typo_table); // avoid recursion
1.21      paf       206:                                                        THROW(0, 0, 
                    207:                                                                typo_table->origin_string(), 
                    208:                                                                "typo table column elements must not be empty");
                    209:                                                }
                    210:                                                // overflow check:
                    211:                                                //   b allowed to be max UNTAINT_TIMES_BIGGER then a
                    212:                                                if(b.size()>UNTAINT_TIMES_BIGGER*a.size()) {
1.26      paf       213:                                                        pool().set_tag(default_typo_table); // avoid recursion
1.21      paf       214:                                                        THROW(0, 0, 
                    215:                                                                &b, 
                    216:                                                                "is %g times longer then '%s', "
                    217:                                                                "while maximum, handled by Parser, is %d", 
                    218:                                                                ((double)b.size())/a.size(), 
                    219:                                                                a.cstr(), 
                    220:                                                                UNTAINT_TIMES_BIGGER);
                    221:                                                }
                    222:                                                
                    223:                                                // skip 'a' in 'src'
                    224:                                                src+=a.size();
                    225:                                                // write 'b' to 'dest'
                    226:                                                b.store_to(dest);
                    227:                                                dest+=b.size();
                    228:                                        } else
                    229:                                                *dest++=*src++;
                    230:                                } while(*src);
1.1       paf       231:                                break;
1.13      paf       232:                                }
1.1       paf       233:                        default:
1.18      paf       234:                                THROW(0, 0, 
                    235:                                        this, 
1.1       paf       236:                                        "unknown untaint language #%d of %d piece", 
1.18      paf       237:                                                static_cast<int>(row->item.lang), 
1.1       paf       238:                                                i);
                    239:                        }
                    240:                }
                    241:                chunk=row->link;
                    242:        } while(chunk);
                    243: break2:
1.13      paf       244:        return dest;
1.1       paf       245: }

E-mail: