Skip to content

Commit 94dd243

Browse files
committed
exhaustruct httpmw package
1 parent c37b9ce commit 94dd243

File tree

5 files changed

+23
-5
lines changed

5 files changed

+23
-5
lines changed

coderd/coderd.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ func New(options *Options) *API {
389389
RedirectToLogin: false,
390390
DisableSessionExpiryRefresh: options.DeploymentValues.DisableSessionExpiryRefresh.Value(),
391391
Optional: false,
392+
SessionTokenFunc: nil, // Default behaviour
392393
})
393394
// Same as above but it redirects to the login page.
394395
apiKeyMiddlewareRedirect := httpmw.ExtractAPIKeyMW(httpmw.ExtractAPIKeyConfig{
@@ -397,6 +398,7 @@ func New(options *Options) *API {
397398
RedirectToLogin: true,
398399
DisableSessionExpiryRefresh: options.DeploymentValues.DisableSessionExpiryRefresh.Value(),
399400
Optional: false,
401+
SessionTokenFunc: nil, // Default behaviour
400402
})
401403
// Same as the first but it's optional.
402404
apiKeyMiddlewareOptional := httpmw.ExtractAPIKeyMW(httpmw.ExtractAPIKeyConfig{
@@ -405,6 +407,7 @@ func New(options *Options) *API {
405407
RedirectToLogin: false,
406408
DisableSessionExpiryRefresh: options.DeploymentValues.DisableSessionExpiryRefresh.Value(),
407409
Optional: true,
410+
SessionTokenFunc: nil, // Default behaviour
408411
})
409412

410413
// 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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,15 @@ func (h *Handler) renderHTMLWithState(rw http.ResponseWriter, r *http.Request, f
290290
// Cookies are sent when requesting HTML, so we can get the user
291291
// and pre-populate the state for the frontend to reduce requests.
292292
apiKey, actor, _ := httpmw.ExtractAPIKey(rw, r, httpmw.ExtractAPIKeyConfig{
293-
Optional: true,
294-
DB: h.opts.Database,
293+
DB: h.opts.Database,
294+
OAuth2Configs: &httpmw.OAuth2Configs{
295+
Github: nil,
296+
OIDC: nil,
297+
},
298+
RedirectToLogin: false,
299+
DisableSessionExpiryRefresh: false,
300+
Optional: true,
301+
SessionTokenFunc: nil,
295302
})
296303
if apiKey != nil && actor != nil {
297304
ctx := dbauthz.As(r.Context(), actor.Actor)

0 commit comments

Comments
 (0)