Skip to content

Commit

Permalink
feat: return error when there are extraneous fields in the config file.
Browse files Browse the repository at this point in the history
When the config file is parsed, any field that is not known causes an error instead of being ignored.
  • Loading branch information
plusvic committed Jan 31, 2025
1 parent 22459db commit d3c4750
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
9 changes: 8 additions & 1 deletion cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ use strum_macros::{Display, EnumString};

/// Configuration structure for "yr" commands.
#[derive(Deserialize, Serialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct Config {
/// Format specific configuration information.
pub fmt: FormatConfig,

/// Check specific configuration information.
pub check: CheckConfig,
}

/// Format specific configuration information.
#[derive(Deserialize, Serialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct FormatConfig {
/// Rule specific formatting information.
pub rule: RuleFormatConfig,
Expand All @@ -32,6 +33,7 @@ pub struct FormatConfig {
/// Types allowed in the check.metadata table of the config file. Used to
/// require specific metadata identifiers have specific types by "yr check".
#[derive(Display, Deserialize, Serialize, Debug, Clone, EnumString)]
#[serde(deny_unknown_fields)]
pub enum MetaValueType {
/// Represents a String type
#[serde(rename = "string")]
Expand Down Expand Up @@ -69,6 +71,7 @@ pub enum MetaValueType {

/// Format specific configuration information.
#[derive(Deserialize, Serialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct CheckConfig {
/// Meta specific formatting information.
// Note: Using a BTreeMap here because we want a consistent ordering when
Expand All @@ -80,6 +83,7 @@ pub struct CheckConfig {
}

#[derive(Deserialize, Serialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct MetadataConfig {
#[serde(rename = "type")]
pub ty: MetaValueType,
Expand All @@ -89,6 +93,7 @@ pub struct MetadataConfig {

/// Rule specific formatting information.
#[derive(Deserialize, Serialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct RuleFormatConfig {
/// Indent section headers (meta, strings, condition).
pub indent_section_headers: bool,
Expand All @@ -106,13 +111,15 @@ pub struct RuleFormatConfig {

/// Meta specific formatting information.
#[derive(Deserialize, Serialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct MetaFormatConfig {
/// Align values to longest key.
pub align_values: bool,
}

/// Pattern specific formatting information.
#[derive(Deserialize, Serialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct PatternsFormatConfig {
/// Align patterns to the longest name.
pub align_values: bool,
Expand Down
29 changes: 29 additions & 0 deletions cli/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,35 @@ warning[text_as_hex]: hex pattern could be written as text literal
));
}

#[test]
fn cli_check_config_error() {
let temp_dir = TempDir::new().unwrap();
let config_file = temp_dir.child("config.toml");

config_file
.write_str(
r#"
[check.foo]
author = { type = "string" }
"#,
)
.unwrap();

Command::cargo_bin("yr")
.unwrap()
.arg("--config")
.arg(config_file.path())
.arg("check")
.arg("src/tests/testdata/foo.yar")
.assert()
.failure()
.stderr(
predicate::str::contains(
r#"error: unknown field: found `foo`, expected ``metadata` or `rule_name_regexp`` for key "default.check.foo""#,
)
);
}

#[test]
fn cli_fmt() {
let temp_dir = TempDir::new().unwrap();
Expand Down

0 comments on commit d3c4750

Please sign in to comment.