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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
use std::collections::BTreeMap;
use crate::entry::Entry;
use crate::validator::Validator;
use crate::{
de::FogDeserializer,
element::Parser,
error::{Error, Result},
ser::FogSerializer,
validator::{Checklist, DataChecklist},
value_ref::ValueRef,
MAX_QUERY_SIZE,
};
use fog_crypto::hash::Hash;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct InnerQuery {
key: String,
query: Validator,
}
#[derive(Clone, Debug)]
pub struct NewQuery {
inner: InnerQuery,
}
impl NewQuery {
pub fn new(key: &str, query: Validator) -> Self {
Self {
inner: InnerQuery {
key: key.to_owned(),
query,
},
}
}
pub fn validator(&self) -> &Validator {
&self.inner.query
}
pub fn key(&self) -> &str {
&self.inner.key
}
pub(crate) fn complete(self, max_regex: u8) -> Result<Vec<u8>> {
fn parse_validator(v: &Validator) -> usize {
match v {
Validator::Str(val) => val.matches.is_some() as usize,
Validator::Map(val) => {
val.keys.matches.is_some() as usize
+ val
.req
.values()
.fold(0, |acc, val| acc + parse_validator(val))
+ val
.opt
.values()
.fold(0, |acc, val| acc + parse_validator(val))
+ val.values.as_ref().map_or(0, |val| parse_validator(val))
}
Validator::Array(val) => {
val.contains
.iter()
.fold(0, |acc, val| acc + parse_validator(val))
+ parse_validator(val.items.as_ref())
+ val
.prefix
.iter()
.fold(0, |acc, val| acc + parse_validator(val))
}
Validator::Hash(val) => val.link.as_ref().map_or(0, |val| parse_validator(val)),
Validator::Enum(val) => val
.values()
.fold(0, |acc, val| acc + val.as_ref().map_or(0, parse_validator)),
Validator::Multi(val) => val.iter().fold(0, |acc, val| acc + parse_validator(val)),
_ => 0,
}
}
let regexes = parse_validator(&self.inner.query);
if regexes > (max_regex as usize) {
return Err(Error::FailValidate(format!(
"Found {} regexes in query, only {} allowed",
regexes, max_regex
)));
}
let mut ser = FogSerializer::default();
self.inner.serialize(&mut ser)?;
let buf = ser.finish();
if buf.len() > MAX_QUERY_SIZE {
Err(Error::LengthTooLong {
max: MAX_QUERY_SIZE,
actual: buf.len(),
})
} else {
Ok(buf)
}
}
}
#[derive(Clone, Debug)]
pub struct Query {
inner: InnerQuery,
schema: Hash,
types: BTreeMap<String, Validator>,
}
impl Query {
pub(crate) fn new(buf: Vec<u8>, max_regex: u8) -> Result<Self> {
fn parse_validator(v: &ValueRef) -> usize {
if let ValueRef::Map(map) = v {
if map.len() > 1 {
return 0;
}
match map.iter().next() {
Some((&"Str", val)) => val["matches"].is_str() as usize,
Some((&"Map", val)) => {
if !val.is_map() {
return 0;
}
let key_matches = if val["keys"]["matches"].is_str() {
1
} else {
0
};
let req_matches = val["req"].as_map().map_or(0, |map| {
map.values().fold(0, |acc, val| acc + parse_validator(val))
});
let opt_matches = val["opt"].as_map().map_or(0, |map| {
map.values().fold(0, |acc, val| acc + parse_validator(val))
});
let values_matches = parse_validator(&val["values"]);
key_matches + req_matches + opt_matches + values_matches
}
Some((&"Array", val)) => {
if !val.is_map() {
return 0;
}
let contains_matches = val["contains"].as_array().map_or(0, |array| {
array.iter().fold(0, |acc, val| acc + parse_validator(val))
});
let items_matches = parse_validator(&val["items"]);
let prefix_matches = val["contains"].as_array().map_or(0, |array| {
array.iter().fold(0, |acc, val| acc + parse_validator(val))
});
contains_matches + items_matches + prefix_matches
}
Some((&"Hash", val)) => {
if !val.is_map() {
return 0;
}
parse_validator(&val["link"])
}
Some((&"Enum", val)) => val.as_map().map_or(0, |map| {
map.values().fold(0, |acc, val| acc + parse_validator(val))
}),
Some((&"Multi", val)) => val.as_array().map_or(0, |array| {
array.iter().fold(0, |acc, val| acc + parse_validator(val))
}),
_ => 0,
}
} else {
0
}
}
let mut de = FogDeserializer::new(&buf);
let regex_check = ValueRef::deserialize(&mut de)?;
let regexes = parse_validator(®ex_check["query"]);
if regexes > (max_regex as usize) {
return Err(Error::FailValidate(format!(
"Found {} regexes in query, only {} allowed",
regexes, max_regex
)));
}
let mut de = FogDeserializer::new(&buf);
let inner = InnerQuery::deserialize(&mut de)?;
Ok(Self {
inner,
schema: Hash::new(&[]),
types: BTreeMap::new(),
})
}
pub fn validator(&self) -> &Validator {
&self.inner.query
}
pub fn key(&self) -> &str {
&self.inner.key
}
pub fn query(&self, entry: &Entry) -> Result<DataChecklist<()>> {
let parser = Parser::new(entry.data());
let checklist = Some(Checklist::new(&self.schema, &self.types));
let (_, checklist) = self.inner.query.validate(&self.types, parser, checklist)?;
Ok(DataChecklist::from_checklist(checklist.unwrap(), ()))
}
}
#[cfg(test)]
mod test {
use regex::Regex;
use crate::validator::{KeyValidator, MapValidator, StrValidator};
use super::*;
#[test]
fn max_regex_in_key() {
let map = MapValidator {
keys: KeyValidator {
matches: Some(Box::new(Regex::new("[a-z]").unwrap())),
..Default::default()
},
..Default::default()
};
let validator = Validator::Map(map);
NewQuery::new("test", validator.clone())
.complete(0)
.unwrap_err();
let enc_query = NewQuery::new("test", validator).complete(1).unwrap();
assert!(Query::new(enc_query.clone(), 0).is_err());
assert!(Query::new(enc_query.clone(), 1).is_ok());
assert!(Query::new(enc_query, 2).is_ok());
}
#[test]
fn max_regex_in_str() {
let matches = Some(Box::new(Regex::new("[a-z]").unwrap()));
let validator = Validator::Str(StrValidator {
matches,
..Default::default()
});
NewQuery::new("test", validator.clone())
.complete(0)
.unwrap_err();
let enc_query = NewQuery::new("test", validator).complete(1).unwrap();
assert!(Query::new(enc_query.clone(), 0).is_err());
assert!(Query::new(enc_query.clone(), 1).is_ok());
assert!(Query::new(enc_query, 2).is_ok());
}
}