|
|
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.47 ! paf 8: $Id: untaint.C,v 1.46 2001/04/23 10:23:40 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
1.37 paf 50: ":\\~"
1.31 paf 51: #endif
1.39 paf 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: }
1.36 paf 60: inline bool need_http_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.41 paf 80: /*
81:
82: HTTP-header = field-name ":" [ field-value ] CRLF
83:
84: field-name = token
85: field-value = *( field-content | LWS )
86:
87: field-content = <the OCTETs making up the field-value
88: and consisting of either *TEXT or combinations
89: of token, tspecials, and quoted-string>
90:
91:
92:
93: word = token | quoted-string
94:
95: token = 1*<any CHAR except CTLs or tspecials>
96:
97:
98:
99: tspecials = "(" | ")" | "<" | ">" | "@"
100: | "," | ";" | ":" | "\" | <">
101: | "/" | "[" | "]" | "?" | "="
102: | "{" | "}" | SP | HT
103:
104: SP = <US-ASCII SP, space (32)>
105: HT = <US-ASCII HT, horizontal-tab (9)>
106:
107: LWS = [CRLF] 1*( SP | HT )
108: TEXT = <any OCTET except CTLs,
109: but including LWS>
110:
111: quoted-pair = "\" CHAR
112:
113: if(strchr("()<>@,;:\\\"/[]?={} \t", *ptr))
114: */
115: inline bool need_quote_http_header(const char *ptr, size_t size) {
116: for(; size--; ptr++)
1.42 paf 117: if(strchr(";\\\"= \t" /* excluded ()<>@, :/ ? []{} */, *ptr))
1.41 paf 118: return true;
119: return false;
120: }
121:
1.30 paf 122: /**
123: @todo fix theoretical \n mem overrun in TYPO replacements
124: */
1.43 paf 125: char *String::store_to(char *dest, Untaint_lang lang,
126: SQL_Connection *connection,
127: const char *charset) const {
1.13 paf 128: // $MAIN:html-typo table
1.26 paf 129: Table *user_typo_table=static_cast<Table *>(pool().tag());
130: Table *typo_table=user_typo_table?user_typo_table:default_typo_table;
1.1 paf 131:
1.44 paf 132: bool whitespace=true;
1.1 paf 133: const Chunk *chunk=&head;
134: do {
135: const Chunk::Row *row=chunk->rows;
1.28 paf 136: for(size_t i=0; i<chunk->count; i++, row++) {
1.1 paf 137: if(row==append_here)
138: goto break2;
139:
140: // WARNING:
141: // string can grow only UNTAINT_TIMES_BIGGER
1.35 paf 142: switch(lang==UL_UNSPECIFIED?row->item.lang:lang) {
1.29 paf 143: case UL_CLEAN:
1.1 paf 144: // clean piece
1.44 paf 145: { // optimizing whitespace
146: const char *src=row->item.ptr;
147: for(int size=row->item.size; size--; src++)
148: switch(*src) {
149: case ' ': case '\n': case '\r': case '\t':
150: if(!whitespace) {
151: *dest++=*src;
152: whitespace=true;
153: }
154: break;
155: default:
156: whitespace=false;
157: *dest++=*src;
158: break;
159: }
160: }
161: break;
1.29 paf 162: case UL_TAINTED:
1.1 paf 163: // tainted piece, but undefined untaint language
1.23 paf 164: // for VString.as_double of tainted values
1.1 paf 165: // for ^process{body} evaluation
1.11 paf 166: case UL_AS_IS:
1.1 paf 167: // tainted, untaint language: as-is
1.13 paf 168: memcpy(dest, row->item.ptr, row->item.size);
169: dest+=row->item.size;
1.1 paf 170: break;
1.11 paf 171: case UL_FILE_NAME:
1.9 paf 172: // tainted, untaint language: file [name]
1.18 paf 173: escape(switch(*src) {
174: case ' ': to_char('_'); break;
1.39 paf 175: encode(need_file_encode, '+');
1.18 paf 176: });
1.9 paf 177: break;
1.11 paf 178: case UL_URI:
1.4 paf 179: // tainted, untaint language: uri
1.18 paf 180: escape(switch(*src) {
181: case ' ': to_char('+'); break;
1.13 paf 182: encode(need_uri_encode, '%');
1.18 paf 183: });
1.5 paf 184: break;
1.36 paf 185: case UL_HTTP_HEADER:
186: // tainted, untaint language: http-header
1.41 paf 187: if(need_quote_http_header(row->item.ptr, row->item.size)) {
188: *dest++='\"';
189: escape(switch(*src) {
190: case '\"': to_string("\\\"", 2); break;
191: _default;
192: });
193: *dest++='\"';
194: } else {
195: memcpy(dest, row->item.ptr, row->item.size);
196: dest+=row->item.size;
197: }
1.36 paf 198: break;
199: case UL_MAIL_HEADER:
200: // tainted, untaint language: mail-header
1.46 paf 201: if(charset) {
1.43 paf 202: // Subject: Re: parser3: =?koi8-r?Q?=D3=C5=CD=C9=CE=C1=D2?=
203: const char *src=row->item.ptr;
1.45 paf 204: bool to_base_64=false;
1.43 paf 205: for(int size=row->item.size; size--; src++) {
206: if(*src & 0x80) {
1.45 paf 207: if(!to_base_64) {
1.43 paf 208: dest+=sprintf(dest, "=?%.15s?Q?", charset);
1.45 paf 209: to_base_64=true;
1.43 paf 210: }
1.45 paf 211: dest+=sprintf(dest, "=%02X", *src & 0xFF);
1.43 paf 212: } else {
213: *dest++=*src;
214: }
215: }
1.45 paf 216: if(to_base_64) // close
1.43 paf 217: dest+=sprintf(dest, "?=");
1.46 paf 218: } else {
219: memcpy(dest, row->item.ptr, row->item.size);
220: dest+=row->item.size;
1.43 paf 221: }
1.4 paf 222: break;
1.11 paf 223: case UL_TABLE:
1.15 paf 224: // tainted, untaint language: table
1.18 paf 225: escape(switch(*src) {
226: case '\t': to_char(' '); break;
227: case '\n': to_char(' '); break;
1.13 paf 228: _default;
1.18 paf 229: });
1.1 paf 230: break;
1.11 paf 231: case UL_SQL:
1.1 paf 232: // tainted, untaint language: sql
1.34 paf 233: if(connection)
234: dest+=connection->quote(dest, row->item.ptr, row->item.size);
235: else
236: THROW(0, 0,
237: this,
238: "untaint in SQL language failed - no connection specified");
1.1 paf 239: break;
1.11 paf 240: case UL_JS:
1.18 paf 241: escape(switch(*src) {
242: case '"': to_string("\\\"", 2); break;
243: case '\'': to_string("\\'", 2); break;
244: case '\n': to_string("\\n", 2); break;
245: case '\\': to_string("\\\\", 2); break;
246: case '\xFF': to_string("\\\xFF", 2); break;
1.13 paf 247: _default;
1.18 paf 248: });
1.1 paf 249: break;
1.11 paf 250: case UL_HTML:
1.18 paf 251: escape(switch(*src) {
252: case '&': to_string("&", 5); break;
253: case '>': to_string(">", 4); break;
254: case '<': to_string("<", 4); break;
255: case '"': to_string(""", 6); break;
1.19 paf 256: //TODO: XSLT case '\'': to_string("'", 6); break;
1.13 paf 257: _default;
1.18 paf 258: });
1.1 paf 259: break;
1.47 ! paf 260: case UL_USER_HTML: {
1.1 paf 261: // tainted, untaint language: html-typo
1.19 paf 262: char *html_for_typo=(char *)malloc(size()*2/* '\n' -> '\' 'n' */+1);
263: // note:
264: // there still is a possibility that user
265: // would not replace \n as she supposed to
266: // and rather replace \ and n into huge strings
267: // thus causing memory overrun
268: // this can be dealed by allocating *2 memory, but that's too expensive
1.18 paf 269: size_t html_for_typo_size;
1.13 paf 270: { // local dest
1.18 paf 271: char *dest=html_for_typo;
272: escape(switch(*src) {
1.16 paf 273: // convinient name for typo match "\n"
274: case '\r':
1.18 paf 275: if(typo_table) {
276: *dest++='\\'; *dest++='n'; // \r -> \n
1.24 paf 277: if(src[1]=='\n') { // \r\n -> remove \n
278: size--; src++;
279: }
1.18 paf 280: }
281: break;
282: case '\n':
283: if(typo_table)
284: to_string("\\n", 2);
1.16 paf 285: break;
1.19 paf 286: //TODO: XSLT case '\'': to_string("'", 6); break;
1.13 paf 287: _default;
1.18 paf 288: });
1.13 paf 289: *dest=0;
1.18 paf 290: html_for_typo_size=dest-html_for_typo;
1.13 paf 291: }
292: // typo table replacements
1.21 paf 293: const char *src=html_for_typo;
294: do {
295: // there is a row where first column starts 'src'
296: if(Table::Item *item=typo_table->first_that(typo_present, src)) {
297: // get a=>b values
298: const String& a=*static_cast<Array *>(item)->get_string(0);
299: const String& b=*static_cast<Array *>(item)->get_string(1);
300: // empty 'a' | 'b' checks
301: if(a.size()==0 || b.size()==0) {
1.26 paf 302: pool().set_tag(default_typo_table); // avoid recursion
1.21 paf 303: THROW(0, 0,
304: typo_table->origin_string(),
305: "typo table column elements must not be empty");
306: }
307: // overflow check:
308: // b allowed to be max UNTAINT_TIMES_BIGGER then a
309: if(b.size()>UNTAINT_TIMES_BIGGER*a.size()) {
1.26 paf 310: pool().set_tag(default_typo_table); // avoid recursion
1.21 paf 311: THROW(0, 0,
312: &b,
313: "is %g times longer then '%s', "
314: "while maximum, handled by Parser, is %d",
315: ((double)b.size())/a.size(),
316: a.cstr(),
317: UNTAINT_TIMES_BIGGER);
318: }
319:
320: // skip 'a' in 'src'
321: src+=a.size();
322: // write 'b' to 'dest'
323: b.store_to(dest);
324: dest+=b.size();
325: } else
326: *dest++=*src++;
327: } while(*src);
1.1 paf 328: break;
1.13 paf 329: }
1.1 paf 330: default:
1.18 paf 331: THROW(0, 0,
332: this,
1.1 paf 333: "unknown untaint language #%d of %d piece",
1.18 paf 334: static_cast<int>(row->item.lang),
1.38 paf 335: i); // never
1.1 paf 336: }
1.44 paf 337:
338: if((lang==UL_UNSPECIFIED?row->item.lang:lang)!=UL_CLEAN)
339: whitespace=false;
1.1 paf 340: }
341: chunk=row->link;
342: } while(chunk);
343: break2:
1.13 paf 344: return dest;
1.1 paf 345: }