|
|
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.18 ! paf 8: $Id: untaint.C, v 1.17 2001/03/25 09:10: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.9 paf 74: /// @todo optimize whitespaces for all but 'html'
1.13 paf 75: char *String::store_to(char *dest) const {
76: // $MAIN:html-typo table
77: Table *typo_table=static_cast<Table *>(pool().tag());
1.1 paf 78:
79: const Chunk *chunk=&head;
80: do {
81: const Chunk::Row *row=chunk->rows;
82: for(int i=0; i<chunk->count; i++) {
83: if(row==append_here)
84: goto break2;
85:
86: // WARNING:
87: // string can grow only UNTAINT_TIMES_BIGGER
88: switch(row->item.lang) {
1.11 paf 89: case UL_NO:
1.1 paf 90: // clean piece
1.11 paf 91: case UL_YES:
1.1 paf 92: // tainted piece, but undefined untaint language
93: // for VString.get_double of tainted values
94: // for ^process{body} evaluation
1.11 paf 95: case UL_AS_IS:
1.1 paf 96: // tainted, untaint language: as-is
1.13 paf 97: memcpy(dest, row->item.ptr, row->item.size);
98: dest+=row->item.size;
1.1 paf 99: break;
1.11 paf 100: case UL_FILE_NAME:
1.9 paf 101: // tainted, untaint language: file [name]
1.18 ! paf 102: escape(switch(*src) {
! 103: case ' ': to_char('_'); break;
1.13 paf 104: encode(need_file_encode, '-');
1.18 ! paf 105: });
1.9 paf 106: break;
1.11 paf 107: case UL_URI:
1.4 paf 108: // tainted, untaint language: uri
1.18 ! paf 109: escape(switch(*src) {
! 110: case ' ': to_char('+'); break;
1.13 paf 111: encode(need_uri_encode, '%');
1.18 ! paf 112: });
1.5 paf 113: break;
1.11 paf 114: case UL_HEADER:
1.5 paf 115: // tainted, untaint language: header
1.18 ! paf 116: escape(switch(*src) {
1.13 paf 117: encode(need_header_encode, '%');
1.18 ! paf 118: });
1.4 paf 119: break;
1.11 paf 120: case UL_TABLE:
1.15 paf 121: // tainted, untaint language: table
1.18 ! paf 122: escape(switch(*src) {
! 123: case '\t': to_char(' '); break;
! 124: case '\n': to_char(' '); break;
1.13 paf 125: _default;
1.18 ! paf 126: });
1.1 paf 127: break;
1.11 paf 128: case UL_SQL:
1.1 paf 129: // tainted, untaint language: sql
130: // TODO: зависимость от sql сервера
1.13 paf 131: memset(dest, '?', row->item.size);
132: dest+=row->item.size;
1.1 paf 133: break;
1.11 paf 134: case UL_JS:
1.18 ! paf 135: escape(switch(*src) {
! 136: case '"': to_string("\\\"", 2); break;
! 137: case '\'': to_string("\\'", 2); break;
! 138: case '\n': to_string("\\n", 2); break;
! 139: case '\\': to_string("\\\\", 2); break;
! 140: case '\xFF': to_string("\\\xFF", 2); break;
1.13 paf 141: _default;
1.18 ! paf 142: });
1.1 paf 143: break;
1.11 paf 144: case UL_HTML:
1.18 ! paf 145: escape(switch(*src) {
! 146: case '&': to_string("&", 5); break;
! 147: case '>': to_string(">", 4); break;
! 148: case '<': to_string("<", 4); break;
! 149: case '"': to_string(""", 6); break;
! 150: //TODO: XSLT to_string!'\'', "'", 6)
1.13 paf 151: _default;
1.18 ! paf 152: });
1.1 paf 153: break;
1.13 paf 154: case UL_HTML_TYPO: {
1.1 paf 155: // tainted, untaint language: html-typo
1.18 ! paf 156: char *html_for_typo=
! 157: (char *)malloc(size()*6/*""" the longest possible*/+1);
! 158: size_t html_for_typo_size;
1.13 paf 159: { // local dest
1.18 ! paf 160: char *dest=html_for_typo;
! 161: escape(switch(*src) {
! 162: // BEWARE: check maximum replacement length in malloc above
! 163: case '&': to_string("&", 5); break;
! 164: case '>': to_string(">", 4); break;
! 165: case '<': to_string("<", 4); break;
! 166: case '"': to_string(""", 6); break;
1.16 paf 167: // convinient name for typo match "\n"
168: case '\r':
1.18 ! paf 169: if(typo_table) {
! 170: *dest++='\\'; *dest++='n'; // \r -> \n
! 171: if(src[1]=='\n') // \r\n -> remove \n
! 172: src++;
! 173: }
! 174: break;
! 175: case '\n':
! 176: if(typo_table)
! 177: to_string("\\n", 2);
1.16 paf 178: break;
1.18 ! paf 179: //TODO: XSLT to_string!'\'', "'", 6)
1.13 paf 180: _default;
1.18 ! paf 181: });
1.13 paf 182: *dest=0;
1.18 ! paf 183: html_for_typo_size=dest-html_for_typo;
1.13 paf 184: }
185: // typo table replacements
186: if(typo_table) {
1.18 ! paf 187: const char *src=html_for_typo;
1.13 paf 188: do {
189: // there is a row where first column starts 'src'
190: if(Table::Item *item=typo_table->first_that(typo_present, src)) {
191: // get a=>b values
192: const String& a=*static_cast<Array *>(item)->get_string(0);
193: const String& b=*static_cast<Array *>(item)->get_string(1);
1.18 ! paf 194: // empty 'a' | 'b' checks
! 195: if(a.size()==0 || b.size()==0) {
1.13 paf 196: pool().set_tag(0); // avoid recursion
1.18 ! paf 197: THROW(0, 0,
! 198: typo_table->origin_string(),
! 199: "typo table column elements must not be empty");
1.13 paf 200: }
201: // overflow check:
202: // b allowed to be max UNTAINT_TIMES_BIGGER then a
203: if(b.size()>UNTAINT_TIMES_BIGGER*a.size()) {
204: pool().set_tag(0); // avoid recursion
1.18 ! paf 205: THROW(0, 0,
! 206: &b,
1.13 paf 207: "is %g times longer then '%s', "
1.18 ! paf 208: "while maximum, handled by Parser, is %d",
1.13 paf 209: ((double)b.size())/a.size(),
1.18 ! paf 210: a.cstr(),
1.13 paf 211: UNTAINT_TIMES_BIGGER);
212: }
213:
214: // skip 'a' in 'src'
215: src+=a.size();
216: // write 'b' to 'dest'
217: b.store_to(dest);
218: dest+=b.size();
219: } else
220: *dest++=*src++;
221: } while(*src);
222: } else {
1.18 ! paf 223: memcpy(dest, html_for_typo, html_for_typo_size);
! 224: dest+=html_for_typo_size;
1.13 paf 225: }
1.1 paf 226: break;
1.13 paf 227: }
1.1 paf 228: default:
1.18 ! paf 229: THROW(0, 0,
! 230: this,
1.1 paf 231: "unknown untaint language #%d of %d piece",
1.18 ! paf 232: static_cast<int>(row->item.lang),
1.1 paf 233: i);
234: }
235: row++;
236: }
237: chunk=row->link;
238: } while(chunk);
239: break2:
1.13 paf 240: return dest;
1.1 paf 241: }