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