Skip to content

Commit 623512e

Browse files
committed
fix(prover): match whole middle-label host wildcards like the runtime
The prover's host-pattern overlap only wildcard-matched the first DNS label and compared the remaining labels literally, so it missed overlap for the whole middle-label wildcards this PR added to the aws-s3 profile (e.g. *.s3.*.amazonaws.com). It therefore under-reported credential-associated policy risk (CWE-693). Match label-for-label instead: a * (or partial * glob) matches exactly one label in any position, literal labels compare literally, and a leading ** still matches one or more labels. Cover matching and non-matching label counts in tests. Signed-off-by: Russell Bryant <[email protected]>
1 parent 6c58d2a commit 623512e

1 file changed

Lines changed: 56 additions & 15 deletions

File tree

‎crates/openshell-prover/src/credentials.rs‎

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -186,26 +186,39 @@ fn host_pattern_covers(pattern: &str, host: &str) -> bool {
186186
return false;
187187
};
188188

189+
// A leading `**` matches one or more leading labels; the remaining labels
190+
// match the tail of the host label-for-label, mirroring the runtime.
189191
if first_pattern_label == "**" {
190192
let suffix = &pattern_labels[1..];
191-
let host_suffix = host_labels
192-
.len()
193-
.checked_sub(suffix.len())
194-
.map(|start| &host_labels[start..]);
195-
return !suffix.is_empty()
196-
&& host_labels.len() > suffix.len()
197-
&& matches!(host_suffix, Some(host_suffix) if host_suffix == suffix);
193+
if suffix.is_empty() || host_labels.len() <= suffix.len() {
194+
return false;
195+
}
196+
let start = host_labels.len() - suffix.len();
197+
return labels_match(suffix, &host_labels[start..]);
198198
}
199199

200-
if !first_pattern_label.contains('*') {
201-
return false;
202-
}
200+
// Otherwise the pattern matches exactly one label per position. A `*` (or a
201+
// partial `*` glob) matches a single label, literal labels must be equal, and
202+
// wildcards may appear in any label — not just the first — to match the
203+
// runtime's whole-middle-label wildcards (e.g. `*.s3.*.amazonaws.com`).
204+
labels_match(&pattern_labels, &host_labels)
205+
}
203206

204-
// Runtime host wildcards only apply in the first DNS label. Wildcards in
205-
// later labels are not treated as policy globs here.
207+
/// Match `pattern_labels` against `host_labels` positionally: equal label counts,
208+
/// each `*`-bearing pattern label matched as a single-label glob and every other
209+
/// label compared literally.
210+
fn labels_match(pattern_labels: &[&str], host_labels: &[&str]) -> bool {
206211
pattern_labels.len() == host_labels.len()
207-
&& pattern_labels[1..] == host_labels[1..]
208-
&& wildcard_label_matches(first_pattern_label, host_labels[0])
212+
&& pattern_labels
213+
.iter()
214+
.zip(host_labels)
215+
.all(|(pattern, host)| {
216+
if pattern.contains('*') {
217+
wildcard_label_matches(pattern, host)
218+
} else {
219+
pattern == host
220+
}
221+
})
209222
}
210223

211224
fn wildcard_label_matches(pattern: &str, label: &str) -> bool {
@@ -419,10 +432,38 @@ mod tests {
419432
"api-*.github.com",
420433
"uploads.github.com"
421434
));
422-
assert!(!host_patterns_overlap(
435+
}
436+
437+
#[test]
438+
fn host_patterns_overlap_matches_whole_middle_label_wildcard() {
439+
// A `*` matches exactly one label in any position, mirroring the runtime
440+
// (e.g. the aws-s3 profile's `*.s3.*.amazonaws.com`).
441+
assert!(host_patterns_overlap(
423442
"api.*.github.com",
424443
"api.v3.github.com"
425444
));
445+
assert!(host_patterns_overlap(
446+
"*.s3.*.amazonaws.com",
447+
"bucket.s3.us-east-1.amazonaws.com"
448+
));
449+
assert!(host_patterns_overlap(
450+
"s3.*.amazonaws.com",
451+
"s3.eu-west-1.amazonaws.com"
452+
));
453+
// A single `*` label matches exactly one label — not zero, not several.
454+
assert!(!host_patterns_overlap(
455+
"s3.*.amazonaws.com",
456+
"s3.amazonaws.com"
457+
));
458+
assert!(!host_patterns_overlap(
459+
"*.s3.*.amazonaws.com",
460+
"bucket.s3.dualstack.us-east-1.amazonaws.com"
461+
));
462+
// A literal middle label must still match literally.
463+
assert!(!host_patterns_overlap(
464+
"api.*.github.com",
465+
"api.v3.gitlab.com"
466+
));
426467
}
427468

428469
#[test]

0 commit comments

Comments
 (0)