Skip to content

Commit 7625fb2

Browse files
committed
chore: add DecoderConfig when decoding candid in BN code
1 parent bb0ac2e commit 7625fb2

File tree

8 files changed

+68
-22
lines changed

8 files changed

+68
-22
lines changed

rs/boundary_node/certificate_issuance/certificate_issuer/src/certificate.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use mockall::automock;
1010
use serde::Serialize;
1111

1212
use crate::{
13+
decoder_config,
1314
encode::{Decode, Encode},
1415
verification::Verify,
1516
};
@@ -102,7 +103,8 @@ impl GetCert for CanisterCertGetter {
102103
.await
103104
.context("failed to query canister")?;
104105

105-
let resp = Decode!(&resp, Response).context("failed to decode canister response")?;
106+
let resp = Decode!([decoder_config()]; &resp, Response)
107+
.context("failed to decode canister response")?;
106108

107109
match resp {
108110
Response::Ok(enc_pair) => Ok(Pair(
@@ -154,7 +156,8 @@ impl Upload for CanisterUploader {
154156
.await
155157
.context("failed to query canister")?;
156158

157-
let resp = Decode!(&resp, Response).context("failed to decode canister response")?;
159+
let resp = Decode!([decoder_config()]; &resp, Response)
160+
.context("failed to decode canister response")?;
158161

159162
match resp {
160163
Response::Ok(()) => Ok(()),
@@ -199,7 +202,8 @@ impl Export for CanisterExporter {
199202
.await
200203
.context("failed to query canister")?;
201204

202-
let resp = Decode!(&resp, Response).context("failed to decode canister response")?;
205+
let resp = Decode!([decoder_config()]; &resp, Response)
206+
.context("failed to decode canister response")?;
203207

204208
match resp {
205209
Response::Ok((pkgs, iccert)) => Ok((

rs/boundary_node/certificate_issuance/certificate_issuer/src/main.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use axum::{
2020
routing::{delete, get, post, put},
2121
Extension, Router, Server,
2222
};
23-
use candid::Principal;
23+
use candid::{DecoderConfig, Principal};
2424
use chacha20poly1305::{KeyInit, XChaCha20Poly1305};
2525
use clap::Parser;
2626
use futures::future::TryFutureExt;
@@ -82,6 +82,17 @@ const SERVICE_NAME: &str = "certificate-issuer";
8282
pub(crate) static TASK_DELAY_SEC: AtomicU64 = AtomicU64::new(60);
8383
pub(crate) static TASK_ERROR_DELAY_SEC: AtomicU64 = AtomicU64::new(10 * 60);
8484

85+
/// Limit the amount of work for skipping unneeded data on the wire when parsing Candid.
86+
/// The value of 10_000 follows the Candid recommendation.
87+
const DEFAULT_SKIPPING_QUOTA: usize = 10_000;
88+
89+
pub(crate) fn decoder_config() -> DecoderConfig {
90+
let mut config = DecoderConfig::new();
91+
config.set_skipping_quota(DEFAULT_SKIPPING_QUOTA);
92+
config.set_full_error_message(false);
93+
config
94+
}
95+
8596
#[derive(Parser)]
8697
#[command(name = SERVICE_NAME)]
8798
struct Cli {

rs/boundary_node/certificate_issuance/certificate_issuer/src/registration.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use ic_agent::Agent;
88
use mockall::automock;
99
use serde::{Deserialize, Serialize};
1010

11+
use crate::decoder_config;
1112
use crate::work::ProcessError;
1213

1314
pub type Id = String;
@@ -169,7 +170,8 @@ impl Get for CanisterGetter {
169170
.await
170171
.context("failed to query canister")?;
171172

172-
let resp = Decode!(&resp, Response).context("failed to decode canister response")?;
173+
let resp = Decode!([decoder_config()]; &resp, Response)
174+
.context("failed to decode canister response")?;
173175

174176
match resp {
175177
Response::Ok(reg) => Ok(reg.into()),
@@ -199,7 +201,8 @@ impl Create for CanisterCreator {
199201
.await
200202
.context("failed to query canister")?;
201203

202-
let resp = Decode!(&resp, Response).context("failed to decode canister response")?;
204+
let resp = Decode!([decoder_config()]; &resp, Response)
205+
.context("failed to decode canister response")?;
203206

204207
match resp {
205208
Response::Ok(id) => Ok(id),
@@ -232,7 +235,8 @@ impl Update for CanisterUpdater {
232235
.await
233236
.context("failed to query canister")?;
234237

235-
let resp = Decode!(&resp, Response).context("failed to decode canister response")?;
238+
let resp = Decode!([decoder_config()]; &resp, Response)
239+
.context("failed to decode canister response")?;
236240

237241
match resp {
238242
Response::Ok(()) => Ok(()),
@@ -262,7 +266,8 @@ impl Remove for CanisterRemover {
262266
.await
263267
.context("failed to query canister")?;
264268

265-
let resp = Decode!(&resp, Response).context("failed to decode canister response")?;
269+
let resp = Decode!([decoder_config()]; &resp, Response)
270+
.context("failed to decode canister response")?;
266271

267272
match resp {
268273
Response::Ok(()) => Ok(()),

rs/boundary_node/certificate_issuance/certificate_issuer/src/work.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::{
1515
acme::{self, FinalizeError},
1616
certificate::{self, GetCert, GetCertError, Pair},
1717
check::Check,
18+
decoder_config,
1819
dns::{self, Resolve},
1920
registration::{Id, Registration, State},
2021
TASK_DELAY_SEC, TASK_ERROR_DELAY_SEC,
@@ -152,7 +153,8 @@ impl Queue for CanisterQueuer {
152153
.await
153154
.context("failed to query canister")?;
154155

155-
let resp = Decode!(&resp, Response).context("failed to decode canister response")?;
156+
let resp = Decode!([decoder_config()]; &resp, Response)
157+
.context("failed to decode canister response")?;
156158

157159
match resp {
158160
Response::Ok(()) => Ok(()),
@@ -182,7 +184,8 @@ impl Peek for CanisterPeeker {
182184
.await
183185
.context("failed to query canister")?;
184186

185-
let resp = Decode!(&resp, Response).context("failed to decode canister response")?;
187+
let resp = Decode!([decoder_config()]; &resp, Response)
188+
.context("failed to decode canister response")?;
186189

187190
match resp {
188191
Response::Ok(id) => Ok(id),
@@ -213,7 +216,8 @@ impl Dispense for CanisterDispenser {
213216
.await
214217
.context("failed to query canister")?;
215218

216-
let resp = Decode!(&resp, Response).context("failed to decode canister response")?;
219+
let resp = Decode!([decoder_config()]; &resp, Response)
220+
.context("failed to decode canister response")?;
217221

218222
match resp {
219223
Response::Ok(id) => Ok(id),
@@ -238,7 +242,8 @@ impl Dispense for CanisterDispenser {
238242
.await
239243
.context("failed to query canister")?;
240244

241-
let resp = Decode!(&resp, Response).context("failed to decode canister response")?;
245+
let resp = Decode!([decoder_config()]; &resp, Response)
246+
.context("failed to decode canister response")?;
242247

243248
match resp {
244249
Response::Ok(reg) => Ok(reg.into()),

rs/boundary_node/ic_balance_exporter/src/main.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::{
99
use anyhow::{anyhow, Context, Error};
1010
use async_trait::async_trait;
1111
use axum::{handler::Handler, routing::get, Extension, Router};
12-
use candid::{CandidType, Decode, Encode, Principal};
12+
use candid::{CandidType, Decode, DecoderConfig, Encode, Principal};
1313
use clap::Parser;
1414
use dashmap::DashMap;
1515
use futures::{future::TryFutureExt, stream::FuturesUnordered};
@@ -232,8 +232,15 @@ impl Scrape for Scraper {
232232
.await
233233
.context("failed to query canister")?;
234234

235+
// Limit the amount of work for skipping unneeded data on the wire when parsing Candid.
236+
// The value of 10_000 follows the Candid recommendation.
237+
const DEFAULT_SKIPPING_QUOTA: usize = 10_000;
238+
let mut config = DecoderConfig::new();
239+
config.set_skipping_quota(DEFAULT_SKIPPING_QUOTA);
240+
config.set_full_error_message(false);
241+
235242
let Amount { amount } =
236-
candid::Decode!(&result, Amount).context("failed to decode result")?;
243+
candid::Decode!([config]; &result, Amount).context("failed to decode result")?;
237244

238245
Ok(amount)
239246
}

rs/boundary_node/ic_boundary/src/core.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use axum::{
1919
};
2020
use axum_extra::middleware::option_layer;
2121
use axum_server::{accept::DefaultAcceptor, Server};
22+
use candid::DecoderConfig;
2223
use futures::TryFutureExt;
2324
use ic_interfaces_registry::ZERO_REGISTRY_VERSION;
2425
use ic_registry_client::client::RegistryClientImpl;
@@ -75,6 +76,17 @@ const METRICS_CACHE_CAPACITY: usize = 15 * MB;
7576

7677
pub const MANAGEMENT_CANISTER_ID_PRINCIPAL: CanisterId = CanisterId::ic_00();
7778

79+
/// Limit the amount of work for skipping unneeded data on the wire when parsing Candid.
80+
/// The value of 10_000 follows the Candid recommendation.
81+
const DEFAULT_SKIPPING_QUOTA: usize = 10_000;
82+
83+
pub fn decoder_config() -> DecoderConfig {
84+
let mut config = DecoderConfig::new();
85+
config.set_skipping_quota(DEFAULT_SKIPPING_QUOTA);
86+
config.set_full_error_message(false);
87+
config
88+
}
89+
7890
pub async fn main(cli: Cli) -> Result<(), Error> {
7991
if cli.listen.http_timeout_connect > cli.health.check_timeout {
8092
panic!("--check-timeout should be longer than --http-timeout-connect");

rs/boundary_node/ic_boundary/src/management.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use ratelimit::Ratelimiter;
1313
use serde::Deserialize;
1414

1515
use crate::{
16-
core::MANAGEMENT_CANISTER_ID_PRINCIPAL,
16+
core::{decoder_config, MANAGEMENT_CANISTER_ID_PRINCIPAL},
1717
routes::{ApiError, ErrorCause, RateLimitCause, RequestContext, RequestType},
1818
};
1919

@@ -88,7 +88,8 @@ struct BitcoinNetworkRecord {
8888

8989
fn extract_btc_network(ctx: &RequestContext) -> Result<BitcoinNetwork, Error> {
9090
let arg = ctx.arg.as_ref().ok_or_else(|| anyhow!("missing arg"))?;
91-
let r = Decode!(arg, BitcoinNetworkRecord).context("failed to decode arg")?;
91+
let r =
92+
Decode!([decoder_config()]; arg, BitcoinNetworkRecord).context("failed to decode arg")?;
9293

9394
Ok(r.network.into())
9495
}

rs/boundary_node/ic_boundary/src/routes.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use url::Url;
3636

3737
use crate::{
3838
cache::CacheStatus,
39-
core::MAX_REQUEST_BODY_SIZE,
39+
core::{decoder_config, MAX_REQUEST_BODY_SIZE},
4040
http::{read_streaming_body, reqwest_error_infer, HttpClient},
4141
persist::{RouteSubnet, Routes},
4242
retry::RetryResult,
@@ -579,11 +579,12 @@ pub async fn preprocess_request(
579579
let (arg, http_request) = match (&content.method_name, content.arg) {
580580
(Some(method), Some(arg)) => {
581581
if request_type == RequestType::Query && method == METHOD_HTTP {
582-
let mut req: HttpRequest = Decode!(&arg.0, HttpRequest).map_err(|err| {
583-
ErrorCause::UnableToParseHTTPArg(format!(
584-
"unable to decode arg as HttpRequest: {err}"
585-
))
586-
})?;
582+
let mut req: HttpRequest = Decode!([decoder_config()]; &arg.0, HttpRequest)
583+
.map_err(|err| {
584+
ErrorCause::UnableToParseHTTPArg(format!(
585+
"unable to decode arg as HttpRequest: {err}"
586+
))
587+
})?;
587588

588589
// Remove specific headers
589590
req.headers

0 commit comments

Comments
 (0)