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