Skip to content

Commit c315bb1

Browse files
fix: Use Debug trait formatter for API errors
This change will, instead of using the default Display formatter when logging the API request's error, use the Debug trait's formatter for a sanitized output. The test include will confirm Debug display sanitizes API error. In cases where there are errand special characters like newlines in an API request's payload, this can result in deserialization errors. These errors can result in noisy or "disjointed" logs, such as in the case of newlines. Using the Debug `:?` trait's string formatter should escape special characters. Signed-off-by: Matthew Schlebusch <[email protected]>
1 parent 6da1854 commit c315bb1

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/api_server/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ utils = { path = "../utils" }
2323
vmm = { path = "../vmm" }
2424

2525
[dev-dependencies]
26-
libc = "0.2.117"
26+
libc = "0.2.117"

src/api_server/src/lib.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ impl ApiServer {
243243
response
244244
}
245245
Err(err) => {
246-
error!("{}", err);
246+
error!("{:?}", err);
247247
err.into()
248248
}
249249
}
@@ -320,6 +320,29 @@ mod tests {
320320
use vmm::vmm_config::snapshot::CreateSnapshotParams;
321321

322322
use super::*;
323+
use crate::request::cpu_configuration::parse_put_cpu_config;
324+
325+
/// Test unescaped CPU template in JSON format.
326+
/// Newlines injected into a field's value to
327+
/// test deserialization and logging.
328+
#[cfg(target_arch = "x86_64")]
329+
const TEST_UNESCAPED_JSON_TEMPLATE: &str = r#"{
330+
"msr_modifiers": [
331+
{
332+
"addr": "0x0\n\n\n\nTEST\n\n\n\n",
333+
"bitmap": "0b00"
334+
}
335+
]
336+
}"#;
337+
#[cfg(target_arch = "aarch64")]
338+
pub const TEST_UNESCAPED_JSON_TEMPLATE: &str = r#"{
339+
"reg_modifiers": [
340+
{
341+
"addr": "0x0\n\n\n\nTEST\n\n\n\n",
342+
"bitmap": "0b00"
343+
}
344+
]
345+
}"#;
323346

324347
#[test]
325348
fn test_serve_vmm_action_request() {
@@ -433,6 +456,30 @@ mod tests {
433456
assert_eq!(response.status(), StatusCode::BadRequest);
434457
}
435458

459+
#[test]
460+
fn test_handle_request_logging() {
461+
let cpu_template_json = TEST_UNESCAPED_JSON_TEMPLATE;
462+
let result = parse_put_cpu_config(&Body::new(cpu_template_json.as_bytes()));
463+
assert!(result.is_err());
464+
let result_error = result.unwrap_err();
465+
let err_msg = format!("{}", result_error);
466+
assert_ne!(
467+
1,
468+
err_msg.lines().count(),
469+
"Error Body response:\n{}",
470+
err_msg
471+
);
472+
473+
let err_msg_with_debug = format!("{:?}", result_error);
474+
// Check the loglines are on one line.
475+
assert_eq!(
476+
1,
477+
err_msg_with_debug.lines().count(),
478+
"Error Body response:\n{}",
479+
err_msg_with_debug
480+
);
481+
}
482+
436483
#[test]
437484
fn test_bind_and_run() {
438485
let mut tmp_socket = TempFile::new().unwrap();

0 commit comments

Comments
 (0)