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

Commit

Permalink
capture: don't allow events submitted with an empty distinct_id (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
xvello authored Apr 24, 2024
1 parent 0d48419 commit 55292bd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
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

0 comments on commit 55292bd

Please sign in to comment.