Skip to content

Commit

Permalink
use publicsuffix
Browse files Browse the repository at this point in the history
  • Loading branch information
atoulme committed Jun 7, 2024
1 parent b87c29a commit a5b40be
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
3 changes: 3 additions & 0 deletions config/confighttp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ README](../configtls/README.md).
- [`http2_ping_timeout`](https://pkg.go.dev/golang.org/x/net/http2#Transport)
- [`cookies`](https://pkg.go.dev/net/http#CookieJar)
- [`enabled`] if enabled, the client will store cookies from server responses and reuse them in subsequent requests.
- [`insecure`] if true, the client accepts setting cookies for any domain. This is useful for testing but is insecure:
it means that the HTTP server for foo.co.uk can set a cookie for bar.co.uk.
If false, the client will allow setting cookies based on the list provided by https://publicsuffix.org/

Example:

Expand Down
12 changes: 11 additions & 1 deletion config/confighttp/confighttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel"
"golang.org/x/net/http2"
"golang.org/x/net/publicsuffix"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configauth"
Expand Down Expand Up @@ -112,6 +113,10 @@ type ClientConfig struct {
type CookiesConfig struct {
// Enabled if true, cookies from HTTP responses will be reused in further HTTP requests with the same server.
Enabled bool `mapstructure:"enabled"`
// Insecure if true, the client accepts setting cookies for any domain. This is useful for testing but is insecure:
// it means that the HTTP server for foo.co.uk can set a cookie for bar.co.uk.
// If false, the client will allow setting cookies based on the list provided by https://publicsuffix.org/
Insecure bool `mapstructure:"insecure"`
}

// NewDefaultClientConfig returns ClientConfig type object with
Expand Down Expand Up @@ -242,7 +247,12 @@ func (hcs *ClientConfig) ToClient(ctx context.Context, host component.Host, sett

var jar http.CookieJar
if hcs.Cookies != nil && hcs.Cookies.Enabled {
jar, err = cookiejar.New(nil)
opts := &cookiejar.Options{}
if !hcs.Cookies.Insecure {
opts.PublicSuffixList = publicsuffix.List

Check warning on line 252 in config/confighttp/confighttp.go

View check run for this annotation

Codecov / codecov/patch

config/confighttp/confighttp.go#L252

Added line #L252 was not covered by tests
}

jar, err = cookiejar.New(opts)
if err != nil {
return nil, err

Check warning on line 257 in config/confighttp/confighttp.go

View check run for this annotation

Codecov / codecov/patch

config/confighttp/confighttp.go#L257

Added line #L257 was not covered by tests
}
Expand Down
2 changes: 1 addition & 1 deletion config/confighttp/confighttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestAllHTTPClientSettings(t *testing.T) {
IdleConnTimeout: &idleConnTimeout,
Compression: "",
DisableKeepAlives: true,
Cookies: &CookiesConfig{Enabled: true},
Cookies: &CookiesConfig{Enabled: true, Insecure: true},
HTTP2ReadIdleTimeout: idleConnTimeout,
HTTP2PingTimeout: http2PingTimeout,
},
Expand Down

0 comments on commit a5b40be

Please sign in to comment.