-
-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: parse multiple cookies with spaces (#943)
* fix: parse multiple cookies with spaces --------- Signed-off-by: Felipe Zipitria <felipe.zipitria@owasp.org>
- Loading branch information
Showing
6 changed files
with
171 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package cookies | ||
|
||
import ( | ||
"net/textproto" | ||
"strings" | ||
) | ||
|
||
// ParseCookies parses cookies and splits in name, value pairs. Won't check for valid names nor values. | ||
// If there are multiple cookies with the same name, it will append to the list with the same name key. | ||
// Loosely based in the stdlib src/net/http/cookie.go | ||
func ParseCookies(rawCookies string) map[string][]string { | ||
cookies := make(map[string][]string) | ||
|
||
rawCookies = textproto.TrimString(rawCookies) | ||
|
||
if rawCookies == "" { | ||
return cookies | ||
} | ||
|
||
var part string | ||
for len(rawCookies) > 0 { // continue since we have rest | ||
part, rawCookies, _ = strings.Cut(rawCookies, ";") | ||
part = textproto.TrimString(part) | ||
if part == "" { | ||
continue | ||
} | ||
name, val, _ := strings.Cut(part, "=") | ||
name = textproto.TrimString(name) | ||
// if name is empty (eg: "Cookie: =foo;") skip it | ||
if name == "" { | ||
continue | ||
} | ||
cookies[name] = append(cookies[name], val) | ||
} | ||
return cookies | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package cookies | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func equalMaps(map1 map[string][]string, map2 map[string][]string) bool { | ||
if len(map1) != len(map2) { | ||
return false | ||
} | ||
|
||
// Iterate through the key-value pairs of the first map | ||
for key, slice1 := range map1 { | ||
// Check if the key exists in the second map | ||
slice2, ok := map2[key] | ||
if !ok { | ||
return false | ||
} | ||
|
||
// Compare the values of the corresponding keys | ||
for i, val1 := range slice1 { | ||
val2 := slice2[i] | ||
|
||
// Compare the elements | ||
if val1 != val2 { | ||
return false | ||
} | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
func TestParseCookies(t *testing.T) { | ||
type args struct { | ||
rawCookies string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want map[string][]string | ||
}{ | ||
{ | ||
name: "EmptyString", | ||
args: args{rawCookies: " "}, | ||
want: map[string][]string{}, | ||
}, | ||
{ | ||
name: "SimpleCookie", | ||
args: args{rawCookies: "test=test_value"}, | ||
want: map[string][]string{"test": {"test_value"}}, | ||
}, | ||
{ | ||
name: "MultipleCookies", | ||
args: args{rawCookies: "test1=test_value1; test2=test_value2"}, | ||
want: map[string][]string{"test1": {"test_value1"}, "test2": {"test_value2"}}, | ||
}, | ||
{ | ||
name: "SpacesInCookieName", | ||
args: args{rawCookies: " test1 =test_value1; test2 =test_value2"}, | ||
want: map[string][]string{"test1": {"test_value1"}, "test2": {"test_value2"}}, | ||
}, | ||
{ | ||
name: "SpacesInCookieValue", | ||
args: args{rawCookies: "test1=test _value1; test2 =test_value2"}, | ||
want: map[string][]string{"test1": {"test _value1"}, "test2": {"test_value2"}}, | ||
}, | ||
{ | ||
name: "EmptyCookie", | ||
args: args{rawCookies: ";;foo=bar"}, | ||
want: map[string][]string{"foo": {"bar"}}, | ||
}, | ||
{ | ||
name: "EmptyName", | ||
args: args{rawCookies: "=bar;"}, | ||
want: map[string][]string{}, | ||
}, | ||
{ | ||
name: "MultipleEqualsInValues", | ||
args: args{rawCookies: "test1=val==ue1;test2=value2"}, | ||
want: map[string][]string{"test1": {"val==ue1"}, "test2": {"value2"}}, | ||
}, | ||
{ | ||
name: "RepeatedCookieNameShouldGiveList", | ||
args: args{rawCookies: "test1=value1;test1=value2"}, | ||
want: map[string][]string{"test1": {"value1", "value2"}}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := ParseCookies(tt.args.rawCookies) | ||
if !equalMaps(got, tt.want) { | ||
t.Errorf("ParseCookies() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters