|
|
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.44 ! paf 8: $Id: untaint.C,v 1.43 2001/04/23 08:52:24 paf Exp $
1.1 paf 9: */
10:
1.12 paf 11: #include "pa_config_includes.h"
1.1 paf 12:
13: #include "pa_pool.h"
14: #include "pa_string.h"
15: #include "pa_hash.h"
16: #include "pa_exception.h"
1.13 paf 17: #include "pa_table.h"
1.32 paf 18: #include "pa_globals.h"
1.34 paf 19: #include "pa_sql_connection.h"
1.1 paf 20:
1.18 paf 21: #define escape(action) \
1.1 paf 22: { \
1.13 paf 23: const char *src=row->item.ptr; \
24: for(int size=row->item.size; size--; src++) \
1.18 paf 25: action \
1.1 paf 26: }
1.13 paf 27: #define _default default: *dest++=*src; break
28: #define encode(need_encode_func, prefix) \
1.5 paf 29: default: \
1.13 paf 30: if(need_encode_func(*src)) { \
1.5 paf 31: static const char *hex="0123456789ABCDEF"; \
1.9 paf 32: char chunk[3]={prefix}; \
1.13 paf 33: chunk[1]=hex[((unsigned char)*src)/0x10]; \
34: chunk[2]=hex[((unsigned char)*src)%0x10]; \
35: strncpy(dest, chunk, 3); dest+=3; \
1.5 paf 36: } else \
1.13 paf 37: *dest++=*src; \
1.5 paf 38: break
1.18 paf 39: #define to_char(c) *dest++=c
40: #define to_string(b, bsize) \
41: strncpy(dest, b, bsize); \
42: dest+=bsize; \
1.4 paf 43:
1.9 paf 44: inline bool need_file_encode(unsigned char c){
1.13 paf 45: if((c>='0') &&(c<='9') ||(c>='A') &&(c<='Z') ||(c>='a') &&(c<='z'))
1.9 paf 46: return false;
47:
1.31 paf 48: return !strchr(
49: #ifdef WIN32
1.37 paf 50: ":\\~"
1.31 paf 51: #endif
1.39 paf 52: "./()_-", c);
1.9 paf 53: }
1.5 paf 54: inline bool need_uri_encode(unsigned char c){
1.13 paf 55: if((c>='0') &&(c<='9') ||(c>='A') &&(c<='Z') ||(c>='a') &&(c<='z'))
1.4 paf 56: return false;
57:
1.5 paf 58: return !strchr("_-./", c);
59: }
1.36 paf 60: inline bool need_http_header_encode(unsigned char c){
1.18 paf 61: if(strchr(" , :", c))
1.5 paf 62: return false;
63:
64: return need_uri_encode(c);
1.4 paf 65: }
1.1 paf 66:
67: // String
68:
1.13 paf 69: static bool typo_present(Array::Item *value, const void *info) {
70: Array *row=static_cast<Array *>(value);
71: const char *src=static_cast<const char *>(info);
72:
73: int partial;
1.28 paf 74: row->get_string(0)->cmp(partial, src);
1.14 paf 75: return
76: partial==0 || // full match
77: partial==1; // typo left column starts 'src'
1.13 paf 78: }
79:
1.41 paf 80: /*
81:
82: HTTP-header = field-name ":" [ field-value ] CRLF
83:
84: field-name = token
85: field-value = *( field-content | LWS )
86:
87: field-content = <the OCTETs making up the field-value
88: and consisting of either *TEXT or combinations
89: of token, tspecials, and quoted-string>
90:
91:
92:
93: word = token | quoted-string
94:
95: token = 1*<any CHAR except CTLs or tspecials>
96:
97:
98:
99: tspecials = "(" | ")" | "<" | ">" | "@"
100: | "," | ";" | ":" | "\" | <">
101: | "/" | "[" | "]" | "?" | "="
102: | "{" | "}" | SP | HT
103:
104: SP = <US-ASCII SP, space (32)>
105: HT = <US-ASCII HT, horizontal-tab (9)>
106:
107: LWS = [CRLF] 1*( SP | HT )
108: TEXT = <any OCTET except CTLs,
109: but including LWS>
110:
111: quoted-pair = "\" CHAR
112:
113: if(strchr("()<>@,;:\\\"/[]?={} \t", *ptr))
114: */
115: inline bool need_quote_http_header(const char *ptr, size_t size) {
116: for(; size--; ptr++)
1.42 paf 117: if(strchr(";\\\"= \t" /* excluded ()<>@, :/ ? []{} */, *ptr))
1.41 paf 118: return true;
119: return false;
120: }
121:
1.30 paf 122: /**
123: @todo fix theoretical \n mem overrun in TYPO replacements
124: */
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.43 paf 201: {
202: // Subject: Re: parser3: =?koi8-r?Q?=D3=C5=CD=C9=CE=C1=D2?=
203: const char *src=row->item.ptr;
204: bool down=false;
205: for(int size=row->item.size; size--; src++) {
206: if(*src & 0x80) {
207: if(!down) {
208: dest+=sprintf(dest, "=?%.15s?Q?", charset);
209: down=true;
210: }
211: dest+=sprintf(dest, "=%02X", *src & 0xFF);
212: } else {
213: if(down) {
214: down=false;
215: dest+=sprintf(dest, "?=");
216: }
217: *dest++=*src;
218: }
219: }
220: if(down) // close unclosed
221: dest+=sprintf(dest, "?=");
222: }
1.4 paf 223: break;
1.11 paf 224: case UL_TABLE:
1.15 paf 225: // tainted, untaint language: table
1.18 paf 226: escape(switch(*src) {
227: case '\t': to_char(' '); break;
228: case '\n': to_char(' '); break;
1.13 paf 229: _default;
1.18 paf 230: });
1.1 paf 231: break;
1.11 paf 232: case UL_SQL:
1.1 paf 233: // tainted, untaint language: sql
1.34 paf 234: if(connection)
235: dest+=connection->quote(dest, row->item.ptr, row->item.size);
236: else
237: THROW(0, 0,
238: this,
239: "untaint in SQL language failed - no connection specified");
1.1 paf 240: break;
1.11 paf 241: case UL_JS:
1.18 paf 242: escape(switch(*src) {
243: case '"': to_string("\\\"", 2); break;
244: case '\'': to_string("\\'", 2); break;
245: case '\n': to_string("\\n", 2); break;
246: case '\\': to_string("\\\\", 2); break;
247: case '\xFF': to_string("\\\xFF", 2); break;
1.13 paf 248: _default;
1.18 paf 249: });
1.1 paf 250: break;
1.11 paf 251: case UL_HTML:
1.18 paf 252: escape(switch(*src) {
253: case '&': to_string("&", 5); break;
254: case '>': to_string(">", 4); break;
255: case '<': to_string("<", 4); break;
256: case '"': to_string(""", 6); break;
1.19 paf 257: //TODO: XSLT case '\'': to_string("'", 6); break;
1.13 paf 258: _default;
1.18 paf 259: });
1.1 paf 260: break;
1.13 paf 261: case UL_HTML_TYPO: {
1.1 paf 262: // tainted, untaint language: html-typo
1.19 paf 263: char *html_for_typo=(char *)malloc(size()*2/* '\n' -> '\' 'n' */+1);
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",
316: ((double)b.size())/a.size(),
317: a.cstr(),
318: UNTAINT_TIMES_BIGGER);
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.1 paf 337: }
1.44 ! paf 338:
! 339: if((lang==UL_UNSPECIFIED?row->item.lang:lang)!=UL_CLEAN)
! 340: whitespace=false;
1.1 paf 341: }
342: chunk=row->link;
343: } while(chunk);
344: break2:
1.13 paf 345: return dest;
1.1 paf 346: }