...

Source file src/edge-infra.dev/pkg/sds/emergencyaccess/emulatorsvc/cookie.go

Documentation: edge-infra.dev/pkg/sds/emergencyaccess/emulatorsvc

     1  package emulatorsvc
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/cookiejar"
     6  	"net/url"
     7  )
     8  
     9  // cookieJar is a thin wrapper around an [http.CookieJar] implementation which
    10  // automatically sets the cookies `Secure` attribute to False if the host is
    11  // localhost. It then uses the wrapped CookieJar to store the cookie.
    12  //
    13  // This is required because the standard libraries [cookiejar.Jar] does not
    14  // return Secure cookies when using http and connecting to localhost, see
    15  // https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#restrict_access_to_cookies
    16  // and
    17  // https://github.com/golang/go/issues/60997
    18  //
    19  // # The host should only be set to localhost during dev testing
    20  //
    21  // This implementation should be removed once the upstream issue is closed
    22  type cookieJar struct {
    23  	http.CookieJar
    24  }
    25  
    26  // SetCookies handles the receipt of the cookies in a reply for the
    27  // given URL.  It may or may not choose to save the cookies, depending
    28  // on the jar's policy and implementation.
    29  //
    30  // If the url's hostname is set to localhost, modify the cookies to be not
    31  // Secure before saving to the CookieJar
    32  func (c cookieJar) SetCookies(u *url.URL, cookies []*http.Cookie) {
    33  	if u.Hostname() == "localhost" {
    34  		for _, co := range cookies {
    35  			co.Secure = false
    36  		}
    37  	}
    38  
    39  	c.CookieJar.SetCookies(u, cookies)
    40  }
    41  
    42  func newCookieJar() (http.CookieJar, error) {
    43  	jar, err := cookiejar.New(nil)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	return cookieJar{CookieJar: jar}, nil
    48  }
    49  

View as plain text