Skip to content

Commit 567ccce

Browse files
committed
add: query params validation
1 parent 5cc6114 commit 567ccce

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

sentry/src/db/channel.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ pub async fn insert_channel(
7070
.await
7171
}
7272

73-
7473
pub async fn get_channel_by_creator(
7574
pool: &DbPool,
7675
id: &ChannelId,
@@ -92,7 +91,6 @@ pub async fn get_channel_by_creator(
9291
.await
9392
}
9493

95-
9694
mod list_channels {
9795
use crate::db::DbPool;
9896
use bb8::RunError;

sentry/src/routes/analytics.rs

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,33 @@ struct AnalyticsQuery {
3838
pub timeframe: String,
3939
}
4040

41+
impl AnalyticsQuery {
42+
pub fn is_valid(&self) -> Result<(), ResponseError> {
43+
let valid_event_types = ["IMPRESSION"];
44+
let valid_metric = ["eventPayouts", "eventCounts"];
45+
let valid_timeframe = ["year", "month", "week", "day", "hour"];
46+
47+
if !valid_event_types.iter().any(|e| *e == &self.event_type[..]) {
48+
Err(ResponseError::BadRequest(format!(
49+
"invalid event_type, possible values are: {}",
50+
valid_event_types.join(" ,")
51+
)))
52+
} else if !valid_metric.iter().any(|e| *e == &self.metric[..]) {
53+
Err(ResponseError::BadRequest(format!(
54+
"invalid metric, possible values are: {}",
55+
valid_metric.join(" ,")
56+
)))
57+
} else if !valid_timeframe.iter().any(|e| *e == &self.timeframe[..]) {
58+
Err(ResponseError::BadRequest(format!(
59+
"invalid timeframe, possible values are: {}",
60+
valid_timeframe.join(" ,")
61+
)))
62+
} else {
63+
Ok(())
64+
}
65+
}
66+
}
67+
4168
fn default_limit() -> u32 {
4269
100
4370
}
@@ -104,6 +131,8 @@ pub async fn process_analytics<A: Adapter>(
104131
skip_publisher: bool,
105132
) -> Result<String, ResponseError> {
106133
let query = serde_urlencoded::from_str::<AnalyticsQuery>(&req.uri().query().unwrap_or(""))?;
134+
query.is_valid()?;
135+
107136
let applied_limit = cmp::min(query.limit, 200);
108137
let (interval, period) = get_time_frame(&query.timeframe);
109138
let time_limit = Utc::now().timestamp() - period;
@@ -132,7 +161,7 @@ pub async fn process_analytics<A: Adapter>(
132161
query.event_type, query.metric, session.uid
133162
));
134163
format!(
135-
"select SUM((events->'{}'->'{}'->>'{}')::numeric) as value, extract({} from created) as time from event_aggregates",
164+
"select SUM((events->'{}'->'{}'->>'{}')::numeric) as value, (extract(epoch from created) - (MOD( CAST (extract(epoch from created) AS NUMERIC), {}))) as time from event_aggregates",
136165
query.event_type, query.metric, session.uid, interval
137166
)
138167
}
@@ -142,7 +171,7 @@ pub async fn process_analytics<A: Adapter>(
142171
query.event_type, query.metric
143172
));
144173
format!(
145-
"select SUM(value::numeric)::varchar as value, extract({} from created) as time from event_aggregates, jsonb_each_text(events->'{}'->'{}')",
174+
"select SUM(value::numeric)::varchar as value, (extract(epoch from created) - (MOD( CAST (extract(epoch from created) AS NUMERIC), {}))) as time from event_aggregates, jsonb_each_text(events->'{}'->'{}')",
146175
interval, query.event_type, query.metric
147176
)
148177
}
@@ -194,17 +223,17 @@ async fn cache(redis: &MultiplexedConnection, key: String, value: &str, timefram
194223
}
195224
}
196225

197-
fn get_time_frame(timeframe: &str) -> (String, i64) {
226+
fn get_time_frame(timeframe: &str) -> (i64, i64) {
198227
let minute = 60 * 1000;
199228
let hour = 60 * minute;
200229
let day = 24 * hour;
201230

202231
match timeframe {
203-
"year" => ("month".into(), 365 * day),
204-
"month" => ("day".into(), 30 * day),
205-
"week" => ("week".into(), 7 * day),
206-
"day" => ("hour".into(), day),
207-
"hour" => ("minute".into(), hour),
208-
_ => ("hour".into(), day),
232+
"year" => (30 * day, 365 * day),
233+
"month" => (day, 30 * day),
234+
"week" => (6 * hour, 7 * day),
235+
"day" => (hour, day),
236+
"hour" => (minute, hour),
237+
_ => (hour, day),
209238
}
210239
}

0 commit comments

Comments
 (0)