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