|
|
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.78 ! paf 7: $Id: untaint.C,v 1.77 2001/11/19 12:17:06 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.78 ! paf 261: const Chunk *chunk=&head; \
! 262: do { \
! 263: const Chunk::Row *row=chunk->rows; \
! 264: for(uint i=0; i<chunk->count; i++, row++) { \
! 265: if(row==append_here) \
! 266: goto break2; \
! 267: \
1.77 paf 268: uchar to_lang=lang==UL_UNSPECIFIED?row->item.lang:lang;
269:
270: char *start=dest;
271:
272: switch(to_lang & ~UL_OPTIMIZE_BIT) {
273: case UL_CLEAN:
274: case UL_TAINTED:
275: case UL_AS_IS:
276: // clean piece
277:
278: // tainted piece, but undefined untaint language
279: // for VString.as_double of tainted values
280: // for ^process{body} evaluation
281:
282: // tainted, untaint language: as-is
283: memcpy(dest, row->item.ptr, row->item.size);
284: dest+=row->item.size;
285: break;
286: case UL_FILE_SPEC:
287: // tainted, untaint language: file [name]
288: escape(switch(*src) {
289: case ' ': to_char('_'); break;
290: encode(need_file_encode, '+');
291: });
292: break;
293: case UL_URI:
294: // tainted, untaint language: uri
295: escape(switch(*src) {
296: case ' ': to_char('+'); break;
297: encode(need_uri_encode, '%');
298: });
299: break;
300: case UL_HTTP_HEADER:
301: // tainted, untaint language: http-field-content-text
302: escape(switch(*src) {
303: case ' ': to_char('+'); break;
304: encode(need_uri_encode, '%');
305: });
306: break;
307: case UL_MAIL_HEADER:
308: // tainted, untaint language: mail-header
309: if(charset) {
310: // Subject: Re: parser3: =?koi8-r?Q?=D3=C5=CD=C9=CE=C1=D2?=
311: const char *src=row->item.ptr;
312: bool to_quoted_printable=false;
313: for(int size=row->item.size; size--; src++) {
314: if(*src & 0x80) {
315: if(!to_quoted_printable) {
316: dest+=sprintf(dest, "=?%.15s?Q?", charset);
317: to_quoted_printable=true;
318: }
319: dest+=sprintf(dest, "=%02X", *src & 0xFF);
320: } else {
321: *dest++=*src;
322: }
1.44 paf 323: }
1.77 paf 324: if(to_quoted_printable) // close
325: dest+=sprintf(dest, "?=");
326: } else {
1.13 paf 327: memcpy(dest, row->item.ptr, row->item.size);
328: dest+=row->item.size;
1.1 paf 329: }
1.77 paf 330: break;
331: case UL_TABLE:
332: // tainted, untaint language: table
333: escape(switch(*src) {
334: case '\t': to_char(' '); break;
335: case '\n': to_char(' '); break;
336: _default;
337: });
338: break;
339: case UL_SQL:
340: // tainted, untaint language: sql
341: if(connection)
342: dest+=connection->quote(dest, row->item.ptr, row->item.size);
343: else
344: throw Exception(0, 0,
345: this,
346: "untaint in SQL language failed - no connection specified");
347: break;
348: case UL_JS:
349: escape(switch(*src) {
350: case '"': to_string("\\\"", 2); break;
351: case '\'': to_string("\\'", 2); break;
352: case '\n': to_string("\\n", 2); break;
353: case '\\': to_string("\\\\", 2); break;
354: case '\xFF': to_string("\\\xFF", 2); break;
355: _default;
356: });
357: break;
358: case UL_XML:
359: escape(switch(*src) {
360: case '&': to_string("&", 5); break;
361: case '>': to_string(">", 4); break;
362: case '<': to_string("<", 4); break;
363: case '"': to_string(""", 6); break;
364: case '\'': to_string("'", 6); break;
365: _default;
366: });
367: break;
368: case UL_HTML:
369: escape(switch(*src) {
370: case '&': to_string("&", 5); break;
371: case '>': to_string(">", 4); break;
372: case '<': to_string("<", 4); break;
373: case '"': to_string(""", 6); break;
374: _default;
375: });
376: break;
377: default:
378: throw Exception(0, 0,
379: this,
380: "unknown untaint language #%d of %d piece",
381: static_cast<int>(row->item.lang),
382: i); // never
383: break; // never
1.76 paf 384: }
1.55 parser 385:
1.77 paf 386: if(to_lang & UL_OPTIMIZE_BIT) {
387: // optimizing whitespace
388: char *stop=dest; dest=start;
389: for(char *src=start; src<stop; src++)
390: switch(*src) {
391: // of all consequent white space chars leaving only first one
392: case ' ': case '\n': case '\t':
393: if(!whitespace) {
394: *dest++=*src;
395: whitespace=true;
396: }
397: break;
398: default:
399: whitespace=false;
400: *dest++=*src;
401: break;
402: };
403: } else // piece without optimization
404: whitespace=false;
1.78 ! paf 405:
! 406: } \
! 407: chunk=row->link; \
! 408: } while(chunk); \
! 409:
1.76 paf 410: break2:
411: return dest;
412: }
413:
414: char *String::cstr_debug_origins() const {
415: char *result=(char *)malloc(size()+used_rows()*MAX_STRING*2);
416: char *dest=result;
417:
418: const Chunk *chunk=&head;
419: do {
420: const Chunk::Row *row=chunk->rows;
421: for(uint i=0; i<chunk->count; i++, row++) {
422: if(row==append_here)
423: goto break2;
1.55 parser 424:
1.76 paf 425: #ifndef NO_STRING_ORIGIN
426: if(row->item.origin.file)
427: dest+=sprintf(dest, ORIGIN_FILE_LINE_FORMAT,
428: row->item.origin.file,
429: 1+row->item.origin.line);
430: else
431: dest+=sprintf(dest, "<unknown>");
432: #endif
433: dest+=sprintf(dest, "#%s: ",
434: String_Untaint_lang_name[row->item.lang]);
435: char *dest_after_origins=dest;
436:
437: memcpy(dest, row->item.ptr, row->item.size);
438: dest+=row->item.size;
439:
440: remove_crlf(dest_after_origins, dest);
441: to_char('\n');
1.1 paf 442: }
443: chunk=row->link;
444: } while(chunk);
1.64 parser 445:
1.1 paf 446: break2:
1.76 paf 447: *dest=0;
448: return result;
1.1 paf 449: }