|
|
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.97 ! paf 7: $Id: untaint.C,v 1.96 2002/02/21 14:36:54 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.97 ! paf 205: /* for(Chunk::Row *row=src.last_chunk->rows; row<src.append_here; row++)
1.96 paf 206: if(row->link==(void*)0xcdcdcdcd)
1.97 ! paf 207: _asm int 3;*/
1.94 paf 208: // "chopping off my tail-reserve"
1.96 paf 209: last_chunk->count=append_here-last_chunk->rows;
1.93 paf 210: // "you is my tail"
1.96 paf 211: append_here->link=&src.head;
1.94 paf 212: // "your last_chunk is mine now"
213: last_chunk=src.last_chunk;
214: // "your append_here is mine now"
215: append_here=src.append_here;
1.93 paf 216: }
1.92 paf 217:
1.93 paf 218: // stop-growing mark
1.97 ! paf 219: src.last_chunk=0;
! 220: return *this;
1.91 paf 221: }
222:
1.77 paf 223: // manually unrolled code to avoid do{if(const)} constructs
224: if(forced)
225: STRING_SRC_FOREACH_ROW(
226: APPEND(row->item.ptr, row->item.size,
227: lang, //forcing passed lang
228: row->item.origin.file, row->item.origin.line);
229: )
230: else if(lang==UL_PASS_APPENDED)
231: STRING_SRC_FOREACH_ROW(
232: APPEND(row->item.ptr, row->item.size,
233: row->item.lang, // passing item's lang
234: row->item.origin.file, row->item.origin.line);
235: )
236: else if(lang&UL_OPTIMIZE_BIT) // main idea here
237: // tainted piece would get OPTIMIZED bit from 'lang'
238: // clean piece would be marked OPTIMIZED manually
239: // pieces with determined languages [not tainted|clean] would retain theirs langs
240: STRING_SRC_FOREACH_ROW(
241: APPEND(row->item.ptr, row->item.size,
242: row->item.lang==UL_TAINTED?lang:(
243: row->item.lang==UL_CLEAN?UL_CLEAN|UL_OPTIMIZE_BIT: // ORing with OPTIMIZED flag
244: row->item.lang
245: ),
246: row->item.origin.file, row->item.origin.line);
247: )
248: else
249: STRING_SRC_FOREACH_ROW(
250: APPEND(row->item.ptr, row->item.size,
251: row->item.lang==UL_TAINTED?lang:row->item.lang,
252: row->item.origin.file, row->item.origin.line);
253: );
1.97 ! paf 254: /*
1.96 paf 255: for(Chunk::Row *row=last_chunk->rows; row<append_here; row++)
256: if(row->link==(void*)0xcdcdcdcd)
1.97 ! paf 257: _asm int 3;*/
1.77 paf 258: return *this;
259: }
260:
1.75 paf 261: size_t String::cstr_bufsize(Untaint_lang lang,
262: SQL_Connection *connection,
1.87 paf 263: Charset *buf_charset) const {
1.77 paf 264: size_t dest=1; // for terminating 0
265: STRING_FOREACH_ROW(
266: uchar to_lang=lang==UL_UNSPECIFIED?row->item.lang:lang;
267:
268: switch(to_lang & ~UL_OPTIMIZE_BIT) {
269: case UL_CLEAN:
270: case UL_TAINTED:
271: case UL_AS_IS:
272: // clean piece
273:
274: // tainted piece, but undefined untaint language
275: // for VString.as_double of tainted values
276: // for ^process{body} evaluation
277:
278: // tainted, untaint language: as-is
279: dest+=row->item.size;
280: break;
281: case UL_FILE_SPEC:
282: // tainted, untaint language: file [name]
283: dest+=row->item.size*3/* worst: Z->%XX */;
284: break;
285: case UL_URI:
286: // tainted, untaint language: uri
1.84 paf 287: dest+=row->item.size*6*3/* worst utf8 x worst Z->%XX */;
1.77 paf 288: break;
289: case UL_HTTP_HEADER:
290: // tainted, untaint language: http-field-content-text
291: dest+=row->item.size*3/* worst: Z->%XX */;
292: break;
293: case UL_MAIL_HEADER:
294: // tainted, untaint language: mail-header
1.87 paf 295: if(buf_charset) {
1.77 paf 296: // Subject: Re: parser3: =?koi8-r?Q?=D3=C5=CD=C9=CE=C1=D2?=
1.87 paf 297: dest+=
298: row->item.size*3+
299: buf_charset->name().size()+MAX_STRING/* worst: =?charset?Q?=%XX?= */;
300: } else
1.75 paf 301: dest+=row->item.size;
1.77 paf 302: break;
303: case UL_TABLE:
304: // tainted, untaint language: table
305: dest+=row->item.size;
306: break;
307: case UL_SQL:
308: // tainted, untaint language: sql
309: if(connection)
310: dest+=connection->quote(0, row->item.ptr, row->item.size);
311: break;
312: case UL_JS:
313: escape(switch(*src) {
314: case '"': case '\'': case '\n': case '\\': case '\xFF':
315: dest+=2; break;
316: default:
317: dest++; break;
318: });
319: break;
320: case UL_XML:
321: escape(switch(*src) {
322: case '&': case '>': case '<': case '"': case '\'':
323: dest+= 6; break;
324: default:
325: dest++; break;
326: });
327: break;
328: case UL_HTML:
329: escape(switch(*src) {
330: case '&':
331: case '>':
332: case '<':
333: case '"':
334: dest+=6; break;
335: default:
336: dest++; break;
337: });
338: break;
1.75 paf 339: }
1.77 paf 340: );
1.75 paf 341: return dest;
1.51 parser 342: }
343:
1.43 paf 344: char *String::store_to(char *dest, Untaint_lang lang,
345: SQL_Connection *connection,
1.87 paf 346: Charset *store_to_charset) const {
1.75 paf 347: // WARNING:
348: // before any changes check cstr_bufsize first!!!
1.44 paf 349: bool whitespace=true;
1.96 paf 350: STRING_FOREACH_ROW(
1.77 paf 351: uchar to_lang=lang==UL_UNSPECIFIED?row->item.lang:lang;
352:
353: char *start=dest;
354:
355: switch(to_lang & ~UL_OPTIMIZE_BIT) {
356: case UL_CLEAN:
357: case UL_TAINTED:
358: case UL_AS_IS:
359: // clean piece
360:
361: // tainted piece, but undefined untaint language
362: // for VString.as_double of tainted values
363: // for ^process{body} evaluation
364:
365: // tainted, untaint language: as-is
366: memcpy(dest, row->item.ptr, row->item.size);
367: dest+=row->item.size;
368: break;
369: case UL_FILE_SPEC:
370: // tainted, untaint language: file [name]
1.83 paf 371: escape(
372: encode(need_file_encode, '_');
373: );
1.77 paf 374: break;
375: case UL_URI:
376: // tainted, untaint language: uri
1.85 paf 377: const void *client_ptr;
378: size_t client_size;
379: Charset::transcode(pool(),
380: pool().get_source_charset(), row->item.ptr, row->item.size,
381: pool().get_client_charset(), client_ptr, client_size);
382: {
383: const char *src=(const char *)client_ptr;
384: for(int size=client_size; size--; src++)
385: switch(*src) {
386: case ' ': to_char('+'); break;
387: default: encode(need_uri_encode, '%');
388: };
389: }
1.77 paf 390: break;
391: case UL_HTTP_HEADER:
392: // tainted, untaint language: http-field-content-text
393: escape(switch(*src) {
394: case ' ': to_char('+'); break;
1.83 paf 395: default: encode(need_uri_encode, '%');
1.77 paf 396: });
397: break;
398: case UL_MAIL_HEADER:
399: // tainted, untaint language: mail-header
1.87 paf 400: if(store_to_charset) {
401: const void *mail_ptr;
402: size_t mail_size;
403: Charset::transcode(pool(),
404: pool().get_source_charset(), row->item.ptr, row->item.size,
405: *store_to_charset, mail_ptr, mail_size);
406:
1.77 paf 407: // Subject: Re: parser3: =?koi8-r?Q?=D3=C5=CD=C9=CE=C1=D2?=
1.87 paf 408: const char *src=(const char *)mail_ptr;
1.77 paf 409: bool to_quoted_printable=false;
1.87 paf 410: for(int size=mail_size; size--; src++) {
1.77 paf 411: if(*src & 0x80) {
412: if(!to_quoted_printable) {
1.87 paf 413: dest+=sprintf(dest, "=?%s?Q?", store_to_charset->name().cstr());
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
442: throw Exception(0, 0,
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:
476: throw Exception(0, 0,
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]))
523: throw Exception(0, 0,
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: }