Skip to content

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

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 3 commits into from
Sep 27, 2022
Merged
Changes from all commits
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
28 changes: 28 additions & 0 deletions scripts/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -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))
}