Skip to content

Commit 10d8328

Browse files
committed
Remove symlinks from the student's directory after their code runs
Student code could replace its files with links to host paths. The host then wrote through them when reverting the code patch, and read through them when comparing files.
1 parent 6fbe9c3 commit 10d8328

3 files changed

Lines changed: 90 additions & 0 deletions

File tree

‎app/config.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
use PhpSchool\PhpWorkshop\Listener\OutputRunInfoListener;
5353
use PhpSchool\PhpWorkshop\Listener\PrepareSolutionListener;
5454
use PhpSchool\PhpWorkshop\Listener\RealPathListener;
55+
use PhpSchool\PhpWorkshop\Listener\RemoveStudentSymlinksListener;
5556
use PhpSchool\PhpWorkshop\Listener\SelfCheckListener;
5657
use PhpSchool\PhpWorkshop\Listener\TearDownListener;
5758
use PhpSchool\PhpWorkshop\Logger\ConsoleLogger;
@@ -301,6 +302,7 @@
301302
);
302303
},
303304
RealPathListener::class => create(),
305+
RemoveStudentSymlinksListener::class => create(),
304306
TearDownListener::class => function (ContainerInterface $c) {
305307
return new TearDownListener($c->get(Filesystem::class));
306308
},
@@ -485,6 +487,15 @@ function (CgiResult $result) use ($c) {
485487
containerListener(PrepareSolutionListener::class),
486488
],
487489
],
490+
//before the code patcher and the checks touch the student's files on the host
491+
'remove-student-symlinks' => [
492+
'verify.post.execute' => [
493+
containerListener(RemoveStudentSymlinksListener::class),
494+
],
495+
'run.finish' => [
496+
containerListener(RemoveStudentSymlinksListener::class),
497+
],
498+
],
488499
'code-patcher' => [
489500
'cli.verify.start' => [
490501
containerListener(CodePatchListener::class, 'patch'),
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpSchool\PhpWorkshop\Listener;
6+
7+
use PhpSchool\PhpWorkshop\Event\ExerciseRunnerEvent;
8+
9+
/**
10+
* Deletes every symlink in the student's directory once their code has run.
11+
*
12+
* Student code can replace its own files with links, eg `solution.php -> /var/www/app/.env`. The host then reads and
13+
* writes those files (reverting the code patch, comparing files), which would follow the link out of the directory.
14+
*/
15+
class RemoveStudentSymlinksListener
16+
{
17+
public function __invoke(ExerciseRunnerEvent $event): void
18+
{
19+
$directory = $event->getContext()->getStudentExecutionDirectory();
20+
21+
if (!is_dir($directory)) {
22+
return;
23+
}
24+
25+
//does not descend into linked directories, so this only ever sees what is inside the student's directory
26+
$files = new \RecursiveIteratorIterator(
27+
new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS),
28+
\RecursiveIteratorIterator::SELF_FIRST,
29+
);
30+
31+
$links = [];
32+
/** @var \SplFileInfo $file */
33+
foreach ($files as $file) {
34+
if ($file->isLink()) {
35+
$links[] = $file->getPathname();
36+
}
37+
}
38+
39+
foreach ($links as $link) {
40+
unlink($link);
41+
}
42+
}
43+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshopTest\Listener;
4+
5+
use PhpSchool\PhpWorkshop\Event\ExerciseRunnerEvent;
6+
use PhpSchool\PhpWorkshop\ExerciseRunner\Context\TestContext;
7+
use PhpSchool\PhpWorkshop\Listener\RemoveStudentSymlinksListener;
8+
use PhpSchool\PhpWorkshopTest\BaseTest;
9+
10+
class RemoveStudentSymlinksListenerTest extends BaseTest
11+
{
12+
public function testSymlinksAreRemovedAndEverythingElseIsKept(): void
13+
{
14+
$context = new TestContext();
15+
$context->createStudentSolutionDirectory();
16+
$context->importStudentFileFromString('<?php echo "hi";', 'solution.php');
17+
mkdir($context->getStudentExecutionDirectory() . '/src');
18+
$context->importStudentFileFromString('<?php', 'src/Hello.php');
19+
20+
$outside = dirname($this->getTemporaryFile('secret.txt', 'secret'));
21+
22+
$dir = $context->getStudentExecutionDirectory();
23+
symlink($outside . '/secret.txt', $dir . '/link.txt');
24+
symlink($outside, $dir . '/linked-dir');
25+
symlink($outside . '/secret.txt', $dir . '/src/nested-link.txt');
26+
27+
(new RemoveStudentSymlinksListener())->__invoke(new ExerciseRunnerEvent('run.finish', $context));
28+
29+
$this->assertFileDoesNotExist($dir . '/link.txt');
30+
$this->assertFalse(is_link($dir . '/linked-dir'));
31+
$this->assertFalse(is_link($dir . '/src/nested-link.txt'));
32+
$this->assertFileExists($dir . '/solution.php');
33+
$this->assertFileExists($dir . '/src/Hello.php');
34+
$this->assertSame('secret', file_get_contents($outside . '/secret.txt'));
35+
}
36+
}

0 commit comments

Comments
 (0)