@@ -26,6 +26,24 @@ public function __construct(array $options = [])
26
26
27
27
private function normalizeOptions (array $ options = []): array
28
28
{
29
+ $ aliases = [
30
+ 'supports_credentials ' => 'supportsCredentials ' ,
31
+ 'allowed_origins ' => 'allowedOrigins ' ,
32
+ 'allowed_origins_patterns ' => 'allowedOriginsPatterns ' ,
33
+ 'allowed_headers ' => 'allowedHeaders ' ,
34
+ 'allowed_methods ' => 'allowedMethods ' ,
35
+ 'exposed_headers ' => 'exposedHeaders ' ,
36
+ 'max_age ' => 'maxAge ' ,
37
+ ];
38
+
39
+ // Normalize underscores
40
+ foreach ($ aliases as $ alias => $ option ) {
41
+ if (isset ($ options [$ alias ])) {
42
+ $ options [$ option ] = $ options ['alias ' ];
43
+ unset($ options ['alias ' ]);
44
+ }
45
+ }
46
+
29
47
$ options += [
30
48
'allowedOrigins ' => [],
31
49
'allowedOriginsPatterns ' => [],
@@ -36,6 +54,16 @@ private function normalizeOptions(array $options = []): array
36
54
'maxAge ' => 0 ,
37
55
];
38
56
57
+ if (!is_array ($ options ['exposedHeaders ' ])) {
58
+ throw new \RuntimeException ("CORS option `exposed_headers` should be `false` or an array " );
59
+ }
60
+
61
+ foreach (['allowedOrigins ' , 'allowedOriginsPatterns ' , 'allowedHeaders ' , 'allowedMethods ' ] as $ key ) {
62
+ if (!is_array ($ options [$ key ])) {
63
+ throw new \RuntimeException ("CORS option ` {$ key }` should be an array " );
64
+ }
65
+ }
66
+
39
67
// normalize array('*') to true
40
68
if (in_array ('* ' , $ options ['allowedOrigins ' ])) {
41
69
$ options ['allowedOrigins ' ] = true ;
@@ -52,15 +80,33 @@ private function normalizeOptions(array $options = []): array
52
80
$ options ['allowedMethods ' ] = array_map ('strtoupper ' , $ options ['allowedMethods ' ]);
53
81
}
54
82
83
+ // Transform wildcard pattern
84
+ foreach ($ options ['allowedOrigins ' ] as $ origin ) {
85
+ if (strpos ($ origin , '* ' ) !== false ) {
86
+ $ options ['allowedOriginsPatterns ' ][] = $ this ->convertWildcardToPattern ($ origin );
87
+ }
88
+ }
89
+
55
90
return $ options ;
56
91
}
57
92
58
93
/**
59
- * @deprecated use isOriginAllowed
94
+ * Create a pattern for a wildcard, based on Str::is() from Laravel
95
+ *
96
+ * @see https://github.com/laravel/framework/blob/5.5/src/Illuminate/Support/Str.php
97
+ * @param string $pattern
98
+ * @return string
60
99
*/
61
- public function isActualRequestAllowed ( Request $ request ): bool
100
+ private function convertWildcardToPattern ( $ pattern )
62
101
{
63
- return $ this ->isOriginAllowed ($ request );
102
+ $ pattern = preg_quote ($ pattern , '# ' );
103
+
104
+ // Asterisks are translated into zero-or-more regular expression wildcards
105
+ // to make it convenient to check if the strings starts with the given
106
+ // pattern such as "library/*", making any string check convenient.
107
+ $ pattern = str_replace ('\* ' , '.* ' , $ pattern );
108
+
109
+ return '#^ ' . $ pattern . '\z#u ' ;
64
110
}
65
111
66
112
public function isCorsRequest (Request $ request ): bool
@@ -218,9 +264,4 @@ public function varyHeader(Response $response, $header): Response
218
264
219
265
return $ response ;
220
266
}
221
-
222
- private function isSameHost (Request $ request ): bool
223
- {
224
- return $ request ->headers ->get ('Origin ' ) === $ request ->getSchemeAndHttpHost ();
225
- }
226
267
}
0 commit comments