Skip to content

Commit

Permalink
Ensure that string conversion doesn't recursively join or execute code
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed Dec 26, 2023
1 parent cc6a0f1 commit 18f44f8
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions unpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ function readKey() {
return readFixedString(length)
} else { // not cacheable, go back and do a standard read
position--
return read().toString()
return asSafeString(read())
}
let key = ((length << 5) ^ (length > 1 ? dataView.getUint16(position) : length > 0 ? src[position] : 0)) & 0xfff
let entry = keyCache[key]
Expand Down Expand Up @@ -966,9 +966,15 @@ function readKey() {
return entry.string = readFixedString(length)
}

function asSafeString(property) {
if (typeof property === 'string') return property;
if (typeof property === 'number') return property.toString();
throw new Error('Invalid property type for record', typeof property);
}
// the registration of the record definition extension (as "r")
const recordDefinition = (id, highByte) => {
let structure = read().map(property => property.toString()) // ensure that all keys are strings and that the array is mutable
let structure = read().map(asSafeString) // ensure that all keys are strings and
// that the array is mutable
let firstByte = id
if (highByte !== undefined) {
id = id < 32 ? -((highByte << 5) + id) : ((highByte << 5) + id)
Expand Down

0 comments on commit 18f44f8

Please sign in to comment.