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