-
-
Notifications
You must be signed in to change notification settings - Fork 2k
🩹 fix: Cache middleware hasDirective misses directives with trailing space/tab/= #4144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5091,3 +5091,57 @@ func Test_Cache_ConfigurationAndResponseHandling(t *testing.T) { | |
| require.Equal(t, cacheMiss, rsp2.Header.Get("X-Cache")) | ||
| }) | ||
| } | ||
|
|
||
| // go test -run Test_hasDirective | ||
| func Test_hasDirective(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| cc string | ||
| directive string | ||
| expected bool | ||
| }{ | ||
| // Basic matching | ||
| {name: "exact match", cc: "no-cache", directive: "no-cache", expected: true}, | ||
| {name: "not found", cc: "public", directive: "no-cache", expected: false}, | ||
| {name: "empty header", cc: "", directive: "no-cache", expected: false}, | ||
|
|
||
| // Comma-separated directives | ||
| {name: "first in list", cc: "no-cache, no-store", directive: "no-cache", expected: true}, | ||
| {name: "last in list", cc: "public, no-cache", directive: "no-cache", expected: true}, | ||
| {name: "middle in list", cc: "public, no-cache, no-store", directive: "no-cache", expected: true}, | ||
| {name: "no spaces", cc: "public,no-cache,no-store", directive: "no-cache", expected: true}, | ||
| {name: "tab before directive", cc: "public,\tno-cache", directive: "no-cache", expected: true}, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Verify if the implementation supports tab as a preceding character
rg -n "prev != " middleware/cache/cache.go -A2 -B2Repository: gofiber/fiber Length of output: 161 🏁 Script executed: # Get the complete hasDirective function
rg -n "func.*hasDirective" middleware/cache/cache.go -A 50Repository: gofiber/fiber Length of output: 1377 🏁 Script executed: # Get the complete test case context around line 5115
sed -n '5110,5120p' middleware/cache/cache_test.goRepository: gofiber/fiber Length of output: 859 🏁 Script executed: # Check if there are any updates to the preceding character validation logic
rg -n "prev.*==" middleware/cache/cache.goRepository: gofiber/fiber Length of output: 39 Test case "tab before directive" will fail against the current implementation. The if prev != ' ' && prev != ',' {
continue
}For the test case While the implementation does support tab as a following character (line 927:
🤖 Prompt for AI Agents |
||
|
|
||
| // Trailing/leading whitespace | ||
| {name: "trailing space", cc: "no-cache ", directive: "no-cache", expected: true}, | ||
| {name: "trailing tab", cc: "no-cache\t", directive: "no-cache", expected: true}, | ||
| {name: "space before comma", cc: "no-cache , public", directive: "no-cache", expected: true}, | ||
|
|
||
| // Directives with arguments (=) | ||
| {name: "directive with value", cc: "no-cache=\"Set-Cookie\"", directive: "no-cache", expected: true}, | ||
| {name: "directive with value in list", cc: "public, no-cache=\"Set-Cookie\", no-store", directive: "no-cache", expected: true}, | ||
|
|
||
| // Partial matches should not match | ||
| {name: "prefix match only", cc: "no-cache-extended", directive: "no-cache", expected: false}, | ||
| {name: "suffix match only", cc: "xno-cache", directive: "no-cache", expected: false}, | ||
|
|
||
| // Case insensitivity | ||
| {name: "uppercase", cc: "NO-CACHE", directive: "no-cache", expected: true}, | ||
| {name: "mixed case", cc: "No-Cache", directive: "no-cache", expected: true}, | ||
|
|
||
| // Private directive | ||
| {name: "private standalone", cc: "private", directive: "private", expected: true}, | ||
| {name: "private with trailing space", cc: "private ", directive: "private", expected: true}, | ||
| {name: "private in list with spaces", cc: "max-age=3600, private ", directive: "private", expected: true}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| result := hasDirective(tt.cc, tt.directive) | ||
| require.Equal(t, tt.expected, result) | ||
| }) | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.