-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd59f83
commit d027f9d
Showing
7 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/DDTrace/Integrations/Filesystem/FilesystemIntegration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace DDTrace\Integrations\Filesystem; | ||
|
||
use DDTrace\HookData; | ||
use DDTrace\Integrations\Integration; | ||
|
||
class FilesystemIntegration extends Integration | ||
{ | ||
const NAME = "filesystem"; | ||
|
||
public function init(): int | ||
{ | ||
\DDTrace\install_hook( | ||
'file_get_contents', | ||
self::preHook('file_get_contents'), | ||
null | ||
); | ||
|
||
\DDTrace\install_hook( | ||
'file_put_contents', | ||
self::preHook('file_put_contents'), | ||
null | ||
); | ||
|
||
\DDTrace\install_hook( | ||
'fopen', | ||
self::preHook('fopen'), | ||
null | ||
); | ||
|
||
\DDTrace\install_hook( | ||
'readfile', | ||
self::preHook('readfile'), | ||
null | ||
); | ||
|
||
\DDTrace\install_hook( | ||
'stat', | ||
self::preHook('stat'), | ||
null | ||
); | ||
|
||
|
||
\DDTrace\install_hook( | ||
'lstat', | ||
self::preHook('lstat'), | ||
null | ||
); | ||
|
||
return Integration::LOADED; | ||
} | ||
|
||
private static function preHook($variant) | ||
{ | ||
return static function (HookData $hook) use ($variant) { | ||
if (count($hook->args) == 0 || !is_string($hook->args[0])) { | ||
return; | ||
} | ||
|
||
$filename = $hook->args[0]; | ||
if (function_exists('\datadog\appsec\push_address')) { | ||
\datadog\appsec\push_address("server.io.fs.file", $filename); | ||
} | ||
}; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace DDTrace\Tests\Integrations\DeferredLoading; | ||
|
||
use DDTrace\Tests\Common\SpanAssertion; | ||
use DDTrace\Tests\Common\AppsecTestCase; | ||
use DDTrace\Tests\Frameworks\Util\Request\GetSpec; | ||
use PHPUnit\Framework\TestCase; | ||
use datadog\appsec\AppsecStatus; | ||
|
||
final class FilesystemTest extends AppsecTestCase | ||
{ | ||
protected static function getAppIndexScript() | ||
{ | ||
return __DIR__ . '/index.php'; | ||
} | ||
|
||
protected function assertEvent(string $value) | ||
{ | ||
$events = AppsecStatus::getInstance()->getEvents(); | ||
$this->assertEquals(1, count($events)); | ||
$this->assertEquals($value, $events[0]["server.io.fs.file"]); | ||
$this->assertEquals('push_address', $events[0]['eventName']); | ||
} | ||
|
||
public function testFileGetContents() | ||
{ | ||
$traces = $this->tracesFromWebRequest(function () { | ||
$response = $this->call(GetSpec::create('Root', '/?function=file_get_contents&path=./index.php')); | ||
TestCase::assertSame('OK', $response); | ||
}); | ||
|
||
$this->assertEvent('./index.php'); | ||
} | ||
|
||
public function testFilePutContents() | ||
{ | ||
$traces = $this->tracesFromWebRequest(function () { | ||
$response = $this->call(GetSpec::create('Root', '/?function=file_put_contents&path=./somefile')); | ||
TestCase::assertSame('OK', $response); | ||
}); | ||
$this->assertEvent('./somefile'); | ||
} | ||
|
||
public function testFopen() | ||
{ | ||
$traces = $this->tracesFromWebRequest(function () { | ||
$response = $this->call(GetSpec::create('Root', '/?function=fopen&path=./index.php')); | ||
TestCase::assertSame('OK', $response); | ||
}); | ||
$this->assertEvent('./index.php'); | ||
} | ||
|
||
public function testReadFile() | ||
{ | ||
$traces = $this->tracesFromWebRequest(function () { | ||
$response = $this->call(GetSpec::create('Root', '/?function=readfile&path=./dummy')); | ||
TestCase::assertSame("Dummy file content\nOK", $response); | ||
}); | ||
$this->assertEvent('./dummy'); | ||
} | ||
|
||
public function testStat() | ||
{ | ||
$traces = $this->tracesFromWebRequest(function () { | ||
$response = $this->call(GetSpec::create('Root', '/?function=stat&path=./dummy')); | ||
TestCase::assertSame("OK", $response); | ||
}); | ||
$this->assertEvent('./dummy'); | ||
} | ||
|
||
public function testLstat() | ||
{ | ||
$traces = $this->tracesFromWebRequest(function () { | ||
$response = $this->call(GetSpec::create('Root', '/?function=lstat&path=./dummy')); | ||
TestCase::assertSame("OK", $response); | ||
}); | ||
$this->assertEvent('./dummy'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Dummy file content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
require __DIR__ . '/../../vendor/autoload.php'; | ||
|
||
$function = $_GET['function']; | ||
$path = $_GET['path']; | ||
|
||
switch ($function) { | ||
case 'file_put_contents': | ||
file_put_contents($path, 'some content'); | ||
break; | ||
case 'fopen': | ||
fopen($path, 'r'); | ||
break; | ||
default: | ||
$function($path); | ||
break; | ||
} | ||
|
||
echo "OK"; |