|
|
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.75 ! paf 7: $Id: untaint.C,v 1.74 2001/11/05 11:46:29 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: if(forigins_mode) {
! 152: #ifndef NO_STRING_ORIGIN
! 153: dest+=MAX_STRING;
! 154: #endif
! 155: dest+=MAX_STRING;
! 156: }
! 157:
! 158: switch(to_lang) {
! 159: case UL_CLEAN:
! 160: // clean piece
! 161: { // optimizing whitespace
! 162: escape(switch(*src) {
! 163: case ' ': case '\n': case '\t':
! 164: if(!whitespace) {
! 165: dest++;
! 166: whitespace=true;
! 167: }
! 168: break;
! 169: default:
! 170: whitespace=false;
! 171: dest++;
! 172: break;
! 173: });
! 174: }
! 175: break;
! 176: case UL_TAINTED:
! 177: // tainted piece, but undefined untaint language
! 178: // for VString.as_double of tainted values
! 179: // for ^process{body} evaluation
! 180: case UL_AS_IS:
! 181: // tainted, untaint language: as-is
! 182: dest+=row->item.size;
! 183: break;
! 184: case UL_FILE_SPEC:
! 185: // tainted, untaint language: file [name]
! 186: dest+=row->item.size*3/* worst: Z->%XX */;
! 187: break;
! 188: case UL_URI:
! 189: // tainted, untaint language: uri
! 190: dest+=row->item.size*3/* worst: Z->%XX */;
! 191: break;
! 192: case UL_HTTP_HEADER:
! 193: // tainted, untaint language: http-field-content-text
! 194: dest+=row->item.size*3/* worst: Z->%XX */;
! 195: break;
! 196: case UL_MAIL_HEADER:
! 197: // tainted, untaint language: mail-header
! 198: if(charset) {
! 199: // Subject: Re: parser3: =?koi8-r?Q?=D3=C5=CD=C9=CE=C1=D2?=
! 200: dest+=row->item.size*3+MAX_STRING/* worst: =?charset?Q?=%XX?= */;
! 201: } else {
! 202: dest+=row->item.size;
! 203: }
! 204: break;
! 205: case UL_TABLE:
! 206: // tainted, untaint language: table
! 207: dest+=row->item.size;
! 208: break;
! 209: case UL_SQL:
! 210: // tainted, untaint language: sql
! 211: if(connection)
! 212: dest+=connection->quote(0, row->item.ptr, row->item.size);
! 213: break;
! 214: case UL_JS:
! 215: escape(switch(*src) {
! 216: case '"': case '\'': case '\n': case '\\': case '\xFF':
! 217: dest+=2; break;
! 218: default:
! 219: dest++; break;
! 220: });
! 221: break;
! 222: case UL_XML:
! 223: escape(switch(*src) {
! 224: case '&': case '>': case '<': case '"': case '\'':
! 225: dest+= 6; break;
! 226: default:
! 227: dest++; break;
! 228: });
! 229: break;
! 230: case UL_HTML:
! 231: case UL_OPTIMIZED_HTML:
! 232: escape(switch(*src) {
! 233: case '&':
! 234: case '>':
! 235: case '<':
! 236: case '"':
! 237: dest+=6; break;
! 238: default:
! 239: dest++; break;
! 240: });
! 241: break;
! 242: }
! 243:
! 244: if((lang==UL_UNSPECIFIED?row->item.lang:lang)!=UL_CLEAN)
! 245: whitespace=false;
! 246: }
! 247: chunk=row->link;
! 248: } while(chunk);
! 249:
! 250: break2:
! 251: return dest;
1.51 parser 252: }
253:
1.75 ! paf 254: /// @test UL_OPTIMIZED_HTML optimize
1.43 paf 255: char *String::store_to(char *dest, Untaint_lang lang,
256: SQL_Connection *connection,
257: const char *charset) const {
1.75 ! paf 258: // WARNING:
! 259: // before any changes check cstr_bufsize first!!!
1.44 paf 260: bool whitespace=true;
1.1 paf 261: const Chunk *chunk=&head;
262: do {
263: const Chunk::Row *row=chunk->rows;
1.71 paf 264: for(uint i=0; i<chunk->count; i++, row++) {
1.1 paf 265: if(row==append_here)
266: goto break2;
267:
1.72 paf 268: Untaint_lang to_lang=lang==UL_UNSPECIFIED?(Untaint_lang)row->item.lang:lang;
1.55 parser 269:
270: char *dest_before_origins=dest;
271:
272: if(forigins_mode) {
273: #ifndef NO_STRING_ORIGIN
274: if(row->item.origin.file)
1.75 ! paf 275: dest+=sprintf(dest, ORIGIN_FILE_LINE_FORMAT,
1.55 parser 276: row->item.origin.file,
277: 1+row->item.origin.line);
278: else
1.75 ! paf 279: dest+=sprintf(dest, "<unknown>");
1.55 parser 280: #endif
1.56 parser 281: dest+=sprintf(dest, "#%s: ",
282: String_Untaint_lang_name[to_lang]);
1.55 parser 283: }
284: char *dest_after_origins=dest;
285:
286: switch(to_lang) {
1.29 paf 287: case UL_CLEAN:
1.1 paf 288: // clean piece
1.44 paf 289: { // optimizing whitespace
1.75 ! paf 290: escape(switch(*src) {
1.73 paf 291: case ' ': case '\n': case '\t':
1.44 paf 292: if(!whitespace) {
293: *dest++=*src;
294: whitespace=true;
295: }
1.73 paf 296: break;
1.44 paf 297: default:
298: whitespace=false;
299: *dest++=*src;
300: break;
1.75 ! paf 301: });
1.44 paf 302: }
303: break;
1.29 paf 304: case UL_TAINTED:
1.1 paf 305: // tainted piece, but undefined untaint language
1.23 paf 306: // for VString.as_double of tainted values
1.1 paf 307: // for ^process{body} evaluation
1.11 paf 308: case UL_AS_IS:
1.1 paf 309: // tainted, untaint language: as-is
1.13 paf 310: memcpy(dest, row->item.ptr, row->item.size);
311: dest+=row->item.size;
1.1 paf 312: break;
1.62 parser 313: case UL_FILE_SPEC:
1.9 paf 314: // tainted, untaint language: file [name]
1.18 paf 315: escape(switch(*src) {
316: case ' ': to_char('_'); break;
1.39 paf 317: encode(need_file_encode, '+');
1.18 paf 318: });
1.9 paf 319: break;
1.11 paf 320: case UL_URI:
1.4 paf 321: // tainted, untaint language: uri
1.18 paf 322: escape(switch(*src) {
323: case ' ': to_char('+'); break;
1.13 paf 324: encode(need_uri_encode, '%');
1.18 paf 325: });
1.5 paf 326: break;
1.36 paf 327: case UL_HTTP_HEADER:
1.67 parser 328: // tainted, untaint language: http-field-content-text
329: escape(switch(*src) {
330: case ' ': to_char('+'); break;
331: encode(need_uri_encode, '%');
332: });
1.36 paf 333: break;
334: case UL_MAIL_HEADER:
335: // tainted, untaint language: mail-header
1.46 paf 336: if(charset) {
1.43 paf 337: // Subject: Re: parser3: =?koi8-r?Q?=D3=C5=CD=C9=CE=C1=D2?=
338: const char *src=row->item.ptr;
1.59 parser 339: bool to_quoted_printable=false;
1.43 paf 340: for(int size=row->item.size; size--; src++) {
341: if(*src & 0x80) {
1.59 parser 342: if(!to_quoted_printable) {
1.43 paf 343: dest+=sprintf(dest, "=?%.15s?Q?", charset);
1.59 parser 344: to_quoted_printable=true;
1.43 paf 345: }
1.45 paf 346: dest+=sprintf(dest, "=%02X", *src & 0xFF);
1.43 paf 347: } else {
348: *dest++=*src;
349: }
350: }
1.59 parser 351: if(to_quoted_printable) // close
1.43 paf 352: dest+=sprintf(dest, "?=");
1.46 paf 353: } else {
354: memcpy(dest, row->item.ptr, row->item.size);
355: dest+=row->item.size;
1.43 paf 356: }
1.4 paf 357: break;
1.11 paf 358: case UL_TABLE:
1.15 paf 359: // tainted, untaint language: table
1.18 paf 360: escape(switch(*src) {
361: case '\t': to_char(' '); break;
362: case '\n': to_char(' '); break;
1.13 paf 363: _default;
1.18 paf 364: });
1.1 paf 365: break;
1.11 paf 366: case UL_SQL:
1.1 paf 367: // tainted, untaint language: sql
1.34 paf 368: if(connection)
369: dest+=connection->quote(dest, row->item.ptr, row->item.size);
370: else
1.69 parser 371: throw Exception(0, 0,
1.34 paf 372: this,
373: "untaint in SQL language failed - no connection specified");
1.1 paf 374: break;
1.11 paf 375: case UL_JS:
1.18 paf 376: escape(switch(*src) {
377: case '"': to_string("\\\"", 2); break;
378: case '\'': to_string("\\'", 2); break;
379: case '\n': to_string("\\n", 2); break;
380: case '\\': to_string("\\\\", 2); break;
381: case '\xFF': to_string("\\\xFF", 2); break;
1.13 paf 382: _default;
1.18 paf 383: });
1.1 paf 384: break;
1.61 parser 385: case UL_XML:
386: escape(switch(*src) {
387: case '&': to_string("&", 5); break;
388: case '>': to_string(">", 4); break;
389: case '<': to_string("<", 4); break;
390: case '"': to_string(""", 6); break;
391: case '\'': to_string("'", 6); break;
392: _default;
393: });
394: break;
1.11 paf 395: case UL_HTML:
1.75 ! paf 396: case UL_OPTIMIZED_HTML:
1.18 paf 397: escape(switch(*src) {
398: case '&': to_string("&", 5); break;
399: case '>': to_string(">", 4); break;
400: case '<': to_string("<", 4); break;
401: case '"': to_string(""", 6); break;
1.13 paf 402: _default;
1.18 paf 403: });
1.1 paf 404: break;
405: default:
1.69 parser 406: throw Exception(0, 0,
1.18 paf 407: this,
1.1 paf 408: "unknown untaint language #%d of %d piece",
1.18 paf 409: static_cast<int>(row->item.lang),
1.38 paf 410: i); // never
1.48 parser 411: break; // never
1.1 paf 412: }
1.44 paf 413:
414: if((lang==UL_UNSPECIFIED?row->item.lang:lang)!=UL_CLEAN)
415: whitespace=false;
1.55 parser 416:
417: if(forigins_mode)
418: if(dest==dest_after_origins) // never moved==optimized space
419: dest=dest_before_origins;
420: else {
1.66 parser 421: remove_crlf(dest_after_origins, dest);
1.55 parser 422:
423: to_char('\n');
424: }
1.1 paf 425: }
426: chunk=row->link;
427: } while(chunk);
1.64 parser 428:
1.1 paf 429: break2:
1.13 paf 430: return dest;
1.1 paf 431: }