Skip to content
This repository has been archived by the owner on Jun 21, 2024. It is now read-only.

capture: don't allow events submitted with an empty distinct_id #25

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions capture/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub enum CaptureError {
EmptyBatch,
#[error("event submitted with an empty event name")]
MissingEventName,
#[error("event submitted with an empty distinct_id")]
EmptyDistinctId,
#[error("event submitted without a distinct_id")]
MissingDistinctId,

Expand Down Expand Up @@ -59,6 +61,7 @@ impl IntoResponse for CaptureError {
| CaptureError::RequestParsingError(_)
| CaptureError::EmptyBatch
| CaptureError::MissingEventName
| CaptureError::EmptyDistinctId
| CaptureError::MissingDistinctId
| CaptureError::EventTooBig
| CaptureError::NonRetryableSinkError => (StatusCode::BAD_REQUEST, self.to_string()),
Expand Down
16 changes: 11 additions & 5 deletions capture/src/v0_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,11 @@ impl RawEvent {
.as_str()
.map(|s| s.to_owned())
.unwrap_or_else(|| value.to_string());
Ok(match distinct_id.len() {
0..=200 => distinct_id,
_ => distinct_id.chars().take(200).collect(),
})
match distinct_id.len() {
0 => Err(CaptureError::EmptyDistinctId),
1..=200 => Ok(distinct_id),
_ => Ok(distinct_id.chars().take(200).collect()),
}
}
}

Expand Down Expand Up @@ -303,11 +304,16 @@ mod tests {
parse_and_extract(r#"{"event": "e"}"#),
Err(CaptureError::MissingDistinctId)
));
// Return MissingDistinctId if null, breaking compat with capture-py
// Return MissingDistinctId if null
assert!(matches!(
parse_and_extract(r#"{"event": "e", "distinct_id": null}"#),
Err(CaptureError::MissingDistinctId)
));
// Return EmptyDistinctId if empty string
assert!(matches!(
parse_and_extract(r#"{"event": "e", "distinct_id": ""}"#),
Err(CaptureError::EmptyDistinctId)
));

let assert_extracted_id = |input: &'static str, expected: &str| {
let id = parse_and_extract(input).expect("failed to extract");
Expand Down
Loading