mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 17:33:03 -05:00
fix(iam): share case-sensitive condition matching (#4725)
* fix(iam): share case-sensitive condition matching * fix(iam): use iterative glob matching for conditions * fix(iam): match wildcard before literal asterisk
This commit is contained in:
1 parent
0b53580969
commit
055c411b9a
8 files changed
+97
-116
No files matched your search
+6
-30
@@ -262,36 +262,12 @@ public class AssumeRolePolicyEvaluator {
|
||||
if (!values.isTextual()) {
|
||||
return false;
|
||||
}
|
||||
if ("StringEquals".equals(operator)) {
|
||||
return values.asText().equals(actual);
|
||||
}
|
||||
return globMatchesCaseSensitive(values.asText(), actual);
|
||||
}
|
||||
|
||||
private boolean globMatchesCaseSensitive(String pattern, String value) {
|
||||
int patternIndex = 0;
|
||||
int valueIndex = 0;
|
||||
int starIndex = -1;
|
||||
int starValueIndex = -1;
|
||||
while (valueIndex < value.length()) {
|
||||
if (patternIndex < pattern.length()
|
||||
&& (pattern.charAt(patternIndex) == '?' || pattern.charAt(patternIndex) == value.charAt(valueIndex))) {
|
||||
patternIndex++;
|
||||
valueIndex++;
|
||||
} else if (patternIndex < pattern.length() && pattern.charAt(patternIndex) == '*') {
|
||||
starIndex = patternIndex++;
|
||||
starValueIndex = valueIndex;
|
||||
} else if (starIndex >= 0) {
|
||||
patternIndex = starIndex + 1;
|
||||
valueIndex = ++starValueIndex;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
while (patternIndex < pattern.length() && pattern.charAt(patternIndex) == '*') {
|
||||
patternIndex++;
|
||||
}
|
||||
return patternIndex == pattern.length();
|
||||
return switch (operator) {
|
||||
case "StringEquals" -> values.asText().equals(actual);
|
||||
case "StringLike" -> IamPolicyEvaluator.caseSensitiveGlobMatches(values.asText(), actual);
|
||||
case "ArnEquals", "ArnLike" -> IamPolicyEvaluator.matchesArnCondition(values.asText(), actual);
|
||||
default -> false;
|
||||
};
|
||||
}
|
||||
|
||||
private boolean matchesServicePrincipal(JsonNode principalNode, String servicePrincipal) {
|
||||
|
||||
@@ -806,7 +806,7 @@ public class IamPolicyEvaluator {
|
||||
return false;
|
||||
}
|
||||
for (String pattern : patterns) {
|
||||
if (globMatchesHelper(pattern, value, 0, 0)) {
|
||||
if (globMatchesHelper(pattern, value)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1050,14 +1050,14 @@ public class IamPolicyEvaluator {
|
||||
if (pattern == null || value == null) {
|
||||
return false;
|
||||
}
|
||||
return globMatchesHelper(pattern.toLowerCase(), value.toLowerCase(), 0, 0);
|
||||
return globMatchesHelper(pattern.toLowerCase(), value.toLowerCase());
|
||||
}
|
||||
|
||||
private static boolean caseSensitiveGlobMatches(String pattern, String value) {
|
||||
return pattern != null && value != null && globMatchesHelper(pattern, value, 0, 0);
|
||||
public static boolean caseSensitiveGlobMatches(String pattern, String value) {
|
||||
return pattern != null && value != null && globMatchesHelper(pattern, value);
|
||||
}
|
||||
|
||||
private static boolean matchesArnCondition(String pattern, String value) {
|
||||
public static boolean matchesArnCondition(String pattern, String value) {
|
||||
if (pattern == null || value == null) {
|
||||
return false;
|
||||
}
|
||||
@@ -1075,33 +1075,31 @@ public class IamPolicyEvaluator {
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean globMatchesHelper(String pat, String val, int pi, int vi) {
|
||||
while (pi < pat.length() && vi < val.length()) {
|
||||
char p = pat.charAt(pi);
|
||||
if (p == '*') {
|
||||
while (pi < pat.length() && pat.charAt(pi) == '*') {
|
||||
pi++;
|
||||
}
|
||||
if (pi == pat.length()) {
|
||||
return true;
|
||||
}
|
||||
for (int i = vi; i <= val.length(); i++) {
|
||||
if (globMatchesHelper(pat, val, pi, i)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} else if (p == '?' || p == val.charAt(vi)) {
|
||||
pi++;
|
||||
vi++;
|
||||
private static boolean globMatchesHelper(String pattern, String value) {
|
||||
int patternIndex = 0;
|
||||
int valueIndex = 0;
|
||||
int starIndex = -1;
|
||||
int starValueIndex = -1;
|
||||
|
||||
while (valueIndex < value.length()) {
|
||||
if (patternIndex < pattern.length() && pattern.charAt(patternIndex) == '*') {
|
||||
starIndex = patternIndex++;
|
||||
starValueIndex = valueIndex;
|
||||
} else if (patternIndex < pattern.length()
|
||||
&& (pattern.charAt(patternIndex) == '?' || pattern.charAt(patternIndex) == value.charAt(valueIndex))) {
|
||||
patternIndex++;
|
||||
valueIndex++;
|
||||
} else if (starIndex >= 0) {
|
||||
patternIndex = starIndex + 1;
|
||||
valueIndex = ++starValueIndex;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
while (pi < pat.length() && pat.charAt(pi) == '*') {
|
||||
pi++;
|
||||
while (patternIndex < pattern.length() && pattern.charAt(patternIndex) == '*') {
|
||||
patternIndex++;
|
||||
}
|
||||
return pi == pat.length() && vi == val.length();
|
||||
return patternIndex == pattern.length();
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
@@ -88,14 +88,14 @@ public class SAMLTrustPolicyEvaluator {
|
||||
}
|
||||
if ("StringLike".equals(operator)) {
|
||||
return actual.stream().anyMatch(value -> expected.stream().anyMatch(pattern ->
|
||||
WebIdentityTrustPolicyEvaluator.globMatchesCaseSensitive(pattern, value)));
|
||||
IamPolicyEvaluator.caseSensitiveGlobMatches(pattern, value)));
|
||||
}
|
||||
if ("StringNotEquals".equals(operator)) {
|
||||
return actual.stream().allMatch(value -> expected.stream().noneMatch(value::equals));
|
||||
}
|
||||
if ("StringNotLike".equals(operator)) {
|
||||
return actual.stream().allMatch(value -> expected.stream().noneMatch(pattern ->
|
||||
WebIdentityTrustPolicyEvaluator.globMatchesCaseSensitive(pattern, value)));
|
||||
IamPolicyEvaluator.caseSensitiveGlobMatches(pattern, value)));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
+2
-45
@@ -206,9 +206,9 @@ public class WebIdentityTrustPolicyEvaluator {
|
||||
case "StringEquals" -> actual.stream().anyMatch(expectedValues::contains);
|
||||
case "StringNotEquals" -> actual.stream().noneMatch(expectedValues::contains);
|
||||
case "StringLike" -> actual.stream().anyMatch(
|
||||
a -> expectedValues.stream().anyMatch(e -> globMatchesCaseSensitive(e, a)));
|
||||
a -> expectedValues.stream().anyMatch(e -> IamPolicyEvaluator.caseSensitiveGlobMatches(e, a)));
|
||||
case "StringNotLike" -> actual.stream().noneMatch(
|
||||
a -> expectedValues.stream().anyMatch(e -> globMatchesCaseSensitive(e, a)));
|
||||
a -> expectedValues.stream().anyMatch(e -> IamPolicyEvaluator.caseSensitiveGlobMatches(e, a)));
|
||||
default -> {
|
||||
LOG.debugv("Unsupported web-identity trust policy condition operator: {0}", operator);
|
||||
yield false;
|
||||
@@ -216,49 +216,6 @@ public class WebIdentityTrustPolicyEvaluator {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Glob matching over {@code *} and {@code ?} that preserves case, unlike
|
||||
* {@link IamPolicyEvaluator#globMatches} which lowercases both operands. A {@code StringLike}
|
||||
* constraint on {@code oidc:sub} must not admit a service account differing only in case, so it
|
||||
* needs the same case sensitivity as the {@code StringEquals} branch above.
|
||||
*/
|
||||
static boolean globMatchesCaseSensitive(String pattern, String value) {
|
||||
if (pattern == null || value == null) {
|
||||
return false;
|
||||
}
|
||||
return globMatchesHelper(pattern, value, 0, 0);
|
||||
}
|
||||
|
||||
private static boolean globMatchesHelper(String pattern, String value, int pi, int vi) {
|
||||
while (pi < pattern.length()) {
|
||||
char p = pattern.charAt(pi);
|
||||
if (p == '*') {
|
||||
// Collapse consecutive stars, then try every split point for the remainder.
|
||||
while (pi + 1 < pattern.length() && pattern.charAt(pi + 1) == '*') {
|
||||
pi++;
|
||||
}
|
||||
if (pi == pattern.length() - 1) {
|
||||
return true;
|
||||
}
|
||||
for (int i = vi; i <= value.length(); i++) {
|
||||
if (globMatchesHelper(pattern, value, pi + 1, i)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (vi >= value.length()) {
|
||||
return false;
|
||||
}
|
||||
if (p != '?' && p != value.charAt(vi)) {
|
||||
return false;
|
||||
}
|
||||
pi++;
|
||||
vi++;
|
||||
}
|
||||
return vi == value.length();
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a policy condition key such as
|
||||
* {@code oidc.eks.us-east-1.amazonaws.com/id/ABC123:sub} to the token's claim values. The key
|
||||
|
||||
Reference in new issue
Block a user