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