@@ -56,17 +56,67 @@ public function canServeRequestAsStaticFile(Request $request, RequestContext $co
5656
5757 $ publicPath = $ context ->publicPath ;
5858
59+ $ realpath = realpath ($ publicPath .'/ ' .$ request ->path ());
60+
61+ if ($ this ->checkSymlinkInPath ($ publicPath , $ realpath , $ request ->path ())) {
62+ $ realpath = $ publicPath .'/ ' .$ request ->path ();
63+ }
64+
5965 return $ this ->fileIsServable (
6066 $ publicPath ,
61- realpath ( $ publicPath . ' / ' . $ request -> path ()) ,
67+ $ realpath ,
6268 );
6369 }
6470
6571 /**
66- * Determine if the given file is servable.
72+ * Checks whether the request path contains a Symlink.
73+ * When a Symlink is found, it is checked against the the resolved real path,
74+ * in order to protect against directory traversal.
6775 *
6876 * @param string $publicPath
69- * @param string $pathToFile
77+ * @param string $realPath
78+ * @param string $requestPath
79+ * @return bool
80+ */
81+ private function checkSymlinkInPath (string $ publicPath , string $ realPath , string $ requestPath ): bool
82+ {
83+ $ resolvedPathIfSymlink = $ this ->pathContainsSymlink ($ publicPath , $ requestPath );
84+
85+ if (! $ resolvedPathIfSymlink ) {
86+ return false ;
87+ }
88+
89+ return str_ends_with ($ realPath , $ resolvedPathIfSymlink );
90+ }
91+
92+ /**
93+ * Determine whether the path contains a symlink.
94+ * When a symlink is found, the path after it is returned.
95+ *
96+ * @param string $publicPath
97+ * @param string $path
98+ * @return string|bool
99+ */
100+ private function pathContainsSymlink (string $ publicPath , string $ path ): string |bool
101+ {
102+ $ dirs = explode ('/ ' , $ path );
103+
104+ while ($ dir = array_shift ($ dirs )) {
105+ $ publicPath .= '/ ' .$ dir ;
106+
107+ if (is_link ($ publicPath )) {
108+ return implode ('/ ' , $ dirs );
109+ }
110+ }
111+
112+ return false ;
113+ }
114+
115+ /**
116+ * Determine if the given file is servable.
117+ *
118+ * @param string $publicPath
119+ * @param string $pathToFile
70120 * @return bool
71121 */
72122 protected function fileIsServable (string $ publicPath , string $ pathToFile ): bool
0 commit comments