Skip to content

Commit 1849b0d

Browse files
Properly escape field paths (#4148)
1 parent 772c4d1 commit 1849b0d

File tree

4 files changed

+12
-2
lines changed

4 files changed

+12
-2
lines changed

.changeset/afraid-socks-shop.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@firebase/firestore": patch
3+
---
4+
5+
Fixed a bug that prevented usage of FieldPaths with multiple special characters.

packages/firestore/src/model/path.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ export class FieldPath extends BasePath<FieldPath> {
265265
canonicalString(): string {
266266
return this.toArray()
267267
.map(str => {
268-
str = str.replace('\\', '\\\\').replace('`', '\\`');
268+
str = str.replace(/\\/g, '\\\\').replace(/`/g, '\\`');
269269
if (!FieldPath.isValidIdentifier(str)) {
270270
str = '`' + str + '`';
271271
}

packages/firestore/src/remote/rpc_error.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ export function mapCodeFromHttpStatus(status?: number): Code {
314314
* Code.UNKNOWN.
315315
*/
316316
export function mapCodeFromHttpResponseErrorStatus(status: string): Code {
317-
const serverError = status.toLowerCase().replace('_', '-');
317+
const serverError = status.toLowerCase().replace(/_/g, '-');
318318
return Object.values(Code).indexOf(serverError as Code) >= 0
319319
? (serverError as Code)
320320
: Code.UNKNOWN;

packages/firestore/test/unit/model/path.test.ts

+5
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ describe('Path', () => {
160160
expect(abc.isPrefixOf(ba)).to.equal(false);
161161
});
162162

163+
it('escapes FieldPath with segments', () => {
164+
const path = new FieldPath(['\\foo\\.`bar`']);
165+
expect(path.canonicalString()).to.equal('`\\\\foo\\\\.\\`bar\\``');
166+
});
167+
163168
it('can be constructed from field path.', () => {
164169
const path = FieldPath.fromServerFormat('foo\\..bar\\\\.baz');
165170
expect(path.toArray()).to.deep.equal(['foo.', 'bar\\', 'baz']);

0 commit comments

Comments
 (0)