Skip to content

Commit

Permalink
feat(schema): Allow integers as username (#3328)
Browse files Browse the repository at this point in the history
The django integration sends integers as usernames if the underlying
`User` model returns an integer for `get_username()` (see issue).

Fixes getsentry/sentry#67601.
  • Loading branch information
jjbayer authored Mar 27, 2024
1 parent 1f94761 commit 38e7da4
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**:

Expand Down
2 changes: 1 addition & 1 deletion relay-event-normalization/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}

Expand Down
4 changes: 2 additions & 2 deletions relay-event-normalization/src/normalize/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
17 changes: 15 additions & 2 deletions relay-event-schema/src/protocol/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub struct User {

/// Username of the user.
#[metastructure(pii = "true", max_chars = 128, skip_serialization = "empty")]
pub username: Annotated<String>,
pub username: Annotated<LenientString>,

/// Human readable name of the user.
#[metastructure(pii = "true", max_chars = 128, skip_serialization = "empty")]
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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":[]}"#;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ Event {
),
},
username: Annotated(
"hey man [ip]",
LenientString(
"hey man [ip]",
),
Meta {
remarks: [
Remark {
Expand Down
2 changes: 1 addition & 1 deletion relay-server/src/utils/unreal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 38e7da4

Please sign in to comment.