-
Notifications
You must be signed in to change notification settings - Fork 398
Expand file tree
/
Copy pathMongoDBSelectQuery.java
More file actions
147 lines (129 loc) · 7.53 KB
/
MongoDBSelectQuery.java
File metadata and controls
147 lines (129 loc) · 7.53 KB
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
package sqlancer.mongodb.query;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.bson.Document;
import org.bson.conversions.Bson;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import sqlancer.GlobalState;
import sqlancer.common.query.ExpectedErrors;
import sqlancer.common.query.SQLancerResultSet;
import sqlancer.mongodb.MongoDBConnection;
import sqlancer.mongodb.MongoDBQueryAdapter;
import sqlancer.mongodb.ast.MongoDBExpression;
import sqlancer.mongodb.ast.MongoDBSelect;
import sqlancer.mongodb.visitor.MongoDBVisitor;
public class MongoDBSelectQuery extends MongoDBQueryAdapter {
private final MongoDBSelect<MongoDBExpression> select;
private List<Document> resultSet;
public MongoDBSelectQuery(MongoDBSelect<MongoDBExpression> select) {
this.select = select;
}
@Override
public boolean couldAffectSchema() {
return false;
}
@Override
public <G extends GlobalState<?, ?, MongoDBConnection>> boolean execute(G globalState, String... fills)
throws Exception {
throw new UnsupportedOperationException();
}
@Override
public ExpectedErrors getExpectedErrors() {
ExpectedErrors errors = new ExpectedErrors();
// ARITHMETIC
errors.add("Failed to optimize pipeline :: caused by :: Can't coerce out of range value");
errors.add("Can't coerce out of range value");
errors.add("date overflow in $add");
errors.add("Failed to optimize pipeline :: caused by :: $sqrt only supports numeric types, not");
errors.add("Failed to optimize pipeline :: caused by :: $sqrt's argument must be greater than or equal to 0");
errors.add("Failed to optimize pipeline :: caused by :: $pow's base must be numeric, not");
errors.add("Failed to optimize pipeline :: caused by :: $pow cannot take a base of 0 and a negative exponent");
errors.add("Failed to optimize pipeline :: caused by :: $add only supports numeric or date types, not");
errors.add("Failed to optimize pipeline :: caused by :: $exp only supports numeric types, not");
errors.add("Failed to optimize pipeline :: caused by :: $log's base must be numeric, not");
errors.add("Failed to optimize pipeline :: caused by :: $log's base must be a positive number not equal to 1");
errors.add("Failed to optimize pipeline :: caused by :: $multiply only supports numeric types, not");
errors.add("$log's argument must be numeric, not");
errors.add("$log's argument must be a positive number, but");
errors.add("$log's base must be numeric, not");
errors.add("$log's base must be a positive number not equal to 1");
errors.add("$divide only supports numeric types, not");
errors.add("can't $divide by zero");
errors.add("$pow's exponent must be numeric, not");
errors.add("$pow's base must be numeric, not");
errors.add("$pow cannot take a base of 0 and a negative exponent");
errors.add("$add only supports numeric or date types, not");
errors.add("only one date allowed in an $add expression");
errors.add("$multiply only supports numeric types, not");
errors.add("$exp only supports numeric types, not");
errors.add("$sqrt's argument must be greater than or equal to 0");
errors.add("$sqrt only supports numeric types, not");
// REGEX
errors.add("Regular expression is invalid: nothing to repeat");
errors.add("Regular expression is invalid: missing terminating ] for character class");
errors.add("Regular expression is invalid: unmatched parentheses");
errors.add("Regular expression is invalid: missing )");
errors.add("Regular expression is invalid: invalid UTF-8 string");
errors.add("Regular expression is invalid: \\k is not followed by a braced, angle-bracketed, or quoted name");
errors.add("Regular expression is invalid: missing opening brace after \\\\o");
errors.add("Regular expression is invalid: reference to non-existent subpattern");
errors.add("Regular expression is invalid: \\ at end of pattern");
errors.add("Regular expression is invalid: PCRE does not support \\L, \\l, \\N{name}, \\U, or \\u");
errors.add("Regular expression is invalid: (?R or (?[+-]digits must be followed by )");
errors.add("Regular expression is invalid: unknown property name after \\P or \\p");
errors.add("Regular expression is invalid: (*VERB) not recognized or malformed");
errors.add("Regular expression is invalid: a numbered reference must not be zero");
errors.add("Regular expression is invalid: unrecognized character after (? or (?-");
errors.add("Regular expression is invalid: \\c at end of pattern");
errors.add("Regular expression is invalid: malformed \\P or \\p sequence");
errors.add("Regular expression is invalid: range out of order in character class");
errors.add("Regular expression is invalid: group name must start with a non-digit");
errors.add("Regular expression is invalid: \\c must be followed by an ASCII character");
errors.add("Regular expression is invalid: subpattern name expected");
errors.add("Regular expression is invalid: POSIX collating elements are not supported");
errors.add("Regular expression is invalid: closing ) for (?C expected");
errors.add("Regular expression is invalid: syntax error in subpattern name (missing terminator)");
errors.add("Regular expression is invalid: \\\\N is not supported in a class");
errors.add("Regular expression is invalid: non-octal character in \\o{} (closing brace missing?)");
errors.add("Regular expression is invalid: non-hex character in \\x{} (closing brace missing?)");
errors.add(
"Regular expression is invalid: \\g is not followed by a braced, angle-bracketed, or quoted name/number or by a plain number");
errors.add("Regular expression is invalid: digits missing in \\x{} or \\o{}");
errors.add("Regular expression is invalid: malformed number or name after (?(");
errors.add("Regular expression is invalid: digit expected after (?+");
errors.add("Regular expression is invalid: assertion expected after (?( or (?(?C)");
errors.add("Regular expression is invalid: unrecognized character after (?P");
return errors;
}
@Override
public <G extends GlobalState<?, ?, MongoDBConnection>> SQLancerResultSet executeAndGet(G globalState,
String... fills) throws Exception {
if (globalState.getOptions().logEachSelect()) {
globalState.getLogger().writeCurrent(this.getLogString());
try {
globalState.getLogger().getCurrentFileWriter().flush();
} catch (IOException e) {
e.printStackTrace();
}
}
List<Bson> pipeline = MongoDBVisitor.asQuery(select);
MongoCollection<Document> collection = globalState.getConnection().getDatabase()
.getCollection(select.getMainTableName());
MongoCursor<Document> cursor = collection.aggregate(pipeline).cursor();
resultSet = new ArrayList<>();
while (cursor.hasNext()) {
Document document = cursor.next();
resultSet.add(document);
}
return null;
}
@Override
public String getLogString() {
return MongoDBVisitor.asStringLog(select);
}
public List<Document> getResultSet() {
return resultSet;
}
}