| 
									
										
										
										
											2021-09-04 06:32:42 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace BookStack\Http\Middleware; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-04 20:57:04 +08:00
										 |  |  | use BookStack\Util\CspService; | 
					
						
							| 
									
										
										
										
											2021-09-04 06:32:42 +08:00
										 |  |  | use Closure; | 
					
						
							|  |  |  | use Illuminate\Http\Request; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ApplyCspRules | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-09-04 20:57:04 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @var CspService | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected $cspService; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function __construct(CspService $cspService) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->cspService = $cspService; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-04 06:32:42 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Handle an incoming request. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Request $request | 
					
						
							|  |  |  |      * @param Closure $next | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return mixed | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function handle($request, Closure $next) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-09-04 20:57:04 +08:00
										 |  |  |         view()->share('cspNonce', $this->cspService->getNonce()); | 
					
						
							|  |  |  |         if ($this->cspService->allowedIFrameHostsConfigured()) { | 
					
						
							|  |  |  |             config()->set('session.same_site', 'none'); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-04 06:32:42 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $response = $next($request); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-04 20:57:04 +08:00
										 |  |  |         $this->cspService->setFrameAncestors($response); | 
					
						
							|  |  |  |         $this->cspService->setScriptSrc($response); | 
					
						
							| 
									
										
										
										
											2021-09-04 21:34:43 +08:00
										 |  |  |         $this->cspService->setObjectSrc($response); | 
					
						
							|  |  |  |         $this->cspService->setBaseUri($response); | 
					
						
							| 
									
										
										
										
											2021-09-04 06:32:42 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $response; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |