-
Notifications
You must be signed in to change notification settings - Fork 4.1k
THRIFT-6047: Limit struct read/write recursion depth in PHP library #3552
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -216,6 +216,8 @@ protected function readStruct(string $class, array $spec, TProtocol $input): int | |
| $fname = null; | ||
| $ftype = 0; | ||
| $fid = 0; | ||
| $input->incrementRecursionDepth(); | ||
| try { | ||
| $xfer += $input->readStructBegin($fname); | ||
| while (true) { | ||
| $xfer += $input->readFieldBegin($fname, $ftype, $fid); | ||
|
|
@@ -257,6 +259,9 @@ protected function readStruct(string $class, array $spec, TProtocol $input): int | |
| $xfer += $input->readFieldEnd(); | ||
| } | ||
| $xfer += $input->readStructEnd(); | ||
| } finally { | ||
| $input->decrementRecursionDepth(); | ||
| } | ||
|
|
||
| return $xfer; | ||
| } | ||
|
|
@@ -380,6 +385,8 @@ private function writeList(array $var, array $spec, TProtocol $output, bool $set | |
| protected function writeStruct(string $class, array $spec, TProtocol $output): int | ||
| { | ||
| $xfer = 0; | ||
| $output->incrementRecursionDepth(); | ||
| try { | ||
| $xfer += $output->writeStructBegin($class); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same indent issue as in |
||
| foreach ($spec as $fid => $fspec) { | ||
| $var = $fspec['var']; | ||
|
|
@@ -410,6 +417,9 @@ protected function writeStruct(string $class, array $spec, TProtocol $output): i | |
| } | ||
| $xfer += $output->writeFieldStop(); | ||
| $xfer += $output->writeStructEnd(); | ||
| } finally { | ||
| $output->decrementRecursionDepth(); | ||
| } | ||
|
|
||
| return $xfer; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -264,6 +264,40 @@ public function testSkipBinaryThrowsForUnknownType(): void | |
| TProtocol::skipBinary(new TMemoryBuffer(), 999); | ||
| } | ||
|
|
||
| public function testIncrementRecursionDepthAllowsUpToLimit(): void | ||
| { | ||
| $transport = new TMemoryBuffer(); | ||
| $protocol = new TBinaryProtocol($transport); | ||
| for ($i = 0; $i < TProtocol::DEFAULT_RECURSION_DEPTH; $i++) { | ||
| $protocol->incrementRecursionDepth(); | ||
| } | ||
| $this->assertTrue(true); // no exception thrown | ||
| } | ||
|
|
||
| public function testIncrementRecursionDepthThrowsAtLimit(): void | ||
| { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: PHPUnit canonical way to express “the code under test must not throw and has no value to assert on” is |
||
| $this->expectException(TProtocolException::class); | ||
| $this->expectExceptionCode(TProtocolException::DEPTH_LIMIT); | ||
|
|
||
| $transport = new TMemoryBuffer(); | ||
| $protocol = new TBinaryProtocol($transport); | ||
| for ($i = 0; $i <= TProtocol::DEFAULT_RECURSION_DEPTH; $i++) { | ||
| $protocol->incrementRecursionDepth(); | ||
| } | ||
| } | ||
|
|
||
| public function testDecrementRecursionDepthRestoresCapacity(): void | ||
| { | ||
| $transport = new TMemoryBuffer(); | ||
| $protocol = new TBinaryProtocol($transport); | ||
| for ($i = 0; $i < TProtocol::DEFAULT_RECURSION_DEPTH; $i++) { | ||
| $protocol->incrementRecursionDepth(); | ||
| } | ||
| $protocol->decrementRecursionDepth(); | ||
| $protocol->incrementRecursionDepth(); // should not throw | ||
| $this->assertTrue(true); | ||
| } | ||
|
|
||
| private function buildBinaryBuffer(callable $writer): string | ||
| { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same nit — prefer |
||
| $transport = new TMemoryBuffer(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style: the
try { ... }block body isn't indented relative to the surrounding code, so the body sits at the same column astry {itself. PSR-12 expects the body to be one level deeper. The same shape is inwriteStructbelow. CI is green only because phpcs doesn't enforceGeneric.WhiteSpace.ScopeIndentfor this rule, but visually it reads as if thetryhad no body. Suggest:…and reindent the matched closing brace + every line in between.