Skip to content

Commit 06afa6f

Browse files
authored
feat: error when using insecure identity on mainnet (#4005)
* error when using insecure identity on mainnet no warning on non-mainnet (playground, local replica) * changelog * surpress the error in irrelevant tests * fix typo
1 parent a5e8144 commit 06afa6f

File tree

7 files changed

+43
-18
lines changed

7 files changed

+43
-18
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
# UNRELEASED
44

5+
### feat: error when using insecure identity on mainnet
6+
7+
This used to be a warning. A hard error can abort the command so that no insecure state will be on the mainnet.
8+
9+
Users can surpress this error by setting `export DFX_WARNING=-mainnet_plaintext_identity`.
10+
11+
The warning won't display when executing commands like `dfx deploy --playground`.
12+
513
# 0.24.3
614

715
### feat: Bitcoin support in PocketIC

e2e/tests-dfx/canister_url.bash

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ load ../utils/_
44

55
setup() {
66
standard_setup
7-
7+
# some of the tests run on mainnet with default plaintext identity
8+
# so we need to set this to avoid the error
9+
export DFX_WARNING=-mainnet_plaintext_identity
810
dfx_new_assets hello
911
}
1012

@@ -58,7 +60,7 @@ teardown() {
5860
echo "{}" > canister_ids.json
5961
jq '.hello_frontend.ic = "qsgof-4qaaa-aaaan-qekqq-cai"' canister_ids.json | sponge canister_ids.json
6062
frontend_id=$(dfx canister id hello_frontend --ic)
61-
63+
6264
assert_command dfx canister url hello_frontend --ic
6365
assert_match "https://${frontend_id}.icp0.io"
6466

e2e/tests-dfx/fabricate_cycles.bash

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ teardown() {
3939

4040
@test "ledger fabricate-cycles fails on real IC" {
4141
install_asset greet
42+
# without DFX_WARNING, the command would fail with different error (Failed to create AgentEnvironment...)
43+
export DFX_WARNING=-mainnet_plaintext_identity
4244
assert_command_fail dfx ledger fabricate-cycles --all --network ic
4345
assert_match "Cannot run this on the real IC."
4446
assert_command_fail dfx ledger fabricate-cycles --all --ic

e2e/tests-dfx/identity.bash

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,15 @@ teardown() {
186186
assert_eq '(blob "hello")' "$stdout"
187187
}
188188

189-
@test "using an unencrypted identity on mainnet provokes a warning" {
190-
assert_command dfx ledger balance --network ic
191-
assert_match "WARN: The default identity is not stored securely." "$stderr"
189+
@test "using an unencrypted identity on mainnet provokes a hard error which can be suppressed" {
190+
assert_command_fail dfx ledger balance --network ic
191+
assert_match "The default identity is not stored securely." "$stderr"
192192
assert_command "${BATS_TEST_DIRNAME}/../assets/expect_scripts/init_alice_with_pw.exp"
193193
assert_command "${BATS_TEST_DIRNAME}/../assets/expect_scripts/get_ledger_balance.exp"
194194
dfx identity new bob --storage-mode plaintext
195-
assert_command dfx ledger balance --network ic --identity bob
196-
assert_match "WARN: The bob identity is not stored securely." "$stderr"
197-
195+
assert_command_fail dfx ledger balance --network ic --identity bob
196+
assert_match "The bob identity is not stored securely." "$stderr"
197+
# can suppress the error
198198
export DFX_WARNING=-mainnet_plaintext_identity
199199
assert_command dfx ledger balance --network ic --identity bob
200200
assert_not_contains "not stored securely" "$stderr"

e2e/tests-dfx/network.bash

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,13 @@ teardown() {
9999

100100
assert_command_fail dfx diagnose --network ic
101101
assert_contains "The test_id identity is not stored securely."
102-
assert_contains "use it in mainnet-facing commands"
103-
assert_contains "No wallet found; nothing to do"
102+
assert_contains "in mainnet-facing commands"
103+
assert_contains "you can suppress this warning"
104104

105105
assert_command_fail dfx diagnose --ic
106106
assert_contains "The test_id identity is not stored securely."
107-
assert_contains "use it in mainnet-facing commands"
108-
assert_contains "No wallet found; nothing to do"
107+
assert_contains "in mainnet-facing commands"
108+
assert_contains "you can suppress this warning"
109109

110110
assert_command dfx diagnose
111111
assert_not_contains "identity is not stored securely"

e2e/tests-dfx/sign_send.bash

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ teardown() {
4444
cd "$E2E_TEMP_DIR"
4545
mkdir not-a-project-dir
4646
cd not-a-project-dir
47-
47+
# suppress the error
48+
export DFX_WARNING=-mainnet_plaintext_identity
4849
assert_command dfx canister sign --query rwlgt-iiaaa-aaaaa-aaaaa-cai read --network ic
4950
assert_match "Query message generated at \[message.json\]"
5051
}

src/dfx/src/lib/environment.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ use crate::config::dfx_version;
33
use crate::lib::error::DfxResult;
44
use crate::lib::progress_bar::ProgressBar;
55
use crate::lib::warning::{is_warning_disabled, DfxWarning::MainnetPlainTextIdentity};
6-
use anyhow::anyhow;
6+
use anyhow::{anyhow, bail};
77
use candid::Principal;
88
use dfx_core::config::cache::Cache;
99
use dfx_core::config::model::canister_id_store::CanisterIdStore;
1010
use dfx_core::config::model::dfinity::{Config, NetworksConfig};
11-
use dfx_core::config::model::network_descriptor::NetworkDescriptor;
11+
use dfx_core::config::model::network_descriptor::{NetworkDescriptor, NetworkTypeDescriptor};
1212
use dfx_core::error::canister_id_store::CanisterIdStoreError;
1313
use dfx_core::error::identity::NewIdentityManagerError;
1414
use dfx_core::error::load_dfx_config::LoadDfxConfigError;
@@ -17,7 +17,7 @@ use dfx_core::identity::identity_manager::{IdentityManager, InitializeIdentity};
1717
use fn_error_context::context;
1818
use ic_agent::{Agent, Identity};
1919
use semver::Version;
20-
use slog::{warn, Logger, Record};
20+
use slog::{Logger, Record};
2121
use std::borrow::Cow;
2222
use std::cell::RefCell;
2323
use std::path::PathBuf;
@@ -288,11 +288,23 @@ impl<'a> AgentEnvironment<'a> {
288288
identity_manager.instantiate_selected_identity(&logger)?
289289
};
290290
if network_descriptor.is_ic
291+
&& !matches!(
292+
network_descriptor.r#type,
293+
NetworkTypeDescriptor::Playground { .. }
294+
)
291295
&& identity.insecure
292296
&& !is_warning_disabled(MainnetPlainTextIdentity)
293297
{
294-
warn!(logger, "The {} identity is not stored securely. Do not use it to control a lot of cycles/ICP. Create a new identity with `dfx identity new` \
295-
and use it in mainnet-facing commands with the `--identity` flag", identity.name());
298+
bail!(
299+
"The {} identity is not stored securely. Do not use it to control a lot of cycles/ICP.
300+
- For enhanced security, create a new identity using the command:
301+
dfx identity new
302+
Then, specify the new identity in mainnet-facing commands with the `--identity` flag.
303+
- If you understand the risks and still wish to use the insecure plaintext identity, you can suppress this warning by running:
304+
export DFX_WARNING=-mainnet_plaintext_identity
305+
After setting this environment variable, re-run the command.",
306+
identity.name()
307+
);
296308
}
297309
let url = network_descriptor.first_provider()?;
298310
let effective_canister_id = if let Some(d) = &network_descriptor.local_server_descriptor {

0 commit comments

Comments
 (0)