|
|
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.103 ! paf 6: */
1.8 paf 7:
1.103 ! paf 8: static const char* IDENT_UNTAINT_C="$Id: zzz $";
1.1 paf 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.101 paf 159: linking[dest.last_chunk = src.head.chunk]
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]
1.99 paf 165: throw Exception(0,
1.94 paf 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.101 paf 181: // src.head.chunk.count initally equeals this.head.chunk.count and shrinks-only,
182: // so can't be more than this.head.chunk.count,
1.94 paf 183: // which means that we know that
1.101 paf 184: // src.head.chunk would fit into this.head.chunk
185: if(is_empty()) { // our head.chunk is empty
186: // they have more than head.chunk? we need all head.chunk : we need only filled-part of head.chunk
187: Chunk *src_head_link=src.head.chunk.rows[src.head.chunk.count].link;
188: size_t head_count=src_head_link?src.head.chunk.count:(src.append_here-src.head.chunk.rows);
189: // "your head.chunk is my head.chunk"
190: memcpy(head.chunk.rows, src.head.chunk.rows, sizeof(Chunk::Row)*(head_count));
1.94 paf 191: if(src_head_link) {
192: // "your body is my body"
1.101 paf 193: head.chunk.rows[head.chunk.count=head_count].link=src_head_link;
1.94 paf 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"
1.101 paf 200: last_chunk=&head.chunk;
1.94 paf 201: // "your append_here is recalc-mine now"
1.101 paf 202: append_here=head.chunk.rows+head_count;
1.97 paf 203: }
1.101 paf 204: } else { // our head.chunk 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.101 paf 208: append_here->link=&src.head.chunk;
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.100 paf 343: Charset *store_to_charset,
344: const char *store_to_charset_name) const {
1.75 paf 345: // WARNING:
346: // before any changes check cstr_bufsize first!!!
1.44 paf 347: bool whitespace=true;
1.96 paf 348: STRING_FOREACH_ROW(
1.77 paf 349: uchar to_lang=lang==UL_UNSPECIFIED?row->item.lang:lang;
350:
351: char *start=dest;
352:
353: switch(to_lang & ~UL_OPTIMIZE_BIT) {
354: case UL_CLEAN:
355: case UL_TAINTED:
356: case UL_AS_IS:
357: // clean piece
358:
359: // tainted piece, but undefined untaint language
360: // for VString.as_double of tainted values
361: // for ^process{body} evaluation
362:
363: // tainted, untaint language: as-is
364: memcpy(dest, row->item.ptr, row->item.size);
365: dest+=row->item.size;
366: break;
367: case UL_FILE_SPEC:
368: // tainted, untaint language: file [name]
1.83 paf 369: escape(
370: encode(need_file_encode, '_');
371: );
1.77 paf 372: break;
373: case UL_URI:
374: // tainted, untaint language: uri
1.85 paf 375: const void *client_ptr;
376: size_t client_size;
377: Charset::transcode(pool(),
378: pool().get_source_charset(), row->item.ptr, row->item.size,
379: pool().get_client_charset(), client_ptr, client_size);
380: {
381: const char *src=(const char *)client_ptr;
382: for(int size=client_size; size--; src++)
383: switch(*src) {
384: case ' ': to_char('+'); break;
385: default: encode(need_uri_encode, '%');
386: };
387: }
1.77 paf 388: break;
389: case UL_HTTP_HEADER:
390: // tainted, untaint language: http-field-content-text
391: escape(switch(*src) {
392: case ' ': to_char('+'); break;
1.83 paf 393: default: encode(need_uri_encode, '%');
1.77 paf 394: });
395: break;
396: case UL_MAIL_HEADER:
397: // tainted, untaint language: mail-header
1.100 paf 398: if(store_to_charset && store_to_charset_name) {
1.87 paf 399: const void *mail_ptr;
400: size_t mail_size;
401: Charset::transcode(pool(),
402: pool().get_source_charset(), row->item.ptr, row->item.size,
403: *store_to_charset, mail_ptr, mail_size);
404:
1.77 paf 405: // Subject: Re: parser3: =?koi8-r?Q?=D3=C5=CD=C9=CE=C1=D2?=
1.87 paf 406: const char *src=(const char *)mail_ptr;
1.77 paf 407: bool to_quoted_printable=false;
1.87 paf 408: for(int size=mail_size; size--; src++) {
1.102 paf 409: if((*src & 0x80) // starting quote-printable-encoding on first 8bit char
1.100 paf 410: || (to_quoted_printable && (*src=='?' || *src=='=')) // additionally encoding '?' and '|'
411: ) {
1.77 paf 412: if(!to_quoted_printable) {
1.100 paf 413: dest+=sprintf(dest, "=?%s?Q?", store_to_charset_name);
1.77 paf 414: to_quoted_printable=true;
415: }
416: dest+=sprintf(dest, "=%02X", *src & 0xFF);
417: } else {
418: *dest++=*src;
419: }
1.44 paf 420: }
1.77 paf 421: if(to_quoted_printable) // close
422: dest+=sprintf(dest, "?=");
1.87 paf 423:
1.77 paf 424: } else {
1.13 paf 425: memcpy(dest, row->item.ptr, row->item.size);
426: dest+=row->item.size;
1.1 paf 427: }
1.77 paf 428: break;
429: case UL_TABLE:
430: // tainted, untaint language: table
431: escape(switch(*src) {
432: case '\t': to_char(' '); break;
433: case '\n': to_char(' '); break;
434: _default;
435: });
436: break;
437: case UL_SQL:
438: // tainted, untaint language: sql
439: if(connection)
440: dest+=connection->quote(dest, row->item.ptr, row->item.size);
441: else
1.99 paf 442: throw Exception(0,
1.77 paf 443: this,
444: "untaint in SQL language failed - no connection specified");
445: break;
446: case UL_JS:
447: escape(switch(*src) {
448: case '"': to_string("\\\"", 2); break;
449: case '\'': to_string("\\'", 2); break;
450: case '\n': to_string("\\n", 2); break;
451: case '\\': to_string("\\\\", 2); break;
452: case '\xFF': to_string("\\\xFF", 2); break;
453: _default;
454: });
455: break;
456: case UL_XML:
457: escape(switch(*src) {
458: case '&': to_string("&", 5); break;
459: case '>': to_string(">", 4); break;
460: case '<': to_string("<", 4); break;
461: case '"': to_string(""", 6); break;
462: case '\'': to_string("'", 6); break;
463: _default;
464: });
465: break;
466: case UL_HTML:
467: escape(switch(*src) {
468: case '&': to_string("&", 5); break;
469: case '>': to_string(">", 4); break;
470: case '<': to_string("<", 4); break;
471: case '"': to_string(""", 6); break;
472: _default;
473: });
474: break;
475: default:
1.99 paf 476: throw Exception(0,
1.77 paf 477: this,
1.81 paf 478: "unknown untaint language #%d",
479: static_cast<int>(row->item.lang)); // sould never
1.77 paf 480: break; // never
1.76 paf 481: }
1.55 parser 482:
1.77 paf 483: if(to_lang & UL_OPTIMIZE_BIT) {
484: // optimizing whitespace
485: char *stop=dest; dest=start;
486: for(char *src=start; src<stop; src++)
487: switch(*src) {
488: // of all consequent white space chars leaving only first one
1.80 paf 489: case ' ': case '\r': case '\n': case '\t':
1.77 paf 490: if(!whitespace) {
491: *dest++=*src;
492: whitespace=true;
493: }
494: break;
495: default:
496: whitespace=false;
497: *dest++=*src;
498: break;
499: };
500: } else // piece without optimization
501: whitespace=false;
1.96 paf 502: );
1.78 paf 503:
1.76 paf 504: return dest;
505: }
506:
507: char *String::cstr_debug_origins() const {
1.81 paf 508: //_asm int 3;
1.76 paf 509: char *result=(char *)malloc(size()+used_rows()*MAX_STRING*2);
510: char *dest=result;
511:
1.96 paf 512: STRING_FOREACH_ROW(
513: IFNDEF_NO_STRING_ORIGIN(
514: if(row->item.origin.file)
515: dest+=sprintf(dest, ORIGIN_FILE_LINE_FORMAT,
516: row->item.origin.file,
517: 1+row->item.origin.line);
518: else
519: dest+=sprintf(dest, "<unknown>");
520: );
521: uchar show_lang=row->item.lang & ~UL_OPTIMIZE_BIT;
522: if(show_lang>=sizeof(String_Untaint_lang_name)/sizeof(String_Untaint_lang_name[0]))
1.99 paf 523: throw Exception(0,
1.96 paf 524: this,
525: "unknown untaint language #%d",
526: static_cast<int>(show_lang)); // sould never
527:
528: dest+=sprintf(dest, "#%s%s: ",
529: String_Untaint_lang_name[show_lang],
530: row->item.lang & UL_OPTIMIZE_BIT?".O":"");
531: char *dest_after_origins=dest;
1.76 paf 532:
1.96 paf 533: memcpy(dest, row->item.ptr, row->item.size);
534: dest+=row->item.size;
1.76 paf 535:
1.96 paf 536: remove_crlf(dest_after_origins, dest);
537: to_char('\n');
538: );
1.64 parser 539:
1.76 paf 540: *dest=0;
541: return result;
1.1 paf 542: }