Skip to content

Commit

Permalink
Problem: config allows arbitrary keys to be passed
Browse files Browse the repository at this point in the history
This means that a misspelled config field can cause a lot
of confusion -- things won't work as expected.

Solution: restore more restrictive config deserialization
where unknown keys would trigger an error
  • Loading branch information
yrashk committed Jun 1, 2018
1 parent b46ab21 commit e9df5e0
Showing 1 changed file with 3 additions and 43 deletions.
46 changes: 3 additions & 43 deletions bridge/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ mod load {
use web3::types::Address;

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Config {
pub home: Node,
pub foreign: Node,
Expand All @@ -263,6 +264,7 @@ mod load {
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Node {
pub account: Address,
#[cfg(feature = "deploy")]
Expand All @@ -280,6 +282,7 @@ mod load {
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Transactions {
#[cfg(feature = "deploy")]
pub home_deploy: Option<TransactionConfig>,
Expand Down Expand Up @@ -328,7 +331,6 @@ mod tests {
fn load_full_setup_from_str() {
let toml = r#"
keystore = "/keys"
estimated_gas_cost_of_withdraw = 100000
[home]
account = "0x1B68Cb0B50181FC4006Ce572cF346e596E51818b"
Expand All @@ -338,18 +340,12 @@ rpc_host = "127.0.0.1"
rpc_port = 8545
password = "password"
[home.contract]
bin = "../compiled_contracts/HomeBridge.bin"
[foreign]
account = "0x0000000000000000000000000000000000000001"
rpc_host = "127.0.0.1"
rpc_port = 8545
password = "password"
[foreign.contract]
bin = "../compiled_contracts/ForeignBridge.bin"
[authorities]
accounts = [
"0x0000000000000000000000000000000000000001",
Expand All @@ -359,18 +355,13 @@ accounts = [
required_signatures = 2
[transactions]
home_deploy = { gas = 20 }
"#;

#[allow(unused_mut)]
let mut expected = Config {
txs: Transactions::default(),
home: Node {
account: "1B68Cb0B50181FC4006Ce572cF346e596E51818b".into(),
#[cfg(feature = "deploy")]
contract: ContractConfig {
bin: include_str!("../../compiled_contracts/HomeBridge.bin").from_hex().unwrap().into(),
},
poll_interval: Duration::from_secs(2),
request_timeout: Duration::from_secs(DEFAULT_TIMEOUT),
required_confirmations: 100,
Expand All @@ -385,10 +376,6 @@ home_deploy = { gas = 20 }
},
foreign: Node {
account: "0000000000000000000000000000000000000001".into(),
#[cfg(feature = "deploy")]
contract: ContractConfig {
bin: include_str!("../../compiled_contracts/ForeignBridge.bin").from_hex().unwrap().into(),
},
poll_interval: Duration::from_secs(1),
request_timeout: Duration::from_secs(DEFAULT_TIMEOUT),
required_confirmations: 12,
Expand All @@ -409,19 +396,9 @@ home_deploy = { gas = 20 }
],
required_signatures: 2,
},
#[cfg(feature = "deploy")]
estimated_gas_cost_of_withdraw: 100_000,
keystore: "/keys/".into(),
};

#[cfg(feature = "deploy")] {
expected.txs.home_deploy = TransactionConfig {
gas: 20,
gas_price: 0,
concurrency: DEFAULT_CONCURRENCY,
};
}

let config = Config::load_from_str(toml).unwrap();
assert_eq!(expected, config);
}
Expand All @@ -430,24 +407,17 @@ home_deploy = { gas = 20 }
fn load_minimal_setup_from_str() {
let toml = r#"
keystore = "/keys/"
estimated_gas_cost_of_withdraw = 200000000
[home]
account = "0x1B68Cb0B50181FC4006Ce572cF346e596E51818b"
rpc_host = ""
password = "password"
[home.contract]
bin = "../compiled_contracts/HomeBridge.bin"
[foreign]
account = "0x0000000000000000000000000000000000000001"
rpc_host = ""
password = "password"
[foreign.contract]
bin = "../compiled_contracts/ForeignBridge.bin"
[authorities]
accounts = [
"0x0000000000000000000000000000000000000001",
Expand All @@ -460,10 +430,6 @@ required_signatures = 2
txs: Transactions::default(),
home: Node {
account: "1B68Cb0B50181FC4006Ce572cF346e596E51818b".into(),
#[cfg(feature = "deploy")]
contract: ContractConfig {
bin: include_str!("../../compiled_contracts/HomeBridge.bin").from_hex().unwrap().into(),
},
poll_interval: Duration::from_secs(1),
request_timeout: Duration::from_secs(DEFAULT_TIMEOUT),
required_confirmations: 12,
Expand All @@ -478,10 +444,6 @@ required_signatures = 2
},
foreign: Node {
account: "0000000000000000000000000000000000000001".into(),
#[cfg(feature = "deploy")]
contract: ContractConfig {
bin: include_str!("../../compiled_contracts/ForeignBridge.bin").from_hex().unwrap().into(),
},
poll_interval: Duration::from_secs(1),
request_timeout: Duration::from_secs(DEFAULT_TIMEOUT),
required_confirmations: 12,
Expand All @@ -502,8 +464,6 @@ required_signatures = 2
],
required_signatures: 2,
},
#[cfg(feature = "deploy")]
estimated_gas_cost_of_withdraw: 200_000_000,
keystore: "/keys/".into(),
};

Expand Down

0 comments on commit e9df5e0

Please sign in to comment.