Skip to content

Commit 2520e52

Browse files
committed
fix(config): close date-bound validation gaps
Validate raw config values so non-string since and until entries cannot be silently ignored, and keep command detection aligned with root options that take values. Add regressions for command-specific config validation behind --last and --sections and invalid non-string bounds.
1 parent bf54821 commit 2520e52

2 files changed

Lines changed: 72 additions & 13 deletions

File tree

‎rust/crates/ccusage-cli/src/types.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@ mod tests {
358358
fn rejects_undocumented_spellings() {
359359
assert_eq!(normalize_date_bound("abc"), None);
360360
assert_eq!(normalize_date_bound(""), None);
361+
assert_eq!(normalize_date_bound("2026"), None);
362+
assert_eq!(normalize_date_bound("202601"), None);
361363
assert_eq!(normalize_date_bound("2026/07/10"), None);
362364
assert_eq!(normalize_date_bound("2026-7-10"), None);
363365
assert_eq!(normalize_date_bound("2026-07-10T00:00:00Z"), None);

‎rust/crates/ccusage-config/src/config.rs‎

Lines changed: 70 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,7 @@ impl ConfigContext {
6767
/// so the check runs here and surfaces through `config_error` for every
6868
/// command that applies shared options, not just the pi-store readers.
6969
fn detect_date_bound_error(&self) -> Option<String> {
70-
self.option_maps().into_iter().find_map(|options| {
71-
let options = SharedOptions::from_map(options);
72-
[("since", options.since), ("until", options.until)]
73-
.into_iter()
74-
.find_map(|(key, value)| {
75-
let value = value?;
76-
normalize_date_bound(&value).is_none().then(|| {
77-
config_error(format!(
78-
"{key} '{value}' is not a valid date. Expected {DATE_BOUND_FORMATS}"
79-
))
80-
})
81-
})
82-
})
70+
self.option_maps().into_iter().find_map(date_bound_error)
8371
}
8472

8573
fn option_maps(&self) -> Vec<&Map<String, Value>> {
@@ -353,6 +341,7 @@ fn option_takes_value(arg: &str) -> bool {
353341
"-s" | "--since"
354342
| "-u"
355343
| "--until"
344+
| "--last"
356345
| "-m"
357346
| "--mode"
358347
| "--debug-samples"
@@ -380,9 +369,31 @@ fn option_takes_value(arg: &str) -> bool {
380369
| "--refresh-interval"
381370
| "--context-low-threshold"
382371
| "--context-medium-threshold"
372+
| "--sections"
383373
)
384374
}
385375

376+
fn date_bound_error(options: &Map<String, Value>) -> Option<String> {
377+
[
378+
("since", options.get("since")),
379+
("until", options.get("until")),
380+
]
381+
.into_iter()
382+
.find_map(|(key, value)| {
383+
let value = value?;
384+
let Some(value) = value.as_str() else {
385+
return Some(config_error(format!(
386+
"{key} must be a string. Expected {DATE_BOUND_FORMATS}"
387+
)));
388+
};
389+
normalize_date_bound(value).is_none().then(|| {
390+
config_error(format!(
391+
"{key} '{value}' is not a valid date. Expected {DATE_BOUND_FORMATS}"
392+
))
393+
})
394+
})
395+
}
396+
386397
fn is_agent_command(command: &str) -> bool {
387398
ccusage_core::BUILT_IN_AGENT_NAMES.contains(&command)
388399
}
@@ -1220,6 +1231,23 @@ mod tests {
12201231
);
12211232
}
12221233

1234+
#[test]
1235+
fn rejects_config_date_bounds_that_are_not_strings() {
1236+
let config = config_context_from_json(r#"{ "defaults": { "since": 20260710 } }"#);
1237+
1238+
assert_eq!(
1239+
config.config_error(),
1240+
Some("Invalid ccusage config: since must be a string. Expected YYYY-MM-DD or YYYYMMDD")
1241+
);
1242+
1243+
let config = config_context_from_json(r#"{ "defaults": { "until": null } }"#);
1244+
1245+
assert_eq!(
1246+
config.config_error(),
1247+
Some("Invalid ccusage config: until must be a string. Expected YYYY-MM-DD or YYYYMMDD")
1248+
);
1249+
}
1250+
12231251
#[test]
12241252
fn rejects_config_date_bounds_for_commands_without_named_pi_stores() {
12251253
for command in [
@@ -1273,6 +1301,31 @@ mod tests {
12731301
assert_eq!(shared.until.as_deref(), Some("20260131"));
12741302
}
12751303

1304+
#[test]
1305+
fn detects_command_specific_date_bounds_after_root_value_options() {
1306+
let config = config_context_for_args(
1307+
r#"{ "commands": { "weekly": { "since": "not-a-date" } } }"#,
1308+
&["--last", "1", "weekly"],
1309+
);
1310+
assert_eq!(
1311+
config.config_error(),
1312+
Some(
1313+
"Invalid ccusage config: since 'not-a-date' is not a valid date. Expected YYYY-MM-DD or YYYYMMDD"
1314+
)
1315+
);
1316+
1317+
let config = config_context_for_args(
1318+
r#"{ "commands": { "daily": { "until": "not-a-date" } } }"#,
1319+
&["--sections", "monthly,session"],
1320+
);
1321+
assert_eq!(
1322+
config.config_error(),
1323+
Some(
1324+
"Invalid ccusage config: until 'not-a-date' is not a valid date. Expected YYYY-MM-DD or YYYYMMDD"
1325+
)
1326+
);
1327+
}
1328+
12761329
fn context(value: Value, raw: &str, agent: Option<&str>, report: &str) -> ConfigContext {
12771330
ConfigContext {
12781331
value: Some(value),
@@ -1292,6 +1345,10 @@ mod tests {
12921345
}
12931346

12941347
fn config_context_for_command(raw: &str, command: &[&str]) -> ConfigContext {
1348+
config_context_for_args(raw, command)
1349+
}
1350+
1351+
fn config_context_for_args(raw: &str, command: &[&str]) -> ConfigContext {
12951352
let fixture = fs_fixture!({
12961353
"ccusage.json": raw,
12971354
});

0 commit comments

Comments
 (0)