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

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

E-mail: