Skip to content

Commit eae5325

Browse files
committed
Add path_params to laminas
1 parent 594af25 commit eae5325

File tree

8 files changed

+128
-0
lines changed

8 files changed

+128
-0
lines changed

src/DDTrace/Integrations/Laminas/LaminasIntegration.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,16 @@ static function (SpanData $span) use ($controller, $action) {
274274
}
275275
$rootSpan->meta['laminas.route.name'] = $routeName;
276276
$rootSpan->meta['laminas.route.action'] = "$controller@$action";
277+
278+
// Push path params to appsec
279+
if (function_exists('\datadog\appsec\push_addresses')) {
280+
$params = $routeMatch->getParams();
281+
// Filter out the framework-specific params (controller, action)
282+
$pathParams = array_diff_key($params, array_flip(['controller', 'action']));
283+
if (count($pathParams) > 0) {
284+
\datadog\appsec\push_addresses(["server.request.path_params" => $pathParams]);
285+
}
286+
}
277287
}
278288
);
279289

tests/Frameworks/Laminas/Mvc/Latest/module/Application/config/module.config.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,20 @@
9393
'action' => 'behindAuth',
9494
],
9595
]
96+
],
97+
'dynamic_route' => [
98+
'type' => Segment::class,
99+
'options' => [
100+
'route' => '/dynamic_route[/:param01[/static[/:param02]]]',
101+
'constraints' => [
102+
'param01' => '[a-zA-Z0-9_-]+',
103+
'param02' => '[a-zA-Z0-9_-]+',
104+
],
105+
'defaults' => [
106+
'controller' => Controller\CommonSpecsController::class,
107+
'action' => 'dynamicRoute',
108+
],
109+
]
96110
]
97111
],
98112
],

tests/Frameworks/Laminas/Mvc/Latest/module/Application/src/Controller/CommonSpecsController.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,11 @@ public function errorAction()
3232
{
3333
throw new \Exception('Controller error');
3434
}
35+
36+
public function dynamicRouteAction()
37+
{
38+
$response = new Response();
39+
$response->setContent('dynamicRoute');
40+
return $response;
41+
}
3542
}

tests/Frameworks/Laminas/Mvc/Version_3_3/module/Application/config/module.config.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,20 @@
9393
'action' => 'behindAuth',
9494
],
9595
]
96+
],
97+
'dynamic_route' => [
98+
'type' => Segment::class,
99+
'options' => [
100+
'route' => '/dynamic_route[/:param01[/static[/:param02]]]',
101+
'constraints' => [
102+
'param01' => '[a-zA-Z0-9_-]+',
103+
'param02' => '[a-zA-Z0-9_-]+',
104+
],
105+
'defaults' => [
106+
'controller' => Controller\CommonSpecsController::class,
107+
'action' => 'dynamicRoute',
108+
],
109+
]
96110
]
97111
],
98112
],

tests/Frameworks/Laminas/Mvc/Version_3_3/module/Application/src/Controller/CommonSpecsController.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,11 @@ public function errorAction()
3232
{
3333
throw new \Exception('Controller error');
3434
}
35+
36+
public function dynamicRouteAction()
37+
{
38+
$response = new Response();
39+
$response->setContent('dynamicRoute');
40+
return $response;
41+
}
3542
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace DDTrace\Tests\Integrations\Laminas\Mvc\Latest;
4+
5+
use DDTrace\Tests\Integrations\Laminas\Mvc\PathParamsTestSuite;
6+
7+
/**
8+
* @group appsec
9+
*/
10+
class PathParamsTest extends PathParamsTestSuite
11+
{
12+
public static function getAppIndexScript()
13+
{
14+
return __DIR__ . '/../../../../Frameworks/Laminas/Mvc/Latest/public/index.php';
15+
}
16+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace DDTrace\Tests\Integrations\Laminas\Mvc;
4+
5+
use DDTrace\Tests\Common\AppsecTestCase;
6+
use DDTrace\Tests\Frameworks\Util\Request\GetSpec;
7+
use datadog\appsec\AppsecStatus;
8+
9+
class PathParamsTestSuite extends AppsecTestCase
10+
{
11+
public function testDynamicRouteWithAllParametersGiven()
12+
{
13+
$param01 = 'first_param';
14+
$param02 = 'second_param';
15+
$this->call(
16+
GetSpec::create('Call to dynamic route', "/dynamic_route/$param01/static/$param02")
17+
);
18+
$events = AppsecStatus::getInstance()->getEvents(['push_addresses'], ['server.request.path_params']);
19+
$this->assertEquals(1, count($events));
20+
$this->assertEquals($param01, $events[0][0]["server.request.path_params"]['param01']);
21+
$this->assertEquals($param02, $events[0][0]["server.request.path_params"]['param02']);
22+
}
23+
24+
public function testDynamicRouteWithOptionalParametersNotGiven()
25+
{
26+
$param01 = 'first_param';
27+
$this->call(
28+
GetSpec::create('Call to dynamic route', "/dynamic_route/$param01/static")
29+
);
30+
$events = AppsecStatus::getInstance()->getEvents(['push_addresses'], ['server.request.path_params']);
31+
$this->assertEquals(1, count($events));
32+
$this->assertCount(1, $events[0][0]["server.request.path_params"]);
33+
$this->assertEquals($param01, $events[0][0]["server.request.path_params"]['param01']);
34+
}
35+
36+
public function testStaticRouteDoesNotGenerateEvent()
37+
{
38+
$this->call(
39+
GetSpec::create('Call to static route', "/simple")
40+
);
41+
$events = AppsecStatus::getInstance()->getEvents(['push_addresses'], ['server.request.path_params']);
42+
$this->assertEquals(0, count($events));
43+
}
44+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace DDTrace\Tests\Integrations\Laminas\Mvc\V3_3;
4+
5+
use DDTrace\Tests\Integrations\Laminas\Mvc\PathParamsTestSuite;
6+
7+
/**
8+
* @group appsec
9+
*/
10+
class PathParamsTest extends PathParamsTestSuite
11+
{
12+
public static function getAppIndexScript()
13+
{
14+
return __DIR__ . '/../../../../Frameworks/Laminas/Mvc/Version_3_3/public/index.php';
15+
}
16+
}

0 commit comments

Comments
 (0)