|
|
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.63 parser 5: Author: Alexander Petrosyan <paf@design.ru>(http://design.ru/paf)
1.8 paf 6:
1.64 ! parser 7: $Id: untaint.C,v 1.63 2001/09/26 10:32:26 parser 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.1 paf 18:
1.18 paf 19: #define escape(action) \
1.1 paf 20: { \
1.13 paf 21: const char *src=row->item.ptr; \
22: for(int size=row->item.size; size--; src++) \
1.18 paf 23: action \
1.1 paf 24: }
1.13 paf 25: #define _default default: *dest++=*src; break
26: #define encode(need_encode_func, prefix) \
1.5 paf 27: default: \
1.13 paf 28: if(need_encode_func(*src)) { \
1.5 paf 29: static const char *hex="0123456789ABCDEF"; \
1.9 paf 30: char chunk[3]={prefix}; \
1.13 paf 31: chunk[1]=hex[((unsigned char)*src)/0x10]; \
32: chunk[2]=hex[((unsigned char)*src)%0x10]; \
1.60 parser 33: memcpy(dest, chunk, 3); dest+=3; \
1.5 paf 34: } else \
1.13 paf 35: *dest++=*src; \
1.5 paf 36: break
1.18 paf 37: #define to_char(c) *dest++=c
38: #define to_string(b, bsize) \
1.60 parser 39: memcpy(dest, b, bsize); \
1.18 paf 40: dest+=bsize; \
1.4 paf 41:
1.9 paf 42: inline bool need_file_encode(unsigned char c){
1.13 paf 43: if((c>='0') &&(c<='9') ||(c>='A') &&(c<='Z') ||(c>='a') &&(c<='z'))
1.9 paf 44: return false;
45:
1.31 paf 46: return !strchr(
47: #ifdef WIN32
1.37 paf 48: ":\\~"
1.31 paf 49: #endif
1.39 paf 50: "./()_-", c);
1.9 paf 51: }
1.5 paf 52: inline bool need_uri_encode(unsigned char c){
1.13 paf 53: if((c>='0') &&(c<='9') ||(c>='A') &&(c<='Z') ||(c>='a') &&(c<='z'))
1.4 paf 54: return false;
55:
1.5 paf 56: return !strchr("_-./", c);
57: }
1.36 paf 58: inline bool need_http_header_encode(unsigned char c){
1.18 paf 59: if(strchr(" , :", c))
1.5 paf 60: return false;
61:
62: return need_uri_encode(c);
1.4 paf 63: }
1.1 paf 64:
1.56 parser 65: //
66:
67: static const char * String_Untaint_lang_name[]={
68: "U", ///< zero value handy for hash lookup @see untaint_lang_name2enum
69: "C", ///< clean
70: "T", ///< tainted, untaint language as assigned later
71: // untaint languages. assigned by ^untaint[lang]{...}
72: "P",
73: /**<
74: leave language built into string being appended.
75: just a flag, that value not stored
76: */
77: "A", ///< leave all characters intact
78: "F", ///< filename
79: "H", ///< text in HTTP response header
80: "M", ///< text in mail header
81: "URI", ///< text in uri
82: "T", ///< ^table:set body
83: "SQL", ///< ^table:sql body
84: "JS", ///< JavaScript code
85: "HTML", ///< HTML code (for editing)
86: "UHTML", ///< HTML code with USER chars
87: };
88:
89:
1.1 paf 90: // String
91:
1.13 paf 92: static bool typo_present(Array::Item *value, const void *info) {
93: Array *row=static_cast<Array *>(value);
94: const char *src=static_cast<const char *>(info);
95:
96: int partial;
1.28 paf 97: row->get_string(0)->cmp(partial, src);
1.14 paf 98: return
99: partial==0 || // full match
100: partial==1; // typo left column starts 'src'
1.13 paf 101: }
102:
1.41 paf 103: /*
104:
105: HTTP-header = field-name ":" [ field-value ] CRLF
106:
107: field-name = token
108: field-value = *( field-content | LWS )
109:
110: field-content = <the OCTETs making up the field-value
111: and consisting of either *TEXT or combinations
112: of token, tspecials, and quoted-string>
113:
114:
115:
116: word = token | quoted-string
117:
118: token = 1*<any CHAR except CTLs or tspecials>
119:
120:
121:
122: tspecials = "(" | ")" | "<" | ">" | "@"
123: | "," | ";" | ":" | "\" | <">
124: | "/" | "[" | "]" | "?" | "="
125: | "{" | "}" | SP | HT
126:
127: SP = <US-ASCII SP, space (32)>
128: HT = <US-ASCII HT, horizontal-tab (9)>
129:
130: LWS = [CRLF] 1*( SP | HT )
131: TEXT = <any OCTET except CTLs,
132: but including LWS>
133:
134: quoted-pair = "\" CHAR
135:
136: if(strchr("()<>@,;:\\\"/[]?={} \t", *ptr))
137: */
138: inline bool need_quote_http_header(const char *ptr, size_t size) {
139: for(; size--; ptr++)
1.42 paf 140: if(strchr(";\\\"= \t" /* excluded ()<>@, :/ ? []{} */, *ptr))
1.41 paf 141: return true;
142: return false;
143: }
144:
1.55 parser 145: /** @todo maybe additional check "are all pieces are clean?" would be profitable?
146: @todo fix potential forigins_mode buf overrun
147: */
1.51 parser 148: size_t String::cstr_bufsize(Untaint_lang lang) const {
1.55 parser 149: return (lang==UL_AS_IS?size():size()*UNTAINT_TIMES_BIGGER*(forigins_mode?10:1)) +1;
1.51 parser 150: }
151:
1.54 parser 152: /** @todo fix theoretical \n mem overrun in TYPO replacements
153: */
1.43 paf 154: char *String::store_to(char *dest, Untaint_lang lang,
155: SQL_Connection *connection,
156: const char *charset) const {
1.13 paf 157: // $MAIN:html-typo table
1.58 parser 158: Dictionary *user_typo_dict=static_cast<Dictionary *>(pool().tag());
159: Dictionary *typo_dict=user_typo_dict?user_typo_dict:default_typo_dict;
1.1 paf 160:
1.44 paf 161: bool whitespace=true;
1.64 ! parser 162: bool need_to_close_http_header_quote=false;
1.1 paf 163: const Chunk *chunk=&head;
164: do {
165: const Chunk::Row *row=chunk->rows;
1.28 paf 166: for(size_t i=0; i<chunk->count; i++, row++) {
1.1 paf 167: if(row==append_here)
168: goto break2;
169:
1.55 parser 170: Untaint_lang to_lang=lang==UL_UNSPECIFIED?row->item.lang:lang;
171:
172: char *dest_before_origins=dest;
173:
174: if(forigins_mode) {
175: #ifndef NO_STRING_ORIGIN
176: if(row->item.origin.file)
177: dest+=sprintf(dest, "%s(%d)",
178: row->item.origin.file,
179: 1+row->item.origin.line);
180: else
181: dest+=sprintf(dest, "unknown");
182: #endif
1.56 parser 183: dest+=sprintf(dest, "#%s: ",
184: String_Untaint_lang_name[to_lang]);
1.55 parser 185: }
186: char *dest_after_origins=dest;
187:
1.1 paf 188: // WARNING:
189: // string can grow only UNTAINT_TIMES_BIGGER
1.55 parser 190: switch(to_lang) {
1.29 paf 191: case UL_CLEAN:
1.1 paf 192: // clean piece
1.44 paf 193: { // optimizing whitespace
194: const char *src=row->item.ptr;
195: for(int size=row->item.size; size--; src++)
196: switch(*src) {
197: case ' ': case '\n': case '\r': case '\t':
198: if(!whitespace) {
199: *dest++=*src;
200: whitespace=true;
201: }
202: break;
203: default:
204: whitespace=false;
205: *dest++=*src;
206: break;
207: }
208: }
209: break;
1.29 paf 210: case UL_TAINTED:
1.1 paf 211: // tainted piece, but undefined untaint language
1.23 paf 212: // for VString.as_double of tainted values
1.1 paf 213: // for ^process{body} evaluation
1.11 paf 214: case UL_AS_IS:
1.1 paf 215: // tainted, untaint language: as-is
1.13 paf 216: memcpy(dest, row->item.ptr, row->item.size);
217: dest+=row->item.size;
1.1 paf 218: break;
1.62 parser 219: case UL_FILE_SPEC:
1.9 paf 220: // tainted, untaint language: file [name]
1.18 paf 221: escape(switch(*src) {
222: case ' ': to_char('_'); break;
1.39 paf 223: encode(need_file_encode, '+');
1.18 paf 224: });
1.9 paf 225: break;
1.11 paf 226: case UL_URI:
1.4 paf 227: // tainted, untaint language: uri
1.18 paf 228: escape(switch(*src) {
229: case ' ': to_char('+'); break;
1.13 paf 230: encode(need_uri_encode, '%');
1.18 paf 231: });
1.5 paf 232: break;
1.36 paf 233: case UL_HTTP_HEADER:
234: // tainted, untaint language: http-header
1.41 paf 235: if(need_quote_http_header(row->item.ptr, row->item.size)) {
236: *dest++='\"';
237: escape(switch(*src) {
238: case '\"': to_string("\\\"", 2); break;
239: _default;
240: });
1.64 ! parser 241: need_to_close_http_header_quote=true;
1.41 paf 242: } else {
243: memcpy(dest, row->item.ptr, row->item.size);
244: dest+=row->item.size;
245: }
1.36 paf 246: break;
247: case UL_MAIL_HEADER:
248: // tainted, untaint language: mail-header
1.46 paf 249: if(charset) {
1.43 paf 250: // Subject: Re: parser3: =?koi8-r?Q?=D3=C5=CD=C9=CE=C1=D2?=
251: const char *src=row->item.ptr;
1.59 parser 252: bool to_quoted_printable=false;
1.43 paf 253: for(int size=row->item.size; size--; src++) {
254: if(*src & 0x80) {
1.59 parser 255: if(!to_quoted_printable) {
1.43 paf 256: dest+=sprintf(dest, "=?%.15s?Q?", charset);
1.59 parser 257: to_quoted_printable=true;
1.43 paf 258: }
1.45 paf 259: dest+=sprintf(dest, "=%02X", *src & 0xFF);
1.43 paf 260: } else {
261: *dest++=*src;
262: }
263: }
1.59 parser 264: if(to_quoted_printable) // close
1.43 paf 265: dest+=sprintf(dest, "?=");
1.46 paf 266: } else {
267: memcpy(dest, row->item.ptr, row->item.size);
268: dest+=row->item.size;
1.43 paf 269: }
1.4 paf 270: break;
1.11 paf 271: case UL_TABLE:
1.15 paf 272: // tainted, untaint language: table
1.18 paf 273: escape(switch(*src) {
274: case '\t': to_char(' '); break;
275: case '\n': to_char(' '); break;
1.13 paf 276: _default;
1.18 paf 277: });
1.1 paf 278: break;
1.11 paf 279: case UL_SQL:
1.1 paf 280: // tainted, untaint language: sql
1.34 paf 281: if(connection)
282: dest+=connection->quote(dest, row->item.ptr, row->item.size);
283: else
284: THROW(0, 0,
285: this,
286: "untaint in SQL language failed - no connection specified");
1.1 paf 287: break;
1.11 paf 288: case UL_JS:
1.18 paf 289: escape(switch(*src) {
290: case '"': to_string("\\\"", 2); break;
291: case '\'': to_string("\\'", 2); break;
292: case '\n': to_string("\\n", 2); break;
293: case '\\': to_string("\\\\", 2); break;
294: case '\xFF': to_string("\\\xFF", 2); break;
1.13 paf 295: _default;
1.18 paf 296: });
1.1 paf 297: break;
1.61 parser 298: case UL_XML:
299: escape(switch(*src) {
300: case '&': to_string("&", 5); break;
301: case '>': to_string(">", 4); break;
302: case '<': to_string("<", 4); break;
303: case '"': to_string(""", 6); break;
304: case '\'': to_string("'", 6); break;
305: _default;
306: });
307: break;
1.11 paf 308: case UL_HTML:
1.18 paf 309: escape(switch(*src) {
310: case '&': to_string("&", 5); break;
311: case '>': to_string(">", 4); break;
312: case '<': to_string("<", 4); break;
313: case '"': to_string(""", 6); break;
1.13 paf 314: _default;
1.18 paf 315: });
1.1 paf 316: break;
1.47 paf 317: case UL_USER_HTML: {
1.1 paf 318: // tainted, untaint language: html-typo
1.58 parser 319: if(!typo_dict) // never, always has default
1.57 parser 320: THROW(0, 0,
321: this,
322: "untaint to user-html lang failed, no typo table");
323:
1.50 parser 324: char *html_for_typo=
325: (char *)malloc(row->item.size*2/* '\n' -> '\' 'n' */+1);
1.19 paf 326: // note:
327: // there still is a possibility that user
328: // would not replace \n as she supposed to
329: // and rather replace \ and n into huge strings
330: // thus causing memory overrun
331: // this can be dealed by allocating *2 memory, but that's too expensive
1.18 paf 332: size_t html_for_typo_size;
1.13 paf 333: { // local dest
1.18 paf 334: char *dest=html_for_typo;
335: escape(switch(*src) {
1.16 paf 336: // convinient name for typo match "\n"
337: case '\r':
1.57 parser 338: to_string("\\n", 2); // \r -> "\n"
339: if(size && src[1]=='\n') { // \r\n -> remove \n
340: size--; src++;
1.18 paf 341: }
342: break;
343: case '\n':
1.57 parser 344: to_string("\\n", 2);
1.16 paf 345: break;
1.13 paf 346: _default;
1.18 paf 347: });
1.13 paf 348: *dest=0;
1.18 paf 349: html_for_typo_size=dest-html_for_typo;
1.13 paf 350: }
351: // typo table replacements
1.21 paf 352: const char *src=html_for_typo;
353: do {
354: // there is a row where first column starts 'src'
1.58 parser 355: if(Table::Item *item=typo_dict->first_that_starts(src)) {
1.21 paf 356: // get a=>b values
357: const String& a=*static_cast<Array *>(item)->get_string(0);
358: const String& b=*static_cast<Array *>(item)->get_string(1);
359: // overflow check:
360: // b allowed to be max UNTAINT_TIMES_BIGGER then a
361: if(b.size()>UNTAINT_TIMES_BIGGER*a.size()) {
1.58 parser 362: pool().set_tag(0); // avoid recursion
1.21 paf 363: THROW(0, 0,
364: &b,
365: "is %g times longer then '%s', "
366: "while maximum, handled by Parser, is %d",
1.50 parser 367: ((double)b.size())/a.size(),
368: a.cstr(),
369: UNTAINT_TIMES_BIGGER);
1.21 paf 370: }
371:
372: // skip 'a' in 'src'
373: src+=a.size();
374: // write 'b' to 'dest'
375: b.store_to(dest);
1.59 parser 376: // skip 'b' in 'dest'
1.21 paf 377: dest+=b.size();
378: } else
379: *dest++=*src++;
380: } while(*src);
1.1 paf 381: break;
1.13 paf 382: }
1.1 paf 383: default:
1.18 paf 384: THROW(0, 0,
385: this,
1.1 paf 386: "unknown untaint language #%d of %d piece",
1.18 paf 387: static_cast<int>(row->item.lang),
1.38 paf 388: i); // never
1.48 parser 389: break; // never
1.1 paf 390: }
1.44 paf 391:
392: if((lang==UL_UNSPECIFIED?row->item.lang:lang)!=UL_CLEAN)
393: whitespace=false;
1.55 parser 394:
395: if(forigins_mode)
396: if(dest==dest_after_origins) // never moved==optimized space
397: dest=dest_before_origins;
398: else {
399: for(char *p=dest_after_origins; p<dest; p++)
400: if(*p=='\n')
401: *p='|';
402:
403: to_char('\n');
404: }
1.1 paf 405: }
406: chunk=row->link;
407: } while(chunk);
1.64 ! parser 408:
! 409: if(need_to_close_http_header_quote)
! 410: *dest++='\"';
1.1 paf 411: break2:
1.13 paf 412: return dest;
1.1 paf 413: }