-
Notifications
You must be signed in to change notification settings - Fork 16
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
secure outgoing http client with max connections #3508
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
901fea3
secure outgoing http client with max connections
woutslakhorst 95e5709
docs
woutslakhorst 92f79e8
test fixes
woutslakhorst 42950a8
Update http/client/caching.go
woutslakhorst f8ba1d7
Update docs/pages/deployment/security-considerations.rst
woutslakhorst 4891aaf
add comment
woutslakhorst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,23 +19,59 @@ | |
package client | ||
|
||
import ( | ||
"bytes" | ||
"crypto/tls" | ||
"errors" | ||
"fmt" | ||
"github.com/nuts-foundation/nuts-node/core" | ||
"io" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
// SafeHttpTransport is a http.Transport that can be used as a default transport for HTTP clients. | ||
var SafeHttpTransport *http.Transport | ||
|
||
func init() { | ||
httpTransport := http.DefaultTransport.(*http.Transport) | ||
if httpTransport.TLSClientConfig == nil { | ||
httpTransport.TLSClientConfig = &tls.Config{} | ||
SafeHttpTransport = http.DefaultTransport.(*http.Transport).Clone() | ||
if SafeHttpTransport.TLSClientConfig == nil { | ||
SafeHttpTransport.TLSClientConfig = &tls.Config{} | ||
} | ||
httpTransport.TLSClientConfig.MinVersion = tls.VersionTLS12 | ||
SafeHttpTransport.TLSClientConfig.MinVersion = tls.VersionTLS12 | ||
// to prevent slow responses from public clients to have significant impact (default was unlimited) | ||
SafeHttpTransport.MaxConnsPerHost = 5 | ||
|
||
DefaultCachingTransport = SafeHttpTransport | ||
woutslakhorst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// StrictMode is a flag that can be set to true to enable strict mode for the HTTP client. | ||
var StrictMode bool | ||
|
||
// DefaultMaxHttpResponseSize is a default maximum size of an HTTP response body that will be read. | ||
// Very large or unbounded HTTP responses can cause denial-of-service, so it's good to limit how much data is read. | ||
// This of course heavily depends on the use case, but 1MB is a reasonable default. | ||
const DefaultMaxHttpResponseSize = 1024 * 1024 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be nice if this is configurable |
||
|
||
// limitedReadAll reads the given reader until the DefaultMaxHttpResponseSize is reached. | ||
// It returns an error if more data is available than DefaultMaxHttpResponseSize. | ||
func limitedReadAll(reader io.Reader) ([]byte, error) { | ||
result, err := io.ReadAll(io.LimitReader(reader, DefaultMaxHttpResponseSize+1)) | ||
if len(result) > DefaultMaxHttpResponseSize { | ||
return nil, fmt.Errorf("data to read exceeds max. safety limit of %d bytes", DefaultMaxHttpResponseSize) | ||
} | ||
return result, err | ||
} | ||
|
||
// New creates a new HTTP client with the given timeout. | ||
func New(timeout time.Duration) *StrictHTTPClient { | ||
return &StrictHTTPClient{ | ||
client: &http.Client{ | ||
Transport: SafeHttpTransport, | ||
Timeout: timeout, | ||
}, | ||
} | ||
} | ||
|
||
// NewWithCache creates a new HTTP client with the given timeout. | ||
// It uses the DefaultCachingTransport as the underlying transport. | ||
func NewWithCache(timeout time.Duration) *StrictHTTPClient { | ||
|
@@ -51,7 +87,7 @@ func NewWithCache(timeout time.Duration) *StrictHTTPClient { | |
// It copies the http.DefaultTransport and sets the TLSClientConfig to the given tls.Config. | ||
// As such, it can't be used in conjunction with the CachingRoundTripper. | ||
func NewWithTLSConfig(timeout time.Duration, tlsConfig *tls.Config) *StrictHTTPClient { | ||
transport := http.DefaultTransport.(*http.Transport).Clone() | ||
transport := SafeHttpTransport.Clone() | ||
transport.TLSClientConfig = tlsConfig | ||
return &StrictHTTPClient{ | ||
client: &http.Client{ | ||
|
@@ -69,5 +105,17 @@ func (s *StrictHTTPClient) Do(req *http.Request) (*http.Response, error) { | |
if StrictMode && req.URL.Scheme != "https" { | ||
return nil, errors.New("strictmode is enabled, but request is not over HTTPS") | ||
} | ||
return s.client.Do(req) | ||
req.Header.Set("User-Agent", core.UserAgent()) | ||
result, err := s.client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if result.Body != nil { | ||
body, err := limitedReadAll(result.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
result.Body = io.NopCloser(bytes.NewReader(body)) | ||
} | ||
return result, nil | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a bit weird that this is under
core
and all the other underhttp
like the rest. Import cycle on config?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these are for our commands, the other are outgoing.