|
|
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.25 ! paf 8: $Id: untaint.C,v 1.24 2001/03/28 13:21:30 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.1 paf 18:
1.18 paf 19: #define escape(action) \
1.1 paf 20: { \
1.13 paf 21: const char *src=row->item.ptr; \
22: for(int size=row->item.size; size--; src++) \
1.18 paf 23: action \
1.1 paf 24: }
1.13 paf 25: #define _default default: *dest++=*src; break
26: #define encode(need_encode_func, prefix) \
1.5 paf 27: default: \
1.13 paf 28: if(need_encode_func(*src)) { \
1.5 paf 29: static const char *hex="0123456789ABCDEF"; \
1.9 paf 30: char chunk[3]={prefix}; \
1.13 paf 31: chunk[1]=hex[((unsigned char)*src)/0x10]; \
32: chunk[2]=hex[((unsigned char)*src)%0x10]; \
33: strncpy(dest, chunk, 3); dest+=3; \
1.5 paf 34: } else \
1.13 paf 35: *dest++=*src; \
1.5 paf 36: break
1.18 paf 37: #define to_char(c) *dest++=c
38: #define to_string(b, bsize) \
39: strncpy(dest, b, bsize); \
40: dest+=bsize; \
1.4 paf 41:
1.9 paf 42: inline bool need_file_encode(unsigned char c){
1.13 paf 43: if((c>='0') &&(c<='9') ||(c>='A') &&(c<='Z') ||(c>='a') &&(c<='z'))
1.9 paf 44: return false;
45:
1.13 paf 46: return !strchr("./", c);
1.9 paf 47: }
1.5 paf 48: inline bool need_uri_encode(unsigned char c){
1.13 paf 49: if((c>='0') &&(c<='9') ||(c>='A') &&(c<='Z') ||(c>='a') &&(c<='z'))
1.4 paf 50: return false;
51:
1.5 paf 52: return !strchr("_-./", c);
53: }
54: inline bool need_header_encode(unsigned char c){
1.18 paf 55: if(strchr(" , :", c))
1.5 paf 56: return false;
57:
58: return need_uri_encode(c);
1.4 paf 59: }
1.1 paf 60:
61: // String
62:
1.13 paf 63: static bool typo_present(Array::Item *value, const void *info) {
64: Array *row=static_cast<Array *>(value);
65: const char *src=static_cast<const char *>(info);
66:
67: int partial;
68: row->get_string(0)->cmp(src, partial);
1.14 paf 69: return
70: partial==0 || // full match
71: partial==1; // typo left column starts 'src'
1.13 paf 72: }
73:
1.22 paf 74: /// @todo @b now: optimize whitespaces for all but 'html'
1.13 paf 75: char *String::store_to(char *dest) const {
76: // $MAIN:html-typo table
1.25 ! paf 77: Table *typo_table=0;
! 78: if(Request *request=pool().request())
! 79: typo_table=request->html_typo_table;
! 80: if(!typo_table)
! 81: typo_table=default_typo_table;
1.1 paf 82:
83: const Chunk *chunk=&head;
84: do {
85: const Chunk::Row *row=chunk->rows;
86: for(int i=0; i<chunk->count; i++) {
87: if(row==append_here)
88: goto break2;
89:
90: // WARNING:
91: // string can grow only UNTAINT_TIMES_BIGGER
92: switch(row->item.lang) {
1.11 paf 93: case UL_NO:
1.1 paf 94: // clean piece
1.11 paf 95: case UL_YES:
1.1 paf 96: // tainted piece, but undefined untaint language
1.23 paf 97: // for VString.as_double of tainted values
1.1 paf 98: // for ^process{body} evaluation
1.11 paf 99: case UL_AS_IS:
1.1 paf 100: // tainted, untaint language: as-is
1.13 paf 101: memcpy(dest, row->item.ptr, row->item.size);
102: dest+=row->item.size;
1.1 paf 103: break;
1.11 paf 104: case UL_FILE_NAME:
1.9 paf 105: // tainted, untaint language: file [name]
1.18 paf 106: escape(switch(*src) {
107: case ' ': to_char('_'); break;
1.13 paf 108: encode(need_file_encode, '-');
1.18 paf 109: });
1.9 paf 110: break;
1.11 paf 111: case UL_URI:
1.4 paf 112: // tainted, untaint language: uri
1.18 paf 113: escape(switch(*src) {
114: case ' ': to_char('+'); break;
1.13 paf 115: encode(need_uri_encode, '%');
1.18 paf 116: });
1.5 paf 117: break;
1.11 paf 118: case UL_HEADER:
1.5 paf 119: // tainted, untaint language: header
1.18 paf 120: escape(switch(*src) {
1.13 paf 121: encode(need_header_encode, '%');
1.18 paf 122: });
1.4 paf 123: break;
1.11 paf 124: case UL_TABLE:
1.15 paf 125: // tainted, untaint language: table
1.18 paf 126: escape(switch(*src) {
127: case '\t': to_char(' '); break;
128: case '\n': to_char(' '); break;
1.13 paf 129: _default;
1.18 paf 130: });
1.1 paf 131: break;
1.11 paf 132: case UL_SQL:
1.1 paf 133: // tainted, untaint language: sql
134: // TODO: зависимость от sql сервера
1.13 paf 135: memset(dest, '?', row->item.size);
136: dest+=row->item.size;
1.1 paf 137: break;
1.11 paf 138: case UL_JS:
1.18 paf 139: escape(switch(*src) {
140: case '"': to_string("\\\"", 2); break;
141: case '\'': to_string("\\'", 2); break;
142: case '\n': to_string("\\n", 2); break;
143: case '\\': to_string("\\\\", 2); break;
144: case '\xFF': to_string("\\\xFF", 2); break;
1.13 paf 145: _default;
1.18 paf 146: });
1.1 paf 147: break;
1.11 paf 148: case UL_HTML:
1.18 paf 149: escape(switch(*src) {
150: case '&': to_string("&", 5); break;
151: case '>': to_string(">", 4); break;
152: case '<': to_string("<", 4); break;
153: case '"': to_string(""", 6); break;
1.19 paf 154: //TODO: XSLT case '\'': to_string("'", 6); break;
1.13 paf 155: _default;
1.18 paf 156: });
1.1 paf 157: break;
1.13 paf 158: case UL_HTML_TYPO: {
1.1 paf 159: // tainted, untaint language: html-typo
1.19 paf 160: char *html_for_typo=(char *)malloc(size()*2/* '\n' -> '\' 'n' */+1);
161: // note:
162: // there still is a possibility that user
163: // would not replace \n as she supposed to
164: // and rather replace \ and n into huge strings
165: // thus causing memory overrun
166: // this can be dealed by allocating *2 memory, but that's too expensive
167: // todo parser4: fix that
1.18 paf 168: size_t html_for_typo_size;
1.13 paf 169: { // local dest
1.18 paf 170: char *dest=html_for_typo;
171: escape(switch(*src) {
1.16 paf 172: // convinient name for typo match "\n"
173: case '\r':
1.18 paf 174: if(typo_table) {
175: *dest++='\\'; *dest++='n'; // \r -> \n
1.24 paf 176: if(src[1]=='\n') { // \r\n -> remove \n
177: size--; src++;
178: }
1.18 paf 179: }
180: break;
181: case '\n':
182: if(typo_table)
183: to_string("\\n", 2);
1.16 paf 184: break;
1.19 paf 185: //TODO: XSLT case '\'': to_string("'", 6); break;
1.13 paf 186: _default;
1.18 paf 187: });
1.13 paf 188: *dest=0;
1.18 paf 189: html_for_typo_size=dest-html_for_typo;
1.13 paf 190: }
191: // typo table replacements
1.21 paf 192: const char *src=html_for_typo;
193: do {
194: // there is a row where first column starts 'src'
195: if(Table::Item *item=typo_table->first_that(typo_present, src)) {
196: // get a=>b values
197: const String& a=*static_cast<Array *>(item)->get_string(0);
198: const String& b=*static_cast<Array *>(item)->get_string(1);
199: // empty 'a' | 'b' checks
200: if(a.size()==0 || b.size()==0) {
1.25 ! paf 201: pool().set_request(0); // avoid recursion
1.21 paf 202: THROW(0, 0,
203: typo_table->origin_string(),
204: "typo table column elements must not be empty");
205: }
206: // overflow check:
207: // b allowed to be max UNTAINT_TIMES_BIGGER then a
208: if(b.size()>UNTAINT_TIMES_BIGGER*a.size()) {
1.25 ! paf 209: pool().set_request(0); // avoid recursion
1.21 paf 210: THROW(0, 0,
211: &b,
212: "is %g times longer then '%s', "
213: "while maximum, handled by Parser, is %d",
214: ((double)b.size())/a.size(),
215: a.cstr(),
216: UNTAINT_TIMES_BIGGER);
217: }
218:
219: // skip 'a' in 'src'
220: src+=a.size();
221: // write 'b' to 'dest'
222: b.store_to(dest);
223: dest+=b.size();
224: } else
225: *dest++=*src++;
226: } while(*src);
1.1 paf 227: break;
1.13 paf 228: }
1.1 paf 229: default:
1.18 paf 230: THROW(0, 0,
231: this,
1.1 paf 232: "unknown untaint language #%d of %d piece",
1.18 paf 233: static_cast<int>(row->item.lang),
1.1 paf 234: i);
235: }
236: row++;
237: }
238: chunk=row->link;
239: } while(chunk);
240: break2:
1.13 paf 241: return dest;
1.1 paf 242: }