validate resource name format before PopFirst in bundle parsing#16221
validate resource name format before PopFirst in bundle parsing#16221alhudz wants to merge 1 commit into
Conversation
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 the 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 counterproductive. 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. |
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
|
@alhudz Thanks for the PR. Please sign the CLA and we'll take a look |
Done |
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request adds validation checks to ensure that Firestore resource paths have at least 5 segments and that the 5th segment (index 4) is "documents" before processing them, preventing potential out-of-bounds access. The review comments suggest minor readability improvements to make the size checks (< 5 and >= 5) more intuitive and directly aligned with the 5-segment requirement.
| if (!rpc_serializer_.IsLocalResourceName(path) || path.size() <= 4 || | ||
| path[4] != "documents") { |
There was a problem hiding this comment.
Using path.size() < 5 is slightly more readable here as it directly aligns with the 5 segments required to safely call path.PopFirst(5) later in the function.
| if (!rpc_serializer_.IsLocalResourceName(path) || path.size() <= 4 || | |
| path[4] != "documents") { | |
| if (!rpc_serializer_.IsLocalResourceName(path) || path.size() < 5 || | |
| path[4] != "documents") { |
There was a problem hiding this comment.
Switched to path.size() < 5, reads better against the PopFirst(5) below. Behaviour is the same.
| return IsLocalResourceName(resource) && resource.size() > 4 && | ||
| resource[4] == "documents" && |
There was a problem hiding this comment.
Using resource.size() >= 5 is slightly more intuitive than resource.size() > 4 as it directly corresponds to the 5 segments required for resource.PopFirst(5) and makes the relationship with index 4 (resource[4]) clearer.
| return IsLocalResourceName(resource) && resource.size() > 4 && | |
| resource[4] == "documents" && | |
| return IsLocalResourceName(resource) && resource.size() >= 5 && | |
| resource[4] == "documents" && |
|
I will take a look for this next week. |
a2da660 to
56c5931
Compare
|
Folded in the two readability tweaks from the review ( |
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request adds path validation to ensure resource paths have at least 5 segments and that the 5th segment is "documents" before processing them, preventing potential out-of-bounds crashes. The reviewer notes that while this prevents a crash in PopFirst(5), DecodeName can still return paths that are not valid document keys, which could still trigger a crash in DecodeDocumentMetadata and DecodeDocument. The reviewer suggests adding validation using DocumentKey::IsDocumentKey in those functions.
| if (!rpc_serializer_.IsLocalResourceName(path) || path.size() < 5 || | ||
| path[4] != "documents") { |
There was a problem hiding this comment.
While this change successfully prevents a crash in PopFirst(5) for paths with fewer than 5 segments, DecodeName can still return a path that is not a valid document key (e.g., an odd number of segments or an empty path).
Because DecodeDocumentMetadata (line 643) and DecodeDocument (line 674) directly construct DocumentKey(path) using the result of DecodeName without checking DocumentKey::IsDocumentKey(path), any malformed bundle containing a non-document path (like a collection path or root documents path) will still trigger a HARD_ASSERT and crash the application.
Since DecodeName is also used for queries (where collection paths are valid), we should not enforce IsDocumentKey inside DecodeName itself. Instead, DecodeDocumentMetadata and DecodeDocument should be updated to validate the path using DocumentKey::IsDocumentKey(path) and call reader.Fail(...) if it is invalid, before attempting to construct the DocumentKey.
DecodeName and IsLocalDocumentKey accept a resource name from bundle input and call PopFirst(5) after only checking IsLocalResourceName, which still allows a four-segment name like projects/p/databases/d. That trips the length assertion inside PopFirst. ExtractLocalPathFromResourceName and DocumentKey::FromName already require size() > 4 and a documents segment, so this applies the same format check at the two bundle-reachable sites.