Last active
January 17, 2025 23:44
-
-
Save fyxme/2bceed4038e2be71ce1deb7e6e9db434 to your computer and use it in GitHub Desktop.
Golang Minimal CookieJar Implementation which always returns all cookies, regardless of domain or url (Insecure but usefull for testing or writing POCs)
This file contains hidden or 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
package main | |
/* | |
Minimal CookieJar implementation which always returns all cookies (ie. disregard the url passed and cookie domain) | |
Usefull for testing or writing exploit code (eg. during a CTF where ip/host/domains may be interchanged in the client) | |
WARNING: Do not use in prod.. this is super unsafe... Just good for testing | |
Author: fyx.me | |
gist: https://gist.github.com/fyxme/2bceed4038e2be71ce1deb7e6e9db434 | |
*/ | |
import ( | |
"net/http" | |
"slices" | |
"maps" | |
"net/url" | |
) | |
/* | |
// https://pkg.go.dev/net/http/cookiejar#Jar.Cookies | |
// https://pkg.go.dev/net/http#CookieJar | |
type CookieJar interface { | |
// SetCookies handles the receipt of the cookies in a reply for the | |
// given URL. It may or may not choose to save the cookies, depending | |
// on the jar's policy and implementation. | |
SetCookies(u *url.URL, cookies []*Cookie) | |
// Cookies returns the cookies to send in a request for the given URL. | |
// It is up to the implementation to honor the standard cookie use | |
// restrictions such as in RFC 6265. | |
Cookies(u *url.URL) []*Cookie | |
} | |
*/ | |
type MinimalJar struct { | |
// map the cookie name to the cookie itself | |
cookies map[string]*http.Cookie | |
} | |
func (j *MinimalJar) SetCookies(u *url.URL, cookies []*http.Cookie) { | |
for _, c := range cookies { | |
// cookies should have a name | |
if c.Name == "" { | |
continue | |
} | |
// ignore the domain | |
c.Domain = "" | |
// ignore the path | |
c.Path = "" | |
j.cookies[c.Name] = c | |
} | |
} | |
func (j *MinimalJar) Cookies(u *url.URL) []*http.Cookie { | |
// we dont care about the url, we just always return all cookies | |
return slices.Collect(maps.Values(j.cookies)) | |
} | |
func NewJar() (*MinimalJar) { | |
jar := &MinimalJar{ | |
cookies: make(map[string]*http.Cookie), | |
} | |
return jar | |
} | |
func (j *MinimalJar) AddCookie(name, value string) { | |
cookie := &http.Cookie{ | |
Name: name, | |
Value: value, | |
} | |
j.cookies[name]=cookie | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example Usage:
Request: