|
|
1.7 paf 1: /** @file
1.8 paf 2: Parser: String class part: untaint mechanizm.
3:
1.90 paf 4: Copyright(c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.89 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.8 paf 6:
1.98 ! paf 7: $Id: untaint.C,v 1.97 2002/02/21 14:44:37 paf 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.85 paf 19: #include "pa_charset.h"
1.1 paf 20:
1.95 paf 21: //#define DEBUG_STRING_APPENDS_VS_EXPANDS
1.91 paf 22:
23: #ifdef DEBUG_STRING_APPENDS_VS_EXPANDS
24: ulong string_string_shortcut_economy=0;
25: #endif
26:
1.18 paf 27: #define escape(action) \
1.1 paf 28: { \
1.13 paf 29: const char *src=row->item.ptr; \
30: for(int size=row->item.size; size--; src++) \
1.18 paf 31: action \
1.1 paf 32: }
1.13 paf 33: #define _default default: *dest++=*src; break
34: #define encode(need_encode_func, prefix) \
35: if(need_encode_func(*src)) { \
1.5 paf 36: static const char *hex="0123456789ABCDEF"; \
1.9 paf 37: char chunk[3]={prefix}; \
1.13 paf 38: chunk[1]=hex[((unsigned char)*src)/0x10]; \
39: chunk[2]=hex[((unsigned char)*src)%0x10]; \
1.60 parser 40: memcpy(dest, chunk, 3); dest+=3; \
1.5 paf 41: } else \
1.13 paf 42: *dest++=*src; \
1.5 paf 43: break
1.18 paf 44: #define to_char(c) *dest++=c
45: #define to_string(b, bsize) \
1.60 parser 46: memcpy(dest, b, bsize); \
1.18 paf 47: dest+=bsize; \
1.4 paf 48:
1.9 paf 49: inline bool need_file_encode(unsigned char c){
1.83 paf 50: // theoretical problem with, for instance, "_2B" and "." fragments,
51: // they would yield the same
52: // because need_file_encode('_')=false
53: // but we need to delete such files somehow, getting names from ^index
54:
1.13 paf 55: if((c>='0') &&(c<='9') ||(c>='A') &&(c<='Z') ||(c>='a') &&(c<='z'))
1.9 paf 56: return false;
57:
1.31 paf 58: return !strchr(
1.86 paf 59: " _./()-"
1.31 paf 60: #ifdef WIN32
1.37 paf 61: ":\\~"
1.31 paf 62: #endif
1.83 paf 63: , c);
1.9 paf 64: }
1.5 paf 65: inline bool need_uri_encode(unsigned char c){
1.13 paf 66: if((c>='0') &&(c<='9') ||(c>='A') &&(c<='Z') ||(c>='a') &&(c<='z'))
1.4 paf 67: return false;
68:
1.5 paf 69: return !strchr("_-./", c);
70: }
1.36 paf 71: inline bool need_http_header_encode(unsigned char c){
1.18 paf 72: if(strchr(" , :", c))
1.5 paf 73: return false;
74:
75: return need_uri_encode(c);
1.4 paf 76: }
1.1 paf 77:
1.56 parser 78: //
79:
80: static const char * String_Untaint_lang_name[]={
81: "U", ///< zero value handy for hash lookup @see untaint_lang_name2enum
82: "C", ///< clean
83: "T", ///< tainted, untaint language as assigned later
84: // untaint languages. assigned by ^untaint[lang]{...}
85: "P",
86: /**<
87: leave language built into string being appended.
88: just a flag, that value not stored
89: */
90: "A", ///< leave all characters intact
1.68 parser 91: "F", ///< file specification
92: "H", ///< ext in HTTP response header
1.56 parser 93: "M", ///< text in mail header
94: "URI", ///< text in uri
95: "T", ///< ^table:set body
96: "SQL", ///< ^table:sql body
97: "JS", ///< JavaScript code
1.68 parser 98: "XML", ///< ^dom:set xml
1.82 paf 99: "HTML" ///< HTML code (for editing)
1.56 parser 100: };
101:
102:
1.1 paf 103: // String
104:
1.41 paf 105: /*
106:
107: HTTP-header = field-name ":" [ field-value ] CRLF
108:
109: field-name = token
110: field-value = *( field-content | LWS )
111:
112: field-content = <the OCTETs making up the field-value
113: and consisting of either *TEXT or combinations
114: of token, tspecials, and quoted-string>
115:
116:
117:
118: word = token | quoted-string
119:
120: token = 1*<any CHAR except CTLs or tspecials>
121:
122:
123:
124: tspecials = "(" | ")" | "<" | ">" | "@"
125: | "," | ";" | ":" | "\" | <">
126: | "/" | "[" | "]" | "?" | "="
127: | "{" | "}" | SP | HT
128:
129: SP = <US-ASCII SP, space (32)>
130: HT = <US-ASCII HT, horizontal-tab (9)>
131:
132: LWS = [CRLF] 1*( SP | HT )
133: TEXT = <any OCTET except CTLs,
134: but including LWS>
135:
136: quoted-pair = "\" CHAR
137:
138: if(strchr("()<>@,;:\\\"/[]?={} \t", *ptr))
139: */
140: inline bool need_quote_http_header(const char *ptr, size_t size) {
141: for(; size--; ptr++)
1.42 paf 142: if(strchr(";\\\"= \t" /* excluded ()<>@, :/ ? []{} */, *ptr))
1.41 paf 143: return true;
144: return false;
145: }
146:
1.91 paf 147: //#include "pa_sapi.h"
148: /**
1.92 paf 149: appends other String,
1.91 paf 150: marking all tainted pieces of it with @a lang.
1.92 paf 151: or marking ALL pieces of it with a @a lang when @a forced to,
152: and propagating OPTIMIZE language bit.
1.91 paf 153:
154: using architecture advantage: after string-to-string-append string never modified.
155: algorithm:
156: if no language-change specified and src not yet appended to some other string[last_chunk!=0]
1.92 paf 157: shrinking dest last_chunk[preparing it for linking],
1.93 paf 158: ///shrinking src last_chunk[preparing it to be linked, consequent dest.appends would go there],
1.92 paf 159: linking[dest.last_chunk = src.head]
1.91 paf 160: if some language-change specified or src already appended to some other string[last_chunk==0]
161: cloning pieces.
162: */
1.77 paf 163: String& String::append(const String& src, uchar lang, bool forced) {
1.94 paf 164: if(!last_chunk) // growth stopped [we're appended as string to somebody]
165: throw Exception(0, 0,
166: this,
167: "string growth stopped (append string)");
168:
1.93 paf 169: if(src.is_empty())
170: return *this;
171:
1.94 paf 172: // without language-chage, not-appended-before, big[not fitting our tail] string?
173: if(lang==UL_PASS_APPENDED
174: && src.last_chunk
175: && (uint(&last_chunk->rows[last_chunk->count]-append_here) < src.used_rows())) {
1.91 paf 176: #ifdef DEBUG_STRING_APPENDS_VS_EXPANDS
1.92 paf 177: string_string_shortcut_economy+=src.used_rows()*sizeof(String::Chunk::Row);
1.91 paf 178: #endif
1.94 paf 179:
1.93 paf 180: // using fact:
1.94 paf 181: // src.head.count initally equeals this.head.count and shrinks-only,
182: // so can't be more than this.head.count,
183: // which means that we know that
184: // src.head would fit into this.head
1.93 paf 185: if(is_empty()) { // our head is empty
1.97 paf 186: // they have more than head? we need all head : we need only filled-part of head
1.94 paf 187: Chunk *src_head_link=src.head.rows[src.head.count].link;
188: size_t head_count=src_head_link?src.head.count:(src.append_here-src.head.rows);
1.93 paf 189: // "your head is my head"
1.94 paf 190: memcpy(head.rows, src.head.rows, sizeof(Chunk::Row)*(head_count));
191: if(src_head_link) {
192: // "your body is my body"
193: head.rows[head.count=head_count].link=src_head_link;
194: // "your last_chunk is mine now"
195: last_chunk=src.last_chunk;
196: // "your append_here is mine now"
197: append_here=src.append_here;
198: } else {
199: // "your last_chunk is mine now"
200: last_chunk=&head;
201: // "your append_here is recalc-mine now"
202: append_here=head.rows+head_count;
1.97 paf 203: }
1.93 paf 204: } else { // our head contains something
1.94 paf 205: // "chopping off my tail-reserve"
1.96 paf 206: last_chunk->count=append_here-last_chunk->rows;
1.93 paf 207: // "you is my tail"
1.96 paf 208: append_here->link=&src.head;
1.94 paf 209: // "your last_chunk is mine now"
210: last_chunk=src.last_chunk;
211: // "your append_here is mine now"
212: append_here=src.append_here;
1.93 paf 213: }
1.92 paf 214:
1.93 paf 215: // stop-growing mark
1.97 paf 216: src.last_chunk=0;
217: return *this;
1.91 paf 218: }
219:
1.77 paf 220: // manually unrolled code to avoid do{if(const)} constructs
221: if(forced)
222: STRING_SRC_FOREACH_ROW(
223: APPEND(row->item.ptr, row->item.size,
224: lang, //forcing passed lang
225: row->item.origin.file, row->item.origin.line);
226: )
227: else if(lang==UL_PASS_APPENDED)
228: STRING_SRC_FOREACH_ROW(
229: APPEND(row->item.ptr, row->item.size,
230: row->item.lang, // passing item's lang
231: row->item.origin.file, row->item.origin.line);
232: )
233: else if(lang&UL_OPTIMIZE_BIT) // main idea here
234: // tainted piece would get OPTIMIZED bit from 'lang'
235: // clean piece would be marked OPTIMIZED manually
236: // pieces with determined languages [not tainted|clean] would retain theirs langs
237: STRING_SRC_FOREACH_ROW(
238: APPEND(row->item.ptr, row->item.size,
239: row->item.lang==UL_TAINTED?lang:(
240: row->item.lang==UL_CLEAN?UL_CLEAN|UL_OPTIMIZE_BIT: // ORing with OPTIMIZED flag
241: row->item.lang
242: ),
243: row->item.origin.file, row->item.origin.line);
244: )
245: else
246: STRING_SRC_FOREACH_ROW(
247: APPEND(row->item.ptr, row->item.size,
248: row->item.lang==UL_TAINTED?lang:row->item.lang,
249: row->item.origin.file, row->item.origin.line);
250: );
1.97 paf 251: /*
1.96 paf 252: for(Chunk::Row *row=last_chunk->rows; row<append_here; row++)
253: if(row->link==(void*)0xcdcdcdcd)
1.97 paf 254: _asm int 3;*/
1.77 paf 255: return *this;
256: }
257:
1.75 paf 258: size_t String::cstr_bufsize(Untaint_lang lang,
259: SQL_Connection *connection,
1.87 paf 260: Charset *buf_charset) const {
1.77 paf 261: size_t dest=1; // for terminating 0
262: STRING_FOREACH_ROW(
263: uchar to_lang=lang==UL_UNSPECIFIED?row->item.lang:lang;
264:
265: switch(to_lang & ~UL_OPTIMIZE_BIT) {
266: case UL_CLEAN:
267: case UL_TAINTED:
268: case UL_AS_IS:
269: // clean piece
270:
271: // tainted piece, but undefined untaint language
272: // for VString.as_double of tainted values
273: // for ^process{body} evaluation
274:
275: // tainted, untaint language: as-is
276: dest+=row->item.size;
277: break;
278: case UL_FILE_SPEC:
279: // tainted, untaint language: file [name]
280: dest+=row->item.size*3/* worst: Z->%XX */;
281: break;
282: case UL_URI:
283: // tainted, untaint language: uri
1.84 paf 284: dest+=row->item.size*6*3/* worst utf8 x worst Z->%XX */;
1.77 paf 285: break;
286: case UL_HTTP_HEADER:
287: // tainted, untaint language: http-field-content-text
288: dest+=row->item.size*3/* worst: Z->%XX */;
289: break;
290: case UL_MAIL_HEADER:
291: // tainted, untaint language: mail-header
1.87 paf 292: if(buf_charset) {
1.77 paf 293: // Subject: Re: parser3: =?koi8-r?Q?=D3=C5=CD=C9=CE=C1=D2?=
1.87 paf 294: dest+=
295: row->item.size*3+
296: buf_charset->name().size()+MAX_STRING/* worst: =?charset?Q?=%XX?= */;
297: } else
1.75 paf 298: dest+=row->item.size;
1.77 paf 299: break;
300: case UL_TABLE:
301: // tainted, untaint language: table
302: dest+=row->item.size;
303: break;
304: case UL_SQL:
305: // tainted, untaint language: sql
306: if(connection)
307: dest+=connection->quote(0, row->item.ptr, row->item.size);
308: break;
309: case UL_JS:
310: escape(switch(*src) {
311: case '"': case '\'': case '\n': case '\\': case '\xFF':
312: dest+=2; break;
313: default:
314: dest++; break;
315: });
316: break;
317: case UL_XML:
318: escape(switch(*src) {
319: case '&': case '>': case '<': case '"': case '\'':
320: dest+= 6; break;
321: default:
322: dest++; break;
323: });
324: break;
325: case UL_HTML:
326: escape(switch(*src) {
327: case '&':
328: case '>':
329: case '<':
330: case '"':
331: dest+=6; break;
332: default:
333: dest++; break;
334: });
335: break;
1.75 paf 336: }
1.77 paf 337: );
1.75 paf 338: return dest;
1.51 parser 339: }
340:
1.43 paf 341: char *String::store_to(char *dest, Untaint_lang lang,
342: SQL_Connection *connection,
1.87 paf 343: Charset *store_to_charset) const {
1.75 paf 344: // WARNING:
345: // before any changes check cstr_bufsize first!!!
1.44 paf 346: bool whitespace=true;
1.96 paf 347: STRING_FOREACH_ROW(
1.77 paf 348: uchar to_lang=lang==UL_UNSPECIFIED?row->item.lang:lang;
349:
350: char *start=dest;
351:
352: switch(to_lang & ~UL_OPTIMIZE_BIT) {
353: case UL_CLEAN:
354: case UL_TAINTED:
355: case UL_AS_IS:
356: // clean piece
357:
358: // tainted piece, but undefined untaint language
359: // for VString.as_double of tainted values
360: // for ^process{body} evaluation
361:
362: // tainted, untaint language: as-is
363: memcpy(dest, row->item.ptr, row->item.size);
364: dest+=row->item.size;
365: break;
366: case UL_FILE_SPEC:
367: // tainted, untaint language: file [name]
1.83 paf 368: escape(
369: encode(need_file_encode, '_');
370: );
1.77 paf 371: break;
372: case UL_URI:
373: // tainted, untaint language: uri
1.85 paf 374: const void *client_ptr;
375: size_t client_size;
376: Charset::transcode(pool(),
377: pool().get_source_charset(), row->item.ptr, row->item.size,
378: pool().get_client_charset(), client_ptr, client_size);
379: {
380: const char *src=(const char *)client_ptr;
381: for(int size=client_size; size--; src++)
382: switch(*src) {
383: case ' ': to_char('+'); break;
384: default: encode(need_uri_encode, '%');
385: };
386: }
1.77 paf 387: break;
388: case UL_HTTP_HEADER:
389: // tainted, untaint language: http-field-content-text
390: escape(switch(*src) {
391: case ' ': to_char('+'); break;
1.83 paf 392: default: encode(need_uri_encode, '%');
1.77 paf 393: });
394: break;
395: case UL_MAIL_HEADER:
396: // tainted, untaint language: mail-header
1.87 paf 397: if(store_to_charset) {
398: const void *mail_ptr;
399: size_t mail_size;
400: Charset::transcode(pool(),
401: pool().get_source_charset(), row->item.ptr, row->item.size,
402: *store_to_charset, mail_ptr, mail_size);
403:
1.77 paf 404: // Subject: Re: parser3: =?koi8-r?Q?=D3=C5=CD=C9=CE=C1=D2?=
1.87 paf 405: const char *src=(const char *)mail_ptr;
1.77 paf 406: bool to_quoted_printable=false;
1.87 paf 407: for(int size=mail_size; size--; src++) {
1.77 paf 408: if(*src & 0x80) {
409: if(!to_quoted_printable) {
1.87 paf 410: dest+=sprintf(dest, "=?%s?Q?", store_to_charset->name().cstr());
1.77 paf 411: to_quoted_printable=true;
412: }
413: dest+=sprintf(dest, "=%02X", *src & 0xFF);
414: } else {
415: *dest++=*src;
416: }
1.44 paf 417: }
1.77 paf 418: if(to_quoted_printable) // close
419: dest+=sprintf(dest, "?=");
1.87 paf 420:
1.77 paf 421: } else {
1.13 paf 422: memcpy(dest, row->item.ptr, row->item.size);
423: dest+=row->item.size;
1.1 paf 424: }
1.77 paf 425: break;
426: case UL_TABLE:
427: // tainted, untaint language: table
428: escape(switch(*src) {
429: case '\t': to_char(' '); break;
430: case '\n': to_char(' '); break;
431: _default;
432: });
433: break;
434: case UL_SQL:
435: // tainted, untaint language: sql
436: if(connection)
437: dest+=connection->quote(dest, row->item.ptr, row->item.size);
438: else
439: throw Exception(0, 0,
440: this,
441: "untaint in SQL language failed - no connection specified");
442: break;
443: case UL_JS:
444: escape(switch(*src) {
445: case '"': to_string("\\\"", 2); break;
446: case '\'': to_string("\\'", 2); break;
447: case '\n': to_string("\\n", 2); break;
448: case '\\': to_string("\\\\", 2); break;
449: case '\xFF': to_string("\\\xFF", 2); break;
450: _default;
451: });
452: break;
453: case UL_XML:
454: escape(switch(*src) {
455: case '&': to_string("&", 5); break;
456: case '>': to_string(">", 4); break;
457: case '<': to_string("<", 4); break;
458: case '"': to_string(""", 6); break;
459: case '\'': to_string("'", 6); break;
460: _default;
461: });
462: break;
463: case UL_HTML:
464: escape(switch(*src) {
465: case '&': to_string("&", 5); break;
466: case '>': to_string(">", 4); break;
467: case '<': to_string("<", 4); break;
468: case '"': to_string(""", 6); break;
469: _default;
470: });
471: break;
472: default:
473: throw Exception(0, 0,
474: this,
1.81 paf 475: "unknown untaint language #%d",
476: static_cast<int>(row->item.lang)); // sould never
1.77 paf 477: break; // never
1.76 paf 478: }
1.55 parser 479:
1.77 paf 480: if(to_lang & UL_OPTIMIZE_BIT) {
481: // optimizing whitespace
482: char *stop=dest; dest=start;
483: for(char *src=start; src<stop; src++)
484: switch(*src) {
485: // of all consequent white space chars leaving only first one
1.80 paf 486: case ' ': case '\r': case '\n': case '\t':
1.77 paf 487: if(!whitespace) {
488: *dest++=*src;
489: whitespace=true;
490: }
491: break;
492: default:
493: whitespace=false;
494: *dest++=*src;
495: break;
496: };
497: } else // piece without optimization
498: whitespace=false;
1.96 paf 499: );
1.78 paf 500:
1.76 paf 501: return dest;
502: }
503:
504: char *String::cstr_debug_origins() const {
1.81 paf 505: //_asm int 3;
1.76 paf 506: char *result=(char *)malloc(size()+used_rows()*MAX_STRING*2);
507: char *dest=result;
508:
1.96 paf 509: STRING_FOREACH_ROW(
510: IFNDEF_NO_STRING_ORIGIN(
511: if(row->item.origin.file)
512: dest+=sprintf(dest, ORIGIN_FILE_LINE_FORMAT,
513: row->item.origin.file,
514: 1+row->item.origin.line);
515: else
516: dest+=sprintf(dest, "<unknown>");
517: );
518: uchar show_lang=row->item.lang & ~UL_OPTIMIZE_BIT;
519: if(show_lang>=sizeof(String_Untaint_lang_name)/sizeof(String_Untaint_lang_name[0]))
520: throw Exception(0, 0,
521: this,
522: "unknown untaint language #%d",
523: static_cast<int>(show_lang)); // sould never
524:
525: dest+=sprintf(dest, "#%s%s: ",
526: String_Untaint_lang_name[show_lang],
527: row->item.lang & UL_OPTIMIZE_BIT?".O":"");
528: char *dest_after_origins=dest;
1.76 paf 529:
1.96 paf 530: memcpy(dest, row->item.ptr, row->item.size);
531: dest+=row->item.size;
1.76 paf 532:
1.96 paf 533: remove_crlf(dest_after_origins, dest);
534: to_char('\n');
535: );
1.64 parser 536:
1.76 paf 537: *dest=0;
538: return result;
1.1 paf 539: }