Skip to content

Commit d38b59a

Browse files
Alexandra Iordacheandreeaflorescu
authored andcommitted
api: deny unknown fields in requests
Use serde's deny_unknown_fields feature to cause an error when attempting to deserialize requests with invalid fields in the json payload. Fixes #359 Signed-off-by: Alexandra Iordache <[email protected]>
1 parent d48d9a5 commit d38b59a

File tree

8 files changed

+12
-2
lines changed

8 files changed

+12
-2
lines changed

api_server/src/request/async/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub enum DeviceType {
3232

3333
// Represents the associated json block from the async request body.
3434
#[derive(Debug, Deserialize, Serialize)]
35+
#[serde(deny_unknown_fields)]
3536
struct InstanceDeviceDetachAction {
3637
device_type: DeviceType,
3738
device_resource_id: String,
@@ -50,6 +51,7 @@ pub enum AsyncActionType {
5051
// The model of the json body from an async request. We use Serde to transform each associated
5152
// json body into this.
5253
#[derive(Debug, Deserialize, Serialize)]
54+
#[serde(deny_unknown_fields)]
5355
pub struct AsyncRequestBody {
5456
action_id: String,
5557
action_type: AsyncActionType,
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
1+
#[derive(Clone, Debug, PartialEq, Serialize)]
22
pub enum InstanceState {
33
Uninitialized,
44
Starting,
@@ -8,7 +8,7 @@ pub enum InstanceState {
88
}
99

1010
// This struct represents the strongly typed equivalent of the json body of InstanceInfo
11-
#[derive(Debug, Deserialize, Serialize)]
11+
#[derive(Debug, Serialize)]
1212
pub struct InstanceInfo {
1313
pub state: InstanceState,
1414
}

api_server/src/request/sync/boot_source.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ pub enum BootSourceType {
1313
}
1414

1515
#[derive(Debug, Deserialize, PartialEq, Serialize)]
16+
#[serde(deny_unknown_fields)]
1617
pub struct LocalImage {
1718
pub kernel_image_path: String,
1819
}
1920

2021
#[derive(Debug, Deserialize, PartialEq, Serialize)]
22+
#[serde(deny_unknown_fields)]
2123
pub struct BootSourceBody {
2224
boot_source_id: String,
2325
source_type: BootSourceType,

api_server/src/request/sync/drive.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub enum DrivePermissions {
1818
// This struct represents the strongly typed equivalent of the json body from drive
1919
// related requests.
2020
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
21+
#[serde(deny_unknown_fields)]
2122
pub struct DriveDescription {
2223
pub drive_id: String,
2324
pub path_on_host: String,

api_server/src/request/sync/logger.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub enum APILoggerLevel {
1616
}
1717

1818
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
19+
#[serde(deny_unknown_fields)]
1920
pub struct APILoggerDescription {
2021
pub path: String,
2122
#[serde(skip_serializing_if = "Option::is_none")]

api_server/src/request/sync/net.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use request::ParsedRequest;
1010
// This struct represents the strongly typed equivalent of the json body from net iface
1111
// related requests.
1212
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
13+
#[serde(deny_unknown_fields)]
1314
pub struct NetworkInterfaceBody {
1415
pub iface_id: String,
1516
pub state: DeviceState,

api_server/src/request/sync/rate_limiter.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use fc_util::ratelimiter::RateLimiter;
44

55
// This struct represents the strongly typed equivalent of the json body for TokenBucket
66
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
7+
#[serde(deny_unknown_fields)]
78
pub struct TokenBucketDescription {
89
pub size: u64,
910
pub refill_time: u64,
@@ -20,6 +21,7 @@ impl Default for TokenBucketDescription {
2021

2122
// This struct represents the strongly typed equivalent of the json body for RateLimiter
2223
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
24+
#[serde(deny_unknown_fields)]
2325
pub struct RateLimiterDescription {
2426
#[serde(skip_serializing_if = "Option::is_none")]
2527
pub bandwidth: Option<TokenBucketDescription>,

data_model/src/vm/machine_config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::fmt;
22

33
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
4+
#[serde(deny_unknown_fields)]
45
pub struct MachineConfiguration {
56
#[serde(skip_serializing_if = "Option::is_none")]
67
pub vcpu_count: Option<u8>,

0 commit comments

Comments
 (0)