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

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

E-mail: