-
Notifications
You must be signed in to change notification settings - Fork 40
fix: Handle encoding parameter correctly in writeFile and readFile me… #167
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
Conversation
|
ghostwriternr
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for sending this PR! I think this needs to be tested more - can you pls add tests and ensure you've tested them manually too. And add a changeset as well.
|
(sorry just setting up the ci properly for external contributors. re-opening this in a moment) |
…perations - Add base64 content validation in writeFile to prevent command injection - Validate content contains only valid base64 characters (A-Z, a-z, 0-9, +, /, =) - Return VALIDATION_FAILED error for invalid content - Change from 'echo' to 'printf' for safer shell handling - Support user-specified encoding in readFile - Respect encoding parameter (base64/utf-8/utf8) over MIME detection - Allow forcing base64 for text files or utf-8 for binary files - Maintain backward compatibility with auto-detection when no encoding specified - Add comprehensive test coverage - 4 tests for encoding parameter support - 5 tests for base64 validation and security - All tests passing (35/35 in file-service.test.ts) Addresses reviewer feedback from PR cloudflare#167: - Sanitize base64 content to prevent command injection attacks - Remove unused variable declarations
SummaryThis PR addresses the reviewer feedback from #167 by adding security validation and proper encoding parameter handling in file operations. Changes1. Security: Base64 Content Validation
2. Feature: Respect Encoding Parameter
3. Code Quality
Test CoverageSecurity Tests:
Encoding Tests:
Backward Compatibility✅ Fully compatible - no breaking changes
Files Changed
RelatedAddresses reviewer comments in #167:
|
Remove redundant intermediate variables and assignments in read/write. Clarify shell command comments to accurately explain printf usage and base64 encoding rationale. Use printf consistently for both paths.
Remove 'utf8'/'utf-8' defaults from SDK client and HTTP handler, allowing file service to perform MIME-based detection when encoding is not explicitly specified. This fixes binary file detection.
ghostwriternr
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @lacia-hIE ! Apologies for the long delay on this one, as well as for all the clutter with the repo setup for external contributors. The overall change is quite solid, and I just did some quick cleanup + importantly fixed e2e tests that were failing. See b32abbd. The change looks great overall and good to merge. Thanks for your contribution!
Fix: Handle encoding parameter correctly in writeFile and readFile methods
Problem Description
The
encodingparameter was not being handled correctly in bothsandbox.writeFileandsandbox.readFilemethods:writeFile Issues
options.encodingparameterbase64encodingreadFile Issues
options.encodingparameterFix Details
1. Fix FileService.write Method
File:
packages/sandbox-container/src/services/file-service.tsBefore:
After:
2. Fix FileService.read Method
File:
packages/sandbox-container/src/services/file-service.tsBefore:
After:
Usage Examples
writeFile After Fix
readFile After Fix
Test Coverage
Existing test cases already cover these scenarios:
packages/sandbox/tests/file-client.test.tsline 154: Tests base64 encoding writepackages/sandbox/tests/file-client.test.tsline 251: Tests base64 encoding readpackages/sandbox-container/tests/services/file-service.test.ts: Contains binary file handling testsBackward Compatibility
✅ Fully backward compatible
encodingparameter is not specified, behavior is identical to beforeencodingparameterImpact Scope
Related Issues
This fix resolves the following user-reported problems:
sandbox.writeFilewithencoding: 'base64'parameter was not working correctlysandbox.readFilecould not force a specific encoding formatChecklist
Modified Files:
packages/sandbox-container/src/services/file-service.tsChange Type: Bug Fix
Priority: High (affects binary file handling functionality)