Skip to content

Commit 94d146f

Browse files
committed
fix: nested transformer request scope leakage
1 parent ba1d524 commit 94d146f

6 files changed

Lines changed: 304 additions & 14 deletions

File tree

‎system/API/BaseTransformer.php‎

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@
5454
*/
5555
abstract class BaseTransformer implements TransformerInterface
5656
{
57+
/**
58+
* Nesting depth of the transformation currently in progress (0 = root).
59+
*/
60+
private static int $depth = 0;
61+
5762
/**
5863
* @var list<string>|null
5964
*/
@@ -69,17 +74,24 @@ abstract class BaseTransformer implements TransformerInterface
6974
public function __construct(
7075
private ?IncomingRequest $request = null,
7176
) {
77+
// An explicitly provided request is always honored - its scope is
78+
// intentional. Only the implicit global fallback is suppressed for
79+
// nested transformers, which is the actual leak vector.
80+
$explicitRequest = $request instanceof IncomingRequest;
81+
7282
$this->request = $request ?? request();
7383

74-
$fields = $this->request->getGet('fields');
75-
$this->fields = is_string($fields)
76-
? array_map(trim(...), explode(',', $fields))
77-
: $fields;
84+
if ($explicitRequest || self::$depth === 0) {
85+
$fields = $this->request->getGet('fields');
86+
$this->fields = is_string($fields)
87+
? array_map(trim(...), explode(',', $fields))
88+
: $fields;
7889

79-
$includes = $this->request->getGet('include');
80-
$this->includes = is_string($includes)
81-
? array_map(trim(...), explode(',', $includes))
82-
: $includes;
90+
$includes = $this->request->getGet('include');
91+
$this->includes = is_string($includes)
92+
? array_map(trim(...), explode(',', $includes))
93+
: $includes;
94+
}
8395
}
8496

8597
/**
@@ -207,13 +219,19 @@ private function insertIncludes(array $data): array
207219
}
208220
}
209221

210-
foreach ($this->includes as $include) {
211-
$method = 'include' . ucfirst($include);
212-
if (method_exists($this, $method)) {
213-
$data[$include] = $this->{$method}();
214-
} else {
215-
throw ApiException::forMissingInclude($include);
222+
self::$depth++;
223+
224+
try {
225+
foreach ($this->includes as $include) {
226+
$method = 'include' . ucfirst($include);
227+
if (method_exists($this, $method)) {
228+
$data[$include] = $this->{$method}();
229+
} else {
230+
throw ApiException::forMissingInclude($include);
231+
}
216232
}
233+
} finally {
234+
self::$depth--;
217235
}
218236

219237
return $data;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Tests\Support\API;
15+
16+
use CodeIgniter\API\BaseTransformer;
17+
18+
/**
19+
* Nested transformer used to verify that the root request's scope
20+
* (fields/includes) does not leak into related resources.
21+
*/
22+
class ChildTransformer extends BaseTransformer
23+
{
24+
public function toArray(mixed $resource): array
25+
{
26+
return [
27+
'child_id' => $resource['id'] ?? null,
28+
'status' => 'transformed',
29+
];
30+
}
31+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Tests\Support\API;
15+
16+
use CodeIgniter\API\BaseTransformer;
17+
18+
/**
19+
* Root transformer used to verify that nested transformers do not inherit
20+
* the root request's `fields`/`include` query state.
21+
*/
22+
class ParentTransformer extends BaseTransformer
23+
{
24+
public function toArray(mixed $resource): array
25+
{
26+
return [
27+
'parent_id' => $resource['id'] ?? null,
28+
];
29+
}
30+
31+
/**
32+
* Includes a single related child resource.
33+
*
34+
* @return array<string, mixed>
35+
*/
36+
protected function includeChildren(): array
37+
{
38+
return (new ChildTransformer())->transform(['id' => 99]);
39+
}
40+
41+
/**
42+
* Includes a collection of related child resources.
43+
*
44+
* @return list<array<string, mixed>>
45+
*/
46+
protected function includeChildrenCollection(): array
47+
{
48+
return (new ChildTransformer())->transformMany([
49+
['id' => 77],
50+
['id' => 88],
51+
]);
52+
}
53+
54+
/**
55+
* Includes a single child while explicitly forwarding the request, opting
56+
* the child into the request-derived scope even though it is nested.
57+
*
58+
* @return array<string, mixed>
59+
*/
60+
protected function includeExplicitChild(): array
61+
{
62+
$childRequest = clone request();
63+
$childRequest->setGlobal('get', ['fields' => 'child_id']);
64+
65+
return (new ChildTransformer($childRequest))->transform(['id' => 99]);
66+
}
67+
}

‎tests/system/API/TransformerTest.php‎

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
use Config\Services;
2323
use PHPUnit\Framework\Attributes\Group;
2424
use stdClass;
25+
use Tests\Support\API\ChildTransformer;
26+
use Tests\Support\API\ParentTransformer;
2527

2628
/**
2729
* @internal
@@ -641,4 +643,160 @@ protected function includePosts(): array
641643
$this->assertArrayHasKey('posts', $result);
642644
$this->assertSame([['id' => 1, 'title' => 'Post 1']], $result['posts']);
643645
}
646+
647+
public function testNestedTransformerDoesNotInheritIncludeState(): void
648+
{
649+
// The child transformer has no includeChildren() method. If the root
650+
// request's `include=children` leaked into it, transforming the child
651+
// would raise an ApiException for the missing include method.
652+
$request = $this->createMockRequest('include=children');
653+
Services::injectMock('request', $request);
654+
655+
$transformer = new ParentTransformer($request);
656+
657+
$result = $transformer->transform(['id' => 1]);
658+
659+
$this->assertSame([
660+
'parent_id' => 1,
661+
'children' => ['child_id' => 99, 'status' => 'transformed'],
662+
], $result);
663+
}
664+
665+
public function testNestedTransformerDoesNotInheritFieldFilter(): void
666+
{
667+
// `fields=parent_id` applies to the root only. If it leaked into the
668+
// child, array_intersect_key would strip every child field, leaving [].
669+
$request = $this->createMockRequest('include=children&fields=parent_id');
670+
Services::injectMock('request', $request);
671+
672+
$transformer = new ParentTransformer($request);
673+
674+
$result = $transformer->transform(['id' => 1]);
675+
676+
$this->assertSame([
677+
'parent_id' => 1,
678+
'children' => ['child_id' => 99, 'status' => 'transformed'],
679+
], $result);
680+
}
681+
682+
public function testNestedCollectionTransformerDoesNotInheritState(): void
683+
{
684+
$request = $this->createMockRequest('include=childrenCollection&fields=parent_id');
685+
Services::injectMock('request', $request);
686+
687+
$transformer = new ParentTransformer($request);
688+
689+
$result = $transformer->transform(['id' => 1]);
690+
691+
$this->assertSame([
692+
'parent_id' => 1,
693+
'childrenCollection' => [
694+
['child_id' => 77, 'status' => 'transformed'],
695+
['child_id' => 88, 'status' => 'transformed'],
696+
],
697+
], $result);
698+
}
699+
700+
public function testBareNestedInstantiationDoesNotInheritState(): void
701+
{
702+
// Reproduces the exact leak vector: a child created with a bare
703+
// `new ChildTransformer()` (no request passed) inside an include
704+
// method must not pick up the root request's scope from request().
705+
$request = $this->createMockRequest('include=children&fields=parent_id');
706+
Services::injectMock('request', $request);
707+
708+
$root = new ParentTransformer();
709+
710+
$result = $root->transform(['id' => 5]);
711+
712+
$this->assertSame([
713+
'parent_id' => 5,
714+
'children' => ['child_id' => 99, 'status' => 'transformed'],
715+
], $result);
716+
}
717+
718+
public function testNestedTransformerHonorsExplicitRequest(): void
719+
{
720+
// A child created with an explicitly passed request must honor that
721+
// request's scope even while nested - the isolation only suppresses
722+
// the implicit global fallback, not deliberate developer intent.
723+
$request = $this->createMockRequest('include=explicitChild&fields=child_id,parent_id');
724+
Services::injectMock('request', $request);
725+
726+
$transformer = new ParentTransformer($request);
727+
728+
$result = $transformer->transform(['id' => 1]);
729+
730+
$this->assertSame([
731+
'parent_id' => 1,
732+
'explicitChild' => ['child_id' => 99],
733+
], $result);
734+
}
735+
736+
public function testRootScopeStillAppliesAfterNesting(): void
737+
{
738+
// Sanity check that the root transformer keeps applying its own scope
739+
// while nested children are isolated.
740+
$request = $this->createMockRequest('include=children&fields=parent_id');
741+
Services::injectMock('request', $request);
742+
743+
$transformer = new ParentTransformer($request);
744+
745+
$result = $transformer->transform(['id' => 1, 'secret' => 'hidden']);
746+
747+
// Root keeps only parent_id (plus the include key), the child is intact.
748+
$this->assertArrayHasKey('parent_id', $result);
749+
$this->assertArrayNotHasKey('secret', $result);
750+
$this->assertSame(['child_id' => 99, 'status' => 'transformed'], $result['children']);
751+
}
752+
753+
public function testDepthIsRestoredAfterIncludeThrows(): void
754+
{
755+
$request = $this->createMockRequest('include=nonexistent');
756+
Services::injectMock('request', $request);
757+
758+
$throwingRoot = new class ($request) extends BaseTransformer {
759+
public function toArray(mixed $resource): array
760+
{
761+
return $resource;
762+
}
763+
};
764+
765+
try {
766+
$throwingRoot->transform(['id' => 1, 'name' => 'Test']);
767+
$this->fail('Expected ApiException was not thrown.');
768+
} catch (ApiException) {
769+
// expected
770+
}
771+
772+
// The nesting depth must be balanced after the exception, so a fresh
773+
// root transformer still applies the request scope (depth back to 0).
774+
$fieldsRequest = $this->createMockRequest('fields=id');
775+
Services::injectMock('request', $fieldsRequest);
776+
777+
$nextRoot = new class ($fieldsRequest) extends BaseTransformer {
778+
public function toArray(mixed $resource): array
779+
{
780+
return $resource;
781+
}
782+
};
783+
784+
$result = $nextRoot->transform(['id' => 1, 'name' => 'Test']);
785+
786+
$this->assertSame(['id' => 1], $result);
787+
}
788+
789+
public function testBareNestedTransformerStillUsedByChildTransformerDirectly(): void
790+
{
791+
// When ChildTransformer is itself the root (no parent), it must apply
792+
// request scope as usual - the isolation only affects nesting.
793+
$request = $this->createMockRequest('fields=child_id');
794+
Services::injectMock('request', $request);
795+
796+
$transformer = new ChildTransformer($request);
797+
798+
$result = $transformer->transform(['id' => 42]);
799+
800+
$this->assertSame(['child_id' => 42], $result);
801+
}
644802
}

‎user_guide_src/source/changelogs/v4.7.4.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Deprecations
3030
Bugs Fixed
3131
**********
3232

33+
- **API:** Fixed a bug in Transformers where the root request's ``fields`` and ``include`` query parameters leaked into nested transformers created inside ``include*()`` methods, causing incorrect field filtering, unexpected includes, or infinite recursion.
3334
- **Database:** Fixed a bug where ``updateBatch()`` could be called after Query Builder ``where()`` conditions, even though it's not supported. In this situation, now the ``DatabaseException`` is thrown.
3435
- **HTTP:** Fixed a bug where the User Agent library reported Safari's WebKit version instead of the browser version from the ``Version`` token.
3536

‎user_guide_src/source/outgoing/api_transformers.rst‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,14 @@ The response would include:
174174
]
175175
}
176176
177+
.. note:: The ``fields`` and ``include`` query parameters describe the **root** resource only.
178+
Transformers instantiated inside an ``include*()`` method (such as ``PostTransformer`` above)
179+
are treated as nested resources: when created **without an explicit request** they do **not**
180+
inherit the root request's ``fields``/``include`` state. This prevents the parent's query
181+
parameters from leaking into related resources, which could otherwise cause incorrect field
182+
filtering or unexpected/recursive includes. If you deliberately pass a request to a nested
183+
transformer, that request's scope is honored.
184+
177185
Restricting Available Includes
178186
===============================
179187

@@ -262,6 +270,13 @@ Class Reference
262270

263271
Initializes the transformer and extracts the ``fields`` and ``include`` query parameters from the request.
264272

273+
.. note:: When no request is explicitly passed, the global request's ``fields`` and ``include``
274+
are read **only for the root transformer**. A transformer constructed during a nested
275+
transformation (i.e. inside an ``include*()`` method) without an explicit request still uses
276+
the global request object, but does not read ``fields``/``include`` from it, to avoid leaking
277+
the root's scope. Passing an explicit request always applies that request's scope, regardless
278+
of nesting.
279+
265280
.. php:method:: toArray(mixed $resource)
266281
267282
:param mixed $resource: The resource being transformed (Entity, array, object, or null)

0 commit comments

Comments
 (0)