Skip to content

Commit 3f6a158

Browse files
authored
chore: enable exhaustruct linter (#8403)
* chore: enable exhaustruct linter * add exlusion rules * move to allowlist instead * exhaustruct httpmw package * fixup! exhaustruct httpmw package * make lint * address PR comments
1 parent 75f62dc commit 3f6a158

File tree

7 files changed

+31
-4
lines changed

7 files changed

+31
-4
lines changed

.golangci.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
# Over time we should try tightening some of these.
33

44
linters-settings:
5+
exhaustruct:
6+
include:
7+
# Gradually extend to cover more of the codebase.
8+
- 'httpmw\.\w+'
59
gocognit:
610
min-complexity: 46 # Min code complexity (def 30).
711

@@ -195,6 +199,10 @@ issues:
195199
# We use assertions rather than explicitly checking errors in tests
196200
- errcheck
197201
- forcetypeassert
202+
- exhaustruct # This is unhelpful in tests.
203+
- path: scripts/*
204+
linters:
205+
- exhaustruct
198206

199207
fix: true
200208
max-issues-per-linter: 0
@@ -219,6 +227,7 @@ linters:
219227
- errcheck
220228
- errname
221229
- errorlint
230+
- exhaustruct
222231
- exportloopref
223232
- forcetypeassert
224233
- gocritic

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 behavior
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 behavior
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 behavior
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: 12 additions & 3 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: nil,
61+
TrustedHeaders: nil,
62+
}
6063
}
6164

6265
cf := isContainedIn(config.TrustedOrigins, getRemoteAddress(req.RemoteAddr))
@@ -81,7 +84,10 @@ func ExtractRealIPAddress(config *RealIPConfig, req *http.Request) (net.IP, erro
8184
// of each proxy header is set.
8285
func FilterUntrustedOriginHeaders(config *RealIPConfig, req *http.Request) {
8386
if config == nil {
84-
config = &RealIPConfig{}
87+
config = &RealIPConfig{
88+
TrustedOrigins: nil,
89+
TrustedHeaders: nil,
90+
}
8591
}
8692

8793
cf := isContainedIn(config.TrustedOrigins, getRemoteAddress(req.RemoteAddr))
@@ -208,7 +214,10 @@ func RealIP(ctx context.Context) *RealIPState {
208214
// ParseRealIPConfig takes a raw string array of headers and origins
209215
// to produce a config.
210216
func ParseRealIPConfig(headers, origins []string) (*RealIPConfig, error) {
211-
config := &RealIPConfig{}
217+
config := &RealIPConfig{
218+
TrustedOrigins: []*net.IPNet{},
219+
TrustedHeaders: []string{},
220+
}
212221
for _, origin := range origins {
213222
_, network, err := net.ParseCIDR(origin)
214223
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 behavior
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 behavior
98100
})
99101

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

install.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ distro() {
527527

528528
if [ -f /etc/os-release ]; then
529529
(
530+
# shellcheck disable=SC1091
530531
. /etc/os-release
531532
if [ "${ID_LIKE-}" ]; then
532533
for id_like in $ID_LIKE; do
@@ -553,6 +554,7 @@ distro_name() {
553554

554555
if [ -f /etc/os-release ]; then
555556
(
557+
# shellcheck disable=SC1091
556558
. /etc/os-release
557559
echo "$PRETTY_NAME"
558560
)

site/site.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,8 @@ func (h *Handler) renderHTMLWithState(rw http.ResponseWriter, r *http.Request, f
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+
RedirectToLogin: false,
301+
SessionTokenFunc: nil,
300302
})
301303
if apiKey != nil && actor != nil {
302304
ctx := dbauthz.As(r.Context(), actor.Actor)

0 commit comments

Comments
 (0)