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