relay_pii/generate_selectors.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
use std::collections::BTreeSet;
use relay_event_schema::processor::{
self, Pii, ProcessValue, ProcessingResult, ProcessingState, Processor, ValueType,
};
use relay_event_schema::protocol::{AsPair, PairList};
use relay_protocol::{Annotated, Meta, Value};
use serde::Serialize;
use crate::selector::{SelectorPathItem, SelectorSpec};
use crate::utils;
/// Metadata about a selector found in the event
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Serialize)]
pub struct SelectorSuggestion {
/// The selector that users should be able to use to address the underlying value
pub path: SelectorSpec,
/// The JSON-serialized value for previewing what the selector means.
///
/// Right now this only contains string values.
pub value: Option<String>,
}
struct GenerateSelectorsProcessor {
selectors: BTreeSet<SelectorSuggestion>,
}
impl Processor for GenerateSelectorsProcessor {
fn before_process<T: ProcessValue>(
&mut self,
value: Option<&T>,
_meta: &mut Meta,
state: &ProcessingState<'_>,
) -> ProcessingResult {
// The following skip-conditions are in sync with what the PiiProcessor does.
if state.value_type().contains(ValueType::Boolean)
|| value.is_none()
|| state.attrs().pii == Pii::False
{
return Ok(());
}
let mut insert_path = |path: SelectorSpec| {
if path.matches_path(&state.path()) {
let mut string_value = None;
if let Some(value) = value {
if let Value::String(s) = value.clone().into_value() {
string_value = Some(s);
}
}
self.selectors.insert(SelectorSuggestion {
path,
value: string_value,
});
true
} else {
false
}
};
let mut path = Vec::new();
// Walk through processing state in reverse order and build selector path off of that.
for substate in state.iter() {
if !substate.entered_anything() {
continue;
}
for value_type in substate.value_type() {
match value_type {
// $array.0.foo and $object.bar are not particularly good suggestions.
ValueType::Object | ValueType::Array => {}
// a.b.c.$string is not a good suggestion, so special case those.
ty @ ValueType::String
| ty @ ValueType::Number
| ty @ ValueType::Boolean
| ty @ ValueType::DateTime => {
insert_path(SelectorSpec::Path(vec![SelectorPathItem::Type(ty)]));
}
ty => {
let mut path = path.clone();
path.push(SelectorPathItem::Type(ty));
path.reverse();
if insert_path(SelectorSpec::Path(path)) {
// If we managed to generate $http.header.Authorization, we do not want to
// generate request.headers.Authorization as well.
return Ok(());
}
}
}
}
if let Some(key) = substate.path().key() {
path.push(SelectorPathItem::Key(key.to_owned()));
} else if substate.path().index().is_some() {
path.push(SelectorPathItem::Wildcard);
} else {
debug_assert!(substate.depth() == 0);
break;
}
}
if !path.is_empty() {
path.reverse();
insert_path(SelectorSpec::Path(path));
}
Ok(())
}
fn process_pairlist<T: ProcessValue + AsPair>(
&mut self,
value: &mut PairList<T>,
_meta: &mut Meta,
state: &ProcessingState,
) -> ProcessingResult {
utils::process_pairlist(self, value, state)
}
}
/// Walk through a value and collect selectors that can be applied to it in a PII config. This
/// function is used in the UI to provide auto-completion of selectors.
///
/// This generates a couple of duplicate suggestions such as `request.headers` and `$http.headers`
/// in order to make it more likely that the user input starting with either prefix can be
/// completed.
///
/// The main value in autocompletion is that we can complete `$http.headers.Authorization` as soon
/// as the user types `Auth`.
///
/// XXX: This function should not have to take a mutable ref, we only do that due to restrictions
/// on the Processor trait that we internally use to traverse the event.
pub fn selector_suggestions_from_value<T: ProcessValue>(
value: &mut Annotated<T>,
) -> BTreeSet<SelectorSuggestion> {
let mut processor = GenerateSelectorsProcessor {
selectors: BTreeSet::new(),
};
processor::process_value(value, &mut processor, ProcessingState::root())
.expect("This processor is supposed to be infallible");
processor.selectors
}
#[cfg(test)]
mod tests {
use relay_event_schema::protocol::Event;
use super::*;
#[test]
fn test_empty() {
// Test that an event without PII will generate empty list.
let mut event =
Annotated::<Event>::from_json(r#"{"logentry": {"message": "hi"}}"#).unwrap();
let selectors = selector_suggestions_from_value(&mut event);
assert!(selectors.is_empty());
}
#[test]
fn test_full() {
let mut event = Annotated::<Event>::from_json(
r#"
{
"message": "hi",
"exception": {
"values": [
{
"type": "ZeroDivisionError",
"value": "Divided by zero",
"stacktrace": {
"frames": [
{
"abs_path": "foo/bar/baz",
"filename": "baz",
"vars": {
"foo": "bar"
}
}
]
}
},
{
"type": "BrokenException",
"value": "Something failed",
"stacktrace": {
"frames": [
{
"vars": {
"bam": "bar"
}
}
]
}
}
]
},
"extra": {
"My Custom Value": "123"
},
"request": {
"headers": {
"Authorization": "not really"
}
}
}
"#,
)
.unwrap();
let selectors = selector_suggestions_from_value(&mut event);
insta::assert_json_snapshot!(selectors, @r###"
[
{
"path": "$string",
"value": "123"
},
{
"path": "$string",
"value": "Divided by zero"
},
{
"path": "$string",
"value": "Something failed"
},
{
"path": "$string",
"value": "bar"
},
{
"path": "$string",
"value": "hi"
},
{
"path": "$string",
"value": "not really"
},
{
"path": "$error.value",
"value": "Divided by zero"
},
{
"path": "$error.value",
"value": "Something failed"
},
{
"path": "$frame.abs_path",
"value": "foo/bar/baz"
},
{
"path": "$frame.filename",
"value": "baz"
},
{
"path": "$frame.vars",
"value": null
},
{
"path": "$frame.vars.bam",
"value": "bar"
},
{
"path": "$frame.vars.foo",
"value": "bar"
},
{
"path": "$http.headers",
"value": null
},
{
"path": "$http.headers.Authorization",
"value": "not really"
},
{
"path": "$message",
"value": "hi"
},
{
"path": "extra",
"value": null
},
{
"path": "extra.'My Custom Value'",
"value": "123"
}
]
"###);
}
}