|
|
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.63 parser 5: Author: Alexander Petrosyan <paf@design.ru>(http://design.ru/paf)
1.8 paf 6:
1.66 ! parser 7: $Id: untaint.C,v 1.65 2001/10/08 08:52:45 parser Exp $
1.1 paf 8: */
9:
10: #include "pa_pool.h"
11: #include "pa_string.h"
12: #include "pa_hash.h"
13: #include "pa_exception.h"
1.13 paf 14: #include "pa_table.h"
1.32 paf 15: #include "pa_globals.h"
1.34 paf 16: #include "pa_sql_connection.h"
1.58 parser 17: #include "pa_dictionary.h"
1.66 ! parser 18: #include "pa_common.h"
1.1 paf 19:
1.18 paf 20: #define escape(action) \
1.1 paf 21: { \
1.13 paf 22: const char *src=row->item.ptr; \
23: for(int size=row->item.size; size--; src++) \
1.18 paf 24: action \
1.1 paf 25: }
1.13 paf 26: #define _default default: *dest++=*src; break
27: #define encode(need_encode_func, prefix) \
1.5 paf 28: default: \
1.13 paf 29: if(need_encode_func(*src)) { \
1.5 paf 30: static const char *hex="0123456789ABCDEF"; \
1.9 paf 31: char chunk[3]={prefix}; \
1.13 paf 32: chunk[1]=hex[((unsigned char)*src)/0x10]; \
33: chunk[2]=hex[((unsigned char)*src)%0x10]; \
1.60 parser 34: memcpy(dest, chunk, 3); dest+=3; \
1.5 paf 35: } else \
1.13 paf 36: *dest++=*src; \
1.5 paf 37: break
1.18 paf 38: #define to_char(c) *dest++=c
39: #define to_string(b, bsize) \
1.60 parser 40: memcpy(dest, b, bsize); \
1.18 paf 41: dest+=bsize; \
1.4 paf 42:
1.9 paf 43: inline bool need_file_encode(unsigned char c){
1.13 paf 44: if((c>='0') &&(c<='9') ||(c>='A') &&(c<='Z') ||(c>='a') &&(c<='z'))
1.9 paf 45: return false;
46:
1.31 paf 47: return !strchr(
48: #ifdef WIN32
1.37 paf 49: ":\\~"
1.31 paf 50: #endif
1.39 paf 51: "./()_-", c);
1.9 paf 52: }
1.5 paf 53: inline bool need_uri_encode(unsigned char c){
1.13 paf 54: if((c>='0') &&(c<='9') ||(c>='A') &&(c<='Z') ||(c>='a') &&(c<='z'))
1.4 paf 55: return false;
56:
1.5 paf 57: return !strchr("_-./", c);
58: }
1.36 paf 59: inline bool need_http_header_encode(unsigned char c){
1.18 paf 60: if(strchr(" , :", c))
1.5 paf 61: return false;
62:
63: return need_uri_encode(c);
1.4 paf 64: }
1.1 paf 65:
1.56 parser 66: //
67:
68: static const char * String_Untaint_lang_name[]={
69: "U", ///< zero value handy for hash lookup @see untaint_lang_name2enum
70: "C", ///< clean
71: "T", ///< tainted, untaint language as assigned later
72: // untaint languages. assigned by ^untaint[lang]{...}
73: "P",
74: /**<
75: leave language built into string being appended.
76: just a flag, that value not stored
77: */
78: "A", ///< leave all characters intact
79: "F", ///< filename
80: "H", ///< text in HTTP response header
81: "M", ///< text in mail header
82: "URI", ///< text in uri
83: "T", ///< ^table:set body
84: "SQL", ///< ^table:sql body
85: "JS", ///< JavaScript code
86: "HTML", ///< HTML code (for editing)
87: "UHTML", ///< HTML code with USER chars
88: };
89:
90:
1.1 paf 91: // String
92:
1.13 paf 93: static bool typo_present(Array::Item *value, const void *info) {
94: Array *row=static_cast<Array *>(value);
95: const char *src=static_cast<const char *>(info);
96:
97: int partial;
1.28 paf 98: row->get_string(0)->cmp(partial, src);
1.14 paf 99: return
100: partial==0 || // full match
101: partial==1; // typo left column starts 'src'
1.13 paf 102: }
103:
1.41 paf 104: /*
105:
106: HTTP-header = field-name ":" [ field-value ] CRLF
107:
108: field-name = token
109: field-value = *( field-content | LWS )
110:
111: field-content = <the OCTETs making up the field-value
112: and consisting of either *TEXT or combinations
113: of token, tspecials, and quoted-string>
114:
115:
116:
117: word = token | quoted-string
118:
119: token = 1*<any CHAR except CTLs or tspecials>
120:
121:
122:
123: tspecials = "(" | ")" | "<" | ">" | "@"
124: | "," | ";" | ":" | "\" | <">
125: | "/" | "[" | "]" | "?" | "="
126: | "{" | "}" | SP | HT
127:
128: SP = <US-ASCII SP, space (32)>
129: HT = <US-ASCII HT, horizontal-tab (9)>
130:
131: LWS = [CRLF] 1*( SP | HT )
132: TEXT = <any OCTET except CTLs,
133: but including LWS>
134:
135: quoted-pair = "\" CHAR
136:
137: if(strchr("()<>@,;:\\\"/[]?={} \t", *ptr))
138: */
139: inline bool need_quote_http_header(const char *ptr, size_t size) {
140: for(; size--; ptr++)
1.42 paf 141: if(strchr(";\\\"= \t" /* excluded ()<>@, :/ ? []{} */, *ptr))
1.41 paf 142: return true;
143: return false;
144: }
145:
1.55 parser 146: /** @todo maybe additional check "are all pieces are clean?" would be profitable?
147: @todo fix potential forigins_mode buf overrun
148: */
1.51 parser 149: size_t String::cstr_bufsize(Untaint_lang lang) const {
1.55 parser 150: return (lang==UL_AS_IS?size():size()*UNTAINT_TIMES_BIGGER*(forigins_mode?10:1)) +1;
1.51 parser 151: }
152:
1.54 parser 153: /** @todo fix theoretical \n mem overrun in TYPO replacements
154: */
1.43 paf 155: char *String::store_to(char *dest, Untaint_lang lang,
156: SQL_Connection *connection,
157: const char *charset) const {
1.13 paf 158: // $MAIN:html-typo table
1.58 parser 159: Dictionary *user_typo_dict=static_cast<Dictionary *>(pool().tag());
160: Dictionary *typo_dict=user_typo_dict?user_typo_dict:default_typo_dict;
1.1 paf 161:
1.44 paf 162: bool whitespace=true;
1.64 parser 163: bool need_to_close_http_header_quote=false;
1.1 paf 164: const Chunk *chunk=&head;
165: do {
166: const Chunk::Row *row=chunk->rows;
1.28 paf 167: for(size_t i=0; i<chunk->count; i++, row++) {
1.1 paf 168: if(row==append_here)
169: goto break2;
170:
1.55 parser 171: Untaint_lang to_lang=lang==UL_UNSPECIFIED?row->item.lang:lang;
172:
173: char *dest_before_origins=dest;
174:
175: if(forigins_mode) {
176: #ifndef NO_STRING_ORIGIN
177: if(row->item.origin.file)
178: dest+=sprintf(dest, "%s(%d)",
179: row->item.origin.file,
180: 1+row->item.origin.line);
181: else
182: dest+=sprintf(dest, "unknown");
183: #endif
1.56 parser 184: dest+=sprintf(dest, "#%s: ",
185: String_Untaint_lang_name[to_lang]);
1.55 parser 186: }
187: char *dest_after_origins=dest;
188:
1.1 paf 189: // WARNING:
190: // string can grow only UNTAINT_TIMES_BIGGER
1.55 parser 191: switch(to_lang) {
1.29 paf 192: case UL_CLEAN:
1.1 paf 193: // clean piece
1.44 paf 194: { // optimizing whitespace
195: const char *src=row->item.ptr;
196: for(int size=row->item.size; size--; src++)
197: switch(*src) {
198: case ' ': case '\n': case '\r': case '\t':
199: if(!whitespace) {
200: *dest++=*src;
201: whitespace=true;
202: }
203: break;
204: default:
205: whitespace=false;
206: *dest++=*src;
207: break;
208: }
209: }
210: break;
1.29 paf 211: case UL_TAINTED:
1.1 paf 212: // tainted piece, but undefined untaint language
1.23 paf 213: // for VString.as_double of tainted values
1.1 paf 214: // for ^process{body} evaluation
1.11 paf 215: case UL_AS_IS:
1.1 paf 216: // tainted, untaint language: as-is
1.13 paf 217: memcpy(dest, row->item.ptr, row->item.size);
218: dest+=row->item.size;
1.1 paf 219: break;
1.62 parser 220: case UL_FILE_SPEC:
1.9 paf 221: // tainted, untaint language: file [name]
1.18 paf 222: escape(switch(*src) {
223: case ' ': to_char('_'); break;
1.39 paf 224: encode(need_file_encode, '+');
1.18 paf 225: });
1.9 paf 226: break;
1.11 paf 227: case UL_URI:
1.4 paf 228: // tainted, untaint language: uri
1.18 paf 229: escape(switch(*src) {
230: case ' ': to_char('+'); break;
1.13 paf 231: encode(need_uri_encode, '%');
1.18 paf 232: });
1.5 paf 233: break;
1.36 paf 234: case UL_HTTP_HEADER:
235: // tainted, untaint language: http-header
1.41 paf 236: if(need_quote_http_header(row->item.ptr, row->item.size)) {
237: *dest++='\"';
238: escape(switch(*src) {
239: case '\"': to_string("\\\"", 2); break;
240: _default;
241: });
1.64 parser 242: need_to_close_http_header_quote=true;
1.41 paf 243: } else {
244: memcpy(dest, row->item.ptr, row->item.size);
245: dest+=row->item.size;
246: }
1.36 paf 247: break;
248: case UL_MAIL_HEADER:
249: // tainted, untaint language: mail-header
1.46 paf 250: if(charset) {
1.43 paf 251: // Subject: Re: parser3: =?koi8-r?Q?=D3=C5=CD=C9=CE=C1=D2?=
252: const char *src=row->item.ptr;
1.59 parser 253: bool to_quoted_printable=false;
1.43 paf 254: for(int size=row->item.size; size--; src++) {
255: if(*src & 0x80) {
1.59 parser 256: if(!to_quoted_printable) {
1.43 paf 257: dest+=sprintf(dest, "=?%.15s?Q?", charset);
1.59 parser 258: to_quoted_printable=true;
1.43 paf 259: }
1.45 paf 260: dest+=sprintf(dest, "=%02X", *src & 0xFF);
1.43 paf 261: } else {
262: *dest++=*src;
263: }
264: }
1.59 parser 265: if(to_quoted_printable) // close
1.43 paf 266: dest+=sprintf(dest, "?=");
1.46 paf 267: } else {
268: memcpy(dest, row->item.ptr, row->item.size);
269: dest+=row->item.size;
1.43 paf 270: }
1.4 paf 271: break;
1.11 paf 272: case UL_TABLE:
1.15 paf 273: // tainted, untaint language: table
1.18 paf 274: escape(switch(*src) {
275: case '\t': to_char(' '); break;
276: case '\n': to_char(' '); break;
1.13 paf 277: _default;
1.18 paf 278: });
1.1 paf 279: break;
1.11 paf 280: case UL_SQL:
1.1 paf 281: // tainted, untaint language: sql
1.34 paf 282: if(connection)
283: dest+=connection->quote(dest, row->item.ptr, row->item.size);
284: else
285: THROW(0, 0,
286: this,
287: "untaint in SQL language failed - no connection specified");
1.1 paf 288: break;
1.11 paf 289: case UL_JS:
1.18 paf 290: escape(switch(*src) {
291: case '"': to_string("\\\"", 2); break;
292: case '\'': to_string("\\'", 2); break;
293: case '\n': to_string("\\n", 2); break;
294: case '\\': to_string("\\\\", 2); break;
295: case '\xFF': to_string("\\\xFF", 2); break;
1.13 paf 296: _default;
1.18 paf 297: });
1.1 paf 298: break;
1.61 parser 299: case UL_XML:
300: escape(switch(*src) {
301: case '&': to_string("&", 5); break;
302: case '>': to_string(">", 4); break;
303: case '<': to_string("<", 4); break;
304: case '"': to_string(""", 6); break;
305: case '\'': to_string("'", 6); break;
306: _default;
307: });
308: break;
1.11 paf 309: case UL_HTML:
1.18 paf 310: escape(switch(*src) {
311: case '&': to_string("&", 5); break;
312: case '>': to_string(">", 4); break;
313: case '<': to_string("<", 4); break;
314: case '"': to_string(""", 6); break;
1.13 paf 315: _default;
1.18 paf 316: });
1.1 paf 317: break;
1.47 paf 318: case UL_USER_HTML: {
1.1 paf 319: // tainted, untaint language: html-typo
1.58 parser 320: if(!typo_dict) // never, always has default
1.57 parser 321: THROW(0, 0,
322: this,
323: "untaint to user-html lang failed, no typo table");
324:
1.50 parser 325: char *html_for_typo=
326: (char *)malloc(row->item.size*2/* '\n' -> '\' 'n' */+1);
1.19 paf 327: // note:
328: // there still is a possibility that user
329: // would not replace \n as she supposed to
330: // and rather replace \ and n into huge strings
331: // thus causing memory overrun
332: // this can be dealed by allocating *2 memory, but that's too expensive
1.18 paf 333: size_t html_for_typo_size;
1.13 paf 334: { // local dest
1.18 paf 335: char *dest=html_for_typo;
336: escape(switch(*src) {
1.16 paf 337: // convinient name for typo match "\n"
338: case '\r':
1.57 parser 339: to_string("\\n", 2); // \r -> "\n"
340: if(size && src[1]=='\n') { // \r\n -> remove \n
341: size--; src++;
1.18 paf 342: }
343: break;
344: case '\n':
1.57 parser 345: to_string("\\n", 2);
1.16 paf 346: break;
1.13 paf 347: _default;
1.18 paf 348: });
1.13 paf 349: *dest=0;
1.18 paf 350: html_for_typo_size=dest-html_for_typo;
1.13 paf 351: }
352: // typo table replacements
1.21 paf 353: const char *src=html_for_typo;
354: do {
355: // there is a row where first column starts 'src'
1.58 parser 356: if(Table::Item *item=typo_dict->first_that_starts(src)) {
1.21 paf 357: // get a=>b values
358: const String& a=*static_cast<Array *>(item)->get_string(0);
359: const String& b=*static_cast<Array *>(item)->get_string(1);
360: // overflow check:
361: // b allowed to be max UNTAINT_TIMES_BIGGER then a
362: if(b.size()>UNTAINT_TIMES_BIGGER*a.size()) {
1.58 parser 363: pool().set_tag(0); // avoid recursion
1.21 paf 364: THROW(0, 0,
365: &b,
366: "is %g times longer then '%s', "
367: "while maximum, handled by Parser, is %d",
1.50 parser 368: ((double)b.size())/a.size(),
369: a.cstr(),
370: UNTAINT_TIMES_BIGGER);
1.21 paf 371: }
372:
373: // skip 'a' in 'src'
374: src+=a.size();
375: // write 'b' to 'dest'
376: b.store_to(dest);
1.59 parser 377: // skip 'b' in 'dest'
1.21 paf 378: dest+=b.size();
379: } else
380: *dest++=*src++;
381: } while(*src);
1.1 paf 382: break;
1.13 paf 383: }
1.1 paf 384: default:
1.18 paf 385: THROW(0, 0,
386: this,
1.1 paf 387: "unknown untaint language #%d of %d piece",
1.18 paf 388: static_cast<int>(row->item.lang),
1.38 paf 389: i); // never
1.48 parser 390: break; // never
1.1 paf 391: }
1.44 paf 392:
393: if((lang==UL_UNSPECIFIED?row->item.lang:lang)!=UL_CLEAN)
394: whitespace=false;
1.55 parser 395:
396: if(forigins_mode)
397: if(dest==dest_after_origins) // never moved==optimized space
398: dest=dest_before_origins;
399: else {
1.66 ! parser 400: remove_crlf(dest_after_origins, dest);
1.55 parser 401:
402: to_char('\n');
403: }
1.1 paf 404: }
405: chunk=row->link;
406: } while(chunk);
1.64 parser 407:
408: if(need_to_close_http_header_quote)
409: *dest++='\"';
1.1 paf 410: break2:
1.13 paf 411: return dest;
1.1 paf 412: }