| 
									
										
										
										
											2021-01-02 10:43:50 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace BookStack\Http\Middleware; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Closure; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Sets CSP headers to restrict the hosts that BookStack can be | 
					
						
							|  |  |  |  * iframed within. Also adjusts the cookie samesite options | 
					
						
							|  |  |  |  * so that cookies will operate in the third-party context. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class ControlIframeSecurity | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Handle an incoming request. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |      * @param \Illuminate\Http\Request $request | 
					
						
							|  |  |  |      * @param \Closure                 $next | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-01-02 10:43:50 +08:00
										 |  |  |      * @return mixed | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function handle($request, Closure $next) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $iframeHosts = collect(explode(' ', config('app.iframe_hosts', '')))->filter(); | 
					
						
							|  |  |  |         if ($iframeHosts->count() > 0) { | 
					
						
							|  |  |  |             config()->set('session.same_site', 'none'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $iframeHosts->prepend("'self'"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $response = $next($request); | 
					
						
							|  |  |  |         $cspValue = 'frame-ancestors ' . $iframeHosts->join(' '); | 
					
						
							|  |  |  |         $response->headers->set('Content-Security-Policy', $cspValue); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-02 10:43:50 +08:00
										 |  |  |         return $response; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |