Skip to content

Commit

Permalink
fix: correctly escape field paths with multiple backslashes or backti…
Browse files Browse the repository at this point in the history
…cks (#2259) (#2261)

Co-authored-by: Albert Nisbet <[email protected]>
  • Loading branch information
MarkDuckworth and albertnis authored Dec 17, 2024
1 parent 3fd0de9 commit 7056ba7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion dev/src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ export class FieldPath extends Path<FieldPath> implements firestore.FieldPath {
.map(str => {
return UNESCAPED_FIELD_NAME_RE.test(str)
? str
: '`' + str.replace('\\', '\\\\').replace('`', '\\`') + '`';
: '`' + str.replace(/\\/g, '\\\\').replace(/`/g, '\\`') + '`';
})
.join('.');
}
Expand Down
20 changes: 17 additions & 3 deletions dev/test/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,23 @@ describe('ResourcePath', () => {

describe('FieldPath', () => {
it('encodes field names', () => {
const components = [['foo'], ['foo', 'bar'], ['.', '`'], ['\\']];

const results = ['foo', 'foo.bar', '`.`.`\\``', '`\\\\`'];
const components = [
['foo'],
['foo', 'bar'],
['.', '`'],
['\\'],
['\\\\'],
['``'],
];

const results = [
'foo',
'foo.bar',
'`.`.`\\``',
'`\\\\`',
'`\\\\\\\\`',
'`\\`\\``',
];

for (let i = 0; i < components.length; ++i) {
expect(new FieldPath(...components[i]).toString()).to.equal(results[i]);
Expand Down

0 comments on commit 7056ba7

Please sign in to comment.