Skip to content
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
### Upcoming

- Unserialize bug/security fixes: (by @souvlakias)
- Ensures object keys are either `number` or `string`, and not equal to `__proto__`.
- Ensures serializable classes are not unserialized as `O:notserializable-class`.

### 5.1.3

- Maintenance release with updated homepage in manifest
Expand Down
11 changes: 11 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ export function isInteger(value: any): boolean {
return typeof value === 'number' && Number.parseInt(value.toString(), 10) === value
}

export function isValidKey(key: any): [boolean, string] {
if (!(typeof key === 'string' || typeof key === 'number')) {
return [false, `Invalid key type '${typeof key}' encountered while unserializing`]
}
if (key === '__proto__') {
// Prevent prototype pollution
return [false, 'Key "__proto__" is not allowed']
}
return [true, '']
}

export function getIncompleteClass(name: string) {
return new __PHP_Incomplete_Class(name)
}
Expand Down
5 changes: 4 additions & 1 deletion src/unserialize.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// eslint-disable-next-line import/no-cycle
import Parser from './parser'
import { isInteger, getClass, getIncompleteClass, __PHP_Incomplete_Class, invariant } from './helpers'
import { isInteger, getClass, getIncompleteClass, __PHP_Incomplete_Class, invariant, isValidKey } from './helpers'

export type Options = {
strict: boolean
Expand Down Expand Up @@ -71,6 +71,7 @@ function unserializeItem(parser: Parser, scope: Record<string, any>, options: Op
const isArray = pairs.every((item, idx) => isInteger(item.key) && idx === item.key)
const result = isArray ? [] : {}
pairs.forEach(({ key, value }) => {
invariant(...isValidKey(key))
result[key] = value
})
return result
Expand All @@ -80,10 +81,12 @@ function unserializeItem(parser: Parser, scope: Record<string, any>, options: Op
parser.seekExpected(':')
const pairs = parser.getByLength('{', '}', length => unserializePairs(parser, length, scope, options))
const result = getClassReference(name, scope, options.strict)
invariant(!result.unserialize, `Found unserialize method on class ${name} but expected notserializable-class`)

const PREFIX_PRIVATE = `\u0000${name}\u0000`
const PREFIX_PROTECTED = `\u0000*\u0000`
pairs.forEach(({ key, value }) => {
invariant(... isValidKey(key));
if (key.startsWith(PREFIX_PRIVATE)) {
// Private field
result[key.slice(PREFIX_PRIVATE.length)] = value
Expand Down
Binary file modified test/unserialize-test.ts
Binary file not shown.