|
144 | 144 | } |
145 | 145 |
|
146 | 146 | /* |
147 | | - * Set the full base URL. |
| 147 | + * SECURITY: Validate and set the full base URL. |
148 | 148 | * This URL is used as the base of all absolute links. |
149 | | - * Can be very useful for CLI/Commandline applications. |
| 149 | + * |
| 150 | + * IMPORTANT: In production, App.fullBaseUrl MUST be explicitly configured to prevent |
| 151 | + * Host Header Injection attacks. Relying on the HTTP_HOST header can allow attackers |
| 152 | + * to hijack password reset tokens and other security-critical operations. |
| 153 | + * |
| 154 | + * Set APP_FULL_BASE_URL in your environment variables or configure App.fullBaseUrl |
| 155 | + * in config/app.php or config/app_local.php |
| 156 | + * |
| 157 | + * Example: APP_FULL_BASE_URL=https://yourdomain.com |
150 | 158 | */ |
151 | 159 | $fullBaseUrl = Configure::read('App.fullBaseUrl'); |
152 | 160 | if (!$fullBaseUrl) { |
| 161 | + $httpHost = env('HTTP_HOST'); |
| 162 | + |
153 | 163 | /* |
154 | | - * When using proxies or load balancers, SSL/TLS connections might |
155 | | - * get terminated before reaching the server. If you trust the proxy, |
156 | | - * you can enable `$trustProxy` to rely on the `X-Forwarded-Proto` |
157 | | - * header to determine whether to generate URLs using `https`. |
158 | | - * |
159 | | - * See also https://book.cakephp.org/5/en/controllers/request-response.html#trusting-proxy-headers |
| 164 | + * Only enforce fullBaseUrl requirement when we're in a web request context. |
| 165 | + * This allows CLI tools (like PHPStan) to load the bootstrap without throwing. |
160 | 166 | */ |
161 | | - $trustProxy = false; |
162 | | - |
163 | | - $s = null; |
164 | | - if (env('HTTPS') || ($trustProxy && env('HTTP_X_FORWARDED_PROTO') === 'https')) { |
165 | | - $s = 's'; |
| 167 | + if (!Configure::read('debug') && $httpHost) { |
| 168 | + throw new \Cake\Core\Exception\CakeException( |
| 169 | + 'SECURITY: App.fullBaseUrl is not configured. ' . |
| 170 | + 'This is required in production to prevent Host Header Injection attacks. ' . |
| 171 | + 'Set APP_FULL_BASE_URL environment variable or configure App.fullBaseUrl in config/app.php' |
| 172 | + ); |
166 | 173 | } |
167 | 174 |
|
168 | | - $httpHost = env('HTTP_HOST'); |
| 175 | + /* |
| 176 | + * Development mode fallback: Use HTTP_HOST for convenience. |
| 177 | + * WARNING: This is ONLY safe in development. Never use this pattern in production! |
| 178 | + */ |
169 | 179 | if ($httpHost) { |
| 180 | + $s = null; |
| 181 | + if (env('HTTPS')) { |
| 182 | + $s = 's'; |
| 183 | + } |
170 | 184 | $fullBaseUrl = 'http' . $s . '://' . $httpHost; |
171 | 185 | } |
172 | 186 | unset($httpHost, $s); |
|
0 commit comments