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