|
1
|
|
|
<?php namespace Stevenmaguire\Laravel\Http\Middleware; |
|
2
|
|
|
|
|
3
|
|
|
use Closure; |
|
4
|
|
|
use Illuminate\Http\Response; |
|
5
|
|
|
use GuzzleHttp\Psr7\Response as PsrResponse; |
|
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
7
|
|
|
use Stevenmaguire\Http\Middleware\EnforceContentSecurity as BaseMiddleware; |
|
8
|
|
|
|
|
9
|
|
|
class EnforceContentSecurity extends BaseMiddleware |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Config closure; |
|
13
|
|
|
* |
|
14
|
|
|
* @var Closure |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $config; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Creates new middleware instance. |
|
20
|
|
|
*/ |
|
21
|
6 |
|
public function __construct() |
|
22
|
|
|
{ |
|
23
|
6 |
|
$this->setConfigClosure(function ($key = null, $default = null) { |
|
24
|
|
|
// @codeCoverageIgnoreStart |
|
25
|
|
|
if (function_exists('config')) { |
|
26
|
|
|
return config($key, $default); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
return null; |
|
30
|
|
|
// @codeCoverageIgnoreEnd |
|
31
|
6 |
|
}); |
|
32
|
6 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Handles an incoming request. |
|
36
|
|
|
* |
|
37
|
|
|
* @param \Illuminate\Http\Request $request |
|
38
|
|
|
* @param \Closure $next |
|
39
|
|
|
* @return mixed |
|
40
|
|
|
*/ |
|
41
|
6 |
|
public function handle($request, Closure $next) |
|
42
|
|
|
{ |
|
43
|
6 |
|
$response = $next($request); |
|
44
|
|
|
|
|
45
|
6 |
|
if ($response instanceof Response) { |
|
46
|
4 |
|
$this->setProfiles($this->getProfileConfig()); |
|
47
|
|
|
|
|
48
|
4 |
|
$this->setProfilesWithParameters(func_get_args()); |
|
49
|
|
|
|
|
50
|
4 |
|
$psr7Response = $this->createPsr7Response($response); |
|
51
|
|
|
|
|
52
|
4 |
|
$psr7Response = $this->addPolicyHeader($psr7Response); |
|
53
|
|
|
|
|
54
|
4 |
|
$response = $this->createLaravelResponse($psr7Response); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
6 |
|
return $response; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Creates Laravel response object from PSR 7 response. |
|
62
|
|
|
* |
|
63
|
|
|
* @param ResponseInterface $response |
|
64
|
|
|
* |
|
65
|
|
|
* @return Response |
|
66
|
|
|
*/ |
|
67
|
4 |
|
protected function createLaravelResponse(ResponseInterface $response) |
|
68
|
|
|
{ |
|
69
|
4 |
|
return new Response( |
|
70
|
4 |
|
(string) $response->getBody(), |
|
71
|
4 |
|
$response->getStatusCode(), |
|
72
|
4 |
|
$response->getHeaders() |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Creates PSR 7 response object from Laravel response. |
|
78
|
|
|
* |
|
79
|
|
|
* @param Response $response |
|
80
|
|
|
* |
|
81
|
|
|
* @return ResponseInterface |
|
82
|
|
|
*/ |
|
83
|
4 |
|
protected function createPsr7Response(Response $response) |
|
84
|
|
|
{ |
|
85
|
4 |
|
return new PsrResponse( |
|
86
|
4 |
|
$response->getStatusCode(), |
|
87
|
4 |
|
$response->headers->all(), |
|
88
|
4 |
|
$response->getContent(), |
|
89
|
4 |
|
$response->getProtocolVersion() |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Retrives profile configuration from Laravel config object. |
|
95
|
|
|
* |
|
96
|
|
|
* @return array |
|
97
|
|
|
*/ |
|
98
|
4 |
|
protected function getProfileConfig() |
|
99
|
|
|
{ |
|
100
|
4 |
|
$configCallable = $this->config; |
|
101
|
4 |
|
$config = $configCallable($this->getProfileConfigKey()); |
|
102
|
|
|
|
|
103
|
4 |
|
if (!is_array($config)) { |
|
104
|
1 |
|
$config = [$config]; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
4 |
|
return array_filter($config); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Retrieves configuration key associated with content security profiles. |
|
112
|
|
|
* |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
4 |
|
protected function getProfileConfigKey() |
|
116
|
|
|
{ |
|
117
|
4 |
|
return 'security.content'; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Gets profiles from handle method arguments. |
|
122
|
|
|
* |
|
123
|
|
|
* @param array $arguments |
|
124
|
|
|
* |
|
125
|
|
|
* @return array |
|
126
|
|
|
*/ |
|
127
|
4 |
|
protected function getProfilesFromArguments(array $arguments) |
|
128
|
|
|
{ |
|
129
|
4 |
|
$profiles = []; |
|
130
|
4 |
|
if (count($arguments) > 2) { |
|
131
|
2 |
|
unset($arguments[0]); |
|
132
|
2 |
|
unset($arguments[1]); |
|
133
|
2 |
|
$profiles = $arguments; |
|
134
|
|
|
} |
|
135
|
4 |
|
return $profiles; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Updates config callable used to access application configuration data. |
|
140
|
|
|
* |
|
141
|
|
|
* @param Closure $config |
|
142
|
|
|
* |
|
143
|
|
|
* @return EnforceContentSecurity |
|
144
|
|
|
*/ |
|
145
|
6 |
|
public function setConfigClosure(Closure $config) |
|
146
|
|
|
{ |
|
147
|
6 |
|
$this->config = $config; |
|
148
|
|
|
|
|
149
|
6 |
|
return $this; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Updates policy configuration with rules from each profile in given parameters. |
|
154
|
|
|
* |
|
155
|
|
|
* @param array $parameters |
|
156
|
|
|
* |
|
157
|
|
|
* @return void |
|
158
|
|
|
*/ |
|
159
|
4 |
|
protected function setProfilesWithParameters(array $parameters) |
|
160
|
|
|
{ |
|
161
|
4 |
|
$profiles = $this->getProfilesFromArguments($parameters); |
|
162
|
4 |
|
array_map([$this, 'loadProfileByKey'], $profiles); |
|
163
|
4 |
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|