relay_server/utils/
param_parser.rs

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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
use serde_json::Value;

enum IndexingState {
    LookingForLeftParenthesis,
    Accumulating(usize),
    Starting,
}

/// Updates a json Value at the specified path.
pub fn update_nested_value<V>(target: &mut Value, path: &[&str], value: V)
where
    V: Into<String>,
{
    let map = match target {
        Value::Object(map) => map,
        _ => return,
    };

    let (key, rest) = match path.split_first() {
        Some(tuple) => tuple,
        None => return,
    };

    let entry = map.entry(key.to_owned());

    if rest.is_empty() {
        entry.or_insert_with(|| Value::String(value.into()));
    } else {
        let sub_object = entry.or_insert_with(|| Value::Object(Default::default()));
        update_nested_value(sub_object, rest, value);
    }
}

/// Merge two serde values.
///
/// Taken (with small changes) from stack overflow answer:
/// <https://stackoverflow.com/questions/47070876/how-can-i-merge-two-json-objects-with-rust>.
pub fn merge_values(a: &mut Value, b: Value) {
    match (a, b) {
        //recursively merge dicts
        (a @ &mut Value::Object(_), Value::Object(b)) => {
            let a = a.as_object_mut().unwrap();
            for (k, v) in b {
                merge_values(a.entry(k).or_insert(Value::Null), v);
            }
        }
        //fill in missing left values
        (a @ &mut Value::Null, b) => *a = b,
        //do not override existing values that are not maps
        (_a, _b) => {}
    }
}

/// Extracts indexes from a param string e.g. extracts `[String(abc),String(xyz)]` from `"sentry[abc][xyz]"`
fn get_indexes(full_string: &str) -> Result<Vec<&str>, ()> {
    let mut ret_vals = vec![];
    let mut state = IndexingState::Starting;
    //first iterate by byte (so we can get correct offsets)
    for (idx, by) in full_string.bytes().enumerate() {
        match state {
            IndexingState::Starting => {
                if by == b'[' {
                    state = IndexingState::Accumulating(idx + 1)
                }
            }
            IndexingState::LookingForLeftParenthesis => {
                if by == b'[' {
                    state = IndexingState::Accumulating(idx + 1);
                } else if by == b'=' {
                    return Ok(ret_vals);
                } else {
                    return Err(());
                }
            }
            IndexingState::Accumulating(start_idx) => {
                if by == b']' {
                    let slice = &full_string[start_idx..idx];
                    ret_vals.push(slice);
                    state = IndexingState::LookingForLeftParenthesis;
                }
            }
        }
    }
    Ok(ret_vals)
}

/// Extracts indexes from a param of the form 'sentry[XXX][...]'
pub fn get_sentry_entry_indexes(param_name: &str) -> Option<Vec<&str>> {
    if param_name.starts_with("sentry[") {
        get_indexes(param_name).ok()
    } else {
        None
    }
}

/// Extracts the chunk index of a key with the given prefix.
///
/// Electron SDK splits up long payloads into chunks starting at sentry__1 with an
/// incrementing counter. Assemble these chunks here and then decode them below.
pub fn get_sentry_chunk_index(key: &str, prefix: &str) -> Option<usize> {
    key.strip_prefix(prefix).and_then(|rest| rest.parse().ok())
}

/// Aggregates slices of strings in random order.
#[derive(Clone, Debug, Default)]
pub struct ChunkedFormDataAggregator<'a> {
    parts: Vec<&'a str>,
}

impl<'a> ChunkedFormDataAggregator<'a> {
    /// Creates a new empty aggregator.
    pub fn new() -> Self {
        Self::default()
    }

    /// Adds a part with the given index.
    ///
    /// Fills up unpopulated indexes with empty strings, if there are holes between the last index
    /// and this one. This effectively skips them when calling `join` in the end.
    pub fn insert(&mut self, index: usize, value: &'a str) {
        if index >= self.parts.len() {
            self.parts.resize(index + 1, "");
        }

        self.parts[index] = value;
    }

    /// Returns `true` if no parts have been added.
    pub fn is_empty(&self) -> bool {
        self.parts.is_empty()
    }

    /// Returns the string consisting of all parts.
    pub fn join(&self) -> String {
        self.parts.join("")
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_index_parser() {
        let examples: &[(&str, Option<&[&str]>)] = &[
            ("fafdasd[a][b][33]", Some(&["a", "b", "33"])),
            ("fafdasd[a]b[33]", None),
            ("fafdasd[a][b33]xx", None),
            ("[23a][234][abc123]", Some(&["23a", "234", "abc123"])),
            ("sentry[abc][123][]=SomeVal", Some(&["abc", "123", ""])),
            ("sentry[Grüße][Jürgen][❤]", Some(&["Grüße", "Jürgen", "❤"])),
            (
                "[农22历][新年][b新年c]",
                Some(&["农22历", "新年", "b新年c"]),
            ),
            ("[ὈΔΥΣΣΕΎΣ][abc]", Some(&["ὈΔΥΣΣΕΎΣ", "abc"])),
        ];

        for &(example, expected_result) in examples {
            let indexes = get_indexes(example).ok();
            assert_eq!(indexes, expected_result.map(|vec| vec.into()));
        }
    }

    #[test]
    fn test_update_value() {
        let mut val = Value::Object(serde_json::Map::new());

        update_nested_value(&mut val, &["x", "y", "z"], "xx");

        insta::assert_json_snapshot!(val, @r#"
       ⋮{
       ⋮  "x": {
       ⋮    "y": {
       ⋮      "z": "xx"
       ⋮    }
       ⋮  }
       ⋮}
        "#);

        update_nested_value(&mut val, &["x", "y", "k"], "kk");
        update_nested_value(&mut val, &["w", ""], "w");
        update_nested_value(&mut val, &["z1"], "val1");
        insta::assert_json_snapshot!(val, @r#"
       ⋮{
       ⋮  "w": {
       ⋮    "": "w"
       ⋮  },
       ⋮  "x": {
       ⋮    "y": {
       ⋮      "k": "kk",
       ⋮      "z": "xx"
       ⋮    }
       ⋮  },
       ⋮  "z1": "val1"
       ⋮}
        "#);
    }

    #[test]
    fn test_merge_vals() {
        let mut original = serde_json::json!({
            "k1": "v1",
            "k2": {
                "k3": "v3",
                "k4": "v4"
            },
            "k5": [ 1,2,3]
        });

        let modified = serde_json::json!({
            "k1": "v1bis",
            "k2": {
                "k4": "v4bis",
                "k4-1": "v4-1"
            },
            "k6": "v6"
        });

        merge_values(&mut original, modified);
        insta::assert_json_snapshot!(original, @r#"
       ⋮{
       ⋮  "k1": "v1",
       ⋮  "k2": {
       ⋮    "k3": "v3",
       ⋮    "k4": "v4",
       ⋮    "k4-1": "v4-1"
       ⋮  },
       ⋮  "k5": [
       ⋮    1,
       ⋮    2,
       ⋮    3
       ⋮  ],
       ⋮  "k6": "v6"
       ⋮}
        "#);
    }

    #[test]
    fn test_chunk_index() {
        assert_eq!(get_sentry_chunk_index("sentry__0", "sentry__"), Some(0));
        assert_eq!(get_sentry_chunk_index("sentry__1", "sentry__"), Some(1));

        assert_eq!(get_sentry_chunk_index("foo__0", "sentry__"), None);
        assert_eq!(get_sentry_chunk_index("sentry__", "sentry__"), None);
        assert_eq!(get_sentry_chunk_index("sentry__-1", "sentry__"), None);
        assert_eq!(get_sentry_chunk_index("sentry__xx", "sentry__"), None);
    }

    #[test]
    fn test_aggregator_empty() {
        let aggregator = ChunkedFormDataAggregator::new();
        assert!(aggregator.is_empty());
        assert_eq!(aggregator.join(), "");
    }

    #[test]
    fn test_aggregator_base_0() {
        let mut aggregator = ChunkedFormDataAggregator::new();
        aggregator.insert(0, "hello,");
        aggregator.insert(1, " world");

        assert!(!aggregator.is_empty());
        assert_eq!(aggregator.join(), "hello, world");
    }

    #[test]
    fn test_aggregator_base_1() {
        let mut aggregator = ChunkedFormDataAggregator::new();
        aggregator.insert(1, "hello,");
        aggregator.insert(2, " world");

        assert!(!aggregator.is_empty());
        assert_eq!(aggregator.join(), "hello, world");
    }

    #[test]
    fn test_aggregator_holes() {
        let mut aggregator = ChunkedFormDataAggregator::new();
        aggregator.insert(0, "hello,");
        aggregator.insert(3, " world");

        assert!(!aggregator.is_empty());
        assert_eq!(aggregator.join(), "hello, world");
    }

    #[test]
    fn test_aggregator_reversed() {
        let mut aggregator = ChunkedFormDataAggregator::new();
        aggregator.insert(1, " world");
        aggregator.insert(0, "hello,");

        assert!(!aggregator.is_empty());
        assert_eq!(aggregator.join(), "hello, world");
    }

    #[test]
    fn test_aggregator_override() {
        let mut aggregator = ChunkedFormDataAggregator::new();
        aggregator.insert(0, "hello,");
        aggregator.insert(0, "bye");

        assert!(!aggregator.is_empty());
        assert_eq!(aggregator.join(), "bye");
    }
}