-
Notifications
You must be signed in to change notification settings - Fork 35
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
base: main
Are you sure you want to change the base?
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.
|
|
||
| if (encoding === 'base64') { | ||
| // Content is already base64 encoded, decode it directly to file | ||
| command = `echo '${content}' | base64 -d > ${escapedPath}`; |
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.
can you validate + sanitize the content here? this will cause issues if someone sends content with base64 encoding but content is abc'; rm -rf / #
| const requestedEncoding = options.encoding; | ||
| let content: string; | ||
| let actualEncoding: 'utf-8' | 'base64'; | ||
| let isBinary: boolean; |
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.
Is this even being used?
|
(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:
|
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)