Repository navigation
Check for empty buffers instead of nil in responsewriter - #4576
Conversation
Use Case: I am using a service that implements `http.Handler` to send streaming video to the frontend. Reliably, when sending fragmented MP4 data on MacOS, the app would panic because the buffer being sent through the response passed a not-nil check but would panic when accessing the first byte of the buffer. This PR addresses that by checking if the buffer is not empty instead of nil, accounting for the case where len(buf) == 0 and cap(buf) > 0, or maybe where buf[0] == '\0' (I'm not sure how the nil checks work for slices)
WalkthroughUpdates the unreleased changelog with a new Fixed entry. Adjusts responseWriter.Write on macOS to handle zero-length buffers safely by checking Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant C as Caller
participant RW as responseWriter.Write
participant WB as WebView Bridge (cgo)
participant UST as macOS URLSchemeTask
rect rgba(200,230,255,0.25)
note over RW: Prepare data for URLSchemeTaskDidReceiveData
C->>RW: Write(buf)
alt buf length > 0
RW->>RW: content = &buf[0]\ncontentLen = len(buf)
else buf length == 0
RW->>RW: content = nil\ncontentLen = 0
end
end
rect rgba(220,255,220,0.18)
RW->>WB: URLSchemeTaskDidReceiveData(content, contentLen)
WB->>UST: Deliver data pointer and length
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Pre-merge checks (3 passed)✅ Passed checks (3 passed)
Poem
Tip 👮 Agentic pre-merge checks are now available in preview!Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.
Please see the documentation for more information. Example: reviews:
pre_merge_checks:
custom_checks:
- name: "Undocumented Breaking Changes"
mode: "warning"
instructions: |
Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).Please share your feedback with us on this Discord post. 📜 Recent review detailsConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
✨ Finishing touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
v3/internal/assetserver/webview/responsewriter_darwin.go (1)
130-133: Consider applying the same fix pattern to header data handling.Similar to the buffer fix above, this code has the same potential vulnerability pattern. While
json.Marshalis unlikely to return a zero-length slice with non-zero capacity, applying the same defensive pattern would be consistent and safer.- if len(headerData) != 0 { + if len(headerData) > 0 { headers = unsafe.Pointer(&headerData[0]) headersLen = len(headerData) }
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
v3/UNRELEASED_CHANGELOG.md(1 hunks)v3/internal/assetserver/webview/responsewriter_darwin.go(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
- GitHub Check: Run Go Tests v3 (macos-latest, 1.24)
- GitHub Check: Run Go Tests v3 (ubuntu-latest, 1.24)
- GitHub Check: Run Go Tests v3 (windows-latest, 1.24)
🔇 Additional comments (2)
v3/UNRELEASED_CHANGELOG.md (1)
28-28: LGTM!The changelog entry accurately describes the fix and follows the project's established format. The description "AssetServer can crash on MacOS" clearly communicates the impact, and the PR reference and credit attribution are properly formatted.
v3/internal/assetserver/webview/responsewriter_darwin.go (1)
104-107: Excellent fix for the panic vulnerability!This change correctly addresses the root cause of the panic. The previous
buf != nilcheck was insufficient because Go slices can be non-nil but have zero length (e.g.,make([]byte, 0, 10)), which would cause a panic when accessingbuf[0]. The newlen(buf) > 0check ensures safe array access and proper handling of empty buffers.The logic correctly handles both cases:
- Empty buffer:
contentremainsnil,contentLenis0- Non-empty buffer:
contentpoints to first byte,contentLenis the buffer lengthThis preserves the intended behavior while eliminating the crash scenario.
nil and len check
|
|
Great catch! Thank you! 🙏 |
* Check for empty buffers instead of nil in responsewriter Use Case: I am using a service that implements `http.Handler` to send streaming video to the frontend. Reliably, when sending fragmented MP4 data on MacOS, the app would panic because the buffer being sent through the response passed a not-nil check but would panic when accessing the first byte of the buffer. This PR addresses that by checking if the buffer is not empty instead of nil, accounting for the case where len(buf) == 0 and cap(buf) > 0, or maybe where buf[0] == '\0' (I'm not sure how the nil checks work for slices) * Update UNRELEASED_CHANGELOG * Update v3/internal/assetserver/webview/responsewriter_darwin.go nil and len check --------- Co-authored-by: Atterpac <[email protected]>



Description
Use Case:
I am using a service that implements
http.Handlerto send streaming video to the frontend. Reliably, when sending fragmented MP4 data on MacOS, the app would panic because the buffer being sent through the response passed a not-nil check but would panic when accessing the first byte of the buffer.This PR addresses that by checking if the buffer is not empty instead of nil, accounting for the case where len(buf) == 0 and cap(buf) > 0, or maybe where buf[0] == '\0' (I'm not sure how the nil checks work for slices)
Fixes # (issue)
Type of change
Please select the option that is relevant.
How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration using
wails doctor.If you checked Linux, please specify the distro and version.
Test Configuration
Please paste the output of
wails doctor. If you are unable to run this command, please describe your environment in as much detail as possible.Checklist:
website/src/pages/changelog.mdxwith details of this PRSummary by CodeRabbit