Skip to content

Commit 61792c5

Browse files
committed
added HTTP_Origin validation (app_save_paste path)
1 parent 8f4aa10 commit 61792c5

4 files changed

Lines changed: 40 additions & 3 deletions

File tree

.env.dev

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ APP_SECRET=6514635062ba34f218535f9f106fd20b
33
# allow access only with https protocol
44
SSL_ONLY=false
55

6+
# allowed origin
7+
ALLOWED_ORIGIN=localhost
8+
69
# enable maintenance mode
710
MAINTENANCE_MODE=false
811

.env.test

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ APP_SECRET=0ec3c0e9ae0961bb9e1d7ae47f458f3f
33
# allow access only with https protocol
44
SSL_ONLY=false
55

6+
# allowed origin
7+
ALLOWED_ORIGIN=localhost
8+
69
# enable maintenance mode
710
MAINTENANCE_MODE=false
811

src/Controller/PasteController.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Controller;
44

55
use Exception;
6+
use App\Util\AppUtil;
67
use App\Util\SecurityUtil;
78
use App\Manager\PasteManager;
89
use Symfony\Component\HttpFoundation\Request;
@@ -19,11 +20,13 @@
1920
*/
2021
class PasteController extends AbstractController
2122
{
23+
private AppUtil $appUtil;
2224
private SecurityUtil $securityUtil;
2325
private PasteManager $pasteManager;
2426

25-
public function __construct(SecurityUtil $securityUtil, PasteManager $pasteManager)
27+
public function __construct(AppUtil $appUtil, SecurityUtil $securityUtil, PasteManager $pasteManager)
2628
{
29+
$this->appUtil = $appUtil;
2730
$this->securityUtil = $securityUtil;
2831
$this->pasteManager = $pasteManager;
2932
}
@@ -53,6 +56,16 @@ public function save(Request $request): Response
5356
$content = (string) $request->request->get('paste-content');
5457
$token = (string) $request->request->get('token');
5558

59+
// check if origin is allowed
60+
$origin = $request->headers->get('Origin');
61+
if ($origin == null || !str_contains($origin, $this->appUtil->getEnvValue('ALLOWED_ORIGIN'))) {
62+
return $this->json([
63+
'code' => Response::HTTP_FORBIDDEN,
64+
'status' => 'error',
65+
'message' => 'Invalid origin'
66+
], Response::HTTP_FORBIDDEN);
67+
}
68+
5669
try {
5770
// save paste
5871
$this->pasteManager->savePaste($token, $content);

tests/Controller/PasteControllerTest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,24 @@ public function testLoadIndexPage(): void
3737
$this->assertResponseStatusCodeSame(Response::HTTP_OK);
3838
}
3939

40+
/**
41+
* Test save paste when origin is invalid
42+
*
43+
* @return void
44+
*/
45+
public function testSavePasteWhenOriginIsInvalid(): void
46+
{
47+
$_ENV['ALLOWED_ORIGIN'] = 'http://localhost';
48+
49+
$this->client->request('POST', '/save', [
50+
'paste-content' => 'test content',
51+
'token' => ByteString::fromRandom(16)
52+
], server: ['HTTP_Origin' => 'http://invalid.origin']);
53+
54+
// assert response
55+
$this->assertResponseStatusCodeSame(Response::HTTP_FORBIDDEN);
56+
}
57+
4058
/**
4159
* Test save empty paste
4260
*
@@ -47,7 +65,7 @@ public function testSaveEmptyPaste(): void
4765
$this->client->request('POST', '/save', [
4866
'paste-content' => '',
4967
'token' => ByteString::fromRandom(16)
50-
]);
68+
], server: ['HTTP_Origin' => 'http://localhost']);
5169

5270
// assert response
5371
$this->assertResponseStatusCodeSame(Response::HTTP_BAD_REQUEST);
@@ -63,7 +81,7 @@ public function testSaveNewPasteSuccess(): void
6381
$this->client->request('POST', '/save', [
6482
'paste-content' => 'test content',
6583
'token' => ByteString::fromRandom(16)
66-
]);
84+
], server: ['HTTP_Origin' => 'http://localhost']);
6785

6886
// assert response
6987
$this->assertResponseStatusCodeSame(Response::HTTP_OK);

0 commit comments

Comments
 (0)