Skip to content

Commit bf2ab5e

Browse files
committed
exhaustruct httpmw package
1 parent c2c8ff2 commit bf2ab5e

File tree

5 files changed

+19
-6
lines changed

5 files changed

+19
-6
lines changed

coderd/coderd.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ func New(options *Options) *API {
390390
RedirectToLogin: false,
391391
DisableSessionExpiryRefresh: options.DeploymentValues.DisableSessionExpiryRefresh.Value(),
392392
Optional: false,
393+
SessionTokenFunc: nil, // Default behaviour
393394
})
394395
// Same as above but it redirects to the login page.
395396
apiKeyMiddlewareRedirect := httpmw.ExtractAPIKeyMW(httpmw.ExtractAPIKeyConfig{
@@ -398,6 +399,7 @@ func New(options *Options) *API {
398399
RedirectToLogin: true,
399400
DisableSessionExpiryRefresh: options.DeploymentValues.DisableSessionExpiryRefresh.Value(),
400401
Optional: false,
402+
SessionTokenFunc: nil, // Default behaviour
401403
})
402404
// Same as the first but it's optional.
403405
apiKeyMiddlewareOptional := httpmw.ExtractAPIKeyMW(httpmw.ExtractAPIKeyConfig{
@@ -406,6 +408,7 @@ func New(options *Options) *API {
406408
RedirectToLogin: false,
407409
DisableSessionExpiryRefresh: options.DeploymentValues.DisableSessionExpiryRefresh.Value(),
408410
Optional: true,
411+
SessionTokenFunc: nil, // Default behaviour
409412
})
410413

411414
// API rate limit middleware. The counter is local and not shared between

coderd/httpmw/hsts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type HSTSConfig struct {
2020
func HSTSConfigOptions(maxAge int, options []string) (HSTSConfig, error) {
2121
if maxAge <= 0 {
2222
// No header, so no need to build the header string.
23-
return HSTSConfig{}, nil
23+
return HSTSConfig{HeaderValue: ""}, nil
2424
}
2525

2626
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security

coderd/httpmw/realip.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ func ExtractRealIP(config *RealIPConfig) func(next http.Handler) http.Handler {
5656
// configuration and headers. It does not mutate the original request.
5757
func ExtractRealIPAddress(config *RealIPConfig, req *http.Request) (net.IP, error) {
5858
if config == nil {
59-
config = &RealIPConfig{}
59+
config = &RealIPConfig{
60+
TrustedOrigins: []*net.IPNet{},
61+
TrustedHeaders: []string{},
62+
}
6063
}
6164

6265
cf := isContainedIn(config.TrustedOrigins, getRemoteAddress(req.RemoteAddr))
@@ -208,7 +211,10 @@ func RealIP(ctx context.Context) *RealIPState {
208211
// ParseRealIPConfig takes a raw string array of headers and origins
209212
// to produce a config.
210213
func ParseRealIPConfig(headers, origins []string) (*RealIPConfig, error) {
211-
config := &RealIPConfig{}
214+
config := &RealIPConfig{
215+
TrustedOrigins: []*net.IPNet{},
216+
TrustedHeaders: []string{},
217+
}
212218
for _, origin := range origins {
213219
_, network, err := net.ParseCIDR(origin)
214220
if err != nil {

enterprise/coderd/coderd.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,15 @@ func New(ctx context.Context, options *Options) (_ *API, err error) {
8888
RedirectToLogin: false,
8989
DisableSessionExpiryRefresh: options.DeploymentValues.DisableSessionExpiryRefresh.Value(),
9090
Optional: false,
91+
SessionTokenFunc: nil, // Default behaviour
9192
})
9293
apiKeyMiddlewareOptional := httpmw.ExtractAPIKeyMW(httpmw.ExtractAPIKeyConfig{
9394
DB: options.Database,
9495
OAuth2Configs: oauthConfigs,
9596
RedirectToLogin: false,
9697
DisableSessionExpiryRefresh: options.DeploymentValues.DisableSessionExpiryRefresh.Value(),
9798
Optional: true,
99+
SessionTokenFunc: nil, // Default behaviour
98100
})
99101

100102
deploymentID, err := options.Database.GetDeploymentID(ctx)

site/site.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,12 +291,14 @@ func (h *Handler) renderHTMLWithState(rw http.ResponseWriter, r *http.Request, f
291291
// Cookies are sent when requesting HTML, so we can get the user
292292
// and pre-populate the state for the frontend to reduce requests.
293293
apiKey, actor, _ := httpmw.ExtractAPIKey(rw, r, httpmw.ExtractAPIKeyConfig{
294-
Optional: true,
295-
DB: h.opts.Database,
296-
OAuth2Configs: h.opts.OAuth2Configs,
294+
DB: h.opts.Database,
295+
OAuth2Configs: h.opts.OAuth2Configs,
296+
RedirectToLogin: false,
297297
// Special case for site, we can always disable refresh here because
298298
// the frontend will perform API requests if this fails.
299299
DisableSessionExpiryRefresh: true,
300+
Optional: true,
301+
SessionTokenFunc: nil,
300302
})
301303
if apiKey != nil && actor != nil {
302304
ctx := dbauthz.As(r.Context(), actor.Actor)

0 commit comments

Comments
 (0)