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

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

E-mail: