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