Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion crates/cmtraceopen-parser/src/intune/event_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ fn extract_appworkload_event(
.map(|m| m.as_str().to_string());
let short = guid
.as_deref()
.map(|g| &g[..8.min(g.len())])
.map(|g| utf8_prefix(g, 8))
.unwrap_or("unknown");
let phase = if flags.sidecar_script_complete {
"Complete"
Expand Down Expand Up @@ -1494,6 +1494,13 @@ fn short_guid(value: &str) -> &str {
value
}

fn utf8_prefix(value: &str, max_chars: usize) -> &str {
value
.char_indices()
.nth(max_chars)
.map_or(value, |(byte_index, _)| &value[..byte_index])
}

fn contains_case_insensitive(value: &str, needle: &str) -> bool {
contains_ascii_case_insensitive(value, needle)
}
Expand Down Expand Up @@ -2033,4 +2040,29 @@ mod tests {
assert_eq!(events[0].status, IntuneStatus::Success);
assert_eq!(events[0].detail, message);
}

#[test]
fn sidecar_non_guid_multibyte_app_id_is_not_an_identity() {
let events = extract_events(
&[line(
r#"SidecarScriptDetectionManager launch {"AppId":"aaaaaa你好"}"#,
"01-15-2024 10:00:05.000",
1,
)],
"C:/Logs/AppWorkload.log",
&empty_registry(),
);

assert_eq!(events.len(), 1);
assert_eq!(events[0].event_type, IntuneEventType::PowerShellScript);
assert_eq!(events[0].guid, None);
assert_eq!(events[0].name, "Script Detection Running (unknown)");
}

#[test]
fn utf8_prefix_counts_characters_without_splitting_them() {
assert_eq!(utf8_prefix("aaaaaa你好世界", 8), "aaaaaa你好");
assert_eq!(utf8_prefix("a1b2c3d4-e5f6", 8), "a1b2c3d4");
assert_eq!(utf8_prefix("short", 8), "short");
}
}
81 changes: 70 additions & 11 deletions crates/cmtraceopen-parser/src/intune/guid_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ use std::sync::OnceLock;

pub(crate) fn app_id_json_re() -> &'static Regex {
static CELL: OnceLock<Regex> = OnceLock::new();
CELL.get_or_init(|| Regex::new(r#"\"AppId\"\s*:\s*\"([0-9a-fA-F-]{36})\""#).unwrap())
CELL.get_or_init(|| {
Regex::new(
r#"\"AppId\"\s*:\s*\"([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})\""#,
)
.unwrap()
})
}
pub(crate) fn app_name_json_re() -> &'static Regex {
static CELL: OnceLock<Regex> = OnceLock::new();
Expand Down Expand Up @@ -282,17 +287,17 @@ fn extract_all_field_values(msg: &str, prefix: &str, suffix: &str) -> Vec<String
/// GUID regex when a name field is also present on the same line.
pub(crate) fn extract_app_id(msg: &str) -> Option<String> {
// Try "AppId" — direct and escaped JSON
if let Some(value) = extract_json_field(msg, "\"AppId\":\"", "\"") {
return Some(value.to_string());
if let Some(value) = extract_guid_field(msg, "\"AppId\":\"", "\"") {
return Some(value);
}
if let Some(value) = extract_json_field(msg, "\\\"AppId\\\":\\\"", "\\\"") {
return Some(value.to_string());
if let Some(value) = extract_guid_field(msg, "\\\"AppId\\\":\\\"", "\\\"") {
return Some(value);
}
// Try "Id" — appears in policy payloads like Get policies = [{"Id":"<GUID>","Name":"..."}]
if let Some(value) = extract_guid_from_id_field(msg, "\"Id\":\"", "\"") {
if let Some(value) = extract_guid_field(msg, "\"Id\":\"", "\"") {
return Some(value);
}
if let Some(value) = extract_guid_from_id_field(msg, "\\\"Id\\\":\\\"", "\\\"") {
if let Some(value) = extract_guid_field(msg, "\\\"Id\\\":\\\"", "\\\"") {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
return Some(value);
}
// Try regex for "AppId" specifically
Expand All @@ -314,10 +319,8 @@ pub(crate) fn extract_app_id(msg: &str) -> Option<String> {
})
}

/// Extract a GUID from an `"Id"` field, validating it looks like a UUID.
/// This is more conservative than `extract_json_field` alone because `"Id"`
/// is a very generic key — we only accept values that are 36-char UUIDs.
fn extract_guid_from_id_field(msg: &str, prefix: &str, suffix: &str) -> Option<String> {
/// Extract a GUID from an identity field, rejecting arbitrary log content.
fn extract_guid_field(msg: &str, prefix: &str, suffix: &str) -> Option<String> {
let value = extract_json_field(msg, prefix, suffix)?;
if value.len() == 36 && guid_re().is_match(value) {
Some(value.to_string())
Expand Down Expand Up @@ -783,4 +786,60 @@ mod tests {
Some("SetUpFilePath")
);
}

#[test]
fn app_id_fields_only_supply_valid_guid_identities() {
let valid_guid = "a1b2c3d4-e5f6-7890-abcd-ef1234567890";

assert_eq!(
extract_app_id(&format!(r#"launch {{"AppId":"{valid_guid}"}}"#)),
Some(valid_guid.to_string())
);
assert_eq!(extract_app_id(r#"launch {"AppId":"script-你好"}"#), None);
assert_eq!(
extract_app_id(r#"launch {"AppId":"zzzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz"}"#),
None
);

let mut registry = GuidRegistry::new();
registry.ingest_lines(&[line(
r#"Processing app: {"AppId":"script-你好","ApplicationName":"Contoso Script"}"#,
)]);
assert!(registry.is_empty());
}

#[test]
fn spaced_app_id_fallback_rejects_malformed_guid_shapes() {
let malformed_values = [
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"a1b2c3d-e5f67-890a-abcd-ef1234567890",
];

for value in malformed_values {
let message = format!(
r#"Processing app: {{"AppId" : "{value}","ApplicationName":"Contoso App"}}"#
);
assert_eq!(extract_app_id(&message), None, "accepted {value}");

let mut registry = GuidRegistry::new();
registry.ingest_lines(&[line(&message)]);
assert!(registry.is_empty(), "registered {value}");
}
}

#[test]
fn spaced_app_id_and_named_context_guid_fallback_remain_supported() {
let valid_guid = "a1b2c3d4-e5f6-7890-abcd-ef1234567890";

assert_eq!(
extract_app_id(&format!(r#"launch {{"AppId" : "{valid_guid}"}}"#)),
Some(valid_guid.to_string())
);
assert_eq!(
extract_app_id(&format!(
r#"Processing identity {valid_guid} for {{"ApplicationName":"Contoso App"}}"#
)),
Some(valid_guid.to_string())
);
}
}