From 38e7da4d3c6111a712b7add25713c691b1db752b Mon Sep 17 00:00:00 2001 From: Joris Bayer Date: Wed, 27 Mar 2024 13:41:10 +0100 Subject: [PATCH] feat(schema): Allow integers as username (#3328) The django integration sends integers as usernames if the underlying `User` model returns an integer for `get_username()` (see issue). Fixes https://github.com/getsentry/sentry/issues/67601. --- CHANGELOG.md | 1 + relay-event-normalization/src/event.rs | 2 +- .../src/normalize/utils.rs | 4 ++-- relay-event-schema/src/protocol/user.rs | 17 +++++++++++++++-- ..._processor__tests__scrub_original_value.snap | 4 +++- relay-server/src/utils/unreal.rs | 2 +- 6 files changed, 23 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6fa8c2d65b2..3ede9c1fda5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - Extract op and description while converting opentelemetry spans to sentry spans. ([#3287](https://github.com/getsentry/relay/pull/3287)) - Drop `event_id` and `remote_addr` from all outcomes. ([#3319](https://github.com/getsentry/relay/pull/3319)) - Support for AI token metrics ([#3250](https://github.com/getsentry/relay/pull/3250)) +- Accept integers in `event.user.username`. ([#3328](https://github.com/getsentry/relay/pull/3328)) **Internal**: diff --git a/relay-event-normalization/src/event.rs b/relay-event-normalization/src/event.rs index d292503723d..0d2c9bcaaba 100644 --- a/relay-event-normalization/src/event.rs +++ b/relay-event-normalization/src/event.rs @@ -2973,7 +2973,7 @@ mod tests { Annotated::new(map) }); assert_eq!(user.other, Object::new()); - assert_eq!(user.username, Annotated::new("john".to_string())); + assert_eq!(user.username, Annotated::new("john".to_string().into())); assert_eq!(user.sentry_user, Annotated::new("id:123456".to_string())); } diff --git a/relay-event-normalization/src/normalize/utils.rs b/relay-event-normalization/src/normalize/utils.rs index cc7a86c7729..aabc4bb9b59 100644 --- a/relay-event-normalization/src/normalize/utils.rs +++ b/relay-event-normalization/src/normalize/utils.rs @@ -205,7 +205,7 @@ mod tests { // has to be changed. Though it is probably not a good idea! let user = User { id: Annotated::new("ident".to_owned().into()), - username: Annotated::new("username".to_owned()), + username: Annotated::new("username".to_owned().into()), email: Annotated::new("email".to_owned()), ip_address: Annotated::new("127.0.0.1".parse().unwrap()), ..User::default() @@ -214,7 +214,7 @@ mod tests { assert_eq!(get_event_user_tag(&user).unwrap(), "id:ident"); let user = User { - username: Annotated::new("username".to_owned()), + username: Annotated::new("username".to_owned().into()), email: Annotated::new("email".to_owned()), ip_address: Annotated::new("127.0.0.1".parse().unwrap()), ..User::default() diff --git a/relay-event-schema/src/protocol/user.rs b/relay-event-schema/src/protocol/user.rs index 6738f827ba2..88263fa9c08 100644 --- a/relay-event-schema/src/protocol/user.rs +++ b/relay-event-schema/src/protocol/user.rs @@ -62,7 +62,7 @@ pub struct User { /// Username of the user. #[metastructure(pii = "true", max_chars = 128, skip_serialization = "empty")] - pub username: Annotated, + pub username: Annotated, /// Human readable name of the user. #[metastructure(pii = "true", max_chars = 128, skip_serialization = "empty")] @@ -163,7 +163,7 @@ mod tests { email: Annotated::new("mail@example.org".to_string()), ip_address: Annotated::new(IpAddr::auto()), name: Annotated::new("John Doe".to_string()), - username: Annotated::new("john_doe".to_string()), + username: Annotated::new(LenientString("john_doe".to_owned())), geo: Annotated::empty(), segment: Annotated::new("vip".to_string()), data: { @@ -202,6 +202,19 @@ mod tests { assert_eq!(output, user.to_json().unwrap()); } + #[test] + fn test_user_lenient_username() { + let input = r#"{"username":42}"#; + let output = r#"{"username":"42"}"#; + let user = Annotated::new(User { + username: Annotated::new("42".to_string().into()), + ..User::default() + }); + + assert_eq!(user, Annotated::from_json(input).unwrap()); + assert_eq!(output, user.to_json().unwrap()); + } + #[test] fn test_user_invalid_id() { let json = r#"{"id":[]}"#; diff --git a/relay-pii/src/snapshots/relay_pii__processor__tests__scrub_original_value.snap b/relay-pii/src/snapshots/relay_pii__processor__tests__scrub_original_value.snap index 8a4421213b4..9fad3a9cb80 100644 --- a/relay-pii/src/snapshots/relay_pii__processor__tests__scrub_original_value.snap +++ b/relay-pii/src/snapshots/relay_pii__processor__tests__scrub_original_value.snap @@ -47,7 +47,9 @@ Event { ), }, username: Annotated( - "hey man [ip]", + LenientString( + "hey man [ip]", + ), Meta { remarks: [ Remark { diff --git a/relay-server/src/utils/unreal.rs b/relay-server/src/utils/unreal.rs index ac6e62283c6..03990a62dc2 100644 --- a/relay-server/src/utils/unreal.rs +++ b/relay-server/src/utils/unreal.rs @@ -196,7 +196,7 @@ fn merge_unreal_context(event: &mut Event, context: Unreal4Context) { .user .get_or_insert_with(User::default) .username - .set_value(Some(username)); + .set_value(Some(username.into())); } let contexts = event.contexts.get_or_insert_with(Contexts::default);