Fix unhandled JSON.parse in upload metadata parameters#149
Merged
Conversation
Wrap base64 + JSON decoding of customMetadata and httpMetadata query parameters in try-catch blocks for PutObject and CreateUpload endpoints. Previously, malformed input would crash with an unhandled exception returning a 500 error. Now returns a descriptive 400 error instead. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Deploying with
|
| Status | Name | Latest Commit | Preview URL | Updated (UTC) |
|---|---|---|---|---|
| ✅ Deployment successful! View logs |
r2-explorer-docs | 8581550 | Commit Preview URL Branch Preview URL |
Mar 08 2026, 02:08 AM |
G4brym
commented
Mar 8, 2026
Owner
Author
G4brym
left a comment
There was a problem hiding this comment.
Automated Code Review — APPROVED ✅
Review Scores: 5/5 reviewers approved
Summary
Reviewed the addition of try-catch guards around JSON.parse(decodeURIComponent(escape(atob(...)))) for customMetadata and httpMetadata in both PutObject and CreateUpload endpoints. The change correctly converts unhandled 500 crashes into descriptive 400 responses. Four new integration tests cover the malformed metadata paths.
Review Perspectives
- Correctness: ✅ Try-catch covers the full decode chain (atob → escape → decodeURIComponent → JSON.parse), all failure modes caught
- Security: ✅ Improves error handling — static error messages, no user input reflection, uses established HTTPException pattern
- Performance: ✅ Negligible overhead from try-catch on a request-handling path
- Code Quality: ✅ Consistent pattern across both endpoints, proper import added to createUpload.ts, clear error messages
- Testing: ✅ 4 new integration tests verify 400 status and error messages for both parameters on both endpoints
Minor notes (non-blocking)
- Pre-existing:
createUpload.tsusesResponse.json()for bucket-not-found butHTTPExceptionfor metadata errors — minor inconsistency not introduced by this PR - Tests cover invalid JSON in valid base64; could additionally test invalid base64 strings to exercise the
atobfailure path (both are caught by the same catch block)
🤖 Automated review by prodboard
This was referenced Mar 9, 2026
Merged
This was referenced Mar 16, 2026
This was referenced Mar 23, 2026
This was referenced Apr 3, 2026
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
JSON.parse()calls forcustomMetadataandhttpMetadataquery parameters in try-catch blocks inPutObjectandCreateUploadendpointsWhat changed
packages/worker/src/modules/buckets/putObject.ts— Added try-catch aroundJSON.parse(decodeURIComponent(escape(atob(...))))for bothcustomMetadataandhttpMetadataquery parameters. On failure, throwsHTTPException(400)with a descriptive message.packages/worker/src/modules/buckets/multipart/createUpload.ts— Same fix applied. Also added the missingHTTPExceptionimport fromhono/http-exception.Tests — Added tests in
buckets.test.tsandmultipart.test.tsthat send invalid base64-encoded JSON and verify a 400 response is returned.Why
Both upload endpoints accept optional
customMetadataandhttpMetadataas base64-encoded JSON strings in query parameters. If a client sends a valid base64 string that decodes to invalid JSON (or an invalid base64 string), theJSON.parse()call throws an unhandled exception, resulting in a generic 500 error. This makes debugging difficult for API consumers. The fix returns a clear 400 error message indicating which parameter is invalid.Test plan
🤖 Generated with Claude Code