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