Skip to content

Fix Storage emulator Firestore lookups for document IDs with special characters - #11651

Open
pcbeingused33 wants to merge 3 commits into
firebase:mainfrom
pcbeingused33:fix/c9102-storage-firestore-path
Open

pcbeingused33 wants to merge 3 commits into
firebase:mainfrom
pcbeingused33:fix/c9102-storage-firestore-path

Conversation

@pcbeingused33

Copy link
Copy Markdown

Fixes #11650

Description

The Storage rules runtime sends Firestore document paths without URL encoding. When a document ID contains a pipe, such as an Auth0 UID, the Firestore emulator can leave the lookup unanswered and the Storage upload hangs.

Encode each path segment before making the Firestore request so the document ID is escaped and the / separators are preserved.

Scenarios Tested

The Storage rules runtime regression test exercises the Firestore lookup with an Auth0-style UID and checks that the request contains %7C in the document ID. The regression test fails on the unpatched runtime. All 14 tests in the Storage rules runtime and config suites pass with the fix.

Sample Commands

npx mocha src/emulator/storage/rules/runtime.spec.ts src/emulator/storage/rules/config.spec.ts


This PR was prepared with the help of an AI coding assistant.

@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 fixes an issue where uploads hang in the Storage emulator when a rule looks up a Firestore document containing special characters (such as |) in its ID. This is resolved by URL-encoding individual path segments of the Firestore document path before constructing the request URL. A unit test has been added to verify this behavior. The review feedback suggests adding defensive checks on request.context and request.context.path to prevent potential runtime TypeError crashes if the incoming JSON payload is malformed or missing these properties.

Comment on lines +492 to +493
const encodedPath = request.context.path.split("/").map(encodeURIComponent).join("/");
const pathname = `projects/${projectId}${encodedPath}`;

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.

medium

Since the request object is parsed from an external JSON payload (via JSON.parse), we should defensively handle cases where request.context or request.context.path might be missing or malformed at runtime to prevent the emulator from crashing with a TypeError.

  const path = request.context?.path;
  if (typeof path !== "string") {
    return { status: DataLoadStatus.NOT_FOUND, warnings: [], errors: ["Invalid or missing Firestore document path"] };
  }
  const encodedPath = path.split("/").map(encodeURIComponent).join("/");
  const pathname = `projects/\${projectId}\${encodedPath}`;
References
  1. Use strict null checks and handle undefined/null explicitly. (link)

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Storage emulator: upload hangs when a rule reads a Firestore document whose path contains "|"

1 participant