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