Skip to content

chore: enable exhaustruct linter #8403

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

Merged
merged 7 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
exhaustruct httpmw package
  • Loading branch information
johnstcn committed Jul 11, 2023
commit bf2ab5e3c8c6055de745e5e8295506b5508580a7
3 changes: 3 additions & 0 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ func New(options *Options) *API {
RedirectToLogin: false,
DisableSessionExpiryRefresh: options.DeploymentValues.DisableSessionExpiryRefresh.Value(),
Optional: false,
SessionTokenFunc: nil, // Default behaviour
})
// Same as above but it redirects to the login page.
apiKeyMiddlewareRedirect := httpmw.ExtractAPIKeyMW(httpmw.ExtractAPIKeyConfig{
Expand All @@ -398,6 +399,7 @@ func New(options *Options) *API {
RedirectToLogin: true,
DisableSessionExpiryRefresh: options.DeploymentValues.DisableSessionExpiryRefresh.Value(),
Optional: false,
SessionTokenFunc: nil, // Default behaviour
})
// Same as the first but it's optional.
apiKeyMiddlewareOptional := httpmw.ExtractAPIKeyMW(httpmw.ExtractAPIKeyConfig{
Expand All @@ -406,6 +408,7 @@ func New(options *Options) *API {
RedirectToLogin: false,
DisableSessionExpiryRefresh: options.DeploymentValues.DisableSessionExpiryRefresh.Value(),
Optional: true,
SessionTokenFunc: nil, // Default behaviour
})

// API rate limit middleware. The counter is local and not shared between
Expand Down
2 changes: 1 addition & 1 deletion coderd/httpmw/hsts.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type HSTSConfig struct {
func HSTSConfigOptions(maxAge int, options []string) (HSTSConfig, error) {
if maxAge <= 0 {
// No header, so no need to build the header string.
return HSTSConfig{}, nil
return HSTSConfig{HeaderValue: ""}, nil
}

// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security
Expand Down
10 changes: 8 additions & 2 deletions coderd/httpmw/realip.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ func ExtractRealIP(config *RealIPConfig) func(next http.Handler) http.Handler {
// configuration and headers. It does not mutate the original request.
func ExtractRealIPAddress(config *RealIPConfig, req *http.Request) (net.IP, error) {
if config == nil {
config = &RealIPConfig{}
config = &RealIPConfig{
TrustedOrigins: []*net.IPNet{},
TrustedHeaders: []string{},
}
}

cf := isContainedIn(config.TrustedOrigins, getRemoteAddress(req.RemoteAddr))
Expand Down Expand Up @@ -208,7 +211,10 @@ func RealIP(ctx context.Context) *RealIPState {
// ParseRealIPConfig takes a raw string array of headers and origins
// to produce a config.
func ParseRealIPConfig(headers, origins []string) (*RealIPConfig, error) {
config := &RealIPConfig{}
config := &RealIPConfig{
TrustedOrigins: []*net.IPNet{},
TrustedHeaders: []string{},
}
for _, origin := range origins {
_, network, err := net.ParseCIDR(origin)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions enterprise/coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,15 @@ func New(ctx context.Context, options *Options) (_ *API, err error) {
RedirectToLogin: false,
DisableSessionExpiryRefresh: options.DeploymentValues.DisableSessionExpiryRefresh.Value(),
Optional: false,
SessionTokenFunc: nil, // Default behaviour
})
apiKeyMiddlewareOptional := httpmw.ExtractAPIKeyMW(httpmw.ExtractAPIKeyConfig{
DB: options.Database,
OAuth2Configs: oauthConfigs,
RedirectToLogin: false,
DisableSessionExpiryRefresh: options.DeploymentValues.DisableSessionExpiryRefresh.Value(),
Optional: true,
SessionTokenFunc: nil, // Default behaviour
})

deploymentID, err := options.Database.GetDeploymentID(ctx)
Expand Down
8 changes: 5 additions & 3 deletions site/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,14 @@ func (h *Handler) renderHTMLWithState(rw http.ResponseWriter, r *http.Request, f
// Cookies are sent when requesting HTML, so we can get the user
// and pre-populate the state for the frontend to reduce requests.
apiKey, actor, _ := httpmw.ExtractAPIKey(rw, r, httpmw.ExtractAPIKeyConfig{
Optional: true,
DB: h.opts.Database,
OAuth2Configs: h.opts.OAuth2Configs,
DB: h.opts.Database,
OAuth2Configs: h.opts.OAuth2Configs,
RedirectToLogin: false,
// Special case for site, we can always disable refresh here because
// the frontend will perform API requests if this fails.
DisableSessionExpiryRefresh: true,
Optional: true,
SessionTokenFunc: nil,
})
if apiKey != nil && actor != nil {
ctx := dbauthz.As(r.Context(), actor.Actor)
Expand Down