|
|
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.53 ! parser 8: static const char *RCSId="$Id: double.C,v 1.31 2001/06/28 07:41:59 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.51 parser 119: /// @todo maybe additional check "are all pieces are clean?" would be profitable?
120: size_t String::cstr_bufsize(Untaint_lang lang) const {
121: return (lang==UL_AS_IS?size():size()*UNTAINT_TIMES_BIGGER) +1;
122: }
123:
124: /// @todo fix theoretical \n mem overrun in TYPO replacements
1.43 paf 125: char *String::store_to(char *dest, Untaint_lang lang,
126: SQL_Connection *connection,
127: const char *charset) const {
1.13 paf 128: // $MAIN:html-typo table
1.26 paf 129: Table *user_typo_table=static_cast<Table *>(pool().tag());
130: Table *typo_table=user_typo_table?user_typo_table:default_typo_table;
1.1 paf 131:
1.44 paf 132: bool whitespace=true;
1.1 paf 133: const Chunk *chunk=&head;
134: do {
135: const Chunk::Row *row=chunk->rows;
1.28 paf 136: for(size_t i=0; i<chunk->count; i++, row++) {
1.1 paf 137: if(row==append_here)
138: goto break2;
139:
140: // WARNING:
141: // string can grow only UNTAINT_TIMES_BIGGER
1.35 paf 142: switch(lang==UL_UNSPECIFIED?row->item.lang:lang) {
1.29 paf 143: case UL_CLEAN:
1.1 paf 144: // clean piece
1.44 paf 145: { // optimizing whitespace
146: const char *src=row->item.ptr;
147: for(int size=row->item.size; size--; src++)
148: switch(*src) {
149: case ' ': case '\n': case '\r': case '\t':
150: if(!whitespace) {
151: *dest++=*src;
152: whitespace=true;
153: }
154: break;
155: default:
156: whitespace=false;
157: *dest++=*src;
158: break;
159: }
160: }
161: break;
1.29 paf 162: case UL_TAINTED:
1.1 paf 163: // tainted piece, but undefined untaint language
1.23 paf 164: // for VString.as_double of tainted values
1.1 paf 165: // for ^process{body} evaluation
1.11 paf 166: case UL_AS_IS:
1.1 paf 167: // tainted, untaint language: as-is
1.13 paf 168: memcpy(dest, row->item.ptr, row->item.size);
169: dest+=row->item.size;
1.1 paf 170: break;
1.11 paf 171: case UL_FILE_NAME:
1.9 paf 172: // tainted, untaint language: file [name]
1.18 paf 173: escape(switch(*src) {
174: case ' ': to_char('_'); break;
1.39 paf 175: encode(need_file_encode, '+');
1.18 paf 176: });
1.9 paf 177: break;
1.11 paf 178: case UL_URI:
1.4 paf 179: // tainted, untaint language: uri
1.18 paf 180: escape(switch(*src) {
181: case ' ': to_char('+'); break;
1.13 paf 182: encode(need_uri_encode, '%');
1.18 paf 183: });
1.5 paf 184: break;
1.36 paf 185: case UL_HTTP_HEADER:
186: // tainted, untaint language: http-header
1.41 paf 187: if(need_quote_http_header(row->item.ptr, row->item.size)) {
188: *dest++='\"';
189: escape(switch(*src) {
190: case '\"': to_string("\\\"", 2); break;
191: _default;
192: });
193: *dest++='\"';
194: } else {
195: memcpy(dest, row->item.ptr, row->item.size);
196: dest+=row->item.size;
197: }
1.36 paf 198: break;
199: case UL_MAIL_HEADER:
200: // tainted, untaint language: mail-header
1.46 paf 201: if(charset) {
1.43 paf 202: // Subject: Re: parser3: =?koi8-r?Q?=D3=C5=CD=C9=CE=C1=D2?=
203: const char *src=row->item.ptr;
1.45 paf 204: bool to_base_64=false;
1.43 paf 205: for(int size=row->item.size; size--; src++) {
206: if(*src & 0x80) {
1.45 paf 207: if(!to_base_64) {
1.43 paf 208: dest+=sprintf(dest, "=?%.15s?Q?", charset);
1.45 paf 209: to_base_64=true;
1.43 paf 210: }
1.45 paf 211: dest+=sprintf(dest, "=%02X", *src & 0xFF);
1.43 paf 212: } else {
213: *dest++=*src;
214: }
215: }
1.45 paf 216: if(to_base_64) // close
1.43 paf 217: dest+=sprintf(dest, "?=");
1.46 paf 218: } else {
219: memcpy(dest, row->item.ptr, row->item.size);
220: dest+=row->item.size;
1.43 paf 221: }
1.4 paf 222: break;
1.11 paf 223: case UL_TABLE:
1.15 paf 224: // tainted, untaint language: table
1.18 paf 225: escape(switch(*src) {
226: case '\t': to_char(' '); break;
227: case '\n': to_char(' '); break;
1.13 paf 228: _default;
1.18 paf 229: });
1.1 paf 230: break;
1.11 paf 231: case UL_SQL:
1.1 paf 232: // tainted, untaint language: sql
1.34 paf 233: if(connection)
234: dest+=connection->quote(dest, row->item.ptr, row->item.size);
235: else
236: THROW(0, 0,
237: this,
238: "untaint in SQL language failed - no connection specified");
1.1 paf 239: break;
1.11 paf 240: case UL_JS:
1.18 paf 241: escape(switch(*src) {
242: case '"': to_string("\\\"", 2); break;
243: case '\'': to_string("\\'", 2); break;
244: case '\n': to_string("\\n", 2); break;
245: case '\\': to_string("\\\\", 2); break;
246: case '\xFF': to_string("\\\xFF", 2); break;
1.13 paf 247: _default;
1.18 paf 248: });
1.1 paf 249: break;
1.11 paf 250: case UL_HTML:
1.18 paf 251: escape(switch(*src) {
252: case '&': to_string("&", 5); break;
253: case '>': to_string(">", 4); break;
254: case '<': to_string("<", 4); break;
255: case '"': to_string(""", 6); break;
1.19 paf 256: //TODO: XSLT case '\'': to_string("'", 6); break;
1.13 paf 257: _default;
1.18 paf 258: });
1.1 paf 259: break;
1.47 paf 260: case UL_USER_HTML: {
1.1 paf 261: // tainted, untaint language: html-typo
1.50 parser 262: char *html_for_typo=
263: (char *)malloc(row->item.size*2/* '\n' -> '\' 'n' */+1);
1.19 paf 264: // note:
265: // there still is a possibility that user
266: // would not replace \n as she supposed to
267: // and rather replace \ and n into huge strings
268: // thus causing memory overrun
269: // this can be dealed by allocating *2 memory, but that's too expensive
1.18 paf 270: size_t html_for_typo_size;
1.13 paf 271: { // local dest
1.18 paf 272: char *dest=html_for_typo;
273: escape(switch(*src) {
1.16 paf 274: // convinient name for typo match "\n"
275: case '\r':
1.18 paf 276: if(typo_table) {
277: *dest++='\\'; *dest++='n'; // \r -> \n
1.24 paf 278: if(src[1]=='\n') { // \r\n -> remove \n
279: size--; src++;
280: }
1.18 paf 281: }
282: break;
283: case '\n':
284: if(typo_table)
285: to_string("\\n", 2);
1.16 paf 286: break;
1.19 paf 287: //TODO: XSLT case '\'': to_string("'", 6); break;
1.13 paf 288: _default;
1.18 paf 289: });
1.13 paf 290: *dest=0;
1.18 paf 291: html_for_typo_size=dest-html_for_typo;
1.13 paf 292: }
293: // typo table replacements
1.21 paf 294: const char *src=html_for_typo;
295: do {
296: // there is a row where first column starts 'src'
297: if(Table::Item *item=typo_table->first_that(typo_present, src)) {
298: // get a=>b values
299: const String& a=*static_cast<Array *>(item)->get_string(0);
300: const String& b=*static_cast<Array *>(item)->get_string(1);
301: // empty 'a' | 'b' checks
302: if(a.size()==0 || b.size()==0) {
1.26 paf 303: pool().set_tag(default_typo_table); // avoid recursion
1.21 paf 304: THROW(0, 0,
305: typo_table->origin_string(),
306: "typo table column elements must not be empty");
307: }
308: // overflow check:
309: // b allowed to be max UNTAINT_TIMES_BIGGER then a
310: if(b.size()>UNTAINT_TIMES_BIGGER*a.size()) {
1.26 paf 311: pool().set_tag(default_typo_table); // avoid recursion
1.21 paf 312: THROW(0, 0,
313: &b,
314: "is %g times longer then '%s', "
315: "while maximum, handled by Parser, is %d",
1.50 parser 316: ((double)b.size())/a.size(),
317: a.cstr(),
318: UNTAINT_TIMES_BIGGER);
1.21 paf 319: }
320:
321: // skip 'a' in 'src'
322: src+=a.size();
323: // write 'b' to 'dest'
324: b.store_to(dest);
325: dest+=b.size();
326: } else
327: *dest++=*src++;
328: } while(*src);
1.1 paf 329: break;
1.13 paf 330: }
1.1 paf 331: default:
1.18 paf 332: THROW(0, 0,
333: this,
1.1 paf 334: "unknown untaint language #%d of %d piece",
1.18 paf 335: static_cast<int>(row->item.lang),
1.38 paf 336: i); // never
1.48 parser 337: break; // never
1.1 paf 338: }
1.44 paf 339:
340: if((lang==UL_UNSPECIFIED?row->item.lang:lang)!=UL_CLEAN)
341: whitespace=false;
1.1 paf 342: }
343: chunk=row->link;
344: } while(chunk);
345: break2:
1.13 paf 346: return dest;
1.1 paf 347: }