Skip to content

validate resource name format before PopFirst in bundle parsing#16221

Open
alhudz wants to merge 2 commits into
firebase:mainfrom
alhudz:bundle-name-popfirst-guard
Open

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

Conversation

@alhudz

@alhudz alhudz commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

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
Contributor 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
Contributor 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
Contributor 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
Contributor 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 thread Firestore/core/src/bundle/bundle_serializer.cc
@alhudz

alhudz commented Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

any update?

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

Thank you so much for your contribution! The PR looks great overall—there are just a few minor things that could be improved.

return IsLocalResourceName(resource) &&
return IsLocalResourceName(resource) && resource.size() >= 5 &&
resource[4] == "documents" &&
DocumentKey::IsDocumentKey(resource.PopFirst(5));

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.

The current implementation calls DocumentKey::IsDocumentKey(resource.PopFirst(5)). PopFirst allocates a new ResourcePath vector and copies all segment strings in memory. This allocation is unnecessary because we only need to verify if the number of segments remaining after the database prefix is even.

Suggested Diff

bool Serializer::IsLocalDocumentKey(absl::string_view path) const {
  auto resource = ResourcePath::FromStringView(path);
  return IsLocalResourceName(resource) && resource.size() >= 5 &&
         resource[4] == "documents" &&
-        DocumentKey::IsDocumentKey(resource.PopFirst(5));
+        (resource.size() - 5) % 2 == 0;
}

if (!reader.ok()) {
return {};
}
if (!DocumentKey::IsDocumentKey(path)) {

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.

-  if (!DocumentKey::IsDocumentKey(path)) {
+  if (path.empty() || !DocumentKey::IsDocumentKey(path)) {

if (!reader.ok()) {
return {};
}
if (!DocumentKey::IsDocumentKey(path)) {

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.

-  if (!DocumentKey::IsDocumentKey(path)) {
+  if (path.empty() || !DocumentKey::IsDocumentKey(path)) {

}
}

TEST_F(BundleSerializerTest, DecodeNonDocumentKeyResourceNameFails) {

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.

The current DecodeNonDocumentKeyResourceNameFails test only validates collection paths. Could you expand the test suite to verify that:

Short paths (size < 5, e.g., /projects/p/databases/default) fail gracefully instead of crashing.
Root documents paths (which resolve to an empty key) fail gracefully.

for example:

TEST_F(BundleSerializerTest, DecodeNonDocumentKeyResourceNameFails) {
  // 1. Collection Path (Odd number of segments, fails key check)
  {
    ProtoDocument document = TestDocument(ProtoValue());
    document.set_name(FullPath("bundle"));
    std::string json_string;
    MessageToJsonString(document, &json_string);

    JsonReader reader;
    bundle_serializer.DecodeDocument(reader, Parse(json_string));
    EXPECT_NOT_OK(reader.status());
  }

  // 2. Root Path (Popped path is empty, size 0, must fail)
  {
    ProtoDocument document = TestDocument(ProtoValue());
    document.set_name(FullPath(""));
    std::string json_string;
    MessageToJsonString(document, &json_string);

    JsonReader reader;
    bundle_serializer.DecodeDocument(reader, Parse(json_string));
    EXPECT_NOT_OK(reader.status());
  }

  // 3. Short Path (Size < 5, must fail prefix validation instead of crashing PopFirst)
  {
    ProtoDocument document = TestDocument(ProtoValue());
    document.set_name("projects/p/databases/default");
    std::string json_string;
    MessageToJsonString(document, &json_string);

    JsonReader reader;
    bundle_serializer.DecodeDocument(reader, Parse(json_string));
    EXPECT_NOT_OK(reader.status());
  }
  
  // (Duplicate these three cases for DecodeDocumentMetadata below as well)
}

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