relay_event_normalization/
remove_other.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
use relay_event_schema::processor::{ProcessValue, ProcessingResult, ProcessingState, Processor};
use relay_event_schema::protocol::{Breadcrumb, Event};
use relay_protocol::{Annotated, ErrorKind, Meta, Object, Value};

/// Replace remaining values and all existing meta with an errors.
fn create_errors(other: &mut Object<Value>) {
    for value in other.values_mut() {
        *value = Annotated::from_error(ErrorKind::InvalidAttribute, None);
    }
}

/// Removes unknown, internal and deprecated fields from a payload.
pub struct RemoveOtherProcessor;

impl Processor for RemoveOtherProcessor {
    fn process_other(
        &mut self,
        other: &mut Object<Value>,
        state: &ProcessingState<'_>,
    ) -> ProcessingResult {
        // Drop unknown attributes at all levels without error messages, unless `retain = "true"`
        // was specified explicitly on the field.
        if !state.attrs().retain {
            other.clear();
        }

        Ok(())
    }

    fn process_breadcrumb(
        &mut self,
        breadcrumb: &mut Breadcrumb,
        _meta: &mut Meta,
        state: &ProcessingState<'_>,
    ) -> ProcessingResult {
        // Move the current map out so we don't clear it in `process_other`
        let mut other = std::mem::take(&mut breadcrumb.other);
        create_errors(&mut other);

        // Recursively clean all `other`s now. Note that this won't touch the event's other
        breadcrumb.process_child_values(self, state)?;
        breadcrumb.other = other;
        Ok(())
    }

    fn process_event(
        &mut self,
        event: &mut Event,
        _meta: &mut Meta,
        state: &ProcessingState<'_>,
    ) -> ProcessingResult {
        // Move the current map out so we don't clear it in `process_other`
        let mut other = std::mem::take(&mut event.other);

        // Drop Sentry internal attributes
        other.remove("metadata");
        other.remove("hashes");

        // Drop known legacy attributes at top-level without errors
        other.remove("applecrashreport");
        other.remove("device");
        other.remove("repos");
        other.remove("query");

        // Replace remaining values and all existing meta with an errors
        create_errors(&mut other);

        // Recursively clean all `other`s now. Note that this won't touch the event's other
        event.process_child_values(self, state)?;

        event.other = other;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use relay_event_schema::processor::process_value;
    use relay_event_schema::protocol::{Context, Contexts, OsContext, User, Values};
    use relay_protocol::{get_value, FromValue};
    use similar_asserts::assert_eq;

    use super::*;

    #[test]
    fn test_remove_legacy_attributes() {
        let mut event = Annotated::new(Event {
            other: {
                let mut other = Object::new();
                other.insert("applecrashreport".to_string(), Value::U64(42).into());
                other.insert("device".to_string(), Value::U64(42).into());
                other.insert("repos".to_string(), Value::U64(42).into());
                other.insert("query".to_string(), Value::U64(42).into());
                other
            },
            ..Default::default()
        });

        process_value(
            &mut event,
            &mut RemoveOtherProcessor,
            ProcessingState::root(),
        )
        .unwrap();

        assert!(event.value().unwrap().other.is_empty());
    }

    #[test]
    fn test_remove_unknown_attributes() {
        let mut event = Annotated::new(Event {
            other: {
                let mut other = Object::new();
                other.insert("foo".to_string(), Value::U64(42).into());
                other.insert("bar".to_string(), Value::U64(42).into());
                other
            },
            ..Default::default()
        });

        process_value(
            &mut event,
            &mut RemoveOtherProcessor,
            ProcessingState::root(),
        )
        .unwrap();

        let other = &event.value().unwrap().other;
        assert_eq!(
            *other.get("foo").unwrap(),
            Annotated::from_error(ErrorKind::InvalidAttribute, None)
        );
        assert_eq!(
            *other.get("bar").unwrap(),
            Annotated::from_error(ErrorKind::InvalidAttribute, None)
        );
    }

    #[test]
    fn test_remove_nested_other() {
        let mut event = Annotated::new(Event {
            user: Annotated::from(User {
                other: {
                    let mut other = Object::new();
                    other.insert("foo".to_string(), Value::U64(42).into());
                    other.insert("bar".to_string(), Value::U64(42).into());
                    other
                },
                ..Default::default()
            }),
            ..Default::default()
        });

        process_value(
            &mut event,
            &mut RemoveOtherProcessor,
            ProcessingState::root(),
        )
        .unwrap();

        assert!(get_value!(event.user!).other.is_empty());
    }

    #[test]
    fn test_retain_context_other() {
        let mut os = OsContext::default();
        os.other
            .insert("foo".to_string(), Annotated::from(Value::U64(42)));

        let mut contexts = Contexts::new();
        contexts.insert("renamed".to_string(), Context::Os(Box::new(os)));

        let mut event = Annotated::new(Event {
            contexts: Annotated::new(contexts.clone()),
            ..Default::default()
        });

        process_value(
            &mut event,
            &mut RemoveOtherProcessor,
            ProcessingState::root(),
        )
        .unwrap();

        assert_eq!(get_value!(event.contexts!).0, contexts.0);
    }

    #[test]
    fn test_breadcrumb_errors() {
        let mut event = Annotated::new(Event {
            breadcrumbs: Annotated::new(Values::new(vec![Annotated::new(Breadcrumb {
                other: {
                    let mut other = Object::new();
                    other.insert("foo".to_string(), Value::U64(42).into());
                    other.insert("bar".to_string(), Value::U64(42).into());
                    other
                },
                ..Breadcrumb::default()
            })])),
            ..Default::default()
        });

        process_value(
            &mut event,
            &mut RemoveOtherProcessor,
            ProcessingState::root(),
        )
        .unwrap();

        let other = &event
            .value()
            .unwrap()
            .breadcrumbs
            .value()
            .unwrap()
            .values
            .value()
            .unwrap()[0]
            .value()
            .unwrap()
            .other;

        assert_eq!(
            *other.get("foo").unwrap(),
            Annotated::from_error(ErrorKind::InvalidAttribute, None)
        );
        assert_eq!(
            *other.get("bar").unwrap(),
            Annotated::from_error(ErrorKind::InvalidAttribute, None)
        );
    }

    #[test]
    fn test_scrape_attempts() {
        let json = serde_json::json!({
            "scraping_attempts": [
                {"status": "not_attempted", "url": "http://example.com/embedded.js"},
                {"status": "not_attempted", "url": "http://example.com/embedded.js.map"},
            ]
        });

        let mut event = Event::from_value(json.into());
        process_value(
            &mut event,
            &mut RemoveOtherProcessor,
            ProcessingState::root(),
        )
        .unwrap();
        assert!(event.value().unwrap().other.is_empty());
    }
}