Skip to content

validate resource name format before PopFirst in bundle parsing#16221

Open
alhudz wants to merge 1 commit into
firebase:mainfrom
alhudz:bundle-name-popfirst-guard
Open

validate resource name format before PopFirst in bundle parsing#16221
alhudz wants to merge 1 commit into
firebase:mainfrom
alhudz:bundle-name-popfirst-guard

Conversation

@alhudz

@alhudz alhudz commented Jun 2, 2026

Copy link
Copy Markdown

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.

@gemini-code-assist

Copy link
Copy Markdown
Contributor
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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.

@google-cla

google-cla Bot commented Jun 2, 2026

Copy link
Copy Markdown

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.

@paulb777

paulb777 commented Jun 5, 2026

Copy link
Copy Markdown
Member

@alhudz Thanks for the PR. Please sign the CLA and we'll take a look

@alhudz

alhudz commented Jun 5, 2026

Copy link
Copy Markdown
Author

@alhudz Thanks for the PR. Please sign the CLA and we'll take a look

Done

@paulb777

Copy link
Copy Markdown
Member

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

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 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.

Comment on lines +403 to +404
if (!rpc_serializer_.IsLocalResourceName(path) || path.size() <= 4 ||
path[4] != "documents") {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

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.

Suggested change
if (!rpc_serializer_.IsLocalResourceName(path) || path.size() <= 4 ||
path[4] != "documents") {
if (!rpc_serializer_.IsLocalResourceName(path) || path.size() < 5 ||
path[4] != "documents") {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to path.size() < 5, reads better against the PopFirst(5) below. Behaviour is the same.

Comment thread Firestore/core/src/remote/serializer.cc Outdated
Comment on lines +1524 to +1525
return IsLocalResourceName(resource) && resource.size() > 4 &&
resource[4] == "documents" &&

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

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.

Suggested change
return IsLocalResourceName(resource) && resource.size() > 4 &&
resource[4] == "documents" &&
return IsLocalResourceName(resource) && resource.size() >= 5 &&
resource[4] == "documents" &&

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, resource.size() >= 5 now.

@cherylEnkidu

Copy link
Copy Markdown
Contributor

I will take a look for this next week.

@alhudz alhudz force-pushed the bundle-name-popfirst-guard branch from a2da660 to 56c5931 Compare June 16, 2026 09:12
@alhudz

alhudz commented Jun 16, 2026

Copy link
Copy Markdown
Author

Folded in the two readability tweaks from the review (< 5 / >= 5 rather than <= 4 / > 4); the checks are behaviour-identical, just clearer against the PopFirst(5). The actual guard is the path[4] != "documents" segment check, which stops a 4-segment projects/p/databases/d name from reaching PopFirst(5) and tripping its length assert. Ready whenever you get to it.

@paulb777

Copy link
Copy Markdown
Member

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

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 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.

Comment on lines +403 to +404
if (!rpc_serializer_.IsLocalResourceName(path) || path.size() < 5 ||
path[4] != "documents") {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants