Skip to content

fix(firestore): only reject ids fully matching the reserved __.*__ pattern - #7858

Closed
CorgiBoyG wants to merge 1 commit into
google:mainfrom
CorgiBoyG:fix/firestore-reserved-id-length
Closed

CorgiBoyG wants to merge 1 commit into
google:mainfrom
CorgiBoyG:fix/firestore-reserved-id-length

Conversation

@CorgiBoyG

Copy link
Copy Markdown
Contributor

Link to Issue or Description of Change

Problem:

The reserved-id check added in #7801 uses:

if value.startswith("__") and value.endswith("__"):

Firestore reserves only ids that fully match __.*__, which needs at least four characters. Because the prefix and suffix overlap, the check above also matches "__" and "___". Those are valid Firestore document ids and do not change the document path, but FirestoreSessionService now rejects them, and any sessions already stored under them can no longer be read or deleted through the service.

Solution:

Require a minimum length of four characters:

if len(value) >= 4 and value.startswith("__") and value.endswith("__"):

This makes the check equivalent to re.fullmatch(r"__.*__", value). Nothing else in the validation changes: /, ., .., empty ids and genuinely reserved ids such as "____" and "__x__" are still rejected, so the path-injection protection from #7800 is unaffected.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All relevant unit tests pass locally.

New tests:

  • test_validate_identity_component_matches_firestore_reserved_pattern checks "__", "___", "____", "__x__", "__alice" and "alice__" against the reserved rule.
  • test_create_session_accepts_short_underscore_identity checks that create_session accepts "__" and "___" as user_id.

The 4 cases for "__" / "___" fail on current main with ValueError: user_id must not match the Firestore-reserved __.*__ pattern and pass with the fix.

pytest -q tests/unittests/integrations/firestore/
85 passed

pytest -q tests/unittests/sessions/
720 passed, 1 xfailed

I also compared the check against re.fullmatch(r"__.*__", value) for every string over {"_", "a"} up to length 8 (510 values): 0 mismatches.

Static checks:

pyink --check: passed
ruff check: passed
mypy (firestore_session_service.py): no issues
git diff --check: passed

The full repository test suite was not run.

Manual End-to-End (E2E) Tests:

Not run. The change is limited to the id validation predicate, which is covered by the unit tests above.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have added tests that prove my fix is effective.
  • Relevant unit tests pass locally.
  • I have manually tested my changes end-to-end.
  • No downstream dependency changes are required.

Additional context

This boundary was introduced by my own change in #7801.

…ttern

The reserved-id check added in google#7801 used startswith("__") and
endswith("__"), which also matches "__" and "___" because the prefix and
suffix overlap. Firestore reserves only ids matching __.*__, which needs
at least four characters, so those two valid ids were rejected and any
sessions already stored under them could no longer be read or deleted.

Require a minimum length of four so the check is equivalent to
re.fullmatch(r"__.*__", value).

Fixes google#7855
@CorgiBoyG
CorgiBoyG force-pushed the fix/firestore-reserved-id-length branch from fa17d53 to b61a33d Compare November 1, 2026 15:16
copybara-service Bot pushed a commit that referenced this pull request Nov 2, 2026
Merge #7858

FirestoreSessionService again accepts "__" and "___" as app_name, user_id
or session_id, so sessions stored under them are readable and deletable
through the service. Ids of four or more characters that start and end
with "__" are still rejected.

Fixes #7855

PiperOrigin-RevId: 1012330913
@adk-bot

adk-bot commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

Thank you @CorgiBoyG for your contribution! 🎉

Your changes have been successfully imported and merged via Copybara in commit e533f55.

Closing this PR as the changes are now in the main branch.

@adk-bot adk-bot added the merged [Status] This PR is merged label Nov 2, 2026
@adk-bot adk-bot closed this Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged [Status] This PR is merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FirestoreSessionService rejects valid ids "__" and "___" as reserved

3 participants