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