Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/utils/escape-filter-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,22 @@ function escapeFilterValue (toEscape) {
}

function escapeBuffer (buf) {
const bufLen = buf.byteLength
let result = ''
for (let i = 0; i < buf.length; i += 1) {
if (buf[i] >= 0xc0 && buf[i] <= 0xdf) {
// Represents the first byte in a 2-byte UTF-8 character.
result += '\\' + buf[i].toString(16) + '\\' + buf[i + 1].toString(16)
result += '\\' + buf[i].toString(16)
if (i < (bufLen - 1)) { result += '\\' + buf[i + 1].toString(16).padStart(2, '0') } // Only add second byte if exist in buffer
i += 1
continue
}

if (buf[i] >= 0xe0 && buf[i] <= 0xef) {
// Represents the first byte in a 3-byte UTF-8 character.
result += [
'\\', buf[i].toString(16),
'\\', buf[i + 1].toString(16),
'\\', buf[i + 2].toString(16)
].join('')
result += '\\' + buf[i].toString(16)
if (i < (bufLen - 1)) { result += '\\' + buf[i + 1].toString(16).padStart(2, '0') } // Only add second byte if exist in buffer
if (i < (bufLen - 2)) { result += '\\' + buf[i + 2].toString(16).padStart(2, '0') } // Only add third byte if exist in buffer
i += 2
continue
}
Expand Down
12 changes: 12 additions & 0 deletions lib/utils/escape-filter-value.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,15 @@ tap.test('leaves encoded characters intact', async t => {
t.equal(result, expected[i])
}
})

tap.test('handle start of two byte utf-8 character at end of buffer', async t => {
const input = Buffer.from([0x30, 0xc2])
const expected = '0\\c2'
t.equal(escapeFilterValue(input), expected)
})

tap.test('handle start of three byte utf-8 character at end of buffer', async t => {
const input = Buffer.from([0x30, 0xe2])
const expected = '0\\e2'
t.equal(escapeFilterValue(input), expected)
})