Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
21 changes: 7 additions & 14 deletions middleware/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -907,23 +907,16 @@ func New(config ...Config) fiber.Handler {

// hasDirective checks if a cache-control header contains a directive (case-insensitive)
func hasDirective(cc, directive string) bool {
ccLen := len(cc)
dirLen := len(directive)
for i := 0; i <= ccLen-dirLen; i++ {
if !utils.EqualFold(cc[i:i+dirLen], directive) {
continue
}
if i > 0 {
prev := cc[i-1]
if prev != ' ' && prev != ',' {
continue
}
}
if i+dirLen == ccLen || cc[i+dirLen] == ',' {
for _, part := range strings.Split(cc, ",") {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better performance and to reduce allocations, consider using strings.SplitSeq instead of strings.Split. strings.SplitSeq provides an iterator over the substrings, avoiding the allocation of a slice to hold all of them at once. This is more memory-efficient, especially for Cache-Control headers with many directives.

This change would also be consistent with the implementation of parseVary in this file, which already uses strings.SplitSeq.

Suggested change
for _, part := range strings.Split(cc, ",") {
for part := range strings.SplitSeq(cc, ",") {

part = strings.TrimSpace(part)

key, _, _ := strings.Cut(part, "=")
key = strings.TrimSpace(key)

if strings.EqualFold(key, directive) {
Comment on lines +910 to +916
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Quoted commas can produce false directive matches

The current comma-split approach can misparse valid quoted directive values and report a directive that only appears inside a quoted string. Consider reusing parseCacheControlDirectives here for RFC-consistent tokenization.

Suggested fix
 func hasDirective(cc, directive string) bool {
-	for _, part := range strings.Split(cc, ",") {
-		part = strings.TrimSpace(part)
-
-		key, _, _ := strings.Cut(part, "=")
-		key = strings.TrimSpace(key)
-
-		if strings.EqualFold(key, directive) {
-			return true
-		}
-	}
-	return false
+	found := false
+	parseCacheControlDirectives(utils.UnsafeBytes(cc), func(key, _ []byte) {
+		if !found && utils.EqualFold(utils.UnsafeString(key), directive) {
+			found = true
+		}
+	})
+	return found
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
for _, part := range strings.Split(cc, ",") {
part = strings.TrimSpace(part)
key, _, _ := strings.Cut(part, "=")
key = strings.TrimSpace(key)
if strings.EqualFold(key, directive) {
func hasDirective(cc, directive string) bool {
found := false
parseCacheControlDirectives(utils.UnsafeBytes(cc), func(key, _ []byte) {
if !found && utils.EqualFold(utils.UnsafeString(key), directive) {
found = true
}
})
return found
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@middleware/cache/cache.go` around lines 910 - 916, The loop that does
strings.Split(cc, ",") can misinterpret commas inside quoted directive values;
replace this ad-hoc comma-splitting with the RFC-aware tokenizer by calling
parseCacheControlDirectives(cc) and iterate its returned directives (or map of
key→value) instead of splitting, then use strings.EqualFold against the
directive variable to match keys (and use the provided value from
parseCacheControlDirectives when needed); update the logic in the function
containing the current loop so it consumes parseCacheControlDirectives' output
to avoid false matches from quoted commas.

return true
}
}

return false
}

Expand Down
105 changes: 105 additions & 0 deletions middleware/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5091,3 +5091,108 @@ func Test_Cache_ConfigurationAndResponseHandling(t *testing.T) {
require.Equal(t, cacheMiss, rsp2.Header.Get("X-Cache"))
})
}

func Test_Cache_hasDirective(t *testing.T) {
t.Parallel()

tests := []struct {
name string
cc string
directive string
expected bool
}{
{
name: "directive at end of string",
cc: "no-cache",
directive: "no-cache",
expected: true,
},
{
name: "directive before comma",
cc: "no-cache, no-store",
directive: "no-cache",
expected: true,
},
{
name: "directive after comma",
cc: "no-store, no-cache",
directive: "no-cache",
expected: true,
},
{
name: "directive not present",
cc: "no-store, public",
directive: "no-cache",
expected: false,
},
{
name: "trailing space",
cc: "no-cache ",
directive: "no-cache",
expected: true,
},
{
name: "trailing tab",
cc: "no-cache\t",
directive: "no-cache",
expected: true,
},
{
name: "directive with value using equals",
cc: `no-cache="Set-Cookie"`,
directive: "no-cache",
expected: true,
},
{
name: "max-age with value",
cc: "max-age=604800",
directive: "max-age",
expected: true,
},
{
name: "private with trailing space",
cc: "private ",
directive: "private",
expected: true,
},
{
name: "s-maxage with value in middle",
cc: "public, s-maxage=3600, must-revalidate",
directive: "s-maxage",
expected: true,
},

{
name: "partial match should fail",
cc: "no-cache-extended",
directive: "no-cache",
expected: false,
},
{
name: "directive in middle with spaces",
cc: "public, no-cache , max-age=60",
directive: "no-cache",
expected: true,
},
{
name: "empty string",
cc: "",
directive: "no-cache",
expected: false,
},
{
name: "case insensitive match",
cc: "No-Cache",
directive: "no-cache",
expected: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := hasDirective(tt.cc, tt.directive)
require.Equal(t, tt.expected, got)
})
}
}
Loading