|
|
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.45 ! paf 8: $Id: untaint.C,v 1.44 2001/04/23 09:38:53 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.43 paf 201: {
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, "?=");
218: }
1.4 paf 219: break;
1.11 paf 220: case UL_TABLE:
1.15 paf 221: // tainted, untaint language: table
1.18 paf 222: escape(switch(*src) {
223: case '\t': to_char(' '); break;
224: case '\n': to_char(' '); break;
1.13 paf 225: _default;
1.18 paf 226: });
1.1 paf 227: break;
1.11 paf 228: case UL_SQL:
1.1 paf 229: // tainted, untaint language: sql
1.34 paf 230: if(connection)
231: dest+=connection->quote(dest, row->item.ptr, row->item.size);
232: else
233: THROW(0, 0,
234: this,
235: "untaint in SQL language failed - no connection specified");
1.1 paf 236: break;
1.11 paf 237: case UL_JS:
1.18 paf 238: escape(switch(*src) {
239: case '"': to_string("\\\"", 2); break;
240: case '\'': to_string("\\'", 2); break;
241: case '\n': to_string("\\n", 2); break;
242: case '\\': to_string("\\\\", 2); break;
243: case '\xFF': to_string("\\\xFF", 2); break;
1.13 paf 244: _default;
1.18 paf 245: });
1.1 paf 246: break;
1.11 paf 247: case UL_HTML:
1.18 paf 248: escape(switch(*src) {
249: case '&': to_string("&", 5); break;
250: case '>': to_string(">", 4); break;
251: case '<': to_string("<", 4); break;
252: case '"': to_string(""", 6); break;
1.19 paf 253: //TODO: XSLT case '\'': to_string("'", 6); break;
1.13 paf 254: _default;
1.18 paf 255: });
1.1 paf 256: break;
1.13 paf 257: case UL_HTML_TYPO: {
1.1 paf 258: // tainted, untaint language: html-typo
1.19 paf 259: char *html_for_typo=(char *)malloc(size()*2/* '\n' -> '\' 'n' */+1);
260: // note:
261: // there still is a possibility that user
262: // would not replace \n as she supposed to
263: // and rather replace \ and n into huge strings
264: // thus causing memory overrun
265: // this can be dealed by allocating *2 memory, but that's too expensive
1.18 paf 266: size_t html_for_typo_size;
1.13 paf 267: { // local dest
1.18 paf 268: char *dest=html_for_typo;
269: escape(switch(*src) {
1.16 paf 270: // convinient name for typo match "\n"
271: case '\r':
1.18 paf 272: if(typo_table) {
273: *dest++='\\'; *dest++='n'; // \r -> \n
1.24 paf 274: if(src[1]=='\n') { // \r\n -> remove \n
275: size--; src++;
276: }
1.18 paf 277: }
278: break;
279: case '\n':
280: if(typo_table)
281: to_string("\\n", 2);
1.16 paf 282: break;
1.19 paf 283: //TODO: XSLT case '\'': to_string("'", 6); break;
1.13 paf 284: _default;
1.18 paf 285: });
1.13 paf 286: *dest=0;
1.18 paf 287: html_for_typo_size=dest-html_for_typo;
1.13 paf 288: }
289: // typo table replacements
1.21 paf 290: const char *src=html_for_typo;
291: do {
292: // there is a row where first column starts 'src'
293: if(Table::Item *item=typo_table->first_that(typo_present, src)) {
294: // get a=>b values
295: const String& a=*static_cast<Array *>(item)->get_string(0);
296: const String& b=*static_cast<Array *>(item)->get_string(1);
297: // empty 'a' | 'b' checks
298: if(a.size()==0 || b.size()==0) {
1.26 paf 299: pool().set_tag(default_typo_table); // avoid recursion
1.21 paf 300: THROW(0, 0,
301: typo_table->origin_string(),
302: "typo table column elements must not be empty");
303: }
304: // overflow check:
305: // b allowed to be max UNTAINT_TIMES_BIGGER then a
306: if(b.size()>UNTAINT_TIMES_BIGGER*a.size()) {
1.26 paf 307: pool().set_tag(default_typo_table); // avoid recursion
1.21 paf 308: THROW(0, 0,
309: &b,
310: "is %g times longer then '%s', "
311: "while maximum, handled by Parser, is %d",
312: ((double)b.size())/a.size(),
313: a.cstr(),
314: UNTAINT_TIMES_BIGGER);
315: }
316:
317: // skip 'a' in 'src'
318: src+=a.size();
319: // write 'b' to 'dest'
320: b.store_to(dest);
321: dest+=b.size();
322: } else
323: *dest++=*src++;
324: } while(*src);
1.1 paf 325: break;
1.13 paf 326: }
1.1 paf 327: default:
1.18 paf 328: THROW(0, 0,
329: this,
1.1 paf 330: "unknown untaint language #%d of %d piece",
1.18 paf 331: static_cast<int>(row->item.lang),
1.38 paf 332: i); // never
1.1 paf 333: }
1.44 paf 334:
335: if((lang==UL_UNSPECIFIED?row->item.lang:lang)!=UL_CLEAN)
336: whitespace=false;
1.1 paf 337: }
338: chunk=row->link;
339: } while(chunk);
340: break2:
1.13 paf 341: return dest;
1.1 paf 342: }