File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 33namespace App \Controller ;
44
55use Exception ;
6+ use App \Util \SecurityUtil ;
67use App \Manager \PasteManager ;
78use Symfony \Component \HttpFoundation \Request ;
89use Symfony \Component \HttpFoundation \Response ;
1819 */
1920class 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 ' );
Original file line number Diff line number Diff 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 *
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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 *
You can’t perform that action at this time.
0 commit comments