Skip to content

Commit 1c52641

Browse files
committed
Strip YAML inline comments in config parser
The ad-hoc YAML line parser in chain_spec_from_dir did not handle inline comments (e.g. `CONFIG_NAME: testnet # ephemery`), causing trailing comment text to leak into parsed values. Add a yaml_value helper that strips ` #`-prefixed comments before returning the value.
1 parent 82f27e8 commit 1c52641

1 file changed

Lines changed: 34 additions & 2 deletions

File tree

crates/core/src/application/validator/deposit.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ pub fn chain_spec_for_network(name: &str) -> Option<ChainSpec> {
4848
})
4949
}
5050

51+
/// Extracts the scalar value from a YAML `KEY: value # comment` line,
52+
/// stripping inline comments and surrounding whitespace.
53+
fn yaml_value(raw: &str) -> &str {
54+
let trimmed = raw.trim();
55+
// YAML inline comments start with ` #` (space then hash).
56+
match trimmed.find(" #") {
57+
Some(pos) => trimmed[..pos].trim_end(),
58+
None => trimmed,
59+
}
60+
}
61+
5162
/// Loads a [`ChainSpec`] from a directory containing a `config.yaml` file.
5263
///
5364
/// Only reads `GENESIS_FORK_VERSION` and `CONFIG_NAME` — everything else is ignored.
@@ -66,7 +77,7 @@ pub fn chain_spec_from_dir(path: &Path) -> Result<ChainSpec> {
6677
for line in contents.lines() {
6778
let line = line.trim();
6879
if let Some(value) = line.strip_prefix("GENESIS_FORK_VERSION:") {
69-
let hex = value.trim().trim_start_matches("0x");
80+
let hex = yaml_value(value).trim_start_matches("0x");
7081
let bytes = hex::decode(hex)
7182
.map_err(|e| eyre!("Invalid GENESIS_FORK_VERSION hex: {e}"))?;
7283
genesis_fork_version = Some(
@@ -75,7 +86,7 @@ pub fn chain_spec_from_dir(path: &Path) -> Result<ChainSpec> {
7586
.map_err(|_| eyre!("GENESIS_FORK_VERSION must be exactly 4 bytes"))?,
7687
);
7788
} else if let Some(value) = line.strip_prefix("CONFIG_NAME:") {
78-
config_name = Some(value.trim().to_string());
89+
config_name = Some(yaml_value(value).to_string());
7990
}
8091
}
8192

@@ -320,6 +331,27 @@ mod tests {
320331
assert_eq!(&creds[12..], addr.as_slice());
321332
}
322333

334+
#[test]
335+
fn yaml_value_strips_inline_comments() {
336+
assert_eq!(yaml_value(" testnet # ephemery rotation"), "testnet");
337+
assert_eq!(yaml_value(" 0x10000910 # hoodi"), "0x10000910");
338+
assert_eq!(yaml_value(" plain_value"), "plain_value");
339+
assert_eq!(yaml_value(" hash#tag"), "hash#tag"); // no space before #
340+
}
341+
342+
#[test]
343+
fn chain_spec_from_dir_strips_yaml_comments() {
344+
let tmp = tempfile::tempdir().unwrap();
345+
std::fs::write(
346+
tmp.path().join("config.yaml"),
347+
"GENESIS_FORK_VERSION: 0x10000910 # hoodi\nCONFIG_NAME: testnet # ephemery\n",
348+
)
349+
.unwrap();
350+
let spec = chain_spec_from_dir(tmp.path()).unwrap();
351+
assert_eq!(spec.genesis_fork_version, [0x10, 0x00, 0x09, 0x10]);
352+
assert_eq!(spec.config_name.as_deref(), Some("testnet"));
353+
}
354+
323355
#[test]
324356
fn signing_root_is_sha256_of_root_and_domain() {
325357
let root = [0xAA; 32];

0 commit comments

Comments
 (0)