fix: race in Diff mode crashes Hoverfly (GHSA-qrh4-p6v4-mrfg)#1227
Merged
Conversation
Diff mode's responsesDiff map had no synchronization, so concurrent proxy requests tripped Go's built-in concurrent-map check and crashed the process with "fatal error: concurrent map read and map write". Add a dedicated sync.RWMutex covering AddDiff, ClearDiff, GetDiff, and GetFilteredDiff. GetDiff now returns a deep snapshot so DiffHandler can iterate the result outside the lock without re-introducing the race. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
fatal error: concurrent map read and map writeunder concurrent proxy traffic becauseresponsesDiff(incore/hoverfly.go) was a plainmapwith no synchronization. Trivially triggerable: any client with proxy access could send a handful of simultaneous requests and DoS the process. Reported as GHSA-qrh4-p6v4-mrfg.sync.RWMutexnext toresponsesDiff(mirroring thestate.Statepattern) and wrapped all four accessors:AddDiff/ClearDiff(write lock),GetDiff/GetFilteredDiff(read lock).GetDiffnow returns a deep snapshot soDiffHandler.convertToResponseDiffViewcan iterate the result after the lock is released without reintroducing the race.Test plan
go test -race -count=1 -v -run 'Test_Hoverfly_AddDiff|Test_Hoverfly_Diff_ConcurrentAccess|Test_Hoverfly_processRequest_CanHandleResponseDiff' github.com/SpectoLabs/hoverfly/core— all green.Test_Hoverfly_Diff_ConcurrentAccess(32 writer goroutines × 64 readers × 1 clearer × 100 iterations each) — reliably trips Go's map-race check on pre-patch code; passes under-racewith the patch (~110 ms).go vet github.com/SpectoLabs/hoverfly/core— clean.🤖 Generated with Claude Code