|
|
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.74 paf 5: Author: Alexander Petrosyan <paf@design.ru>(http://paf.design.ru)
1.8 paf 6:
1.76 ! paf 7: $Id: untaint.C,v 1.75 2001/11/16 12:38:44 paf Exp $
1.1 paf 8: */
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.58 parser 17: #include "pa_dictionary.h"
1.66 parser 18: #include "pa_common.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]; \
1.60 parser 34: memcpy(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) \
1.60 parser 40: memcpy(dest, b, bsize); \
1.18 paf 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:
1.56 parser 66: //
67:
68: static const char * String_Untaint_lang_name[]={
69: "U", ///< zero value handy for hash lookup @see untaint_lang_name2enum
70: "C", ///< clean
71: "T", ///< tainted, untaint language as assigned later
72: // untaint languages. assigned by ^untaint[lang]{...}
73: "P",
74: /**<
75: leave language built into string being appended.
76: just a flag, that value not stored
77: */
78: "A", ///< leave all characters intact
1.68 parser 79: "F", ///< file specification
80: "H", ///< ext in HTTP response header
1.56 parser 81: "M", ///< text in mail header
82: "URI", ///< text in uri
83: "T", ///< ^table:set body
84: "SQL", ///< ^table:sql body
85: "JS", ///< JavaScript code
1.68 parser 86: "XML", ///< ^dom:set xml
1.56 parser 87: "HTML", ///< HTML code (for editing)
88: "UHTML", ///< HTML code with USER chars
89: };
90:
91:
1.1 paf 92: // String
93:
1.41 paf 94: /*
95:
96: HTTP-header = field-name ":" [ field-value ] CRLF
97:
98: field-name = token
99: field-value = *( field-content | LWS )
100:
101: field-content = <the OCTETs making up the field-value
102: and consisting of either *TEXT or combinations
103: of token, tspecials, and quoted-string>
104:
105:
106:
107: word = token | quoted-string
108:
109: token = 1*<any CHAR except CTLs or tspecials>
110:
111:
112:
113: tspecials = "(" | ")" | "<" | ">" | "@"
114: | "," | ";" | ":" | "\" | <">
115: | "/" | "[" | "]" | "?" | "="
116: | "{" | "}" | SP | HT
117:
118: SP = <US-ASCII SP, space (32)>
119: HT = <US-ASCII HT, horizontal-tab (9)>
120:
121: LWS = [CRLF] 1*( SP | HT )
122: TEXT = <any OCTET except CTLs,
123: but including LWS>
124:
125: quoted-pair = "\" CHAR
126:
127: if(strchr("()<>@,;:\\\"/[]?={} \t", *ptr))
128: */
129: inline bool need_quote_http_header(const char *ptr, size_t size) {
130: for(; size--; ptr++)
1.42 paf 131: if(strchr(";\\\"= \t" /* excluded ()<>@, :/ ? []{} */, *ptr))
1.41 paf 132: return true;
133: return false;
134: }
135:
1.75 paf 136: /// @test UL_OPTIMIZED_HTML optimize
137: size_t String::cstr_bufsize(Untaint_lang lang,
138: SQL_Connection *connection,
139: const char *charset) const {
140: size_t dest=1;
141: bool whitespace=true;
142: const Chunk *chunk=&head;
143: do {
144: const Chunk::Row *row=chunk->rows;
145: for(uint i=0; i<chunk->count; i++, row++) {
146: if(row==append_here)
147: goto break2;
148:
149: Untaint_lang to_lang=lang==UL_UNSPECIFIED?(Untaint_lang)row->item.lang:lang;
150:
151: switch(to_lang) {
152: case UL_CLEAN:
153: // clean piece
154: { // optimizing whitespace
155: escape(switch(*src) {
156: case ' ': case '\n': case '\t':
157: if(!whitespace) {
158: dest++;
159: whitespace=true;
160: }
161: break;
162: default:
163: whitespace=false;
164: dest++;
165: break;
166: });
167: }
168: break;
169: case UL_TAINTED:
170: // tainted piece, but undefined untaint language
171: // for VString.as_double of tainted values
172: // for ^process{body} evaluation
173: case UL_AS_IS:
174: // tainted, untaint language: as-is
175: dest+=row->item.size;
176: break;
177: case UL_FILE_SPEC:
178: // tainted, untaint language: file [name]
179: dest+=row->item.size*3/* worst: Z->%XX */;
180: break;
181: case UL_URI:
182: // tainted, untaint language: uri
183: dest+=row->item.size*3/* worst: Z->%XX */;
184: break;
185: case UL_HTTP_HEADER:
186: // tainted, untaint language: http-field-content-text
187: dest+=row->item.size*3/* worst: Z->%XX */;
188: break;
189: case UL_MAIL_HEADER:
190: // tainted, untaint language: mail-header
191: if(charset) {
192: // Subject: Re: parser3: =?koi8-r?Q?=D3=C5=CD=C9=CE=C1=D2?=
193: dest+=row->item.size*3+MAX_STRING/* worst: =?charset?Q?=%XX?= */;
194: } else {
195: dest+=row->item.size;
196: }
197: break;
198: case UL_TABLE:
199: // tainted, untaint language: table
200: dest+=row->item.size;
201: break;
202: case UL_SQL:
203: // tainted, untaint language: sql
204: if(connection)
205: dest+=connection->quote(0, row->item.ptr, row->item.size);
206: break;
207: case UL_JS:
208: escape(switch(*src) {
209: case '"': case '\'': case '\n': case '\\': case '\xFF':
210: dest+=2; break;
211: default:
212: dest++; break;
213: });
214: break;
215: case UL_XML:
216: escape(switch(*src) {
217: case '&': case '>': case '<': case '"': case '\'':
218: dest+= 6; break;
219: default:
220: dest++; break;
221: });
222: break;
223: case UL_HTML:
224: case UL_OPTIMIZED_HTML:
225: escape(switch(*src) {
226: case '&':
227: case '>':
228: case '<':
229: case '"':
230: dest+=6; break;
231: default:
232: dest++; break;
233: });
234: break;
235: }
236:
237: if((lang==UL_UNSPECIFIED?row->item.lang:lang)!=UL_CLEAN)
238: whitespace=false;
239: }
240: chunk=row->link;
241: } while(chunk);
242:
243: break2:
244: return dest;
1.51 parser 245: }
246:
1.75 paf 247: /// @test UL_OPTIMIZED_HTML optimize
1.43 paf 248: char *String::store_to(char *dest, Untaint_lang lang,
249: SQL_Connection *connection,
250: const char *charset) const {
1.75 paf 251: // WARNING:
252: // before any changes check cstr_bufsize first!!!
1.44 paf 253: bool whitespace=true;
1.1 paf 254: const Chunk *chunk=&head;
255: do {
256: const Chunk::Row *row=chunk->rows;
1.71 paf 257: for(uint i=0; i<chunk->count; i++, row++) {
1.1 paf 258: if(row==append_here)
259: goto break2;
260:
1.72 paf 261: Untaint_lang to_lang=lang==UL_UNSPECIFIED?(Untaint_lang)row->item.lang:lang;
1.55 parser 262:
263: switch(to_lang) {
1.29 paf 264: case UL_CLEAN:
1.1 paf 265: // clean piece
1.44 paf 266: { // optimizing whitespace
1.75 paf 267: escape(switch(*src) {
1.73 paf 268: case ' ': case '\n': case '\t':
1.44 paf 269: if(!whitespace) {
270: *dest++=*src;
271: whitespace=true;
272: }
1.73 paf 273: break;
1.44 paf 274: default:
275: whitespace=false;
276: *dest++=*src;
277: break;
1.75 paf 278: });
1.44 paf 279: }
280: break;
1.29 paf 281: case UL_TAINTED:
1.1 paf 282: // tainted piece, but undefined untaint language
1.23 paf 283: // for VString.as_double of tainted values
1.1 paf 284: // for ^process{body} evaluation
1.11 paf 285: case UL_AS_IS:
1.1 paf 286: // tainted, untaint language: as-is
1.13 paf 287: memcpy(dest, row->item.ptr, row->item.size);
288: dest+=row->item.size;
1.1 paf 289: break;
1.62 parser 290: case UL_FILE_SPEC:
1.9 paf 291: // tainted, untaint language: file [name]
1.18 paf 292: escape(switch(*src) {
293: case ' ': to_char('_'); break;
1.39 paf 294: encode(need_file_encode, '+');
1.18 paf 295: });
1.9 paf 296: break;
1.11 paf 297: case UL_URI:
1.4 paf 298: // tainted, untaint language: uri
1.18 paf 299: escape(switch(*src) {
300: case ' ': to_char('+'); break;
1.13 paf 301: encode(need_uri_encode, '%');
1.18 paf 302: });
1.5 paf 303: break;
1.36 paf 304: case UL_HTTP_HEADER:
1.67 parser 305: // tainted, untaint language: http-field-content-text
306: escape(switch(*src) {
307: case ' ': to_char('+'); break;
308: encode(need_uri_encode, '%');
309: });
1.36 paf 310: break;
311: case UL_MAIL_HEADER:
312: // tainted, untaint language: mail-header
1.46 paf 313: if(charset) {
1.43 paf 314: // Subject: Re: parser3: =?koi8-r?Q?=D3=C5=CD=C9=CE=C1=D2?=
315: const char *src=row->item.ptr;
1.59 parser 316: bool to_quoted_printable=false;
1.43 paf 317: for(int size=row->item.size; size--; src++) {
318: if(*src & 0x80) {
1.59 parser 319: if(!to_quoted_printable) {
1.43 paf 320: dest+=sprintf(dest, "=?%.15s?Q?", charset);
1.59 parser 321: to_quoted_printable=true;
1.43 paf 322: }
1.45 paf 323: dest+=sprintf(dest, "=%02X", *src & 0xFF);
1.43 paf 324: } else {
325: *dest++=*src;
326: }
327: }
1.59 parser 328: if(to_quoted_printable) // close
1.43 paf 329: dest+=sprintf(dest, "?=");
1.46 paf 330: } else {
331: memcpy(dest, row->item.ptr, row->item.size);
332: dest+=row->item.size;
1.43 paf 333: }
1.4 paf 334: break;
1.11 paf 335: case UL_TABLE:
1.15 paf 336: // tainted, untaint language: table
1.18 paf 337: escape(switch(*src) {
338: case '\t': to_char(' '); break;
339: case '\n': to_char(' '); break;
1.13 paf 340: _default;
1.18 paf 341: });
1.1 paf 342: break;
1.11 paf 343: case UL_SQL:
1.1 paf 344: // tainted, untaint language: sql
1.34 paf 345: if(connection)
346: dest+=connection->quote(dest, row->item.ptr, row->item.size);
347: else
1.69 parser 348: throw Exception(0, 0,
1.34 paf 349: this,
350: "untaint in SQL language failed - no connection specified");
1.1 paf 351: break;
1.11 paf 352: case UL_JS:
1.18 paf 353: escape(switch(*src) {
354: case '"': to_string("\\\"", 2); break;
355: case '\'': to_string("\\'", 2); break;
356: case '\n': to_string("\\n", 2); break;
357: case '\\': to_string("\\\\", 2); break;
358: case '\xFF': to_string("\\\xFF", 2); break;
1.13 paf 359: _default;
1.18 paf 360: });
1.1 paf 361: break;
1.61 parser 362: case UL_XML:
363: escape(switch(*src) {
364: case '&': to_string("&", 5); break;
365: case '>': to_string(">", 4); break;
366: case '<': to_string("<", 4); break;
367: case '"': to_string(""", 6); break;
368: case '\'': to_string("'", 6); break;
369: _default;
370: });
371: break;
1.11 paf 372: case UL_HTML:
1.75 paf 373: case UL_OPTIMIZED_HTML:
1.18 paf 374: escape(switch(*src) {
375: case '&': to_string("&", 5); break;
376: case '>': to_string(">", 4); break;
377: case '<': to_string("<", 4); break;
378: case '"': to_string(""", 6); break;
1.13 paf 379: _default;
1.18 paf 380: });
1.1 paf 381: break;
382: default:
1.69 parser 383: throw Exception(0, 0,
1.18 paf 384: this,
1.1 paf 385: "unknown untaint language #%d of %d piece",
1.18 paf 386: static_cast<int>(row->item.lang),
1.38 paf 387: i); // never
1.48 parser 388: break; // never
1.1 paf 389: }
1.44 paf 390:
391: if((lang==UL_UNSPECIFIED?row->item.lang:lang)!=UL_CLEAN)
392: whitespace=false;
1.76 ! paf 393: }
! 394: chunk=row->link;
! 395: } while(chunk);
1.55 parser 396:
1.76 ! paf 397: break2:
! 398: return dest;
! 399: }
! 400:
! 401: char *String::cstr_debug_origins() const {
! 402: char *result=(char *)malloc(size()+used_rows()*MAX_STRING*2);
! 403: char *dest=result;
! 404:
! 405: const Chunk *chunk=&head;
! 406: do {
! 407: const Chunk::Row *row=chunk->rows;
! 408: for(uint i=0; i<chunk->count; i++, row++) {
! 409: if(row==append_here)
! 410: goto break2;
1.55 parser 411:
1.76 ! paf 412: #ifndef NO_STRING_ORIGIN
! 413: if(row->item.origin.file)
! 414: dest+=sprintf(dest, ORIGIN_FILE_LINE_FORMAT,
! 415: row->item.origin.file,
! 416: 1+row->item.origin.line);
! 417: else
! 418: dest+=sprintf(dest, "<unknown>");
! 419: #endif
! 420: dest+=sprintf(dest, "#%s: ",
! 421: String_Untaint_lang_name[row->item.lang]);
! 422: char *dest_after_origins=dest;
! 423:
! 424: memcpy(dest, row->item.ptr, row->item.size);
! 425: dest+=row->item.size;
! 426:
! 427: remove_crlf(dest_after_origins, dest);
! 428: to_char('\n');
1.1 paf 429: }
430: chunk=row->link;
431: } while(chunk);
1.64 parser 432:
1.1 paf 433: break2:
1.76 ! paf 434: *dest=0;
! 435: return result;
1.1 paf 436: }