Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: cookies not stored in irmaclient when received from a Set-Cookie… #359

Merged
merged 5 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased
### Fixed
- HTTP cookies not stored in `irmaclient` when received from a `Set-Cookie` header
- Invalid hostname specified in MX record bypasses e-mail address revalidation
- Background revocation tasks not stopped when closing an `irmaclient`

Expand Down
21 changes: 21 additions & 0 deletions irmago_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,27 @@ func TestRetryHTTPRequest(t *testing.T) {
require.Equal(t, "42\n", string(bts))
}

func TestHTTPTransportCookieJar(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/setcookie", func(w http.ResponseWriter, r *http.Request) {
http.SetCookie(w, &http.Cookie{Name: "testcookie", Value: "42", Domain: "localhost"})
w.WriteHeader(http.StatusNoContent)
})
mux.HandleFunc("/checkcookie", func(w http.ResponseWriter, r *http.Request) {
c, err := r.Cookie("testcookie")
require.NoError(t, err)
require.Equal(t, "42", c.Value)
w.WriteHeader(http.StatusNoContent)
})
server := &http.Server{Addr: "localhost:48682", Handler: mux}
go server.ListenAndServe()
defer server.Close()

transport := NewHTTPTransport("http://localhost:48682", false)
require.NoError(t, transport.Get("/setcookie", nil))
require.NoError(t, transport.Get("/checkcookie", nil))
}

func TestInvalidIrmaConfigurationRestoreFromRemote(t *testing.T) {
test.StartSchemeManagerHttpServer()
defer test.StopSchemeManagerHttpServer()
Expand Down
21 changes: 21 additions & 0 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"log"
"net"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"time"
Expand Down Expand Up @@ -104,6 +105,13 @@ func NewHTTPTransport(serverURL string, forceHTTPS bool) *HTTPTransport {
},
}

// Create cookie jar to store cookies in
cookieJar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: httpPublicSuffixList{}})
if err != nil {
Logger.Warnf("failed to create cookie jar: %s", err.Error())
cookieJar = nil
}

client := &retryablehttp.Client{
Logger: transportlogger,
RetryWaitMin: 100 * time.Millisecond,
Expand All @@ -120,6 +128,7 @@ func NewHTTPTransport(serverURL string, forceHTTPS bool) *HTTPTransport {
HTTPClient: &http.Client{
Timeout: time.Second * 5,
Transport: innerTransport,
Jar: cookieJar,
},
}

Expand Down Expand Up @@ -343,3 +352,15 @@ func (transport *HTTPTransport) Get(url string, result interface{}) error {
func (transport *HTTPTransport) Delete() error {
return transport.jsonRequest("", http.MethodDelete, nil, nil)
}

// httpPublicSuffixList implements the PublicSuffixList interface for use in cookiejar.
// It is used to prevent cookies from being sent to other domains and subdomains as the host.
type httpPublicSuffixList struct{}

func (p httpPublicSuffixList) PublicSuffix(domain string) string {
return domain
}

func (p httpPublicSuffixList) String() string {
return "github.com/privacybydesign/irmago/httpPublicSuffixList-v1"
}