Skip to content

Commit 740fe7c

Browse files
committed
Escape backslashes in query tag keys
Co-authored-by: Isaac
1 parent 6f1118f commit 740fe7c

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

lib/utils/queryTags.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
*
44
* Format: comma-separated key:value pairs, e.g. "key1:value1,key2:value2"
55
* - If a value is null or undefined, the key is included without a colon or value
6+
* - Backslashes in keys are escaped; other special characters in keys are not escaped
67
* - Special characters (backslash, colon, comma) in values are backslash-escaped
7-
* - Keys are not escaped
88
*
99
* @param queryTags - dictionary of query tag key-value pairs
1010
* @returns serialized string, or undefined if input is empty/null/undefined
@@ -23,12 +23,13 @@ export function serializeQueryTags(
2323

2424
return keys
2525
.map((key) => {
26+
const escapedKey = key.replace(/\\/g, '\\\\');
2627
const value = queryTags[key];
2728
if (value == null) {
28-
return key;
29+
return escapedKey;
2930
}
3031
const escapedValue = value.replace(/[\\:,]/g, (c) => `\\${c}`);
31-
return `${key}:${escapedValue}`;
32+
return `${escapedKey}:${escapedValue}`;
3233
})
3334
.join(',');
3435
}

tests/unit/utils/queryTags.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,15 @@ describe('serializeQueryTags', () => {
5252
expect(serializeQueryTags({ val: 'a\\b:c,d' })).to.equal('val:a\\\\b\\:c\\,d');
5353
});
5454

55-
it('should not escape special characters in keys', () => {
55+
it('should escape backslash in key', () => {
56+
expect(serializeQueryTags({ 'a\\b': 'value' })).to.equal('a\\\\b:value');
57+
});
58+
59+
it('should escape backslash in key with null value', () => {
60+
expect(serializeQueryTags({ 'a\\b': null })).to.equal('a\\\\b');
61+
});
62+
63+
it('should not escape other special characters in keys', () => {
5664
expect(serializeQueryTags({ 'key:name': 'value' })).to.equal('key:name:value');
5765
});
5866
});

0 commit comments

Comments
 (0)