|
|
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.39 ! paf 8: $Id: untaint.C,v 1.38 2001/04/10 11:24:00 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.30 paf 80: /**
81: @test optimize whitespaces for all but 'html'
82: @todo fix theoretical \n mem overrun in TYPO replacements
1.36 paf 83: @test mail-header
1.30 paf 84: */
1.34 paf 85: char *String::store_to(char *dest, Untaint_lang lang, SQL_Connection *connection) const {
1.13 paf 86: // $MAIN:html-typo table
1.26 paf 87: Table *user_typo_table=static_cast<Table *>(pool().tag());
88: Table *typo_table=user_typo_table?user_typo_table:default_typo_table;
1.1 paf 89:
90: const Chunk *chunk=&head;
91: do {
92: const Chunk::Row *row=chunk->rows;
1.28 paf 93: for(size_t i=0; i<chunk->count; i++, row++) {
1.1 paf 94: if(row==append_here)
95: goto break2;
96:
97: // WARNING:
98: // string can grow only UNTAINT_TIMES_BIGGER
1.35 paf 99: switch(lang==UL_UNSPECIFIED?row->item.lang:lang) {
1.29 paf 100: case UL_CLEAN:
1.1 paf 101: // clean piece
1.29 paf 102: case UL_TAINTED:
1.1 paf 103: // tainted piece, but undefined untaint language
1.23 paf 104: // for VString.as_double of tainted values
1.1 paf 105: // for ^process{body} evaluation
1.11 paf 106: case UL_AS_IS:
1.1 paf 107: // tainted, untaint language: as-is
1.13 paf 108: memcpy(dest, row->item.ptr, row->item.size);
109: dest+=row->item.size;
1.1 paf 110: break;
1.11 paf 111: case UL_FILE_NAME:
1.9 paf 112: // tainted, untaint language: file [name]
1.18 paf 113: escape(switch(*src) {
114: case ' ': to_char('_'); break;
1.39 ! paf 115: encode(need_file_encode, '+');
1.18 paf 116: });
1.9 paf 117: break;
1.11 paf 118: case UL_URI:
1.4 paf 119: // tainted, untaint language: uri
1.18 paf 120: escape(switch(*src) {
121: case ' ': to_char('+'); break;
1.13 paf 122: encode(need_uri_encode, '%');
1.18 paf 123: });
1.5 paf 124: break;
1.36 paf 125: case UL_HTTP_HEADER:
126: // tainted, untaint language: http-header
1.18 paf 127: escape(switch(*src) {
1.36 paf 128: encode(need_http_header_encode, '%');
1.18 paf 129: });
1.36 paf 130: break;
131: case UL_MAIL_HEADER:
132: // tainted, untaint language: mail-header
133: memcpy(dest, row->item.ptr, row->item.size);
134: dest+=row->item.size;
1.4 paf 135: break;
1.11 paf 136: case UL_TABLE:
1.15 paf 137: // tainted, untaint language: table
1.18 paf 138: escape(switch(*src) {
139: case '\t': to_char(' '); break;
140: case '\n': to_char(' '); break;
1.13 paf 141: _default;
1.18 paf 142: });
1.1 paf 143: break;
1.11 paf 144: case UL_SQL:
1.1 paf 145: // tainted, untaint language: sql
1.34 paf 146: if(connection)
147: dest+=connection->quote(dest, row->item.ptr, row->item.size);
148: else
149: THROW(0, 0,
150: this,
151: "untaint in SQL language failed - no connection specified");
1.1 paf 152: break;
1.11 paf 153: case UL_JS:
1.18 paf 154: escape(switch(*src) {
155: case '"': to_string("\\\"", 2); break;
156: case '\'': to_string("\\'", 2); break;
157: case '\n': to_string("\\n", 2); break;
158: case '\\': to_string("\\\\", 2); break;
159: case '\xFF': to_string("\\\xFF", 2); break;
1.13 paf 160: _default;
1.18 paf 161: });
1.1 paf 162: break;
1.11 paf 163: case UL_HTML:
1.18 paf 164: escape(switch(*src) {
165: case '&': to_string("&", 5); break;
166: case '>': to_string(">", 4); break;
167: case '<': to_string("<", 4); break;
168: case '"': to_string(""", 6); break;
1.19 paf 169: //TODO: XSLT case '\'': to_string("'", 6); break;
1.13 paf 170: _default;
1.18 paf 171: });
1.1 paf 172: break;
1.13 paf 173: case UL_HTML_TYPO: {
1.1 paf 174: // tainted, untaint language: html-typo
1.19 paf 175: char *html_for_typo=(char *)malloc(size()*2/* '\n' -> '\' 'n' */+1);
176: // note:
177: // there still is a possibility that user
178: // would not replace \n as she supposed to
179: // and rather replace \ and n into huge strings
180: // thus causing memory overrun
181: // this can be dealed by allocating *2 memory, but that's too expensive
1.18 paf 182: size_t html_for_typo_size;
1.13 paf 183: { // local dest
1.18 paf 184: char *dest=html_for_typo;
185: escape(switch(*src) {
1.16 paf 186: // convinient name for typo match "\n"
187: case '\r':
1.18 paf 188: if(typo_table) {
189: *dest++='\\'; *dest++='n'; // \r -> \n
1.24 paf 190: if(src[1]=='\n') { // \r\n -> remove \n
191: size--; src++;
192: }
1.18 paf 193: }
194: break;
195: case '\n':
196: if(typo_table)
197: to_string("\\n", 2);
1.16 paf 198: break;
1.19 paf 199: //TODO: XSLT case '\'': to_string("'", 6); break;
1.13 paf 200: _default;
1.18 paf 201: });
1.13 paf 202: *dest=0;
1.18 paf 203: html_for_typo_size=dest-html_for_typo;
1.13 paf 204: }
205: // typo table replacements
1.21 paf 206: const char *src=html_for_typo;
207: do {
208: // there is a row where first column starts 'src'
209: if(Table::Item *item=typo_table->first_that(typo_present, src)) {
210: // get a=>b values
211: const String& a=*static_cast<Array *>(item)->get_string(0);
212: const String& b=*static_cast<Array *>(item)->get_string(1);
213: // empty 'a' | 'b' checks
214: if(a.size()==0 || b.size()==0) {
1.26 paf 215: pool().set_tag(default_typo_table); // avoid recursion
1.21 paf 216: THROW(0, 0,
217: typo_table->origin_string(),
218: "typo table column elements must not be empty");
219: }
220: // overflow check:
221: // b allowed to be max UNTAINT_TIMES_BIGGER then a
222: if(b.size()>UNTAINT_TIMES_BIGGER*a.size()) {
1.26 paf 223: pool().set_tag(default_typo_table); // avoid recursion
1.21 paf 224: THROW(0, 0,
225: &b,
226: "is %g times longer then '%s', "
227: "while maximum, handled by Parser, is %d",
228: ((double)b.size())/a.size(),
229: a.cstr(),
230: UNTAINT_TIMES_BIGGER);
231: }
232:
233: // skip 'a' in 'src'
234: src+=a.size();
235: // write 'b' to 'dest'
236: b.store_to(dest);
237: dest+=b.size();
238: } else
239: *dest++=*src++;
240: } while(*src);
1.1 paf 241: break;
1.13 paf 242: }
1.1 paf 243: default:
1.18 paf 244: THROW(0, 0,
245: this,
1.1 paf 246: "unknown untaint language #%d of %d piece",
1.18 paf 247: static_cast<int>(row->item.lang),
1.38 paf 248: i); // never
1.1 paf 249: }
250: }
251: chunk=row->link;
252: } while(chunk);
253: break2:
1.13 paf 254: return dest;
1.1 paf 255: }