Skip to content
This repository has been archived by the owner on Aug 16, 2023. It is now read-only.

Commit

Permalink
Add handle id validation (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
feymartynov committed Apr 22, 2021
1 parent 46eeecb commit 2607305
Show file tree
Hide file tree
Showing 17 changed files with 1,152 additions and 370 deletions.
2 changes: 2 additions & 0 deletions docs/src/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ One must rely on the `type` field of the error for error identification, not the
The following types are a part of the service's API and are guaranteed to maintain compatibility.

- `access_denied` – The action was forbidden by [authorization](authz.md#Authorization).
- `agent_not_connected` – The agent has not connected to the RTC.
- `agent_not_entered_the_room` – The agent must preliminary make [room.enter](room/enter.md#room.enter) request.
- `authorization_failed` – Authorization request failed due to a network error or another reason.
- `backend_recording_missing` – The backend responded that it doesn't have the recording for the RTC.
Expand All @@ -36,6 +37,7 @@ The following types are a part of the service's API and are guaranteed to mainta
- `config_key_missing` – The service couldn't perform an operation due to misconfiguration.
- `database_connection_acquisition_failed` – The service couldn't obtain a DB connection from the pool.
- `database_query_failed` – The database returned an error while executing a query.
- `invalid_handle_id` – Specified `handle_id` has corrupted or expired information.
- `invalid_jsep_format` – Failed to determine whether the SDP is recvonly.
- `invalid_sdp_type` – Failed to parse SDP type or an SDP answer is received.
- `invalid_subscription_object` – An object for dynamic subscription is not of format `["rooms", UUID, "events"]`.
Expand Down
12 changes: 10 additions & 2 deletions src/app/endpoint/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,16 @@ impl RequestHandler for ListHandler {
payload: Self::Payload,
reqp: &IncomingRequestProperties,
) -> Result {
let room =
helpers::find_room_by_id(context, payload.room_id, helpers::RoomTimeRequirement::Open)?;
let room = {
let conn = context.get_conn()?;

helpers::find_room_by_id(
context,
payload.room_id,
helpers::RoomTimeRequirement::Open,
&conn,
)?
};

// Authorize agents listing in the room.
let room_id = room.id().to_string();
Expand Down
22 changes: 16 additions & 6 deletions src/app/endpoint/agent_reader_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,14 @@ impl RequestHandler for UpdateHandler {
.error(AppErrorKind::InvalidPayload)?;
}

let room =
helpers::find_room_by_id(context, payload.room_id, helpers::RoomTimeRequirement::Open)?;
let conn = context.get_conn()?;

let room = helpers::find_room_by_id(
context,
payload.room_id,
helpers::RoomTimeRequirement::Open,
&conn,
)?;

if room.rtc_sharing_policy() != db::rtc::SharingPolicy::Owned {
return Err(anyhow!(
Expand All @@ -101,7 +107,6 @@ impl RequestHandler for UpdateHandler {
.error(AppErrorKind::InvalidPayload)?;
}

let conn = context.get_conn()?;
helpers::check_room_presence(&room, reqp.as_agent_id(), &conn)?;

let rtc_reader_configs_with_rtcs = conn.transaction::<_, AppError, _>(|| {
Expand Down Expand Up @@ -206,8 +211,14 @@ impl RequestHandler for ReadHandler {
payload: Self::Payload,
reqp: &IncomingRequestProperties,
) -> Result {
let room =
helpers::find_room_by_id(context, payload.room_id, helpers::RoomTimeRequirement::Open)?;
let conn = context.get_conn()?;

let room = helpers::find_room_by_id(
context,
payload.room_id,
helpers::RoomTimeRequirement::Open,
&conn,
)?;

if room.rtc_sharing_policy() != db::rtc::SharingPolicy::Owned {
return Err(anyhow!(
Expand All @@ -216,7 +227,6 @@ impl RequestHandler for ReadHandler {
.error(AppErrorKind::InvalidPayload)?;
}

let conn = context.get_conn()?;
helpers::check_room_presence(&room, reqp.as_agent_id(), &conn)?;

let rtc_reader_configs_with_rtcs =
Expand Down
39 changes: 25 additions & 14 deletions src/app/endpoint/agent_writer_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,26 @@ impl RequestHandler for UpdateHandler {
.error(AppErrorKind::InvalidPayload)?;
}

let room =
helpers::find_room_by_id(context, payload.room_id, helpers::RoomTimeRequirement::Open)?;
let room = {
let conn = context.get_conn()?;

if room.rtc_sharing_policy() != db::rtc::SharingPolicy::Owned {
return Err(anyhow!(
"Agent writer config is available only for rooms with owned RTC sharing policy"
))
.error(AppErrorKind::InvalidPayload)?;
}
let room = helpers::find_room_by_id(
context,
payload.room_id,
helpers::RoomTimeRequirement::Open,
&conn,
)?;

if room.rtc_sharing_policy() != db::rtc::SharingPolicy::Owned {
return Err(anyhow!(
"Agent writer config is available only for rooms with owned RTC sharing policy"
))
.error(AppErrorKind::InvalidPayload)?;
}

{
let conn = context.get_conn()?;
helpers::check_room_presence(&room, reqp.as_agent_id(), &conn)?;
}
room
};

// Authorize agent writer config updating on the tenant.
let room_id = room.id().to_string();
Expand Down Expand Up @@ -248,8 +254,14 @@ impl RequestHandler for ReadHandler {
payload: Self::Payload,
reqp: &IncomingRequestProperties,
) -> Result {
let room =
helpers::find_room_by_id(context, payload.room_id, helpers::RoomTimeRequirement::Open)?;
let conn = context.get_conn()?;

let room = helpers::find_room_by_id(
context,
payload.room_id,
helpers::RoomTimeRequirement::Open,
&conn,
)?;

if room.rtc_sharing_policy() != db::rtc::SharingPolicy::Owned {
return Err(anyhow!(
Expand All @@ -258,7 +270,6 @@ impl RequestHandler for ReadHandler {
.error(AppErrorKind::InvalidPayload)?;
}

let conn = context.get_conn()?;
helpers::check_room_presence(&room, reqp.as_agent_id(), &conn)?;

let rtc_writer_configs_with_rtcs =
Expand Down
25 changes: 13 additions & 12 deletions src/app/endpoint/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,33 +64,34 @@ pub(crate) fn find_room_by_id<C: Context>(
context: &mut C,
id: Uuid,
opening_requirement: RoomTimeRequirement,
conn: &PgConnection,
) -> Result<db::room::Object, AppError> {
context.add_logger_tags(o!("room_id" => id.to_string()));
let query = db::room::FindQuery::new(id);
find_room(context, query, opening_requirement)
find_room(context, query, opening_requirement, conn)
}

pub(crate) fn find_room_by_rtc_id<C: Context>(
context: &mut C,
rtc_id: Uuid,
opening_requirement: RoomTimeRequirement,
conn: &PgConnection,
) -> Result<db::room::Object, AppError> {
context.add_logger_tags(o!("rtc_id" => rtc_id.to_string()));
let query = db::room::FindByRtcIdQuery::new(rtc_id);
find_room(context, query, opening_requirement)
find_room(context, query, opening_requirement, conn)
}

fn find_room<C, Q>(
context: &mut C,
query: Q,
opening_requirement: RoomTimeRequirement,
conn: &PgConnection,
) -> Result<Room, AppError>
where
C: Context,
Q: db::room::FindQueryable,
{
let conn = context.get_conn()?;

let room = query
.execute(&conn)?
.ok_or_else(|| anyhow!("Room not found"))
Expand Down Expand Up @@ -157,14 +158,6 @@ where
}
}

pub(crate) fn add_room_logger_tags<C: Context>(context: &mut C, room: &db::room::Object) {
context.add_logger_tags(o!("room_id" => room.id().to_string()));

if let Some(scope) = room.tags().get("scope") {
context.add_logger_tags(o!("scope" => scope.to_string()));
}
}

pub(crate) fn check_room_presence(
room: &db::room::Object,
agent_id: &AgentId,
Expand All @@ -181,3 +174,11 @@ pub(crate) fn check_room_presence(
Ok(())
}
}

pub(crate) fn add_room_logger_tags<C: Context>(context: &mut C, room: &db::room::Object) {
context.add_logger_tags(o!("room_id" => room.id().to_string()));

if let Some(scope) = room.tags().get("scope") {
context.add_logger_tags(o!("scope" => scope.to_string()));
}
}
8 changes: 6 additions & 2 deletions src/app/endpoint/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ impl RequestHandler for UnicastHandler {
reqp: &IncomingRequestProperties,
) -> Result {
{
let conn = context.get_conn()?;

let room = helpers::find_room_by_id(
context,
payload.room_id,
helpers::RoomTimeRequirement::Open,
&conn,
)?;

let conn = context.get_conn()?;
helpers::check_room_presence(&room, reqp.as_agent_id(), &conn)?;
helpers::check_room_presence(&room, &payload.agent_id, &conn)?;
}
Expand Down Expand Up @@ -111,13 +113,15 @@ impl RequestHandler for BroadcastHandler {
reqp: &IncomingRequestProperties,
) -> Result {
let room = {
let conn = context.get_conn()?;

let room = helpers::find_room_by_id(
context,
payload.room_id,
helpers::RoomTimeRequirement::Open,
&conn,
)?;

let conn = context.get_conn()?;
helpers::check_room_presence(&room, &reqp.as_agent_id(), &conn)?;
room
};
Expand Down
49 changes: 37 additions & 12 deletions src/app/endpoint/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,16 @@ impl RequestHandler for ReadHandler {
payload: Self::Payload,
reqp: &IncomingRequestProperties,
) -> Result {
let room =
helpers::find_room_by_id(context, payload.id, helpers::RoomTimeRequirement::Any)?;
let room = {
let conn = context.get_conn()?;

helpers::find_room_by_id(
context,
payload.id,
helpers::RoomTimeRequirement::Any,
&conn,
)?
};

// Authorize room reading on the tenant.
let room_id = room.id().to_string();
Expand Down Expand Up @@ -189,11 +197,16 @@ impl RequestHandler for UpdateHandler {
payload: Self::Payload,
reqp: &IncomingRequestProperties,
) -> Result {
let room = helpers::find_room_by_id(
context,
payload.id,
helpers::RoomTimeRequirement::NotClosedOrUnboundedOpen,
)?;
let room = {
let conn = context.get_conn()?;

helpers::find_room_by_id(
context,
payload.id,
helpers::RoomTimeRequirement::NotClosedOrUnboundedOpen,
&conn,
)?
};

// Authorize room updating on the tenant.
let room_id = room.id().to_string();
Expand Down Expand Up @@ -314,8 +327,16 @@ impl RequestHandler for EnterHandler {
payload: Self::Payload,
reqp: &IncomingRequestProperties,
) -> Result {
let room =
helpers::find_room_by_id(context, payload.id, helpers::RoomTimeRequirement::NotClosed)?;
let room = {
let conn = context.get_conn()?;

helpers::find_room_by_id(
context,
payload.id,
helpers::RoomTimeRequirement::NotClosed,
&conn,
)?
};

// Authorize subscribing to the room's events.
let room_id = room.id().to_string();
Expand Down Expand Up @@ -381,11 +402,15 @@ impl RequestHandler for LeaveHandler {
reqp: &IncomingRequestProperties,
) -> Result {
let (room, presence) = {
let room =
helpers::find_room_by_id(context, payload.id, helpers::RoomTimeRequirement::Any)?;

let conn = context.get_conn()?;

let room = helpers::find_room_by_id(
context,
payload.id,
helpers::RoomTimeRequirement::Any,
&conn,
)?;

// Check room presence.
let presence = db::agent::ListQuery::new()
.room_id(room.id())
Expand Down
Loading

0 comments on commit 2607305

Please sign in to comment.