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