Skip to content

Commit f5d782a

Browse files
committed
added unescapeString method to unescape data in raw view
1 parent f347456 commit f5d782a

4 files changed

Lines changed: 40 additions & 2 deletions

File tree

src/Controller/PasteController.php

Lines changed: 7 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\SecurityUtil;
67
use App\Manager\PasteManager;
78
use Symfony\Component\HttpFoundation\Request;
89
use Symfony\Component\HttpFoundation\Response;
@@ -18,10 +19,12 @@
1819
*/
1920
class PasteController extends AbstractController
2021
{
22+
private SecurityUtil $securityUtil;
2123
private PasteManager $pasteManager;
2224

23-
public function __construct(PasteManager $pasteManager)
25+
public function __construct(SecurityUtil $securityUtil, PasteManager $pasteManager)
2426
{
27+
$this->securityUtil = $securityUtil;
2528
$this->pasteManager = $pasteManager;
2629
}
2730

@@ -107,6 +110,9 @@ public function raw(Request $request): Response
107110
// get paste content
108111
$paste = $this->pasteManager->getPaste($pasteFile);
109112

113+
// unescape HTML entities to show original content
114+
$paste = $this->securityUtil->unescapeString($paste ?? '');
115+
110116
// return raw content
111117
$response = new Response($paste);
112118
$response->headers->set('Content-Type', 'text/plain; charset=UTF-8');

src/Util/SecurityUtil.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ public function escapeString(string $string): ?string
2323
return htmlspecialchars($string, ENT_QUOTES | ENT_HTML5);
2424
}
2525

26+
/**
27+
* Unescape HTML entities back to their original characters
28+
*
29+
* @param string $string The input string to unescape
30+
*
31+
* @return string The unescaped string
32+
*/
33+
public function unescapeString(string $string): string
34+
{
35+
return htmlspecialchars_decode($string, ENT_QUOTES | ENT_HTML5);
36+
}
37+
2638
/**
2739
* Encrypt string using AES encryption
2840
*

tests/Controller/PasteControllerTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,15 @@ public function testRawViewPaste(): void
9292
{
9393
$this->client->request('GET', '/raw?f=zSc0Uh8L1gsA7a6u');
9494

95+
// get response content
96+
$content = $this->client->getResponse()->getContent();
97+
9598
// assert response
99+
$this->assertNotEmpty($content);
96100
$this->assertResponseStatusCodeSame(Response::HTTP_OK);
101+
$this->assertStringNotContainsString('<', $content);
102+
$this->assertStringNotContainsString('>', $content);
103+
$this->assertStringNotContainsString('"', $content);
97104
$this->assertResponseHeaderSame('Content-Type', 'text/plain; charset=UTF-8');
98-
$this->assertNotEmpty($this->client->getResponse()->getContent());
99105
}
100106
}

tests/Util/SecurityUtilTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ public function testEscapeString(): void
3838
$this->assertEquals($expectedOutput, $this->securityUtil->escapeString($input));
3939
}
4040

41+
/**
42+
* Test unescape HTML entities
43+
*
44+
* @return void
45+
*/
46+
public function testUnescapeString(): void
47+
{
48+
$escapedString = '<script>alert("xss")</script>';
49+
$expectedOutput = '<script>alert("xss")</script>';
50+
51+
// assert result
52+
$this->assertEquals($expectedOutput, $this->securityUtil->unescapeString($escapedString));
53+
}
54+
4155
/**
4256
* Test encrypt AES
4357
*

0 commit comments

Comments
 (0)