Skip to content

Commit

Permalink
Properly escape field paths (#4148)
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidt-sebastian committed Dec 2, 2020
1 parent 772c4d1 commit 1849b0d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/afraid-socks-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@firebase/firestore": patch
---

Fixed a bug that prevented usage of FieldPaths with multiple special characters.
2 changes: 1 addition & 1 deletion packages/firestore/src/model/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export class FieldPath extends BasePath<FieldPath> {
canonicalString(): string {
return this.toArray()
.map(str => {
str = str.replace('\\', '\\\\').replace('`', '\\`');
str = str.replace(/\\/g, '\\\\').replace(/`/g, '\\`');
if (!FieldPath.isValidIdentifier(str)) {
str = '`' + str + '`';
}
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/src/remote/rpc_error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ export function mapCodeFromHttpStatus(status?: number): Code {
* Code.UNKNOWN.
*/
export function mapCodeFromHttpResponseErrorStatus(status: string): Code {
const serverError = status.toLowerCase().replace('_', '-');
const serverError = status.toLowerCase().replace(/_/g, '-');
return Object.values(Code).indexOf(serverError as Code) >= 0
? (serverError as Code)
: Code.UNKNOWN;
Expand Down
5 changes: 5 additions & 0 deletions packages/firestore/test/unit/model/path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ describe('Path', () => {
expect(abc.isPrefixOf(ba)).to.equal(false);
});

it('escapes FieldPath with segments', () => {
const path = new FieldPath(['\\foo\\.`bar`']);
expect(path.canonicalString()).to.equal('`\\\\foo\\\\.\\`bar\\``');
});

it('can be constructed from field path.', () => {
const path = FieldPath.fromServerFormat('foo\\..bar\\\\.baz');
expect(path.toArray()).to.deep.equal(['foo.', 'bar\\', 'baz']);
Expand Down

0 comments on commit 1849b0d

Please sign in to comment.