firebase-functions icon indicating copy to clipboard operation
firebase-functions copied to clipboard

fix: support encoding of special characters in path params for gen2 f…

Open IzaakGough opened this issue 5 months ago • 1 comments

Summary

Resolves #1459

Fix Gen 2 path parameter decoding when Eventarc delivers UTF-8 mojibake instead of already-correct Unicode, so wildcard params like Helvétios, 中文, and emoji are exposed correctly in event.params.

Problem/Root Cause

Firestore Gen 2 triggers could receive path wildcard values with special characters mangled into mojibake such as Helvétios instead of Helvétios. The existing path param decoding only handled percent-encoded values via decodeURIComponent, so non-percent-encoded mojibake was passed through unchanged.

Solution/Changes

Update the shared path pattern decoder to:

  • keep the existing decodeURIComponent behavior for properly encoded values
  • preserve malformed percent-encoded input
  • detect byte sequences that look like UTF-8 mojibake and re-decode them as UTF-8 before returning path params

Added shared regression coverage for accented Latin text, CJK, emoji, Hindi, Arabic, already-correct Unicode input, and multi-segment captures.

Testing

  • npm test
  • Manual verification in a sample Gen 2 Firestore trigger with accented Latin text, emoji, CJK, Hindi, and Arabic path params

IzaakGough avatar Jun 17 '26 16:06 IzaakGough

AI just made a good point:

It treats some raw strings as “obviously broken UTF-8 mojibake” when they are not obviously broken.

Current code in src/common/utilities/path-pattern.ts:

  • runs decodeURIComponent
  • then always runs tryDecodeUtf8Mojibake
  • if string matches byte-looking pattern, it reinterprets chars as UTF-8 bytes

Bug: same observed string can mean 2 different things.

Example:

  • é might be:
    • legit literal path segment é
    • broken form of intended é

Code cannot know which one user meant. Current implementation always picks “broken UTF-8” and converts:

  • é -> é
  • £ -> £
  • © -> ©
  • fooébar -> fooébar

So ambiguity is:

  • fix real mojibake from issue #1459
  • but corrupt legit IDs/path segments that happen to look like mojibake bytes

Why impossible in current layer:

  • PathPattern only sees final string
  • no extra metadata says “this came in broken”
  • Firestore/RTDB/Data Connect current param path gives no trusted second source

So current implementation has no safe rule for:

  • repair all mojibake-like strings vs
  • preserve all raw strings

Tests prove repair cases, but miss legit ambiguous cases.

IzaakGough avatar Jun 25 '26 14:06 IzaakGough