|
|
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.96 ! paf 7: $Id: untaint.C,v 1.95 2002/02/20 12:40:24 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.96 ! 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.96 ! paf 203: }*/
1.93 paf 204: } else { // our head contains something
1.96 ! paf 205: /*
! 206: for(Chunk::Row *row=src.last_chunk->rows; row<src.append_here; row++)
! 207: if(row->link==(void*)0xcdcdcdcd)
! 208: _asm int 3;
1.94 paf 209: // "chopping off my tail-reserve"
1.96 ! paf 210: last_chunk->count=append_here-last_chunk->rows;
1.93 paf 211: // "you is my tail"
1.96 ! paf 212: append_here->link=&src.head;
1.94 paf 213: // "your last_chunk is mine now"
214: last_chunk=src.last_chunk;
215: // "your append_here is mine now"
216: append_here=src.append_here;
1.96 ! paf 217: // stop-growing mark
! 218: src.last_chunk=0;
! 219: return *this;*/
1.93 paf 220: }
1.92 paf 221:
1.93 paf 222: // stop-growing mark
1.96 ! paf 223: // src.last_chunk=0;
! 224: // return *this;
1.91 paf 225: }
226:
1.77 paf 227: // manually unrolled code to avoid do{if(const)} constructs
228: if(forced)
229: STRING_SRC_FOREACH_ROW(
230: APPEND(row->item.ptr, row->item.size,
231: lang, //forcing passed lang
232: row->item.origin.file, row->item.origin.line);
233: )
234: else if(lang==UL_PASS_APPENDED)
235: STRING_SRC_FOREACH_ROW(
236: APPEND(row->item.ptr, row->item.size,
237: row->item.lang, // passing item's lang
238: row->item.origin.file, row->item.origin.line);
239: )
240: else if(lang&UL_OPTIMIZE_BIT) // main idea here
241: // tainted piece would get OPTIMIZED bit from 'lang'
242: // clean piece would be marked OPTIMIZED manually
243: // pieces with determined languages [not tainted|clean] would retain theirs langs
244: STRING_SRC_FOREACH_ROW(
245: APPEND(row->item.ptr, row->item.size,
246: row->item.lang==UL_TAINTED?lang:(
247: row->item.lang==UL_CLEAN?UL_CLEAN|UL_OPTIMIZE_BIT: // ORing with OPTIMIZED flag
248: row->item.lang
249: ),
250: row->item.origin.file, row->item.origin.line);
251: )
252: else
253: STRING_SRC_FOREACH_ROW(
254: APPEND(row->item.ptr, row->item.size,
255: row->item.lang==UL_TAINTED?lang:row->item.lang,
256: row->item.origin.file, row->item.origin.line);
257: );
1.96 ! paf 258:
! 259: for(Chunk::Row *row=last_chunk->rows; row<append_here; row++)
! 260: if(row->link==(void*)0xcdcdcdcd)
! 261: _asm int 3;
1.77 paf 262: return *this;
263: }
264:
1.75 paf 265: size_t String::cstr_bufsize(Untaint_lang lang,
266: SQL_Connection *connection,
1.87 paf 267: Charset *buf_charset) const {
1.77 paf 268: size_t dest=1; // for terminating 0
269: STRING_FOREACH_ROW(
270: uchar to_lang=lang==UL_UNSPECIFIED?row->item.lang:lang;
271:
272: switch(to_lang & ~UL_OPTIMIZE_BIT) {
273: case UL_CLEAN:
274: case UL_TAINTED:
275: case UL_AS_IS:
276: // clean piece
277:
278: // tainted piece, but undefined untaint language
279: // for VString.as_double of tainted values
280: // for ^process{body} evaluation
281:
282: // tainted, untaint language: as-is
283: dest+=row->item.size;
284: break;
285: case UL_FILE_SPEC:
286: // tainted, untaint language: file [name]
287: dest+=row->item.size*3/* worst: Z->%XX */;
288: break;
289: case UL_URI:
290: // tainted, untaint language: uri
1.84 paf 291: dest+=row->item.size*6*3/* worst utf8 x worst Z->%XX */;
1.77 paf 292: break;
293: case UL_HTTP_HEADER:
294: // tainted, untaint language: http-field-content-text
295: dest+=row->item.size*3/* worst: Z->%XX */;
296: break;
297: case UL_MAIL_HEADER:
298: // tainted, untaint language: mail-header
1.87 paf 299: if(buf_charset) {
1.77 paf 300: // Subject: Re: parser3: =?koi8-r?Q?=D3=C5=CD=C9=CE=C1=D2?=
1.87 paf 301: dest+=
302: row->item.size*3+
303: buf_charset->name().size()+MAX_STRING/* worst: =?charset?Q?=%XX?= */;
304: } else
1.75 paf 305: dest+=row->item.size;
1.77 paf 306: break;
307: case UL_TABLE:
308: // tainted, untaint language: table
309: dest+=row->item.size;
310: break;
311: case UL_SQL:
312: // tainted, untaint language: sql
313: if(connection)
314: dest+=connection->quote(0, row->item.ptr, row->item.size);
315: break;
316: case UL_JS:
317: escape(switch(*src) {
318: case '"': case '\'': case '\n': case '\\': case '\xFF':
319: dest+=2; break;
320: default:
321: dest++; break;
322: });
323: break;
324: case UL_XML:
325: escape(switch(*src) {
326: case '&': case '>': case '<': case '"': case '\'':
327: dest+= 6; break;
328: default:
329: dest++; break;
330: });
331: break;
332: case UL_HTML:
333: escape(switch(*src) {
334: case '&':
335: case '>':
336: case '<':
337: case '"':
338: dest+=6; break;
339: default:
340: dest++; break;
341: });
342: break;
1.75 paf 343: }
1.77 paf 344: );
1.75 paf 345: return dest;
1.51 parser 346: }
347:
1.43 paf 348: char *String::store_to(char *dest, Untaint_lang lang,
349: SQL_Connection *connection,
1.87 paf 350: Charset *store_to_charset) const {
1.75 paf 351: // WARNING:
352: // before any changes check cstr_bufsize first!!!
1.44 paf 353: bool whitespace=true;
1.96 ! paf 354: STRING_FOREACH_ROW(
1.77 paf 355: uchar to_lang=lang==UL_UNSPECIFIED?row->item.lang:lang;
356:
357: char *start=dest;
358:
359: switch(to_lang & ~UL_OPTIMIZE_BIT) {
360: case UL_CLEAN:
361: case UL_TAINTED:
362: case UL_AS_IS:
363: // clean piece
364:
365: // tainted piece, but undefined untaint language
366: // for VString.as_double of tainted values
367: // for ^process{body} evaluation
368:
369: // tainted, untaint language: as-is
370: memcpy(dest, row->item.ptr, row->item.size);
371: dest+=row->item.size;
372: break;
373: case UL_FILE_SPEC:
374: // tainted, untaint language: file [name]
1.83 paf 375: escape(
376: encode(need_file_encode, '_');
377: );
1.77 paf 378: break;
379: case UL_URI:
380: // tainted, untaint language: uri
1.85 paf 381: const void *client_ptr;
382: size_t client_size;
383: Charset::transcode(pool(),
384: pool().get_source_charset(), row->item.ptr, row->item.size,
385: pool().get_client_charset(), client_ptr, client_size);
386: {
387: const char *src=(const char *)client_ptr;
388: for(int size=client_size; size--; src++)
389: switch(*src) {
390: case ' ': to_char('+'); break;
391: default: encode(need_uri_encode, '%');
392: };
393: }
1.77 paf 394: break;
395: case UL_HTTP_HEADER:
396: // tainted, untaint language: http-field-content-text
397: escape(switch(*src) {
398: case ' ': to_char('+'); break;
1.83 paf 399: default: encode(need_uri_encode, '%');
1.77 paf 400: });
401: break;
402: case UL_MAIL_HEADER:
403: // tainted, untaint language: mail-header
1.87 paf 404: if(store_to_charset) {
405: const void *mail_ptr;
406: size_t mail_size;
407: Charset::transcode(pool(),
408: pool().get_source_charset(), row->item.ptr, row->item.size,
409: *store_to_charset, mail_ptr, mail_size);
410:
1.77 paf 411: // Subject: Re: parser3: =?koi8-r?Q?=D3=C5=CD=C9=CE=C1=D2?=
1.87 paf 412: const char *src=(const char *)mail_ptr;
1.77 paf 413: bool to_quoted_printable=false;
1.87 paf 414: for(int size=mail_size; size--; src++) {
1.77 paf 415: if(*src & 0x80) {
416: if(!to_quoted_printable) {
1.87 paf 417: dest+=sprintf(dest, "=?%s?Q?", store_to_charset->name().cstr());
1.77 paf 418: to_quoted_printable=true;
419: }
420: dest+=sprintf(dest, "=%02X", *src & 0xFF);
421: } else {
422: *dest++=*src;
423: }
1.44 paf 424: }
1.77 paf 425: if(to_quoted_printable) // close
426: dest+=sprintf(dest, "?=");
1.87 paf 427:
1.77 paf 428: } else {
1.13 paf 429: memcpy(dest, row->item.ptr, row->item.size);
430: dest+=row->item.size;
1.1 paf 431: }
1.77 paf 432: break;
433: case UL_TABLE:
434: // tainted, untaint language: table
435: escape(switch(*src) {
436: case '\t': to_char(' '); break;
437: case '\n': to_char(' '); break;
438: _default;
439: });
440: break;
441: case UL_SQL:
442: // tainted, untaint language: sql
443: if(connection)
444: dest+=connection->quote(dest, row->item.ptr, row->item.size);
445: else
446: throw Exception(0, 0,
447: this,
448: "untaint in SQL language failed - no connection specified");
449: break;
450: case UL_JS:
451: escape(switch(*src) {
452: case '"': to_string("\\\"", 2); break;
453: case '\'': to_string("\\'", 2); break;
454: case '\n': to_string("\\n", 2); break;
455: case '\\': to_string("\\\\", 2); break;
456: case '\xFF': to_string("\\\xFF", 2); break;
457: _default;
458: });
459: break;
460: case UL_XML:
461: escape(switch(*src) {
462: case '&': to_string("&", 5); break;
463: case '>': to_string(">", 4); break;
464: case '<': to_string("<", 4); break;
465: case '"': to_string(""", 6); break;
466: case '\'': to_string("'", 6); break;
467: _default;
468: });
469: break;
470: case UL_HTML:
471: escape(switch(*src) {
472: case '&': to_string("&", 5); break;
473: case '>': to_string(">", 4); break;
474: case '<': to_string("<", 4); break;
475: case '"': to_string(""", 6); break;
476: _default;
477: });
478: break;
479: default:
480: throw Exception(0, 0,
481: this,
1.81 paf 482: "unknown untaint language #%d",
483: static_cast<int>(row->item.lang)); // sould never
1.77 paf 484: break; // never
1.76 paf 485: }
1.55 parser 486:
1.77 paf 487: if(to_lang & UL_OPTIMIZE_BIT) {
488: // optimizing whitespace
489: char *stop=dest; dest=start;
490: for(char *src=start; src<stop; src++)
491: switch(*src) {
492: // of all consequent white space chars leaving only first one
1.80 paf 493: case ' ': case '\r': case '\n': case '\t':
1.77 paf 494: if(!whitespace) {
495: *dest++=*src;
496: whitespace=true;
497: }
498: break;
499: default:
500: whitespace=false;
501: *dest++=*src;
502: break;
503: };
504: } else // piece without optimization
505: whitespace=false;
1.96 ! paf 506: );
1.78 paf 507:
1.76 paf 508: return dest;
509: }
510:
511: char *String::cstr_debug_origins() const {
1.81 paf 512: //_asm int 3;
1.76 paf 513: char *result=(char *)malloc(size()+used_rows()*MAX_STRING*2);
514: char *dest=result;
515:
1.96 ! paf 516: STRING_FOREACH_ROW(
! 517: IFNDEF_NO_STRING_ORIGIN(
! 518: if(row->item.origin.file)
! 519: dest+=sprintf(dest, ORIGIN_FILE_LINE_FORMAT,
! 520: row->item.origin.file,
! 521: 1+row->item.origin.line);
! 522: else
! 523: dest+=sprintf(dest, "<unknown>");
! 524: );
! 525: uchar show_lang=row->item.lang & ~UL_OPTIMIZE_BIT;
! 526: if(show_lang>=sizeof(String_Untaint_lang_name)/sizeof(String_Untaint_lang_name[0]))
! 527: throw Exception(0, 0,
! 528: this,
! 529: "unknown untaint language #%d",
! 530: static_cast<int>(show_lang)); // sould never
! 531:
! 532: dest+=sprintf(dest, "#%s%s: ",
! 533: String_Untaint_lang_name[show_lang],
! 534: row->item.lang & UL_OPTIMIZE_BIT?".O":"");
! 535: char *dest_after_origins=dest;
1.76 paf 536:
1.96 ! paf 537: memcpy(dest, row->item.ptr, row->item.size);
! 538: dest+=row->item.size;
1.76 paf 539:
1.96 ! paf 540: remove_crlf(dest_after_origins, dest);
! 541: to_char('\n');
! 542: );
1.64 parser 543:
1.76 paf 544: *dest=0;
545: return result;
1.1 paf 546: }