Skip to content

Commit 4f15e97

Browse files
authored
Reject STELLAR_SECRET_KEY when --secure-store is requested. (#2504)
1 parent 5e090c0 commit 4f15e97

2 files changed

Lines changed: 43 additions & 3 deletions

File tree

  • cmd

cmd/crates/soroban-test/tests/it/config.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,34 @@ fn seed_phrase() {
194194
.stdout(predicates::str::contains("test_seed\n"));
195195
}
196196

197+
#[test]
198+
fn secure_store_rejects_env_secret_key() {
199+
let sandbox = TestEnv::default();
200+
201+
// --secure-store with STELLAR_SECRET_KEY set should be rejected rather than
202+
// silently writing the raw secret to a plaintext identity file.
203+
sandbox
204+
.new_assert_cmd("keys")
205+
.env(
206+
"STELLAR_SECRET_KEY",
207+
"SDIY6AQQ75WMD4W46EYB7O6UYMHOCGQHLAQGQTKHDX4J2DYQCHVCQYFD",
208+
)
209+
.arg("add")
210+
.arg("alice")
211+
.arg("--secure-store")
212+
.assert()
213+
.failure()
214+
.stderr(predicate::str::contains(
215+
"--secure-store only supports seed phrases",
216+
));
217+
218+
// The identity file must not exist — no plaintext fallback.
219+
assert!(
220+
!sandbox.config_dir().join("identity/alice.toml").exists(),
221+
"identity file should not be created when --secure-store is rejected"
222+
);
223+
}
224+
197225
#[test]
198226
fn use_env() {
199227
let sandbox = TestEnv::default();

cmd/soroban-cli/src/commands/keys/add.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ pub enum Error {
3333

3434
#[error("An identity with the name '{0}' already exists")]
3535
IdentityAlreadyExists(String),
36+
37+
#[error(
38+
"--secure-store only supports seed phrases; \
39+
unset STELLAR_SECRET_KEY or provide a seed phrase instead"
40+
)]
41+
SecureStoreRequiresSeedPhrase,
3642
}
3743

3844
#[derive(Debug, clap::Parser, Clone)]
@@ -82,9 +88,15 @@ impl Cmd {
8288
}
8389

8490
fn read_secret(&self, print: &Print) -> Result<Secret, Error> {
85-
if let Ok(secret_key) = std::env::var("STELLAR_SECRET_KEY") {
86-
Ok(Secret::SecretKey { secret_key })
87-
} else if self.secrets.secure_store {
91+
if self.secrets.secure_store {
92+
if std::env::var("STELLAR_SECRET_KEY").is_ok() {
93+
return Err(Error::SecureStoreRequiresSeedPhrase);
94+
}
95+
} else if let Ok(secret_key) = std::env::var("STELLAR_SECRET_KEY") {
96+
return Ok(Secret::SecretKey { secret_key });
97+
}
98+
99+
if self.secrets.secure_store {
88100
let prompt = "Type a 12/24 word seed phrase:";
89101
let secret_key = read_password(print, prompt)?;
90102
if secret_key.split_whitespace().count() < 24 {

0 commit comments

Comments
 (0)