Skip to content
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

internal/appsec: fix blocking atomicity #2955

Merged
merged 1 commit into from
Oct 30, 2024
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
21 changes: 11 additions & 10 deletions internal/appsec/emitter/httpsec/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func BeforeHandle(
PathParams: pathParams,
})
tr := r.WithContext(ctx)
var blocked atomic.Bool

afterHandle := func() {
var statusCode int
Expand All @@ -147,27 +148,27 @@ func BeforeHandle(
StatusCode: statusCode,
}, span)

if blockPtr := blockAtomic.Swap(nil); blockPtr != nil {
blockPtr.Handler.ServeHTTP(w, tr)
blocked.Store(true)
}

// Execute the onBlock functions to make sure blocking works properly
// in case we are instrumenting the Gin framework
if blockPtr := blockAtomic.Load(); blockPtr != nil {
if blocked.Load() {
for _, f := range opts.OnBlock {
f()
}

if blockPtr.Handler != nil {
blockPtr.Handler.ServeHTTP(w, tr)
}
}
}

handled := false
if blockPtr := blockAtomic.Load(); blockPtr != nil && blockPtr.Handler != nil {
if blockPtr := blockAtomic.Swap(nil); blockPtr != nil {
// handler is replaced
blockPtr.Handler.ServeHTTP(w, tr)
blockPtr.Handler = nil
handled = true
blocked.Store(true)
}
return w, tr, afterHandle, handled

return w, tr, afterHandle, blocked.Load()
}

// WrapHandler wraps the given HTTP handler with the abstract HTTP operation defined by HandlerOperationArgs and
Expand Down
Loading