agent: Fix crasher if UpdateRoutes request empty
Check if the routes specified in the `UpdateRoutes` API is set before
using it to avoid crashing the agent.
Fixes:
@@ -954,6 +954,13 @@ impl protocols::agent_ttrpc::AgentService for agentService {
req: protocols::agent::UpdateRoutesRequest,
) -> ttrpc::Result<Routes> {
let mut routes = protocols::agent::Routes::new();
+ if req.routes.is_none() {
+ return Err(ttrpc::Error::RpcStatus(ttrpc::get_status(
+ ttrpc::Code::INVALID_ARGUMENT,
+ format!("empty update routes request"),
+ )));
+ }
+
let rs = req.routes.clone().unwrap().Routes.into_vec();
let s = Arc::clone(&self.sandbox);
@@ -1839,4 +1846,21 @@ mod tests {
assert!(result.is_err(), "expected update interface to fail");
}
+
+ #[test]
+ fn test_update_routes() {
+ let logger = slog::Logger::root(slog::Discard, o!());
+ let sandbox = Sandbox::new(&logger).unwrap();
+
+ let agent_service = Box::new(agentService {
+ sandbox: Arc::new(Mutex::new(sandbox)),
+ });
+
+ let req = protocols::agent::UpdateRoutesRequest::default();
+ let (ctx, _) = mk_ttrpc_context();
+
+ let result = agent_service.update_routes(&ctx, req);
+
+ assert!(result.is_err(), "expected update routes to fail");
+ }
}
