-
Notifications
You must be signed in to change notification settings - Fork 2
/
tainting2.js
182 lines (182 loc) · 4.46 KB
/
tainting2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
String.prototype.untaint = function(){
this.__taint = false;
}
String.prototype.taint = function(){
this.__taint = true;
}
String.prototype.isTainted = function(){
if(this.__taint)
return true;
else
return false;
}
function String(s){
var __taint = false;
var length = ("" + s).length;
this.untaint = function(){
__taint = false;
}
this.taint = function(){
__taint = true;
}
this.isTainted = function(){
if(__taint)
return true;
else
return false;
}
this.concat = function(){
var val = __String.concat.apply(s,arguments);
var taint = this.isTainted();
for(var i = 0, l = arguments.length; i < l; i++){
if(typeof arguments[i] == "string")
taint = taint || arguments[i].isTainted();
}
val = new String(val);
if(taint){
val.taint();
}
return val;
}
this.toLowerCase = function(){
var str = new String(__String.toLowerCase.apply(s,arguments));
if(this.isTainted()){
str.taint();
}
return str;
}
this.toUpperCase = function(){
var str = new String(__String.toUpperCase.apply(s,arguments));
if(this.isTainted()){
str.taint();
}
return str;
}
this.charCodeAt = function(f){
return __String.charCodeAt.apply(s,arguments);
}
this.fromCharCode = function(f){
return __String.fromCharCode.apply(s,arguments);
}
this.toString = this.valueOf = function(){
return s;
}
this.charAt = function(f){
return __String.charAt(f);
}
this.indexOf = function(searchstring, start){
return __String.indexOf.apply(s,arguments);
}
this.lastIndexOf = function(searchstring, start){
return __String.lastIndexOf.apply(s,arguments);
}
this.match = function(regexp){
var a = __String.match.apply(s,arguments);
if(this.isTainted() && a != null){
for(var i = 0, l = a.length; i < l; i++){
a[i] = new String(a[i]);
a[i].taint();
}
}
return a;
}
this.replace = function(regexp, newstr){
var str = new String(__String.replace.apply(s,arguments));
if(this.isTainted() || newstr.isTainted()){
str.taint();
}
return str;
}
this.search = function(regexp){
return __String.search.apply(s,arguments);
}
this.slice = function(begin,end){
var str = __String.slice.apply(s,arguments);
if(str != -1){
str = new String(str);
if(this.isTainted()){
str.taint();
}
}
return str;
}
this.split = function(separator, limit){
var a = __String.split.apply(s,arguments);
if(this.isTainted() && a != null){
for(var i = 0, l = a.length; i < l; i++){
a[i] = new String(a[i]);
a[i].taint();
}
}
return a;
}
this.substr = function(start,length){
var str = new String(__String.substr.apply(s,arguments));
if(this.isTainted())
str.taint();
return str;
}
this.substring = function(from, to){
var str = new String(__String.substring.apply(s,arguments));
if(this.isTainted())
str.taint();
return str;
}
}
function Taint(idsn, validation_functionsn, sync_functionsn){
var ids = idsn;
var validation_functions = validation_functionsn;
var sync_functions = sync_functionsn;
this.get = function(idc){
var str = new String(document.getElementById(idc).value);
str.taint();
return str;
}
this.runValidation = function(idc, val){
for(var i = 0, l = ids.length; i < l; i++){
if(idc == ids[i]){
var valid = validation_functions[i](val);
valid.untaint();
return valid;
}
}
throw "Invalid ID in Validation.";
}
this.runOneValidation = function(vals){
if(typeof validation_functions == "function"){
var valid = validation_functions(vals);
switch(typeof valid){
case "array":
for(var i = 0, l = valid.length; i < l; i++){
valid[i].untaint();
}
break;
case "string":valid.untaint();break;
}
return valid;
}else{
throw "Not a function!";
}
}
this.runSync = function(idc, val){
if(val.isTainted())
throw "Value is Tainted!";
for(var i = 0, l = ids.length; i < l; i++){
if(idc == ids[i]){
var valid = sync_functions[i](val);
return valid;
}
}
throw "Invalid ID in Sync.";
}
this.runOneSync = function(vals){
for(var i = 0, l = vals.length; i < l; i++){
if(vals[i].isTainted())
throw "Value is Tainted!";
}
if(typeof sync_functions == "function")
return sync_functions(vals);
else
throw "Not a function!";
}
}