Annotation of parser3/src/main/untaint.C, revision 1.115.2.12.2.16
1.7 paf 1: /** @file
1.8 paf 2: Parser: String class part: untaint mechanizm.
3:
1.115.2.6 paf 4: Copyright(c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)
1.89 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.103 paf 6: */
1.8 paf 7:
1.115.2.12.2. 6(paf 8:3): static const char* IDENT_UNTAINT_C="$Date: 2003/03/25 12:39:14 $";
(paf 9:):
1.1 paf 10:
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.58 parser 16: #include "pa_dictionary.h"
1.66 parser 17: #include "pa_common.h"
1.85 paf 18: #include "pa_charset.h"
1.115.2.4 paf 19: #include "pa_request_charsets.h"
1.1 paf 20:
1.115.2.12.2. (paf 21:): extern "C" { // author forgot to do that
22:): #include "ec.h"
23:): }
24:):
6(paf 25:3): #define PA_SQL
(paf 26:):
27:): #ifdef PA_SQL
28:): #include "pa_sql_connection.h"
29:): #endif
30:):
3(paf 31:3): // forwards
32:3):
33:3): void CORD_ec_flush_buf_optimize_whitespace(CORD_ec x);
34:3):
35:3): // defines
36:3):
37:3):
38:3): #undef CORD_ec_append
39:3): // redefining to intercept flushes and implement whitespace optimization
40:3): // of all consequent white space chars leaving only first one
41:3): #define CORD_ec_append(x, c) \
42:3): { \
43:3): bool skip=false; \
44:3): if(optimize) switch(c) { \
45:3): case ' ': case '\r': case '\n': case '\t': \
46:3): if(whitespace) \
47:3): skip=true; /*skipping subsequent*/ \
48:3): else \
49:3): whitespace=true; \
50:3): break; \
51:3): default: \
52:3): whitespace=false; \
53:3): break; \
54:3): } \
55:3): if(!skip) { \
56:3): if ((x)[0].ec_bufptr == (x)[0].ec_buf + CORD_BUFSZ) { \
57:3): CORD_ec_flush_buf(x); \
58:3): } \
59:3): *((x)[0].ec_bufptr)++ = (c); \
60:3): } \
61:3): }
62:3):
63:3):
1.18 paf 64: #define escape(action) \
1.115.2.12.2. (paf 65:): for(; fragment_length--; CORD_next(pos)) { \
66:): char c=CORD_pos_fetch(pos); \
67:): action \
1.1 paf 68: }
1.115.2.12.2. (paf 69:): #define _default default: CORD_ec_append(result, c); break
5(paf 70:3): #define encode(need_encode_func, prefix, otherwise) \
(paf 71:): if(need_encode_func(c)) { \
72:): static const char* hex="0123456789ABCDEF"; \
73:): CORD_ec_append(result, prefix); \
74:): CORD_ec_append(result, hex[((unsigned char)c)/0x10]); \
75:): CORD_ec_append(result, hex[((unsigned char)c)%0x10]); \
76:): } else \
5(paf 77:3): CORD_ec_append(result, otherwise);
(paf 78:): #define to_char(c) CORD_ec_append(result, c)
79:): #define to_string(s) CORD_ec_append_cord(result, s)
1.4 paf 80:
1.9 paf 81: inline bool need_file_encode(unsigned char c){
1.108 paf 82: // russian letters and space ENABLED
83: // encoding only these...
84: return strchr(
85: "*?'\"<>|"
86: #ifndef WIN32
87: ":\\~"
1.31 paf 88: #endif
1.108 paf 89: , c)!=0;
1.9 paf 90: }
1.5 paf 91: inline bool need_uri_encode(unsigned char c){
1.13 paf 92: if((c>='0') &&(c<='9') ||(c>='A') &&(c<='Z') ||(c>='a') &&(c<='z'))
1.4 paf 93: return false;
94:
1.5 paf 95: return !strchr("_-./", c);
96: }
1.36 paf 97: inline bool need_http_header_encode(unsigned char c){
1.18 paf 98: if(strchr(" , :", c))
1.5 paf 99: return false;
100:
101: return need_uri_encode(c);
1.4 paf 102: }
1.1 paf 103:
1.56 parser 104: //
105:
1.115.2.6 paf 106: static const char* String_Untaint_lang_name[]={
1.56 parser 107: "U", ///< zero value handy for hash lookup @see untaint_lang_name2enum
108: "C", ///< clean
109: "T", ///< tainted, untaint language as assigned later
110: // untaint languages. assigned by ^untaint[lang]{...}
111: "P",
112: /**<
113: leave language built into string being appended.
114: just a flag, that value not stored
115: */
116: "A", ///< leave all characters intact
1.68 parser 117: "F", ///< file specification
118: "H", ///< ext in HTTP response header
1.56 parser 119: "M", ///< text in mail header
120: "URI", ///< text in uri
121: "T", ///< ^table:set body
122: "SQL", ///< ^table:sql body
123: "JS", ///< JavaScript code
1.68 parser 124: "XML", ///< ^dom:set xml
1.82 paf 125: "HTML" ///< HTML code (for editing)
1.56 parser 126: };
127:
128:
1.1 paf 129: // String
130:
1.41 paf 131: /*
132:
133: HTTP-header = field-name ":" [ field-value ] CRLF
134:
135: field-name = token
136: field-value = *( field-content | LWS )
137:
138: field-content = <the OCTETs making up the field-value
139: and consisting of either *TEXT or combinations
140: of token, tspecials, and quoted-string>
141:
142:
143:
144: word = token | quoted-string
145:
146: token = 1*<any CHAR except CTLs or tspecials>
147:
148:
149:
150: tspecials = "(" | ")" | "<" | ">" | "@"
151: | "," | ";" | ":" | "\" | <">
152: | "/" | "[" | "]" | "?" | "="
153: | "{" | "}" | SP | HT
154:
155: SP = <US-ASCII SP, space (32)>
156: HT = <US-ASCII HT, horizontal-tab (9)>
157:
158: LWS = [CRLF] 1*( SP | HT )
159: TEXT = <any OCTET except CTLs,
160: but including LWS>
161:
162: quoted-pair = "\" CHAR
163:
164: if(strchr("()<>@,;:\\\"/[]?={} \t", *ptr))
165: */
1.115.2.6 paf 166: inline bool need_quote_http_header(const char* ptr, size_t size) {
1.41 paf 167: for(; size--; ptr++)
1.42 paf 168: if(strchr(";\\\"= \t" /* excluded ()<>@, :/ ? []{} */, *ptr))
1.41 paf 169: return true;
170: return false;
171: }
172:
1.115.2.12.2. (paf 173:):
1.91 paf 174: /**
1.115.2.1 paf 175: appends to other String,
1.91 paf 176: marking all tainted pieces of it with @a lang.
1.92 paf 177: or marking ALL pieces of it with a @a lang when @a forced to,
178: and propagating OPTIMIZE language bit.
1.91 paf 179: */
1.115.2.12.2. (paf 180:): String& String::append_to(String& dest, Language lang, bool forced) const {
0(paf 181:3): if(is_empty())
1.115.2.1 paf 182: return dest;
183:
1.115.2.12.2. (paf 184:): // first: letters
0(paf 185:3): dest.body.append(body);
1.91 paf 186:
1.115.2.12.2. (paf 187:): // next: fragment infos
188:):
189:): if(lang==L_PASS_APPENDED) // without language-chage string?
190:): dest.fragments.append(fragments);
191:): else if(forced) //forcing passed lang?
192:): dest.fragments+=Fragment(lang, length());
193:): else if(lang&L_OPTIMIZE_BIT) {
194:): Array_iterator<ArrayFragment::element_type> i(fragments);
195:): while(i.has_next()) {
196:): const Fragment& fragment=i.next();
197:): // main idea here:
198:): // tainted piece would get OPTIMIZED bit from 'lang'
199:): // clean piece would be marked OPTIMIZED manually
200:): // pieces with determined languages [not tainted|clean] would retain theirs langs
201:): dest.fragments+=Fragment(static_cast<Language>(
202:): fragment.lang==L_TAINTED?lang:(
203:): fragment.lang==L_CLEAN?L_CLEAN|L_OPTIMIZE_BIT: // ORing with OPTIMIZED flag
204:): fragment.lang)),
205:): fragment.length);
206:): }
207:): } else { // The core idea: tainted pieces got marked with context's lang
208:): Array_iterator<ArrayFragment::element_type> i(fragments);
209:): while(i.has_next()) {
210:): const Fragment& fragment=i.next();
211:): dest.fragments+=Fragment(
212:): fragment.lang==L_TAINTED?lang:fragment.lang,
213:): fragment.length);
214:): }
215:): }
1.115.2.1 paf 216:
217: return dest;
1.77 paf 218: }
219:
1.109 paf 220: /** http://www.ietf.org/rfc/rfc2047.txt
221: RFC
222: (3) As a replacement for a 'word' entity within a 'phrase', for example,
223: one that precedes an address in a From, To, or Cc header. The ABNF
224: definition for 'phrase' from RFC 822 thus becomes:
225:
226: phrase = 1*( encoded-word / word )
227:
228: In this case the set of characters that may be used in a "Q"-encoded
229: 'encoded-word' is restricted to: <upper and lower case ASCII
230: letters, decimal digits, "!", "*", "+", "-", "/", "=", and "_"
231: (underscore, ASCII 95.)>. An 'encoded-word' that appears within a
232: 'phrase' MUST be separated from any adjacent 'word', 'text' or
233: 'special' by 'linear-white-space'.
234: ...
235: (2) The 8-bit hexadecimal value 20 (e.g., ISO-8859-1 SPACE) may be
236: represented as "_" (underscore, ASCII 95.). (This character may
237: not pass through some internetwork mail gateways, but its use
238: will greatly enhance readability of "Q" encoded data with mail
239: readers that do not support this encoding.) Note that the "_"
240: always represents hexadecimal 20, even if the SPACE character
241: occupies a different code position in the character set in use.
242:
243: paf: obviously,
244: without "=", or one could not differ "=E0" and "russian letter a"
245: and without "_", or in would mean 0x20
246: */
1.115.2.12.2. (paf 247:): inline bool mail_header_char_valid_within_Qencoded(char c) {
1.109 paf 248: return c>='A' && c<='Z'
249: || c>='a' && c<='Z'
250: || c>='0' && c<='9'
251: || strchr("!*+-/", c);
252: }
1.115.2.12.2. 5(paf 253:3): /**
254:3): RFC
255:3): Upper case should be used for hexadecimal digits "A" through "F"
256:3): The 8-bit hexadecimal value 20 (e.g., ISO-8859-1 SPACE)
257:3): may be represented as "_"
258:3): */
259:3): inline bool mail_header_nonspace_char(char c) {
260:3): return c != 0x20;
261:3): }
3(paf 262:3): inline void ec_append(CORD_ec& result, bool& optimize, bool& whitespace, CORD_pos& pos, size_t size) {
(paf 263:): while(size--) {
264:): CORD_ec_append(result, CORD_pos_fetch(pos));
265:): CORD_next(pos);
266:): }
267:): }
0(paf 268:3): StringBody String::cstr_to_string_body(Language lang,
(paf 269:): SQL_Connection* connection,
270:): const Request_charsets *charsets) const {
271:): CORD_ec result; CORD_ec_init(result);
0(paf 272:3): CORD_pos pos; body.set_pos(pos, 0);
(paf 273:):
1.44 paf 274: bool whitespace=true;
1.111 paf 275:
1.115.2.12.2. (paf 276:): size_t fragment_begin=0;
277:): size_t fragment_end;
278:): for(Array_iterator<Fragment> i(fragments); i.has_next(); fragment_begin=fragment_end) {
279:): const Fragment& fragment=i.next();
280:): size_t fragment_length=fragment.length;
281:): fragment_end=fragment_begin+fragment_length;
2(paf 282:3): //fprintf(stderr, "%d, %d\n", fragment.lang, fragment.length);
1.77 paf 283:
1.115.2.12.2. (paf 284:): Language to_lang=lang==L_UNSPECIFIED?fragment.lang:lang;
3(paf 285:3): bool optimize=(to_lang & L_OPTIMIZE_BIT)!=0;
286:3): if(!optimize)
287:3): whitespace=false;
1.77 paf 288:
1.115.2.12.2. (paf 289:): switch(to_lang & ~L_OPTIMIZE_BIT) {
290:): case L_CLEAN:
291:): case L_TAINTED:
292:): case L_AS_IS:
1.77 paf 293: // clean piece
294:
295: // tainted piece, but undefined untaint language
296: // for VString.as_double of tainted values
297: // for ^process{body} evaluation
298:
299: // tainted, untaint language: as-is
1.115.2.12.2. 3(paf 300:3): ec_append(result, optimize, whitespace, pos, fragment_length);
1.77 paf 301: break;
1.115.2.12.2. (paf 302:): case L_FILE_SPEC:
1.77 paf 303: // tainted, untaint language: file [name]
1.83 paf 304: escape(
1.113 paf 305: // Macintosh has problems with small Russian letter 'r'
1.115.2.12.2. (paf 306:): if( c=='\xF0' && charsets && charsets->source().name()=="windows-1251" ) {
1.113 paf 307: // fixing that letter for most common charset
1.115.2.2 paf 308: to_char('p');
1.114 paf 309: } else // fallback to default
1.115.2.12.2. 5(paf 310:3): encode(need_file_encode, '_', c);
1.83 paf 311: );
1.77 paf 312: break;
1.115.2.12.2. (paf 313:): case L_URI:
1.77 paf 314: // tainted, untaint language: uri
1.85 paf 315: {
1.115.2.12.2. 6(paf 316:3): const char *fragment_str=body.mid(fragment_begin, fragment_length).cstr();
4(paf 317:3): // skip source [we use recoded version]
6(paf 318:3): CORD_pos_advance(pos, fragment_length);
(paf 319:): const void *client_ptr;
320:): size_t client_size;
321:): if(charsets)
322:): Charset::transcode(
6(paf 323:3): charsets->source(), fragment_str, fragment_length,
(paf 324:): charsets->client(), client_ptr, client_size);
325:): else {
6(paf 326:3): client_ptr=fragment_str;
327:3): client_size=fragment_length;
(paf 328:): }
329:):
330:): char c;
331:): for(const char* src=(const char* )client_ptr; c=*src++; )
5(paf 332:3): encode(need_uri_encode, '%', c);
1.85 paf 333: }
1.77 paf 334: break;
1.115.2.12.2. (paf 335:): case L_HTTP_HEADER:
1.77 paf 336: // tainted, untaint language: http-field-content-text
1.108 paf 337: escape(
1.115.2.12.2. 5(paf 338:3): encode(need_uri_encode, '%', c);
1.108 paf 339: );
1.77 paf 340: break;
1.115.2.12.2. (paf 341:): case L_MAIL_HEADER:
1.77 paf 342: // tainted, untaint language: mail-header
1.105 paf 343: // http://www.ietf.org/rfc/rfc2047.txt
1.115.2.4 paf 344: if(charsets) {
1.115.2.12.2. 5(paf 345:3): size_t mail_size;
346:3): const char *mail_ptr=
347:3): body.mid(fragment_begin, mail_size=fragment_length).cstr();
348:3): // skip source [we use recoded version]
349:3): CORD_pos_advance(pos, mail_size);
350:3):
351:3): const char* charset_name=charsets->mail().name().cstr();
1.87 paf 352:
1.77 paf 353: // Subject: Re: parser3: =?koi8-r?Q?=D3=C5=CD=C9=CE=C1=D2?=
354: bool to_quoted_printable=false;
1.105 paf 355:
1.111 paf 356: bool email=false;
1.115.2.12.2. 5(paf 357:3): uchar c;
358:3): for(const char* src=mail_ptr; c=(uchar)*src++; ) {
1.111 paf 359: //RFC + An 'encoded-word' MUST NOT appear in any portion of an 'addr-spec'.
1.115.2.12.2. (paf 360:): if(to_quoted_printable && (c==',' || c=='<')) {
361:): email=c=='<';
5(paf 362:3): to_string("?=");
1.107 paf 363: to_quoted_printable=false;
1.105 paf 364: }
1.111 paf 365: if(!email && (
1.115.2.12.2. (paf 366:): !to_quoted_printable && (c & 0x80) // starting quote-printable-encoding on first 8bit char
367:): || to_quoted_printable && !mail_header_char_valid_within_Qencoded(c)
1.105 paf 368: )) {
1.77 paf 369: if(!to_quoted_printable) {
1.115.2.12.2. 5(paf 370:3): to_string("=?");
371:3): to_string(charset_name);
372:3): to_string("?Q?");
1.77 paf 373: to_quoted_printable=true;
374: }
1.115.2.12.2. 5(paf 375:3): encode(mail_header_nonspace_char, '=', '_');
1.105 paf 376: } else
1.115.2.12.2. 5(paf 377:3): to_char(c);
(paf 378:): if(c=='>')
1.111 paf 379: email=false;
1.44 paf 380: }
1.112 paf 381: if(to_quoted_printable) // close
1.115.2.12.2. 5(paf 382:3): to_string("?=");
1.87 paf 383:
1.115.2.12.2. 5(paf 384:3): } else
385:3): ec_append(result, optimize, whitespace, pos, fragment_length);
1.77 paf 386: break;
1.115.2.12.2. (paf 387:): case L_TABLE:
1.77 paf 388: // tainted, untaint language: table
1.115.2.12.2. (paf 389:): escape(switch(c) {
1.77 paf 390: case '\t': to_char(' '); break;
391: case '\n': to_char(' '); break;
392: _default;
393: });
394: break;
1.115.2.12.2. (paf 395:): #ifdef PA_SQL
396:): case L_SQL:
1.77 paf 397: // tainted, untaint language: sql
1.115.2.12.2. (paf 398:): if(connection) {
6(paf 399:3): const char *fragment_str=body.mid(fragment_begin, fragment_length).cstr();
400:3): // skip source [we use recoded version]
401:3): CORD_pos_advance(pos, fragment_length);
(paf 402:):
6(paf 403:3): to_string(connection->quote(fragment_str, fragment_length));
404:3): } else
(paf 405:): throw Exception(0,
406:): 0,
1.77 paf 407: "untaint in SQL language failed - no connection specified");
408: break;
1.115.2.12.2. (paf 409:): #endif
410:): case L_JS:
411:): escape(switch(c) {
412:): case '"': to_string("\\\""); break;
413:): case '\'': to_string("\\'"); break;
414:): case '\n': to_string("\\n"); break;
415:): case '\\': to_string("\\\\"); break;
416:): case '\xFF': to_string("\\\xFF"); break;
1.77 paf 417: _default;
418: });
419: break;
1.115.2.12.2. (paf 420:): case L_XML:
421:): escape(switch(c) {
422:): case '&': to_string("&"); break;
423:): case '>': to_string(">"); break;
424:): case '<': to_string("<"); break;
425:): case '"': to_string("""); break;
426:): case '\'': to_string("'"); break;
1.77 paf 427: _default;
428: });
429: break;
1.115.2.12.2. (paf 430:): case L_HTML:
431:): escape(switch(c) {
432:): case '&': to_string("&"); break;
433:): case '>': to_string(">"); break;
434:): case '<': to_string("<"); break;
435:): case '"': to_string("""); break;
1.77 paf 436: _default;
437: });
438: break;
439: default:
1.115.2.12.2. (paf 440:): throw Exception(0,
441:): 0,
1.81 paf 442: "unknown untaint language #%d",
1.110 paf 443: static_cast<int>(to_lang)); // should never
1.77 paf 444: break; // never
1.76 paf 445: }
1.115.2.1 paf 446: }
447:
1.115.2.12.2. 0(paf 448:3): return StringBody(CORD_ec_to_cord(result));
1.1 paf 449: }
E-mail: