|
| 1 | +<?php |
| 2 | + |
| 3 | +use Codeception\Util\Fixtures; |
| 4 | +use Grav\Common\Grav; |
| 5 | +use Grav\Framework\Form\FormFlash; |
| 6 | + |
| 7 | +/** |
| 8 | + * Class FormFlashSecurityTest |
| 9 | + * |
| 10 | + * Covers: GHSA-hmcx-ch82-3fv2 (unauthenticated path traversal / arbitrary |
| 11 | + * directory creation via FormFlash session_id / unique_id). |
| 12 | + * |
| 13 | + * Naming convention: test{Method}_{GHSA_ID}_{description} |
| 14 | + */ |
| 15 | +class FormFlashSecurityTest extends \PHPUnit\Framework\TestCase |
| 16 | +{ |
| 17 | + /** @var Grav */ |
| 18 | + protected $grav; |
| 19 | + |
| 20 | + protected function setUp(): void |
| 21 | + { |
| 22 | + parent::setUp(); |
| 23 | + $grav = Fixtures::get('grav'); |
| 24 | + $this->grav = $grav(); |
| 25 | + } |
| 26 | + |
| 27 | + // ========================================================================= |
| 28 | + // GHSA-hmcx-ch82-3fv2: Unauthenticated path traversal in FormFlash |
| 29 | + // ========================================================================= |
| 30 | + |
| 31 | + /** |
| 32 | + * @dataProvider providerGHSAhmcx_TraversalIds |
| 33 | + */ |
| 34 | + public function testConstruct_GHSAhmcx_RejectsTraversalSessionId(string $id, string $description): void |
| 35 | + { |
| 36 | + $flash = new FormFlash([ |
| 37 | + 'session_id' => $id, |
| 38 | + 'unique_id' => 'abcdef1234567890', |
| 39 | + 'form_name' => 'test', |
| 40 | + ]); |
| 41 | + |
| 42 | + $this->assertSame('', $flash->getSessionId(), $description); |
| 43 | + $this->assertSame('', $flash->getTmpDir(), "tmp dir must be empty when session id is rejected: {$description}"); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @dataProvider providerGHSAhmcx_TraversalIds |
| 48 | + */ |
| 49 | + public function testConstruct_GHSAhmcx_RejectsTraversalUniqueId(string $id, string $description): void |
| 50 | + { |
| 51 | + $flash = new FormFlash([ |
| 52 | + 'session_id' => 'abcdef1234567890', |
| 53 | + 'unique_id' => $id, |
| 54 | + 'form_name' => 'test', |
| 55 | + ]); |
| 56 | + |
| 57 | + $this->assertSame('', $flash->getUniqueId(), $description); |
| 58 | + $this->assertSame('', $flash->getTmpDir(), "tmp dir must be empty when unique id is rejected: {$description}"); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @dataProvider providerGHSAhmcx_ValidIds |
| 63 | + */ |
| 64 | + public function testConstruct_GHSAhmcx_AcceptsValidIds(string $id, string $description): void |
| 65 | + { |
| 66 | + $flash = new FormFlash([ |
| 67 | + 'session_id' => $id, |
| 68 | + 'unique_id' => $id, |
| 69 | + 'form_name' => 'test', |
| 70 | + ]); |
| 71 | + |
| 72 | + $this->assertSame($id, $flash->getSessionId(), $description); |
| 73 | + $this->assertSame($id, $flash->getUniqueId(), $description); |
| 74 | + } |
| 75 | + |
| 76 | + public static function providerGHSAhmcx_TraversalIds(): array |
| 77 | + { |
| 78 | + return [ |
| 79 | + ['../../user/config/proof_dir', 'parent-dir traversal (advisory PoC)'], |
| 80 | + ['..', 'bare parent-dir'], |
| 81 | + ['foo/../bar', 'embedded traversal'], |
| 82 | + ['foo/bar', 'embedded forward slash'], |
| 83 | + ['foo\\bar', 'embedded backslash'], |
| 84 | + ['foo:bar', 'colon (stream prefix)'], |
| 85 | + ['foo.bar', 'embedded dot'], |
| 86 | + ['tmp://forms/abc', 'explicit stream url'], |
| 87 | + ['%2e%2e%2f', 'url-encoded traversal'], |
| 88 | + [str_repeat('a', 65), 'overlong (>64 chars)'], |
| 89 | + ["abc\x00def", 'null byte'], |
| 90 | + ["abc\ndef", 'newline'], |
| 91 | + [' abc', 'leading space'], |
| 92 | + ]; |
| 93 | + } |
| 94 | + |
| 95 | + public static function providerGHSAhmcx_ValidIds(): array |
| 96 | + { |
| 97 | + return [ |
| 98 | + ['abc123', 'alphanumeric'], |
| 99 | + ['ABCdef123', 'mixed case'], |
| 100 | + ['session-id-with-dashes', 'hyphens'], |
| 101 | + ['session_id_with_underscores', 'underscores'], |
| 102 | + ['session,id,with,commas', 'commas (PHP 5-bit session id)'], |
| 103 | + [str_repeat('a', 64), 'max length (64 chars)'], |
| 104 | + ]; |
| 105 | + } |
| 106 | +} |
0 commit comments