chore: Add linter rule to prevent breaking of sse (#4144)

* chore: Add linter rule to prevent breaking of sse
This commit is contained in:
Steven Masley 2022-09-27 11:14:58 -04:00 committed by GitHub
parent 5a449bf86f
commit 27c8345ef2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 28 additions and 0 deletions

View File

@ -17,6 +17,7 @@ package gorules
import (
"github.com/quasilyte/go-ruleguard/dsl"
"github.com/quasilyte/go-ruleguard/dsl/types"
)
// Use xerrors everywhere! It provides additional stacktrace info!
@ -238,3 +239,30 @@ func ProperRBACReturn(m dsl.Matcher) {
}
`).Report("Must write to 'ResponseWriter' before returning'")
}
// FullResponseWriter ensures that any overridden response writer has full
// functionality. Mainly is hijackable and flushable.
func FullResponseWriter(m dsl.Matcher) {
m.Match(`
type $w struct {
$*_
http.ResponseWriter
$*_
}
`).
At(m["w"]).
Where(m["w"].Filter(notImplementsFullResponseWriter)).
Report("ResponseWriter \"$w\" must implement http.Flusher and http.Hijacker")
}
// notImplementsFullResponseWriter returns false if the type does not implement
// http.Flusher, http.Hijacker, and http.ResponseWriter.
func notImplementsFullResponseWriter(ctx *dsl.VarFilterContext) bool {
flusher := ctx.GetInterface(`net/http.Flusher`)
hijacker := ctx.GetInterface(`net/http.Hijacker`)
writer := ctx.GetInterface(`net/http.ResponseWriter`)
p := types.NewPointer(ctx.Type)
return !(types.Implements(p, writer) || types.Implements(ctx.Type, writer)) ||
!(types.Implements(p, flusher) || types.Implements(ctx.Type, flusher)) ||
!(types.Implements(p, hijacker) || types.Implements(ctx.Type, hijacker))
}