s3api: match policy variable values literally in Like conditions

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Chris LuandClaude Opus 5.5 committed 2026-12-04 16:22:51 -08:00
1 parent e6ee703849
commit 99633b185d
2 files changed
+64 -20

No files matched your search

+43 -20
View File
@@ -51,13 +51,34 @@ func (e *StringNotEqualsEvaluator) Evaluate(conditionValue interface{}, contextV
type StringLikeEvaluator struct{}
func (e *StringLikeEvaluator) Evaluate(conditionValue interface{}, contextValues []string) bool {
patterns := normalizeToStringSlice(conditionValue)
for _, pattern := range patterns {
for _, contextValue := range contextValues {
if wildcard.MatchesWildcard(pattern, contextValue) {
for _, contextValue := range contextValues {
if matchesLikePattern(conditionValue, contextValue) {
return true
}
}
return false
}
// likePatterns holds *Like condition values with their policy variables still
// unexpanded, so the expanded text is matched literally.
type likePatterns struct {
patterns []string
expand func(string) string
}
func matchesLikePattern(conditionValue interface{}, contextValue string) bool {
if lp, ok := conditionValue.(likePatterns); ok {
for _, pattern := range lp.patterns {
if wildcard.MatchesExpandedWildcard(pattern, PolicyVariableRegex, lp.expand, contextValue, false) {
return true
}
}
return false
}
for _, pattern := range normalizeToStringSlice(conditionValue) {
if wildcard.MatchesWildcard(pattern, contextValue) {
return true
}
}
return false
}
@@ -66,12 +87,9 @@ func (e *StringLikeEvaluator) Evaluate(conditionValue interface{}, contextValues
type StringNotLikeEvaluator struct{}
func (e *StringNotLikeEvaluator) Evaluate(conditionValue interface{}, contextValues []string) bool {
patterns := normalizeToStringSlice(conditionValue)
for _, pattern := range patterns {
for _, contextValue := range contextValues {
if wildcard.MatchesWildcard(pattern, contextValue) {
return false
}
for _, contextValue := range contextValues {
if matchesLikePattern(conditionValue, contextValue) {
return false
}
}
return true
@@ -499,12 +517,9 @@ func (e *ArnEqualsEvaluator) Evaluate(conditionValue interface{}, contextValues
type ArnLikeEvaluator struct{}
func (e *ArnLikeEvaluator) Evaluate(conditionValue interface{}, contextValues []string) bool {
patterns := normalizeToStringSlice(conditionValue)
for _, pattern := range patterns {
for _, contextValue := range contextValues {
if wildcard.MatchesWildcard(pattern, contextValue) {
return true
}
for _, contextValue := range contextValues {
if matchesLikePattern(conditionValue, contextValue) {
return true
}
}
return false
@@ -702,12 +717,20 @@ func evaluateConditions(conditions PolicyConditions, contextValues map[string][]
// Substitute variables in expected values
expectedValues := value.Strings()
substitutedValues := make([]string, len(expectedValues))
for i, v := range expectedValues {
substitutedValues[i] = SubstituteVariables(v, contextValues, claims)
var conditionValue interface{}
if strings.HasSuffix(operator, "Like") {
conditionValue = likePatterns{patterns: expectedValues, expand: func(v string) string {
return SubstituteVariables(v, contextValues, claims)
}}
} else {
substitutedValues := make([]string, len(expectedValues))
for i, v := range expectedValues {
substitutedValues[i] = SubstituteVariables(v, contextValues, claims)
}
conditionValue = substitutedValues
}
if !conditionEvaluator.Evaluate(substitutedValues, contextVals) {
if !conditionEvaluator.Evaluate(conditionValue, contextVals) {
return false // If any condition fails, the whole condition block fails
}
}
@@ -205,3 +205,24 @@ func TestPolicyVariableValuesAreNotWildcards(t *testing.T) {
})
}
}
func TestLikeConditionVariableValuesAreNotWildcards(t *testing.T) {
prefix := map[string][]string{"s3:prefix": {"alice@corp.com/"}}
for _, operator := range []string{"StringLike", "ForAnyValue:StringLike", "StringNotLike"} {
conditions := PolicyConditions{operator: {"s3:prefix": NewStringOrStringSlice("${jwt:email}/*")}}
for _, email := range []string{"alice@corp.com", "bob@corp.com", "*@corp.com", "*", "?lice@corp.com"} {
want := email == "alice@corp.com"
if operator == "StringNotLike" {
want = !want
}
if got := EvaluateConditions(conditions, prefix, nil, map[string]interface{}{"email": email}); got != want {
t.Errorf("%s email=%q: got %v, want %v", operator, email, got, want)
}
}
}
arnLike := PolicyConditions{"ArnLike": {"aws:SourceArn": NewStringOrStringSlice("arn:aws:iam::123456789012:user/${jwt:sub}")}}
source := map[string][]string{"aws:SourceArn": {"arn:aws:iam::123456789012:user/alice"}}
if EvaluateConditions(arnLike, source, nil, map[string]interface{}{"sub": "*"}) {
t.Errorf("ArnLike matched another user's ARN through a wildcard sub")
}
}