package emulatorsvc import ( "net/http" "net/http/cookiejar" "net/url" ) // cookieJar is a thin wrapper around an [http.CookieJar] implementation which // automatically sets the cookies `Secure` attribute to False if the host is // localhost. It then uses the wrapped CookieJar to store the cookie. // // This is required because the standard libraries [cookiejar.Jar] does not // return Secure cookies when using http and connecting to localhost, see // https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#restrict_access_to_cookies // and // https://github.com/golang/go/issues/60997 // // # The host should only be set to localhost during dev testing // // This implementation should be removed once the upstream issue is closed type cookieJar struct { http.CookieJar } // 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. // // If the url's hostname is set to localhost, modify the cookies to be not // Secure before saving to the CookieJar func (c cookieJar) SetCookies(u *url.URL, cookies []*http.Cookie) { if u.Hostname() == "localhost" { for _, co := range cookies { co.Secure = false } } c.CookieJar.SetCookies(u, cookies) } func newCookieJar() (http.CookieJar, error) { jar, err := cookiejar.New(nil) if err != nil { return nil, err } return cookieJar{CookieJar: jar}, nil }