-
Notifications
You must be signed in to change notification settings - Fork 184
fix(server): prevent service methods from mutating files in repository #1722
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: master
Are you sure you want to change the base?
Conversation
Several service methods (BalanceFile, BuildFile, GetFileContents, SegmentFileID, FlattenBatches) were mutating files stored in the in-memory repository because FindFile() returns a direct pointer to the stored object. This caused data corruption where: - BalanceFile modified the original file's batches and ID, then stored the same pointer under a new key, leaving two keys pointing to one mutated object - Other methods called Create() on repository files, modifying their control totals The fix adds a cloneFile() helper that creates a deep copy via JSON serialization before any mutations. This ensures the original files in the repository remain unchanged. Added regression tests to verify original files are preserved after each operation
Summary of ChangesHello @MorganaFuture, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a significant data integrity problem within the server's ACH file processing logic. Previously, various service methods were directly modifying file objects retrieved from the repository, causing unintended side effects and corrupting stored data. The core solution involves introducing a deep cloning mechanism for these files, ensuring that any operation performed on a file is done on a copy, thereby safeguarding the original data in the repository from mutation. This change enhances the robustness and reliability of file handling. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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.
Code Review
This pull request effectively addresses a critical data corruption bug by preventing service methods from mutating files in the in-memory repository. The introduction of a cloneFile helper to create deep copies of ach.File objects before modification is a solid approach. The implementation of cloneFile using JSON serialization is well-reasoned and correctly handles validation options. The accompanying regression tests are excellent and provide strong confidence in the fix for the modified methods.
However, I've identified a similar potential mutation issue in the MergeFiles method, which was not modified in this PR. It appears to retrieve files from the repository and use them directly without cloning, which could lead to the same data corruption this PR aims to solve. I strongly recommend applying the same cloning pattern to MergeFiles within this pull request to ensure all mutation paths are covered.
Additionally, there are minor inconsistencies in error handling in the modified methods that could be improved for better debuggability and consistency, which I've pointed out in specific comments.
| if err != nil { | ||
| return nil, fmt.Errorf("build file: error cloning file %s: %v", id, err) | ||
| } |
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.
For better error inspection and consistency with other parts of the code, it's preferable to use %w for error wrapping instead of %v. This allows for unwrapping the error chain using errors.Is or errors.As.
| if err != nil { | |
| return nil, fmt.Errorf("build file: error cloning file %s: %v", id, err) | |
| } | |
| if err != nil { | |
| return nil, fmt.Errorf("build file: error cloning file %s: %w", id, err) | |
| } | |
| if err != nil { | ||
| return nil, fmt.Errorf("problem cloning file %s: %v", id, err) | ||
| } |
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.
Similar to other parts of the code, using %w for error wrapping is preferred over %v for better error handling capabilities.
| if err != nil { | |
| return nil, fmt.Errorf("problem cloning file %s: %v", id, err) | |
| } | |
| if err != nil { | |
| return nil, fmt.Errorf("problem cloning file %s: %w", id, err) | |
| } | |
| if err != nil { | ||
| return nil, err | ||
| } |
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.
The error returned from cloneFile is not wrapped with any context. For consistency with the other methods modified in this PR and for better debuggability, it's a good practice to wrap this error. The other functions add context like which operation failed and for which file ID.
| if err != nil { | |
| return nil, err | |
| } | |
| if err != nil { | |
| return nil, fmt.Errorf("balance file: failed to clone file %s: %w", fileID, err) | |
| } | |
Several service methods (BalanceFile, BuildFile, GetFileContents, SegmentFileID, FlattenBatches) were mutating files stored in the in-memory repository because FindFile() returns a direct pointer to the stored object.
This caused data corruption where:
The fix adds a cloneFile() helper that creates a deep copy via JSON serialization before any mutations. This ensures the original files in the repository remain unchanged.
Added regression tests to verify original files are preserved after each operation