Skip to content

Commit aadbcde

Browse files
committed
chore: Reenable forcetypeassert
Signed-off-by: Lennard Eijsackers <[email protected]>
1 parent 01e198d commit aadbcde

File tree

8 files changed

+38
-4
lines changed

8 files changed

+38
-4
lines changed

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ linters:
4444
- varcheck
4545
- whitespace
4646
- gosec
47+
- forcetypeassert
4748
disable:
4849
# Should be readded in the future with a dedicated PR to do the fix
4950
- cyclop
5051
- exhaustive
51-
- forcetypeassert
5252
- funlen
5353
- gocognit
5454
- gofumpt

cache/filesystem_cache_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,10 @@ func (trw *testResponseWriter) Header() http.Header {
254254

255255
func (trw *testResponseWriter) WriteHeader(statusCode int) {}
256256

257+
func (trw *testResponseWriter) CloseNotify() <-chan bool {
258+
return nil
259+
}
260+
257261
func newTestCache(t *testing.T) *fileSystemCache {
258262
t.Helper()
259263

cache/tmp_file_response_writer.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ type TmpFileResponseWriter struct {
2525
}
2626

2727
func NewTmpFileResponseWriter(rw http.ResponseWriter, dir string) (*TmpFileResponseWriter, error) {
28+
_, ok := rw.(http.CloseNotifier)
29+
if !ok {
30+
return nil, fmt.Errorf("the response writer does not implement http.CloseNotifier")
31+
}
32+
2833
f, err := ioutil.TempFile(dir, "tmp")
2934
if err != nil {
3035
return nil, fmt.Errorf("cannot create temporary file in %q: %w", dir, err)
@@ -120,6 +125,7 @@ func (rw *TmpFileResponseWriter) GetCapturedContentEncoding() string {
120125

121126
// CloseNotify implements http.CloseNotifier
122127
func (rw *TmpFileResponseWriter) CloseNotify() <-chan bool {
128+
// nolint:forcetypeassert // it is guaranteed by NewTmpFileResponseWriter
123129
// The rw.FSResponseWriter must implement http.CloseNotifier.
124130
return rw.ResponseWriter.(http.CloseNotifier).CloseNotify()
125131
}

cache/tmp_file_response_writer_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ func (r *FakeResponse) WriteHeader(status int) {
3838
r.status = status
3939
}
4040

41+
func (r *FakeResponse) CloseNotify() <-chan bool {
42+
return nil
43+
}
44+
4145
const testTmpWriterDir = "./test-tmp-data"
4246

4347
func init() {

config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ func (c *Config) String() string {
7979

8080
func withoutSensitiveInfo(config *Config) *Config {
8181
const pswPlaceHolder = "XXX"
82+
83+
// nolint: forcetypeassert // no need to check type, it is specified by function.
8284
c := deepcopy.Copy(config).(*Config)
8385
for i := range c.Users {
8486
c.Users[i].Password = pswPlaceHolder

io.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ type StatResponseWriter interface {
2626
SetStatusCode(code int)
2727
}
2828

29+
var _ StatResponseWriter = &statResponseWriter{}
30+
2931
// statResponseWriter collects the amount of bytes written.
3032
//
3133
// The wrapped ResponseWriter must implement http.CloseNotifier.
@@ -107,9 +109,15 @@ func (rw *statResponseWriter) WriteHeader(statusCode int) {
107109
// CloseNotify implements http.CloseNotifier
108110
func (rw *statResponseWriter) CloseNotify() <-chan bool {
109111
// The rw.ResponseWriter must implement http.CloseNotifier
110-
return rw.ResponseWriter.(http.CloseNotifier).CloseNotify()
112+
rwc, ok := rw.ResponseWriter.(http.CloseNotifier)
113+
if !ok {
114+
panic("BUG: the wrapped ResponseWriter must implement http.CloseNotifier")
115+
}
116+
return rwc.CloseNotify()
111117
}
112118

119+
var _ io.ReadCloser = &statReadCloser{}
120+
113121
// statReadCloser collects the amount of bytes read.
114122
type statReadCloser struct {
115123
io.ReadCloser
@@ -123,6 +131,8 @@ func (src *statReadCloser) Read(p []byte) (int, error) {
123131
return n, err
124132
}
125133

134+
var _ io.ReadCloser = &cachedReadCloser{}
135+
126136
// cachedReadCloser caches the first 1Kb form the wrapped ReadCloser.
127137
type cachedReadCloser struct {
128138
io.ReadCloser

main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ func serveHTTP(rw http.ResponseWriter, r *http.Request) {
232232
switch r.URL.Path {
233233
case "/favicon.ico":
234234
case "/metrics":
235+
// nolint:forcetypeassert // We will cover this by tests as we control what is stored.
235236
an := allowedNetworksMetrics.Load().(*config.Networks)
236237
if !an.Contains(r.RemoteAddr) {
237238
err := fmt.Errorf("connections to /metrics are not allowed from %s", r.RemoteAddr)
@@ -243,15 +244,17 @@ func serveHTTP(rw http.ResponseWriter, r *http.Request) {
243244
promHandler.ServeHTTP(rw, r)
244245
case "/", "/query":
245246
var err error
246-
247+
// nolint:forcetypeassert // We will cover this by tests as we control what is stored.
247248
proxyHandler := proxyHandler.Load().(*ProxyHandler)
248249
r.RemoteAddr = proxyHandler.GetRemoteAddr(r)
249250

250251
var an *config.Networks
251252
if r.TLS != nil {
253+
// nolint:forcetypeassert // We will cover this by tests as we control what is stored.
252254
an = allowedNetworksHTTPS.Load().(*config.Networks)
253255
err = fmt.Errorf("https connections are not allowed from %s", r.RemoteAddr)
254256
} else {
257+
// nolint:forcetypeassert // We will cover this by tests as we control what is stored.
255258
an = allowedNetworksHTTP.Load().(*config.Networks)
256259
err = fmt.Errorf("http connections are not allowed from %s", r.RemoteAddr)
257260
}

proxy.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,12 @@ func (rp *reverseProxy) proxyRequest(s *scope, rw ResponseWriterWithCode, srw *s
253253
ctx, ctxCancel := context.WithCancel(ctx)
254254
defer ctxCancel()
255255
// rw must implement http.CloseNotifier.
256-
ch := rw.(http.CloseNotifier).CloseNotify()
256+
rwc, ok := rw.(http.CloseNotifier)
257+
if !ok {
258+
panic("BUG: the wrapped ResponseWriter must implement http.CloseNotifier")
259+
}
260+
261+
ch := rwc.CloseNotify()
257262
go func() {
258263
select {
259264
case <-ch:

0 commit comments

Comments
 (0)