-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecurityXssTest.php
More file actions
56 lines (45 loc) · 2.05 KB
/
Copy pathSecurityXssTest.php
File metadata and controls
56 lines (45 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
namespace Tests\Feature\Security;
use App\Models\AppReview;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
/**
* §L7A: a `<script>` payload in public, user-generated content (app
* reviews) must never execute. We never sanitize/strip on the way in (that
* would silently mutate user input); instead we rely on output encoding —
* no `v-html` in any Vue template (grepped in `security-pass`) and the
* page payload only ever reaching the browser inside a `<script
* type="application/json">` tag, JSON-encoded with escaped slashes (so the
* literal bytes `</script>` never appear and can't close that tag early).
*/
class SecurityXssTest extends TestCase
{
use RefreshDatabase;
private const PAYLOAD = '<script>alert(1)</script>';
public function test_a_script_tag_in_a_review_comment_is_stored_raw_and_never_rendered_as_a_live_tag(): void
{
$this->post(route('reviews.store'), [
'reviewer_name' => 'Eve',
'rating' => 5,
'comment' => self::PAYLOAD,
])->assertRedirect(route('home'));
$this->assertDatabaseHas('app_reviews', [
'reviewer_name' => 'Eve',
'comment' => self::PAYLOAD,
]);
$response = $this->get(route('reviews.index'));
$response->assertOk();
// The literal, executable tag never appears as a contiguous
// substring — the page payload's JSON-encoded form escapes the
// closing slash (`<\/script>`), which is what keeps it inert.
$this->assertStringNotContainsString(self::PAYLOAD, $response->getContent());
$this->assertStringContainsString('<script>alert(1)<\/script>', $response->getContent());
}
public function test_a_script_tag_in_a_reviewer_name_is_also_inert(): void
{
AppReview::factory()->create(['reviewer_name' => self::PAYLOAD, 'comment' => 'fine']);
$response = $this->get(route('reviews.index'));
$response->assertOk();
$this->assertStringNotContainsString(self::PAYLOAD, $response->getContent());
}
}