diff --git a/plugins/wasm-go/extensions/jwt-auth/handler/extractor.go b/plugins/wasm-go/extensions/jwt-auth/handler/extractor.go index 3b5a3b79c..c64ccf9f0 100644 --- a/plugins/wasm-go/extensions/jwt-auth/handler/extractor.go +++ b/plugins/wasm-go/extensions/jwt-auth/handler/extractor.go @@ -129,8 +129,8 @@ func findCookie(cookie string, key string) string { for _, pair := range pairs { pair = strings.TrimSpace(pair) - kv := strings.Split(pair, "=") - if kv[0] == key { + kv := strings.SplitN(pair, "=", 2) + if len(kv) == 2 && kv[0] == key { value = kv[1] break } diff --git a/plugins/wasm-go/extensions/jwt-auth/handler/extractor_test.go b/plugins/wasm-go/extensions/jwt-auth/handler/extractor_test.go new file mode 100644 index 000000000..adf30bd01 --- /dev/null +++ b/plugins/wasm-go/extensions/jwt-auth/handler/extractor_test.go @@ -0,0 +1,51 @@ +package handler + +import "testing" + +func TestFindCookie(t *testing.T) { + tests := []struct { + name string + cookie string + key string + want string + }{ + { + name: "extracts matching cookie value", + cookie: "user=alice; other=value", + key: "user", + want: "alice", + }, + { + name: "skips segment without equals sign", + cookie: "user; other=value", + key: "user", + want: "", + }, + { + name: "keeps equals signs in cookie value", + cookie: "user=alice=admin; other=value", + key: "user", + want: "alice=admin", + }, + { + name: "empty cookie returns empty", + cookie: "", + key: "user", + want: "", + }, + { + name: "key not present returns empty", + cookie: "user=alice; other=value", + key: "missing", + want: "", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := findCookie(tt.cookie, tt.key); got != tt.want { + t.Fatalf("findCookie() = %q, want %q", got, tt.want) + } + }) + } +}